From 5ecbea991460980647586e15f1f1a9324cfdc4dd Mon Sep 17 00:00:00 2001 From: Simon Green Date: Sun, 3 Dec 2023 21:58:09 +1100 Subject: Simon's solution to challenge 245 --- challenge-245/sgreen/python/ch-1.py | 29 +++++++++++++++++++++++++++++ challenge-245/sgreen/python/ch-2.py | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100755 challenge-245/sgreen/python/ch-1.py create mode 100755 challenge-245/sgreen/python/ch-2.py (limited to 'challenge-245/sgreen/python') 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) -- cgit