1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
#!/usr/bin/python3
# https://theweeklychallenge.org/blog/perl-weekly-challenge-245/#TASK1
#
# Task 1: Sort Language
# =====================
#
# You are given two array of languages and its popularity.
#
# Write a script to sort the language based on popularity.
# Example 1
#
# Input: @lang = ('perl', 'c', 'python')
# @popularity = (2, 1, 3)
# Output: ('c', 'perl', 'python')
#
# Example 2
#
# Input: @lang = ('c++', 'haskell', 'java')
# @popularity = (1, 3, 2)
# Output: ('c++', 'java', 'haskell')
#
############################################################
##
## discussion
##
############################################################
#
# The solution that jumps right into my face is to fill the data
# into a hash table, using lang as the key and popularity as the
# value. Then sort the keys by their values and put the result into
# the output.
def sort_language(lang: list, popularity: list):
print("Input: (", ", ".join(str(x) for x in lang), "), (", ", ".join(str(x) for x in popularity), ")")
hash = {}
for i in range(len(lang)):
hash[lang[i]] = popularity[i]
print("Output: (", ", ".join(str(x) for x in sorted(hash, key=hash.get)), ")")
sort_language( ['perl', 'c', 'python'], [2, 1, 3] );
sort_language( ['c++', 'haskell', 'java'], [1, 3, 2] );
|