aboutsummaryrefslogtreecommitdiff
path: root/challenge-245/packy-anderson/python/ch-1.py
diff options
context:
space:
mode:
author冯昶 <fengchang@novel-supertv.com>2023-12-04 15:09:23 +0800
committer冯昶 <fengchang@novel-supertv.com>2023-12-04 15:09:23 +0800
commit1b0f0c9cf0a1295b8bf9bef8d902f9a351120785 (patch)
tree538dfb19ef0c66b56d4d743ed37868350b463a0f /challenge-245/packy-anderson/python/ch-1.py
parent6f78aae3efa4642ffa271896203a09afce53e407 (diff)
parentb41848354dc1a09ae68e01e91b551a36d13f8bda (diff)
downloadperlweeklychallenge-club-1b0f0c9cf0a1295b8bf9bef8d902f9a351120785.tar.gz
perlweeklychallenge-club-1b0f0c9cf0a1295b8bf9bef8d902f9a351120785.tar.bz2
perlweeklychallenge-club-1b0f0c9cf0a1295b8bf9bef8d902f9a351120785.zip
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-245/packy-anderson/python/ch-1.py')
-rw-r--r--challenge-245/packy-anderson/python/ch-1.py29
1 files changed, 29 insertions, 0 deletions
diff --git a/challenge-245/packy-anderson/python/ch-1.py b/challenge-245/packy-anderson/python/ch-1.py
new file mode 100644
index 0000000000..28b6ce5e11
--- /dev/null
+++ b/challenge-245/packy-anderson/python/ch-1.py
@@ -0,0 +1,29 @@
+#!/usr/bin/env python
+
+def sortLanguage(lang, popularity):
+ # build a dict associating popularity with lang
+ lang_pop = {
+ v: popularity[i] for i,v in enumerate(lang)
+ }
+ sorted_list = sorted(lang,
+ # sort by lang_pop, not lang
+ key=lambda x: (lang_pop[x]))
+ return sorted_list
+
+def comma_join(arr):
+ return ', '.join(map(lambda i: str(i), arr))
+
+def quoted_comma_join(arr):
+ return "'" + "', '".join(arr) + "'"
+
+def solution(lang, popularity):
+ print(f'Input: @lang = ({quoted_comma_join(lang)})')
+ print(f' @popularity = ({comma_join(popularity)})')
+ output = sortLanguage(lang, popularity)
+ print(f'Output: ({quoted_comma_join(output)})')
+
+print('Example 1:')
+solution(['perl', 'c', 'python'], [2, 1, 3])
+
+print('\nExample 2:')
+solution(['c++', 'haskell', 'java'], [1, 3, 2])