diff options
| author | Steven <steven1170@zoho.eu> | 2023-11-28 16:37:46 +0000 |
|---|---|---|
| committer | Steven <steven1170@zoho.eu> | 2023-11-28 16:37:46 +0000 |
| commit | f2727a903366f08bf54737dfd716ecd01c88248b (patch) | |
| tree | 97eb1fe7da1de1d330a644a3f4f391b2ed92778b | |
| parent | f92ab3612e50d3b814179cc03d5145e9c80ebe0b (diff) | |
| download | perlweeklychallenge-club-f2727a903366f08bf54737dfd716ecd01c88248b.tar.gz perlweeklychallenge-club-f2727a903366f08bf54737dfd716ecd01c88248b.tar.bz2 perlweeklychallenge-club-f2727a903366f08bf54737dfd716ecd01c88248b.zip | |
add solution week 245 in python
| -rw-r--r-- | challenge-245/steven-wilson/python/ch-01.py | 18 | ||||
| -rw-r--r-- | challenge-245/steven-wilson/python/ch-02.py | 43 |
2 files changed, 61 insertions, 0 deletions
diff --git a/challenge-245/steven-wilson/python/ch-01.py b/challenge-245/steven-wilson/python/ch-01.py new file mode 100644 index 0000000000..9017f8cfa6 --- /dev/null +++ b/challenge-245/steven-wilson/python/ch-01.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python3 + + +def sort_language(lang, popularity): + ''' Given 2 arrays: languages and popularity. Sorts the language + based on popularity. + >>> sort_language(('perl', 'c', 'python'), (2, 1, 3)) + ('c', 'perl', 'python') + >>> sort_language(('c++', 'haskell', 'java'), (1, 3, 2)) + ('c++', 'java', 'haskell') + ''' + return tuple(sorted(lang, key=lambda l: popularity[lang.index(l)])) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/challenge-245/steven-wilson/python/ch-02.py b/challenge-245/steven-wilson/python/ch-02.py new file mode 100644 index 0000000000..74f2ade0e3 --- /dev/null +++ b/challenge-245/steven-wilson/python/ch-02.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python3 + + +from itertools import permutations + + +def concate_to_number(elements): + '''Concatinate a list of integers to an integer + >>> concate_to_number([9, 8, 1]) + 981 + >>> concate_to_number([0, 1, 2]) + 12 + ''' + return int("".join(map(str, elements))) + + +def largest_of_three(*elements): + '''Return the largest number formed by concatenating some of the + given integers in any order which is also multiple of 3. Return -1 + if none found. + >>> largest_of_three(8, 1, 9) + 981 + >>> largest_of_three(8, 6, 7, 1, 0) + 8760 + >>> largest_of_three(1) + -1 + ''' + ints = [] + for i, elem in enumerate(elements, 1): + for perm in permutations(elements, i): + concate_number = concate_to_number(perm) + if concate_number % 3 == 0: + ints.append(concate_number) + if len(ints) > 0: + return max(ints) + else: + return -1 + + +if __name__ == "__main__": + import doctest + + doctest.testmod() |
