diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-08-19 11:49:34 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-08-19 11:49:34 +0100 |
| commit | 7805002ab17480269db043a2272ba2b523ec0aa0 (patch) | |
| tree | b83acb1e5bdece12c2d206f42eebdf2cc8960bc0 | |
| parent | 2f959aae8b09ec88c7b846586640f9661b568c28 (diff) | |
| parent | c1ac9d2e4ac0055d01ca625652003464761cfe38 (diff) | |
| download | perlweeklychallenge-club-7805002ab17480269db043a2272ba2b523ec0aa0.tar.gz perlweeklychallenge-club-7805002ab17480269db043a2272ba2b523ec0aa0.tar.bz2 perlweeklychallenge-club-7805002ab17480269db043a2272ba2b523ec0aa0.zip | |
Merge pull request #12538 from pokgopun/pwc335
Pwc335
| -rw-r--r-- | challenge-335/pokgopun/go/ch-1.go | 92 | ||||
| -rw-r--r-- | challenge-335/pokgopun/go/ch-2.go | 169 | ||||
| -rw-r--r-- | challenge-335/pokgopun/python/ch-1.py | 68 | ||||
| -rw-r--r-- | challenge-335/pokgopun/python/ch-2.py | 114 |
4 files changed, 443 insertions, 0 deletions
diff --git a/challenge-335/pokgopun/go/ch-1.go b/challenge-335/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..31a43405b6 --- /dev/null +++ b/challenge-335/pokgopun/go/ch-1.go @@ -0,0 +1,92 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-335/ +/*# + +Task 1: Common Characters + +Submitted by: [41]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given an array of words. + + Write a script to return all characters that is in every word in the + given array including duplicates. + +Example 1 + +Input: @words = ("bella", "label", "roller") +Output: ("e", "l", "l") + +Example 2 + +Input: @words = ("cool", "lock", "cook") +Output: ("c", "o") + +Example 3 + +Input: @words = ("hello", "world", "pole") +Output: ("l", "o") + +Example 4 + +Input: @words = ("abc", "def", "ghi") +Output: () + +Example 5 + +Input: @words = ("aab", "aac", "aaa") +Output: ("a", "a") + +Task 2: Find Winner +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "github.com/google/go-cmp/cmp" + "io" + "os" + "slices" +) + +func cc(words []string) []rune { + res := []rune{} + rmn := [][]rune{} + for _, v := range words[1:] { + rmn = append(rmn, []rune(v)) + } + l := len(rmn) + for _, v := range words[0] { + i := 0 + for i < l { + j := slices.Index(rmn[i], v) + if j < 0 { + break + } + copy(rmn[i][j:], rmn[i][j+1:]) + rmn[i] = rmn[i][:len(rmn[i])-1] + i++ + } + if i == l { + res = append(res, v) + } + } + return res +} + +func main() { + for _, data := range []struct { + input []string + output []rune + }{ + {[]string{"bella", "label", "roller"}, []rune{'e', 'l', 'l'}}, + {[]string{"cool", "lock", "cook"}, []rune{'c', 'o'}}, + {[]string{"hello", "world", "pole"}, []rune{'l', 'o'}}, + {[]string{"abc", "def", "ghi"}, []rune{}}, + {[]string{"aab", "aac", "aaa"}, []rune{'a', 'a'}}, + {[]string{"กาลก่อน", "กรรมตามทันมาก", "กากี"}, []rune{'ก', 'า', 'ก'}}, + {[]string{"รอทัก", "รับกรรม", "ปรปักษ์"}, []rune{'ร', 'ั', 'ก'}}, + } { + io.WriteString(os.Stdout, cmp.Diff(cc(data.input), data.output)) // blank if ok, otherwise show the difference + } +} diff --git a/challenge-335/pokgopun/go/ch-2.go b/challenge-335/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..d6c41f0729 --- /dev/null +++ b/challenge-335/pokgopun/go/ch-2.go @@ -0,0 +1,169 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-335/ +/*# + +Task 2: Find Winner + +Submitted by: [42]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given an array of all moves by the two players. + + Write a script to find the winner of the TicTacToe game if found based + on the moves provided in the given array. + +Example 1 + +Input: @moves = ([0,0],[2,0],[1,1],[2,1],[2,2]) +Output: A + +Game Board: +[ A _ _ ] +[ B A B ] +[ _ _ A ] + +Example 2 + +Input: @moves = ([0,0],[1,1],[0,1],[0,2],[1,0],[2,0]) +Output: B + +Game Board: +[ A A B ] +[ A B _ ] +[ B _ _ ] + +Example 3 + +Input: @moves = ([0,0],[1,1],[2,0],[1,0],[1,2],[2,1],[0,1],[0,2],[2,2]) +Output: Draw + +Game Board: +[ A A B ] +[ B B A ] +[ A B A ] + +Example 4 + +Input: @moves = ([0,0],[1,1]) +Output: Pending + +Game Board: +[ A _ _ ] +[ _ B _ ] +[ _ _ _ ] + +Example 5 + +Input: @moves = ([1,1],[0,0],[2,2],[0,1],[1,0],[0,2]) +Output: B + +Game Board: +[ B B B ] +[ A A _ ] +[ _ _ A ] + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 25th August + 2025. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "github.com/google/go-cmp/cmp" + "io" + "os" +) + +type Result int + +const ( + A Result = iota + B + Draw + Pending + InputError +) + +type Move struct { + x, y int +} + +type Moves []Move + +func (ms Moves) Process() Result { + m2p := make(map[Move]int) + for i, v := range ms { + for _, e := range []int{v.x,v.y}{ + if e < 0 || e > 2 { + return InputError + } + } + if i%2 == 0 { + m2p[v]++ + } else { + m2p[v]-- + } + } + if len(ms) != len(m2p) { + return InputError + } + res := Draw + type score []int + var scores []score + var scoredd, scoreda score + for i := range 3 { + var scoreh, scorev score + for e := range 3 { + scoreh = append(scoreh, m2p[Move{e, i}]) + scorev = append(scorev, m2p[Move{i, e}]) + } + scores = append(scores, scoreh, scorev) + scoredd = append(scoredd, m2p[Move{i, i}]) + scoreda = append(scoreda, m2p[Move{i, 2 - i}]) + } + for _, v := range append(scores, scoredd, scoreda) { + var c, s int + for _, p := range v { + if p != 0 { + c++ + s += p + } + } + if c < 2 { + res = Pending + continue + } + switch s { + case 3: + return A + case -3: + return B + case 2, -2: + res = Pending + } + } + return res +} + +func main() { + for _, data := range []struct { + input Moves + output Result + }{ + {Moves{Move{0, 0}, Move{2, 0}, Move{1, 1}, Move{2, 1}, Move{2, 2}}, A}, + {Moves{Move{0, 0}, Move{1, 1}, Move{0, 1}, Move{0, 2}, Move{1, 0}, Move{2, 0}}, B}, + {Moves{Move{0, 0}, Move{1, 1}, Move{2, 0}, Move{1, 0}, Move{1, 2}, Move{2, 1}, Move{0, 1}, Move{0, 2}, Move{2, 2}}, Draw}, + {Moves{Move{0, 0}, Move{1, 1}}, Pending}, + {Moves{Move{1, 1}, Move{0, 0}, Move{2, 2}, Move{0, 1}, Move{1, 0}, Move{0, 2}}, B}, + {Moves{Move{1, 1}, Move{0, 0}, Move{1, 1}, Move{0, 1}, Move{1, 0}, Move{0, 2}}, InputError}, + {Moves{Move{1, 1}, Move{0, 0}, Move{2, 2}, Move{2, 2}, Move{1, 0}, Move{0, 2}}, InputError}, + {Moves{Move{1, 1}, Move{0, 0}, Move{2, 2}, Move{3, 1}, Move{1, 0}, Move{0, 2}}, InputError}, + {Moves{Move{1, 1}, Move{0, 0}, Move{2, 2}, Move{0, 1}, Move{1, -1}, Move{0, 2}}, InputError}, + } { + io.WriteString(os.Stdout, cmp.Diff(data.input.Process(), data.output)) // blank if ok, otherwise show the difference + } +} diff --git a/challenge-335/pokgopun/python/ch-1.py b/challenge-335/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..bc125d7aa6 --- /dev/null +++ b/challenge-335/pokgopun/python/ch-1.py @@ -0,0 +1,68 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-335/ +""" + +Task 1: Common Characters + +Submitted by: [41]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given an array of words. + + Write a script to return all characters that is in every word in the + given array including duplicates. + +Example 1 + +Input: @words = ("bella", "label", "roller") +Output: ("e", "l", "l") + +Example 2 + +Input: @words = ("cool", "lock", "cook") +Output: ("c", "o") + +Example 3 + +Input: @words = ("hello", "world", "pole") +Output: ("l", "o") + +Example 4 + +Input: @words = ("abc", "def", "ghi") +Output: () + +Example 5 + +Input: @words = ("aab", "aac", "aaa") +Output: ("a", "a") + +Task 2: Find Winner +""" +### solution by pokgopun@gmail.com + +def cc(words: tuple[str]) -> tuple[str]: + res = list(words[0]) + rmn = [list(e) for e in words[1:]] + for c in words[0]: + for word in rmn: + try: + word.remove(c) + except: + res.remove(c) + break + return tuple(res) + +import unittest + +class TestCc(unittest.TestCase): + def test(self): + for inpt, otpt in { + ("bella", "label", "roller"): ("e", "l", "l"), + ("cool", "lock", "cook"): ("c", "o"), + ("hello", "world", "pole"): ("l", "o"), + ("abc", "def", "ghi"): (), + ("aab", "aac", "aaa"): ("a", "a"), + }.items(): + self.assertEqual(cc(inpt),otpt) + +unittest.main() diff --git a/challenge-335/pokgopun/python/ch-2.py b/challenge-335/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..73319fa0a4 --- /dev/null +++ b/challenge-335/pokgopun/python/ch-2.py @@ -0,0 +1,114 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-335/ +""" + +Task 2: Find Winner + +Submitted by: [42]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given an array of all moves by the two players. + + Write a script to find the winner of the TicTacToe game if found based + on the moves provided in the given array. + +Example 1 + +Input: @moves = ([0,0],[2,0],[1,1],[2,1],[2,2]) +Output: A + +Game Board: +[ A _ _ ] +[ B A B ] +[ _ _ A ] + +Example 2 + +Input: @moves = ([0,0],[1,1],[0,1],[0,2],[1,0],[2,0]) +Output: B + +Game Board: +[ A A B ] +[ A B _ ] +[ B _ _ ] + +Example 3 + +Input: @moves = ([0,0],[1,1],[2,0],[1,0],[1,2],[2,1],[0,1],[0,2],[2,2]) +Output: Draw + +Game Board: +[ A A B ] +[ B B A ] +[ A B A ] + +Example 4 + +Input: @moves = ([0,0],[1,1]) +Output: Pending + +Game Board: +[ A _ _ ] +[ _ B _ ] +[ _ _ _ ] + +Example 5 + +Input: @moves = ([1,1],[0,0],[2,2],[0,1],[1,0],[0,2]) +Output: B + +Game Board: +[ B B B ] +[ A A _ ] +[ _ _ A ] + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 25th August + 2025. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +def fw(moves: tuple[tuple[int]]) -> str: + m2s: dict[tuple[int],int] = {} + for i in range(len(moves)): + m2s[moves[i]] = (-1)**(i % 2) + res = "Draw" + scores: list[list[int]] = [] + scoredd: list[int] = [] + scoreda: list[int] = [] + for i in range(3): + scores.extend(( + list(m2s.get((i,e),0) for e in range(3)), + list(m2s.get((e,i),0) for e in range(3)), + )) + scoredd.append(m2s.get((i,i),0)) + scoreda.append(m2s.get((i,2-i),0)) + for score in scores + [scoredd,scoreda]: + if score.count(0) > 1: + res = "Pending" + continue + match sum(score): + case 3: + return "A" + case -3: + return "B" + case 2,-2: + res = "Pending" + return res + +import unittest + +class TestFw(unittest.TestCase): + def test(self): + for inpt, otpt in { + ((0,0),(2,0),(1,1),(2,1),(2,2)): "A", + ((0,0),(1,1),(0,1),(0,2),(1,0),(2,0)): "B", + ((0,0),(1,1),(2,0),(1,0),(1,2),(2,1),(0,1),(0,2),(2,2)): "Draw", + ((0,0),(1,1)): "Pending", + ((1,1),(0,0),(2,2),(0,1),(1,0),(0,2)): "B", + }.items(): + self.assertEqual(fw(inpt),otpt) + +unittest.main() |
