diff options
| author | 冯昶 <fengchang@novel-supertv.com> | 2023-12-04 15:09:23 +0800 |
|---|---|---|
| committer | 冯昶 <fengchang@novel-supertv.com> | 2023-12-04 15:09:23 +0800 |
| commit | 1b0f0c9cf0a1295b8bf9bef8d902f9a351120785 (patch) | |
| tree | 538dfb19ef0c66b56d4d743ed37868350b463a0f /challenge-245/pokgopun/python/ch-2.py | |
| parent | 6f78aae3efa4642ffa271896203a09afce53e407 (diff) | |
| parent | b41848354dc1a09ae68e01e91b551a36d13f8bda (diff) | |
| download | perlweeklychallenge-club-1b0f0c9cf0a1295b8bf9bef8d902f9a351120785.tar.gz perlweeklychallenge-club-1b0f0c9cf0a1295b8bf9bef8d902f9a351120785.tar.bz2 perlweeklychallenge-club-1b0f0c9cf0a1295b8bf9bef8d902f9a351120785.zip | |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-245/pokgopun/python/ch-2.py')
| -rw-r--r-- | challenge-245/pokgopun/python/ch-2.py | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/challenge-245/pokgopun/python/ch-2.py b/challenge-245/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..c1f88a7398 --- /dev/null +++ b/challenge-245/pokgopun/python/ch-2.py @@ -0,0 +1,77 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-245/ +""" + +Task 2: Largest of Three + +Submitted by: [46]Mohammad S Anwar + __________________________________________________________________ + + 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 + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 3rd December + 2023. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +from itertools import permutations, chain + +def permutes(tup: tuple): + for n in range(1,len(tup)+1): ### need permutations(tup,n) loop instead of permutations(tup) alone to avoid a bug in itertools/permutations + yield permutations(tup, n) + +def intsOfThree(tup: tuple): + for ints in chain.from_iterable(permutes(tup)): + if sum(ints) % 3 == 0: ### If a + b = c , then a ( mod N ) + b ( mod N ) ≡ c ( mod N ) + yield ints + +def intOfThree(tup: tuple): + for iStrs in ((str(i) for i in ints) for ints in intsOfThree(tup)): ### int-to-string conversion for ints so we can further concat and convert them to an int + yield int("".join(iStrs)) ### concat and convert int strings to an int + +def largestOfThree(tup: tuple): + return max( ### max() can avoid an error from null permute iterator as it operates on chain.from_iterables that include default (-1,) + chain.from_iterable( + ( + intOfThree(tup), + (-1,) ### add (-1,) to chain.from_iterable to avoid max() error from null iterator from permutations (i.e. when nothing qualifies sum % 3 == 0 + ) + ) + ) + +for inpt, otpt in { + (8, 1, 9): 981, + (8, 6, 7, 1, 0): 8760, + (1,): -1, + (0,0,0): 0, + (4, 8, 911): 9114, + (8, 85, 0): 8850, + (8, 89, 2): 8982, + (8, 76, 0): 8760, + (8, 94, 0): 9480, + }.items(): + print(otpt==largestOfThree(inpt)) |
