diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-01-08 18:24:01 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-01-08 18:24:01 +0000 |
| commit | 3bc37fb8873f74c26d0324e52825ee25c8222177 (patch) | |
| tree | 41f1a366453f0811fb4fdd136a72b86b98091faf | |
| parent | b45d97f7b7b74cbf0fcd34a533b032ccad5b9bff (diff) | |
| parent | 87a1cd6110b045d9095085a87d46515abd8a2da4 (diff) | |
| download | perlweeklychallenge-club-3bc37fb8873f74c26d0324e52825ee25c8222177.tar.gz perlweeklychallenge-club-3bc37fb8873f74c26d0324e52825ee25c8222177.tar.bz2 perlweeklychallenge-club-3bc37fb8873f74c26d0324e52825ee25c8222177.zip | |
Merge pull request #9367 from pokgopun/pwc251
pwc251 solution
| -rw-r--r-- | challenge-251/pokgopun/go/ch-1.go | 119 | ||||
| -rw-r--r-- | challenge-251/pokgopun/go/ch-2.go | 110 | ||||
| -rw-r--r-- | challenge-251/pokgopun/python/ch-1.py | 88 | ||||
| -rw-r--r-- | challenge-251/pokgopun/python/ch-2.py | 78 |
4 files changed, 395 insertions, 0 deletions
diff --git a/challenge-251/pokgopun/go/ch-1.go b/challenge-251/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..f74e5e089d --- /dev/null +++ b/challenge-251/pokgopun/go/ch-1.go @@ -0,0 +1,119 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-251/ +/*# + +Task 1: Concatenation Value + +Submitted by: [52]Mohammad S Anwar + __________________________________________________________________ + + You are given an array of integers, @ints. + + Write a script to find the concatenation value of the given array. + + The concatenation of two numbers is the number formed by concatenating + their numerals. +For example, the concatenation of 10, 21 is 1021. +The concatenation value of @ints is initially equal to 0. +Perform this operation until @ints becomes empty: + +If there exists more than one number in @ints, pick the first element +and last element in @ints respectively and add the value of their +concatenation to the concatenation value of @ints, then delete the +first and last element from @ints. + +If one element exists, add its value to the concatenation value of +@ints, then delete it. + +Example 1 + +Input: @ints = (6, 12, 25, 1) +Output: 1286 + +1st operation: concatenation of 6 and 1 is 61 +2nd operation: concaternation of 12 and 25 is 1225 + +Concatenation Value => 61 + 1225 => 1286 + +Example 2 + +Input: @ints = (10, 7, 31, 5, 2, 2) +Output: 489 + +1st operation: concatenation of 10 and 2 is 102 +2nd operation: concatenation of 7 and 2 is 72 +3rd operation: concatenation of 31 and 5 is 315 + +Concatenation Value => 102 + 72 + 315 => 489 + +Example 3 + +Input: @ints = (1, 2, 10) +Output: 112 + +1st operation: concatenation of 1 and 10 is 110 +2nd operation: only element left is 2 + +Concatenation Value => 110 + 2 => 112 + +Task 2: Lucky Numbers +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + + "github.com/google/go-cmp/cmp" +) + +type ints []int + +func (is ints) concat() int { + l := len(is) + switch l { + case 0: + return 0 + case 1: + return is[0] + default: + c := is[0] + for i := 1; i < l; i++ { + lim := is[i] + for lim > 0 { + lim /= 10 + c *= 10 + } + c += is[i] + } + return c + } +} + +func (is ints) concatenation() int { + l := len(is) + lim := l / 2 + c := 0 + for i := 0; i < lim; i++ { + //fmt.Println(ints{is[i], is[l-1-i]}.concat()) + c += ints{is[i], is[l-1-i]}.concat() + } + if l%2 == 1 { + c += is[lim] + } + return c +} + +func main() { + for _, data := range []struct { + input ints + output int + }{ + {ints{6, 12, 25, 1}, 1286}, + {ints{10, 7, 31, 5, 2, 2}, 489}, + {ints{1, 2, 10}, 112}, + } { + io.WriteString(os.Stdout, cmp.Diff(data.input.concatenation(), data.output)) // display nothing if ok, otherwise show the difference + } +} diff --git a/challenge-251/pokgopun/go/ch-2.go b/challenge-251/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..f4dfec84ea --- /dev/null +++ b/challenge-251/pokgopun/go/ch-2.go @@ -0,0 +1,110 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-251/ +/*# + +Task 2: Lucky Numbers + +Submitted by: [53]Mohammad S Anwar + __________________________________________________________________ + + You are given a m x n matrix of distinct numbers. + + Write a script to return the lucky number, if there is one, or -1 if + not. +A lucky number is an element of the matrix such that it is +the minimum element in its row and maximum in its column. + +Example 1 + +Input: $matrix = [ [ 3, 7, 8], + [ 9, 11, 13], + [15, 16, 17] ]; +Output: 15 + +15 is the only lucky number since it is the minimum in its row +and the maximum in its column. + +Example 2 + +Input: $matrix = [ [ 1, 10, 4, 2], + [ 9, 3, 8, 7], + [15, 16, 17, 12] ]; +Output: 12 + +Example 3 + +Input: $matrix = [ [7 ,8], + [1 ,2] ]; +Output: 7 + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 14th January + 2024. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + + "github.com/google/go-cmp/cmp" +) + +type row []int + +func (rw row) minIdx() int { + l := len(rw) + switch l { + case 0: + return -1 + case 1: + return 0 + default: + j := 0 + for i, v := range rw[1:] { + if rw[j] > v { + j = i + 1 + } + } + return j + } +} + +type matrix []row + +func (mt matrix) lucky() int { + l := len(mt) + switch l { + case 1: + return mt[0][mt[0].minIdx()] + default: + for _, rw := range mt { + i := rw.minIdx() + mx := mt[0][i] + for _, v := range mt[1:] { + mx = max(mx, v[i]) + } + if mx == rw[i] { + return mx + } + } + } + return -1 +} + +func main() { + for _, data := range []struct { + input matrix + output int + }{ + {matrix{row{3, 7, 8}, row{9, 11, 13}, row{15, 16, 17}}, 15}, + {matrix{row{1, 10, 4, 2}, row{9, 3, 8, 7}, row{15, 16, 17, 12}}, 12}, + {matrix{row{7, 8}, row{1, 2}}, 7}, + } { + io.WriteString(os.Stdout, cmp.Diff(data.input.lucky(), data.output)) // display nothing if ok, otherwise show the difference + } +} diff --git a/challenge-251/pokgopun/python/ch-1.py b/challenge-251/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..41fd8c1c0c --- /dev/null +++ b/challenge-251/pokgopun/python/ch-1.py @@ -0,0 +1,88 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-251/ +""" + +Task 1: Concatenation Value + +Submitted by: [52]Mohammad S Anwar + __________________________________________________________________ + + You are given an array of integers, @ints. + + Write a script to find the concatenation value of the given array. + + The concatenation of two numbers is the number formed by concatenating + their numerals. +For example, the concatenation of 10, 21 is 1021. +The concatenation value of @ints is initially equal to 0. +Perform this operation until @ints becomes empty: + +If there exists more than one number in @ints, pick the first element +and last element in @ints respectively and add the value of their +concatenation to the concatenation value of @ints, then delete the +first and last element from @ints. + +If one element exists, add its value to the concatenation value of +@ints, then delete it. + +Example 1 + +Input: @ints = (6, 12, 25, 1) +Output: 1286 + +1st operation: concatenation of 6 and 1 is 61 +2nd operation: concaternation of 12 and 25 is 1225 + +Concatenation Value => 61 + 1225 => 1286 + +Example 2 + +Input: @ints = (10, 7, 31, 5, 2, 2) +Output: 489 + +1st operation: concatenation of 10 and 2 is 102 +2nd operation: concatenation of 7 and 2 is 72 +3rd operation: concatenation of 31 and 5 is 315 + +Concatenation Value => 102 + 72 + 315 => 489 + +Example 3 + +Input: @ints = (1, 2, 10) +Output: 112 + +1st operation: concatenation of 1 and 10 is 110 +2nd operation: only element left is 2 + +Concatenation Value => 110 + 2 => 112 + +Task 2: Lucky Numbers +""" +### solution by pokgopun@gmail.com + +def concatenation(tup: tuple): + #print(tup) + l = len(tup) + sum = 0 + for i in range(l//2): + #print(tup[i],tup[l-1-i],"=>",int(str(tup[i])+str(tup[l-1-i]))) + sum += int(str(tup[i])+str(tup[l-1-i])) + if l % 2: + #print("addition",tup[l//2]) + sum += tup[l//2] + return sum + +import unittest + +class TestConcatenation(unittest.TestCase): + def test(self): + for inpt,otpt in { + (6, 12, 25, 1): 1286, + (10, 7, 31, 5, 2, 2): 489, + (1, 2, 10): 112, + }.items(): + self.assertEqual(concatenation(inpt),otpt) + +unittest.main() + + + diff --git a/challenge-251/pokgopun/python/ch-2.py b/challenge-251/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..c987d9f4df --- /dev/null +++ b/challenge-251/pokgopun/python/ch-2.py @@ -0,0 +1,78 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-251/ +""" + +Task 2: Lucky Numbers + +Submitted by: [53]Mohammad S Anwar + __________________________________________________________________ + + You are given a m x n matrix of distinct numbers. + + Write a script to return the lucky number, if there is one, or -1 if + not. +A lucky number is an element of the matrix such that it is +the minimum element in its row and maximum in its column. + +Example 1 + +Input: $matrix = [ [ 3, 7, 8], + [ 9, 11, 13], + [15, 16, 17] ]; +Output: 15 + +15 is the only lucky number since it is the minimum in its row +and the maximum in its column. + +Example 2 + +Input: $matrix = [ [ 1, 10, 4, 2], + [ 9, 3, 8, 7], + [15, 16, 17, 12] ]; +Output: 12 + +Example 3 + +Input: $matrix = [ [7 ,8], + [1 ,2] ]; +Output: 7 + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 14th January + 2024. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +def luckynumber(tup: list): + #print(tup) + res = -1 + for i in range(len(tup)): + mn = min(tup[i]) + #print("mn =",mn) + j = tup[i].index(mn) + if mn == max(e[j] for e in tup): + res = mn + break + return res + +import unittest + +class TestLuckynumber(unittest.TestCase): + def test(self): + for inpt,otpt in { + ( ( 3, 7, 8), + ( 9, 11, 13), + (15, 16, 17) ): 15, + ( ( 1, 10, 4, 2), + ( 9, 3, 8, 7), + (15, 16, 17, 12) ): 12, + ( (7 ,8), + (1 ,2) ): 7, + }.items(): + self.assertEqual(luckynumber(inpt),otpt) + +unittest.main() + + |
