diff options
| -rw-r--r-- | challenge-322/pokgopun/go/ch-1.go | 87 | ||||
| -rw-r--r-- | challenge-322/pokgopun/go/ch-2.go | 90 | ||||
| -rw-r--r-- | challenge-322/pokgopun/python/ch-1.py | 62 | ||||
| -rw-r--r-- | challenge-322/pokgopun/python/ch-2.py | 60 |
4 files changed, 299 insertions, 0 deletions
diff --git a/challenge-322/pokgopun/go/ch-1.go b/challenge-322/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..00f943fed2 --- /dev/null +++ b/challenge-322/pokgopun/go/ch-1.go @@ -0,0 +1,87 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-322/ +/*# + +Task 1: String Format + +Submitted by: [45]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a string and a positive integer. + + Write a script to format the string, removing any dashes, in groups of + size given by the integer. The first group can be smaller than the + integer but should have at least one character. Groups should be + separated by dashes. + +Example 1 + +Input: $str = "ABC-D-E-F", $i = 3 +Output: "ABC-DEF" + +Example 2 + +Input: $str = "A-BC-D-E", $i = 2 +Output: "A-BC-DE" + +Example 3 + +Input: $str = "-A-B-CD-E", $i = 4 +Output: "A-BCDE" + +Task 2: Rank Array +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + "slices" + + "github.com/google/go-cmp/cmp" +) + +type input struct { + str string + i int +} + +func (in input) process() string { + var ( + b byte + bs []byte + ) + sep := byte('-') + i := len(in.str) + l := 0 + for i > 0 { + i-- + b = in.str[i] + if b == sep { + continue + } + if l < in.i { + bs = append(bs, b) + l++ + } else { + bs = append(bs, sep, b) + l = 1 + } + } + slices.Reverse(bs) + return string(bs) +} + +func main() { + for _, data := range []struct { + input input + output string + }{ + {input{"ABC-D-E-F", 3}, "ABC-DEF"}, + {input{"A-BC-D-E", 2}, "A-BC-DE"}, + {input{"-A-B-CD-E", 4}, "A-BCDE"}, + } { + io.WriteString(os.Stdout, cmp.Diff(data.input.process(), data.output)) // blank if ok, otherwise show the difference + } +} diff --git a/challenge-322/pokgopun/go/ch-2.go b/challenge-322/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..4501775dd7 --- /dev/null +++ b/challenge-322/pokgopun/go/ch-2.go @@ -0,0 +1,90 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-322/ +/*# + +Task 2: Rank Array + +Submitted by: [46]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given an array of integers. + + Write a script to return an array of the ranks of each element: the + lowest value has rank 1, next lowest rank 2, etc. If two elements are + the same then they share the same rank. + +Example 1 + +Input: @ints = (55, 22, 44, 33) +Output: (4, 1, 3, 2) + +Example 2 + +Input: @ints = (10, 10, 10) +Output: (1, 1, 1) + +Example 3 + +Input: @ints = (5, 1, 1, 4, 3) +Output: (4, 1, 1, 3, 2) + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 25th May 2025. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + "sort" + + "github.com/google/go-cmp/cmp" +) + +type intPair struct { + a, b int +} + +type ints []int + +func (in ints) process() ints { + var ps []intPair + for i, v := range in { + ps = append(ps, intPair{v, i}) + } + sort.Slice(ps, func(i, j int) bool { + return ps[i].a < ps[j].a + }) + r := 1 + ps2 := []intPair{intPair{ps[0].b, r}} + for i := range len(ps) - 1 { + if ps[i+1].a > ps[i].a { + r++ + } + ps2 = append(ps2, intPair{ps[i+1].b, r}) + } + sort.Slice(ps2, func(i, j int) bool { + return ps2[i].a < ps2[j].a + }) + var c ints + for _, v := range ps2 { + c = append(c, v.b) + } + return c +} + +func main() { + for _, data := range []struct { + input, output ints + }{ + {ints{55, 22, 44, 33}, ints{4, 1, 3, 2}}, + {ints{10, 10, 10}, ints{1, 1, 1}}, + {ints{5, 1, 1, 4, 3}, ints{4, 1, 1, 3, 2}}, + } { + io.WriteString(os.Stdout, cmp.Diff(data.input.process(), data.output)) //blank if ok, otherwise show the difference + } +} diff --git a/challenge-322/pokgopun/python/ch-1.py b/challenge-322/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..5a01b7908d --- /dev/null +++ b/challenge-322/pokgopun/python/ch-1.py @@ -0,0 +1,62 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-322/ +""" + +Task 1: String Format + +Submitted by: [45]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a string and a positive integer. + + Write a script to format the string, removing any dashes, in groups of + size given by the integer. The first group can be smaller than the + integer but should have at least one character. Groups should be + separated by dashes. + +Example 1 + +Input: $str = "ABC-D-E-F", $i = 3 +Output: "ABC-DEF" + +Example 2 + +Input: $str = "A-BC-D-E", $i = 2 +Output: "A-BC-DE" + +Example 3 + +Input: $str = "-A-B-CD-E", $i = 4 +Output: "A-BCDE" + +Task 2: Rank Array +""" +### solution by pokgopun@gmail.com + +def sf(string: str, n: int) -> str: + slt = "" + i = len(string) + l = 0 + while i > 0: + i -= 1 + if string[i] == "-": + continue + if l < n: + slt = string[i] + slt + l += 1 + else: + slt = string[i] + "-" + slt + l = 1 + return slt + +import unittest + +class TestSf(unittest.TestCase): + def test(self): + for (string,n), otpt in { + ("ABC-D-E-F", 3):"ABC-DEF", + ("A-BC-D-E", 2): "A-BC-DE", + ("-A-B-CD-E", 4): "A-BCDE", + }.items(): + self.assertEqual(sf(string,n),otpt) + +unittest.main() diff --git a/challenge-322/pokgopun/python/ch-2.py b/challenge-322/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..830e4888a2 --- /dev/null +++ b/challenge-322/pokgopun/python/ch-2.py @@ -0,0 +1,60 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-322/ +""" + +Task 2: Rank Array + +Submitted by: [46]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given an array of integers. + + Write a script to return an array of the ranks of each element: the + lowest value has rank 1, next lowest rank 2, etc. If two elements are + the same then they share the same rank. + +Example 1 + +Input: @ints = (55, 22, 44, 33) +Output: (4, 1, 3, 2) + +Example 2 + +Input: @ints = (10, 10, 10) +Output: (1, 1, 1) + +Example 3 + +Input: @ints = (5, 1, 1, 4, 3) +Output: (4, 1, 1, 3, 2) + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 25th May 2025. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +def ra(ints: tuple[int]) -> tuple[int]: + a = [(ints[i],i) for i in range(len(ints)) ] + a.sort() + r = 1 + b = [(a[0][1],r)] + for i in range(1,len(a)): + if a[i][0] > a[i-1][0]: + r += 1 + b.append((a[i][1],r)) + return tuple(e[1] for e in sorted(b)) + +import unittest + +class TestRa(unittest.TestCase): + def test(self): + for inpt, otpt in { + (55, 22, 44, 33):(4, 1, 3, 2), + (10, 10, 10):(1, 1, 1), + (5, 1, 1, 4, 3): (4, 1, 1, 3, 2), + }.items(): + self.assertEqual(ra(inpt),otpt) + +unittest.main() |
