aboutsummaryrefslogtreecommitdiff
path: root/challenge-245/packy-anderson/python/ch-2.py
diff options
context:
space:
mode:
author冯昶 <fengchang@novel-supertv.com>2023-12-04 15:09:23 +0800
committer冯昶 <fengchang@novel-supertv.com>2023-12-04 15:09:23 +0800
commit1b0f0c9cf0a1295b8bf9bef8d902f9a351120785 (patch)
tree538dfb19ef0c66b56d4d743ed37868350b463a0f /challenge-245/packy-anderson/python/ch-2.py
parent6f78aae3efa4642ffa271896203a09afce53e407 (diff)
parentb41848354dc1a09ae68e01e91b551a36d13f8bda (diff)
downloadperlweeklychallenge-club-1b0f0c9cf0a1295b8bf9bef8d902f9a351120785.tar.gz
perlweeklychallenge-club-1b0f0c9cf0a1295b8bf9bef8d902f9a351120785.tar.bz2
perlweeklychallenge-club-1b0f0c9cf0a1295b8bf9bef8d902f9a351120785.zip
Merge remote-tracking branch 'upstream/master'
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])