aboutsummaryrefslogtreecommitdiff
path: root/challenge-245/jeanluc2020/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/jeanluc2020/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/jeanluc2020/python/ch-2.py')
-rwxr-xr-xchallenge-245/jeanluc2020/python/ch-2.py74
1 files changed, 74 insertions, 0 deletions
diff --git a/challenge-245/jeanluc2020/python/ch-2.py b/challenge-245/jeanluc2020/python/ch-2.py
new file mode 100755
index 0000000000..5b11711684
--- /dev/null
+++ b/challenge-245/jeanluc2020/python/ch-2.py
@@ -0,0 +1,74 @@
+#!/usr/bin/python3
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-245/#TASK2
+#
+# Task 2: Largest of Three
+# ========================
+#
+# You are given an array of integers >= 0.
+#
+# Write a script to 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.
+#
+## Example 1
+##
+## Input: @ints = (8, 1, 9)
+## Output: 981
+##
+## 981 % 3 == 0
+#
+## Example 2
+##
+## Input: @ints = (8, 6, 7, 1, 0)
+## Output: 8760
+#
+## Example 3
+##
+## Input: @ints = (1)
+## Output: -1
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# While all examples in the description use single-digit numbers,
+# there is nothing that would require this. So in order to catch all
+# solutions, we need all possible permutations of all subsets of the
+# array and of the numbers created out of those we need the biggest
+# one that is divisible by 3.
+
+from itertools import chain, combinations, permutations
+
+def powerset(iterable):
+ "powerset([1,2,3]) → () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
+ s = list(iterable)
+ return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))
+
+def largest_of_three(ints: list):
+ print("Input: (", ", ".join(str(x) for x in ints), ")")
+ result = int()
+ for myset in powerset(ints):
+ for c in permutations(myset, len(myset)):
+ v = str("".join(str(x) for x in c))
+ # print("-> ", v)
+ if len(v) > 0:
+ # print("Considering", v, "as a value")
+ value = int(v)
+ if value % 3 == 0:
+ if len(str(result)) == 0:
+ result = value
+ if value > result:
+ result = value
+ if len(str(result)) == 0:
+ print("Output: -1")
+ else:
+ print("Output:", str(result))
+
+largest_of_three([8, 1, 9]);
+largest_of_three([8, 6, 7, 1, 0]);
+largest_of_three([1]);
+largest_of_three([8, 60, 7]);
+largest_of_three([80, 6, 7]);
+