diff options
| -rw-r--r-- | challenge-245/pokgopun/go/ch-1.go | 60 | ||||
| -rw-r--r-- | challenge-245/pokgopun/go/ch-2.go | 157 | ||||
| -rw-r--r-- | challenge-245/pokgopun/python/ch-1.py | 42 | ||||
| -rw-r--r-- | challenge-245/pokgopun/python/ch-2.py | 74 |
4 files changed, 333 insertions, 0 deletions
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..b707bf3662 --- /dev/null +++ b/challenge-245/pokgopun/go/ch-2.go @@ -0,0 +1,157 @@ +//# 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 + +package main + +import ( + "fmt" + "reflect" + "slices" +) + +func main() { + for _, data := range []struct { + input ints + output int + }{ + {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 ints []int + +func (d ints) 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 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 { + if r > max { + max = r + } + } + } + closer() + } + return max +} + +// transcribed from https://docs.python.org/3/library/itertools.html#itertools.permutations +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) + for i := range idx { + idx[i] = i + } + cyc := make([]int, r) + for i := range cyc { + cyc[i] = n - i + } + go func() { + r0 := make(ints, 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(ints, 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) + } +} 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..8256bbcfa6 --- /dev/null +++ b/challenge-245/pokgopun/python/ch-2.py @@ -0,0 +1,74 @@ +### 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 lot(tup: tuple): + 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,), + ) + ) + ) + +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)) |
