diff options
| author | Michael Manring <michael@manring> | 2023-11-28 22:45:43 +1100 |
|---|---|---|
| committer | Michael Manring <michael@manring> | 2023-11-28 22:45:43 +1100 |
| commit | c21050f1f14eda7b8dca16bdeb8186cb65255260 (patch) | |
| tree | 7142555ccbfdfe1d520f2f733a69407765546a3e | |
| parent | 2b1cc6e13bda42ad4aab75a822317e6de553e508 (diff) | |
| download | perlweeklychallenge-club-c21050f1f14eda7b8dca16bdeb8186cb65255260.tar.gz perlweeklychallenge-club-c21050f1f14eda7b8dca16bdeb8186cb65255260.tar.bz2 perlweeklychallenge-club-c21050f1f14eda7b8dca16bdeb8186cb65255260.zip | |
pwc245_task2 - add comments and minor modification to apply the modulas propert:"If a + b = c , then a ( mod N ) + b ( mod N ) ≡ c ( mod N )"
| -rw-r--r-- | challenge-245/pokgopun/go/ch-2.go | 12 | ||||
| -rw-r--r-- | challenge-245/pokgopun/python/ch-2.py | 18 |
2 files changed, 21 insertions, 9 deletions
diff --git a/challenge-245/pokgopun/go/ch-2.go b/challenge-245/pokgopun/go/ch-2.go index b707bf3662..fafc8e1a09 100644 --- a/challenge-245/pokgopun/go/ch-2.go +++ b/challenge-245/pokgopun/go/ch-2.go @@ -67,6 +67,14 @@ func main() { type ints []int +func (d ints) sum() int { + s := 0 + for _, v := range d { + s += v + } + return s +} + func (d ints) concat() int { r := d[0] var n, t int @@ -87,8 +95,8 @@ func (d ints) lot() int { for i := l; i > 0; i-- { res, closer := d.permute(i) for v := range res { - r := v.concat() - if r%3 == 0 { + if v.sum()%3 == 0 { // If a + b = c , then a ( mod N ) + b ( mod N ) ≡ c ( mod N ), we can filter lots of them out before concat + r := v.concat() if r > max { max = r } diff --git a/challenge-245/pokgopun/python/ch-2.py b/challenge-245/pokgopun/python/ch-2.py index 8256bbcfa6..362cb4b6d9 100644 --- a/challenge-245/pokgopun/python/ch-2.py +++ b/challenge-245/pokgopun/python/ch-2.py @@ -40,22 +40,26 @@ 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): - return max( + 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( ( ( i for i in ( - int("".join(x)) for x in + int("".join(j)) for j in ### concat and convert int strings to an int ( - (str(e) for e in y) for y in - chain.from_iterable( - permutations(tup, n) for n in range(1,len(tup)+1) + ( str(z) for z in y) for y in ### int-to-string conversion for ints so we can further concat and convert them to an int + ( + x for x 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(x) % 3 == 0 ### If a + b = c , then a ( mod N ) + b ( mod N ) ≡ c ( mod N ) ) ) - ) if i % 3 == 0 + ) ), - (-1,), + (-1,), ### add (-1,) to chain.from_iterable to avoid max() error from null iterator from permutations (i.e. when nothing qualifies sum % 3 == 0 ) ) ) |
