aboutsummaryrefslogtreecommitdiff
path: root/challenge-245/packy-anderson/python/ch-2.py
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-245/packy-anderson/python/ch-2.py')
-rw-r--r--challenge-245/packy-anderson/python/ch-2.py38
1 files changed, 38 insertions, 0 deletions
diff --git a/challenge-245/packy-anderson/python/ch-2.py b/challenge-245/packy-anderson/python/ch-2.py
new file mode 100644
index 0000000000..bb98b36e74
--- /dev/null
+++ b/challenge-245/packy-anderson/python/ch-2.py
@@ -0,0 +1,38 @@
+#!/usr/bin/env python
+
+from itertools import combinations
+
+def largestOfThree(ints):
+ # generate a list of combinations
+ combos = [
+ c for i in range(1, len(ints)+1)
+ for c in combinations(ints, i)
+ ]
+ maxval = -1 # initialize our failure case
+ for combo in combos:
+ combo_list = list(combo)
+ combo_list.sort(reverse=True)
+ num = int(''.join(map(str, combo_list)))
+ if num <= maxval: # not bigger than current max
+ continue
+ if num % 3 != 0: # not divisible by 3
+ continue
+ maxval = num
+ return maxval
+
+def comma_join(arr):
+ return ', '.join(map(str, arr))
+
+def solution(ints):
+ print(f'Input: @arr = ({comma_join(ints)})')
+ output = largestOfThree(ints)
+ print(f'Output: {output}')
+
+print('Example 1:')
+solution([8, 1, 9])
+
+print('\nExample 2:')
+solution([8, 6, 7, 1, 0])
+
+print('\nExample 3:')
+solution([1])