diff options
| -rw-r--r-- | challenge-245/steven-wilson/python/ch-1.py | 7 |
1 files changed, 5 insertions, 2 deletions
diff --git a/challenge-245/steven-wilson/python/ch-1.py b/challenge-245/steven-wilson/python/ch-1.py index 9017f8cfa6..6f6a56eaed 100644 --- a/challenge-245/steven-wilson/python/ch-1.py +++ b/challenge-245/steven-wilson/python/ch-1.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 -def sort_language(lang, popularity): +def sort_language(language, popularity): ''' Given 2 arrays: languages and popularity. Sorts the language based on popularity. >>> sort_language(('perl', 'c', 'python'), (2, 1, 3)) @@ -9,7 +9,10 @@ def sort_language(lang, popularity): >>> sort_language(('c++', 'haskell', 'java'), (1, 3, 2)) ('c++', 'java', 'haskell') ''' - return tuple(sorted(lang, key=lambda l: popularity[lang.index(l)])) + if len(language) != len(popularity): + raise ValueError("Lengths of 'language' and 'popularity' must be the same.") + language_popularity = dict(zip(language, popularity)) + return tuple(sorted(language, key=lambda l: language_popularity.get(l))) if __name__ == "__main__": |
