aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Manring <michael@manring>2023-11-29 01:06:01 +1100
committerMichael Manring <michael@manring>2023-11-29 01:06:01 +1100
commit8f9ccba80819a3bbe697f0e70d200ed75f43d9d5 (patch)
treeadc9ce95ccdc27d78d3395b2540952760b07f95c
parent89872cdb0cfba905fed4a613f78a08677cfd0032 (diff)
downloadperlweeklychallenge-club-8f9ccba80819a3bbe697f0e70d200ed75f43d9d5.tar.gz
perlweeklychallenge-club-8f9ccba80819a3bbe697f0e70d200ed75f43d9d5.tar.bz2
perlweeklychallenge-club-8f9ccba80819a3bbe697f0e70d200ed75f43d9d5.zip
pwc245_task2 refactor to replace complicated list comprehension with couple generators
-rw-r--r--challenge-245/pokgopun/python/ch-2.py33
1 files changed, 17 insertions, 16 deletions
diff --git a/challenge-245/pokgopun/python/ch-2.py b/challenge-245/pokgopun/python/ch-2.py
index de7e1f9713..c1f88a7398 100644
--- a/challenge-245/pokgopun/python/ch-2.py
+++ b/challenge-245/pokgopun/python/ch-2.py
@@ -40,24 +40,25 @@ SO WHAT DO YOU THINK ?
from itertools import permutations, chain
-### this function looks a bit messy as it tries to operate on iterators alone to saving memory resources, avoiding storing any of them to intermediate list/tuple
-def lot(tup: tuple):
+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(
(
- (
- int("".join(iStrs)) for iStrs in ### concat and convert int strings to an int
- (
- ( str(i) for i in ints) for ints in ### int-to-string conversion for ints so we can further concat and convert them to an int
- (
- ints for ints in
- chain.from_iterable( ### need permutations(tup,n) loop instead of permutations(tup) alone to avoid a bug in itertools/permutations
- permutations(tup, n) for n in range(1,len(tup)+1)
- ) if sum(ints) % 3 == 0 ### If a + b = c , then a ( mod N ) + b ( mod N ) ≡ c ( mod N )
- )
- )
- ),
- (-1,), ### add (-1,) to chain.from_iterable to avoid max() error from null iterator from permutations (i.e. when nothing qualifies sum % 3 == 0
+ 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
)
)
)
@@ -73,4 +74,4 @@ for inpt, otpt in {
(8, 76, 0): 8760,
(8, 94, 0): 9480,
}.items():
- print(otpt==lot(inpt))
+ print(otpt==largestOfThree(inpt))