aboutsummaryrefslogtreecommitdiff
path: root/challenge-245/sgreen/python
diff options
context:
space:
mode:
author冯昶 <fengchang@novel-supertv.com>2024-01-08 14:54:58 +0800
committer冯昶 <fengchang@novel-supertv.com>2024-01-08 14:54:58 +0800
commite019e6886d406f9e111e67cb1cdbe39ec996bb2a (patch)
tree7614dde91435b882f70c83213fb5c7301bed12bc /challenge-245/sgreen/python
parente94f52525bf99cded9ba76e696b691c7a657500b (diff)
parent9a485c9bac8e3887b165d67c9aa81d71cdd42f01 (diff)
downloadperlweeklychallenge-club-e019e6886d406f9e111e67cb1cdbe39ec996bb2a.tar.gz
perlweeklychallenge-club-e019e6886d406f9e111e67cb1cdbe39ec996bb2a.tar.bz2
perlweeklychallenge-club-e019e6886d406f9e111e67cb1cdbe39ec996bb2a.zip
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-245/sgreen/python')
-rwxr-xr-xchallenge-245/sgreen/python/ch-1.py29
-rwxr-xr-xchallenge-245/sgreen/python/ch-2.py36
2 files changed, 65 insertions, 0 deletions
diff --git a/challenge-245/sgreen/python/ch-1.py b/challenge-245/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..a39f9980eb
--- /dev/null
+++ b/challenge-245/sgreen/python/ch-1.py
@@ -0,0 +1,29 @@
+#!/usr/bin/env python3
+
+import sys
+
+
+def main(words, places):
+ # Create a dict where the key is the place, and the value is the word
+ rankings = {
+ place: word for place, word in zip(places, words)
+ }
+
+ # Order the words by their ranking
+ solution = [rankings[place] for place in sorted(rankings)]
+ print(*solution, sep=', ')
+
+
+if __name__ == '__main__':
+ inputs = sys.argv[1:]
+
+ # We need an even number of inputs
+ if len(inputs) % 2 == 1:
+ raise ValueError('We require an even number of inputs')
+
+ # Split the input into words and places
+ half = len(inputs) // 2
+ words = inputs[:half]
+ # Convert input into integers
+ places = [int(n) for n in inputs[half:]]
+ main(words, places)
diff --git a/challenge-245/sgreen/python/ch-2.py b/challenge-245/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..20098f5788
--- /dev/null
+++ b/challenge-245/sgreen/python/ch-2.py
@@ -0,0 +1,36 @@
+#!/usr/bin/env python3
+
+from itertools import permutations
+import sys
+
+
+def largest_number(numbers):
+ # Calculate the largest number using the all supplied numbers
+ largest = 0
+ for i in permutations(map(lambda x: str(x), numbers)):
+ n = int(''.join(i))
+ if n > largest:
+ largest = n
+
+ return largest
+
+
+def main(ints):
+ largest = -1
+ for bitwise in range(1, 2 ** len(ints)):
+ numbers = [n for i, n in enumerate(ints) if bitwise & (2 ** i)]
+ if sum(numbers) % 3 != 0:
+ # There is no possible solution with this set of numbers
+ continue
+
+ this_largest = largest_number(numbers)
+ if this_largest > largest:
+ largest = this_largest
+
+ print(largest)
+
+
+if __name__ == '__main__':
+ # Convert input into integers
+ array = [int(n) for n in sys.argv[1:]]
+ main(array)