From 7a0976d301a1090cbf3a9ae302e45b9a612d29b3 Mon Sep 17 00:00:00 2001 From: Michael Manring Date: Mon, 27 Nov 2023 21:51:47 +1100 Subject: pwc245 solution in python --- challenge-245/pokgopun/python/ch-1.py | 42 +++++++++++++++++++++++++ challenge-245/pokgopun/python/ch-2.py | 59 +++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 challenge-245/pokgopun/python/ch-1.py create mode 100644 challenge-245/pokgopun/python/ch-2.py diff --git a/challenge-245/pokgopun/python/ch-1.py b/challenge-245/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..566c3310b1 --- /dev/null +++ b/challenge-245/pokgopun/python/ch-1.py @@ -0,0 +1,42 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-245/ +""" + +Task 1: Sort Language + +Submitted by: [45]Mohammad S Anwar + __________________________________________________________________ + + You are given two array of languages and its popularity. + + Write a script to sort the language based on popularity. + +Example 1 + +Input: @lang = ('perl', 'c', 'python') + @popularity = (2, 1, 3) +Output: ('c', 'perl', 'python') + +Example 2 + +Input: @lang = ('c++', 'haskell', 'java') + @popularity = (1, 3, 2) +Output: ('c++', 'java', 'haskell') + +Task 2: Largest of Three +""" +### solution by pokgopun@gmail.com + +def sortL(tup1,tup2: tuple): + return tuple( + tup[0] for tup in + sorted( + map(lambda e1,e2: (e1,e2), tup1, tup2), + key=lambda tup: tup[1] + ) + ) + +for (inpt1,inpt2), otpt in { + (('perl', 'c', 'python'), (2, 1, 3)): ('c', 'perl', 'python'), + (('c++', 'haskell', 'java'), (1, 3, 2)): ('c++', 'java', 'haskell'), + }.items(): + print(otpt==sortL(inpt1, inpt2)) diff --git a/challenge-245/pokgopun/python/ch-2.py b/challenge-245/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..d689e65c04 --- /dev/null +++ b/challenge-245/pokgopun/python/ch-2.py @@ -0,0 +1,59 @@ +### 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: @digits = (8, 1, 9) +Output: 981 + +981 % 3 == 0 + +Example 2 + +Input: @digits = (8, 6, 7, 1, 0) +Output: 8760 + +Example 3 + +Input: @digits = (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 + +def lot(tup: tuple): + for n in range(len(tup),0,-1): + for x in permutations( + sorted(tup,reverse=True), n + ): + y = int( + "".join( str(e) for e in x ) + ) + if y % 3 == 0: return y + return -1 + +for inpt, otpt in { + (8, 1, 9): 981, + (8, 6, 7, 1, 0): 8760, + (1,): -1, + }.items(): + print(otpt==lot(inpt)) -- cgit From 7afc1f8c9df147bf7e09970a11fd6600cbc4098f Mon Sep 17 00:00:00 2001 From: Michael Manring Date: Tue, 28 Nov 2023 13:26:31 +1100 Subject: pwc245 solution in go --- challenge-245/pokgopun/go/ch-1.go | 60 +++++++++++++++ challenge-245/pokgopun/go/ch-2.go | 150 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 210 insertions(+) create mode 100644 challenge-245/pokgopun/go/ch-1.go create mode 100644 challenge-245/pokgopun/go/ch-2.go diff --git a/challenge-245/pokgopun/go/ch-1.go b/challenge-245/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..f4f1351345 --- /dev/null +++ b/challenge-245/pokgopun/go/ch-1.go @@ -0,0 +1,60 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-245/ +/*# + +Task 1: Sort Language + +Submitted by: [45]Mohammad S Anwar + __________________________________________________________________ + + You are given two array of languages and its popularity. + + Write a script to sort the language based on popularity. + +Example 1 + +Input: @lang = ('perl', 'c', 'python') + @popularity = (2, 1, 3) +Output: ('c', 'perl', 'python') + +Example 2 + +Input: @lang = ('c++', 'haskell', 'java') + @popularity = (1, 3, 2) +Output: ('c++', 'java', 'haskell') + +Task 2: Largest of Three +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "cmp" + "fmt" + "reflect" + "slices" +) + +type ranks []int + +type words []string + +func (w words) sort(r ranks) words { + slices.SortFunc(w, func(a, b string) int { + return cmp.Compare(r[slices.Index(w, a)], r[slices.Index(w, b)]) + }) + return w +} + +func main() { + for _, data := range []struct { + input1 words + input2 ranks + output words + }{ + {words{"perl", "c", "python"}, ranks{2, 1, 3}, words{"c", "perl", "python"}}, + {words{"c++", "haskell", "java"}, ranks{1, 3, 2}, words{"c++", "java", "haskell"}}, + } { + fmt.Println(reflect.DeepEqual(data.input1.sort(data.input2), data.output)) + } +} diff --git a/challenge-245/pokgopun/go/ch-2.go b/challenge-245/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..74527e790b --- /dev/null +++ b/challenge-245/pokgopun/go/ch-2.go @@ -0,0 +1,150 @@ +//# 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: @digits = (8, 1, 9) +Output: 981 + +981 % 3 == 0 + +Example 2 + +Input: @digits = (8, 6, 7, 1, 0) +Output: 8760 + +Example 3 + +Input: @digits = (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 + +package main + +import ( + "fmt" + "reflect" + "slices" +) + +func main() { + for _, data := range []struct { + input digits + output int + }{ + {digits{8, 1, 9}, 981}, + {digits{8, 6, 7, 1, 0}, 8760}, + {digits{1}, -1}, + } { + fmt.Println(reflect.DeepEqual(data.output, data.input.lot())) + } +} + +type digits []int + +func (d digits) concat() int { + r := d[0] + var n, t int + for _, v := range d[1:] { + n, t = v, 10 + for n > 10 { + t *= 10 + n /= 10 + } + r = r*t + v + } + return r +} + +func (d digits) lot() int { + slices.Sort(d) + slices.Reverse(d) + l := len(d) + for i := l; i > 0; i-- { + res, closer := d.permute(i) + for v := range res { + r := v.concat() + if r%3 == 0 { + closer() + return r + } + } + } + return -1 +} + +// transcribed from https://docs.python.org/3/library/itertools.html#itertools.permutations +func (d digits) permute(r int) (res chan digits, closer func()) { + res = make(chan digits) + done := make(chan struct{}) + n := len(d) + idx := make([]int, n) + for i := range idx { + idx[i] = i + } + cyc := make([]int, r) + for i := range cyc { + cyc[i] = n - i + } + go func() { + r0 := make(digits, r) + copy(r0, d[:r]) + select { + case <-done: + close(res) + return + case res <- r0: + } + for { + for i := r - 1; i >= 0; i-- { + cyc[i]-- + if cyc[i] == 0 { + idx = append(idx, idx[i]) + idx = slices.Delete(idx, i, i+1) + cyc[i] = n - i + } else { + j := cyc[i] + idx[i], idx[n-j] = idx[n-j], idx[i] + rn := make(digits, r) + for i, v := range idx[:r] { + rn[i] = d[v] + } + select { + case <-done: + close(res) + return + case res <- rn: + } + break + } + if i == 0 { + close(res) + return + } + } + } + close(res) + }() + return res, func() { + close(done) + } +} -- cgit From 2b1cc6e13bda42ad4aab75a822317e6de553e508 Mon Sep 17 00:00:00 2001 From: Michael Manring Date: Tue, 28 Nov 2023 21:01:49 +1100 Subject: pwc245 solution - updated task2 for go/python to refect the change in requirement from @digits to @ints --- challenge-245/pokgopun/go/ch-2.go | 45 ++++++++++++++++++++--------------- challenge-245/pokgopun/python/ch-2.py | 39 ++++++++++++++++++++---------- 2 files changed, 53 insertions(+), 31 deletions(-) diff --git a/challenge-245/pokgopun/go/ch-2.go b/challenge-245/pokgopun/go/ch-2.go index 74527e790b..b707bf3662 100644 --- a/challenge-245/pokgopun/go/ch-2.go +++ b/challenge-245/pokgopun/go/ch-2.go @@ -14,19 +14,19 @@ Submitted by: [46]Mohammad S Anwar Example 1 -Input: @digits = (8, 1, 9) +Input: @ints = (8, 1, 9) Output: 981 981 % 3 == 0 Example 2 -Input: @digits = (8, 6, 7, 1, 0) +Input: @ints = (8, 6, 7, 1, 0) Output: 8760 Example 3 -Input: @digits = (1) +Input: @ints = (1) Output: -1 __________________________________________________________________ @@ -48,20 +48,26 @@ import ( func main() { for _, data := range []struct { - input digits + input ints output int }{ - {digits{8, 1, 9}, 981}, - {digits{8, 6, 7, 1, 0}, 8760}, - {digits{1}, -1}, + {ints{8, 1, 9}, 981}, + {ints{8, 6, 7, 1, 0}, 8760}, + {ints{1}, -1}, + {ints{0, 0, 0}, 0}, + {ints{4, 8, 911}, 9114}, + {ints{8, 85, 0}, 8850}, + {ints{8, 89, 2}, 8982}, + {ints{8, 76, 0}, 8760}, + {ints{8, 94, 0}, 9480}, } { fmt.Println(reflect.DeepEqual(data.output, data.input.lot())) } } -type digits []int +type ints []int -func (d digits) concat() int { +func (d ints) concat() int { r := d[0] var n, t int for _, v := range d[1:] { @@ -75,26 +81,27 @@ func (d digits) concat() int { return r } -func (d digits) lot() int { - slices.Sort(d) - slices.Reverse(d) +func (d ints) lot() int { + max := -1 l := len(d) for i := l; i > 0; i-- { res, closer := d.permute(i) for v := range res { r := v.concat() if r%3 == 0 { - closer() - return r + if r > max { + max = r + } } } + closer() } - return -1 + return max } // transcribed from https://docs.python.org/3/library/itertools.html#itertools.permutations -func (d digits) permute(r int) (res chan digits, closer func()) { - res = make(chan digits) +func (d ints) permute(r int) (res chan ints, closer func()) { + res = make(chan ints) done := make(chan struct{}) n := len(d) idx := make([]int, n) @@ -106,7 +113,7 @@ func (d digits) permute(r int) (res chan digits, closer func()) { cyc[i] = n - i } go func() { - r0 := make(digits, r) + r0 := make(ints, r) copy(r0, d[:r]) select { case <-done: @@ -124,7 +131,7 @@ func (d digits) permute(r int) (res chan digits, closer func()) { } else { j := cyc[i] idx[i], idx[n-j] = idx[n-j], idx[i] - rn := make(digits, r) + rn := make(ints, r) for i, v := range idx[:r] { rn[i] = d[v] } diff --git a/challenge-245/pokgopun/python/ch-2.py b/challenge-245/pokgopun/python/ch-2.py index d689e65c04..8256bbcfa6 100644 --- a/challenge-245/pokgopun/python/ch-2.py +++ b/challenge-245/pokgopun/python/ch-2.py @@ -14,19 +14,19 @@ Submitted by: [46]Mohammad S Anwar Example 1 -Input: @digits = (8, 1, 9) +Input: @ints = (8, 1, 9) Output: 981 981 % 3 == 0 Example 2 -Input: @digits = (8, 6, 7, 1, 0) +Input: @ints = (8, 6, 7, 1, 0) Output: 8760 Example 3 -Input: @digits = (1) +Input: @ints = (1) Output: -1 __________________________________________________________________ @@ -38,22 +38,37 @@ SO WHAT DO YOU THINK ? """ ### solution by pokgopun@gmail.com -from itertools import permutations +from itertools import permutations, chain def lot(tup: tuple): - for n in range(len(tup),0,-1): - for x in permutations( - sorted(tup,reverse=True), n - ): - y = int( - "".join( str(e) for e in x ) + return max( + chain.from_iterable( + ( + ( i for i in + ( + int("".join(x)) for x in + ( + (str(e) for e in y) for y in + chain.from_iterable( + permutations(tup, n) for n in range(1,len(tup)+1) + ) + ) + ) if i % 3 == 0 + ), + (-1,), ) - if y % 3 == 0: return y - return -1 + ) + ) 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==lot(inpt)) -- cgit