aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-11-28 17:44:27 +0000
committerGitHub <noreply@github.com>2023-11-28 17:44:27 +0000
commitb904a7e64105290bed0bc11d76d4243fd174e7a2 (patch)
tree3dcb7e8d989a116bc79f314a52db722299f8e671
parent871f20d8ecc70f67143baa9961a74d142341e68d (diff)
parentf2727a903366f08bf54737dfd716ecd01c88248b (diff)
downloadperlweeklychallenge-club-b904a7e64105290bed0bc11d76d4243fd174e7a2.tar.gz
perlweeklychallenge-club-b904a7e64105290bed0bc11d76d4243fd174e7a2.tar.bz2
perlweeklychallenge-club-b904a7e64105290bed0bc11d76d4243fd174e7a2.zip
Merge pull request #9162 from oWnOIzRi/week245
add solution week 245 in python
-rw-r--r--challenge-245/steven-wilson/python/ch-01.py18
-rw-r--r--challenge-245/steven-wilson/python/ch-02.py43
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()