aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-11-28 11:58:21 +0000
committerGitHub <noreply@github.com>2023-11-28 11:58:21 +0000
commitf92ab3612e50d3b814179cc03d5145e9c80ebe0b (patch)
tree771b6f5f86049c0972f6d2f9daff3eeb39cb6eac
parentb60cb77fe463f9c97a8170868a2ff874554d5f41 (diff)
parentc21050f1f14eda7b8dca16bdeb8186cb65255260 (diff)
downloadperlweeklychallenge-club-f92ab3612e50d3b814179cc03d5145e9c80ebe0b.tar.gz
perlweeklychallenge-club-f92ab3612e50d3b814179cc03d5145e9c80ebe0b.tar.bz2
perlweeklychallenge-club-f92ab3612e50d3b814179cc03d5145e9c80ebe0b.zip
Merge pull request #9159 from pokgopun/pwc245
pwc245_task2 - add comments and minor modification
-rw-r--r--challenge-245/pokgopun/go/ch-2.go12
-rw-r--r--challenge-245/pokgopun/python/ch-2.py18
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
)
)
)