aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpok <pok@ailouros.nemeses.net>2025-08-18 22:08:09 +0700
committerpok <pok@ailouros.nemeses.net>2025-08-19 14:05:02 +0700
commita25424298927e69f5ab123c6dbc02086b4de67f9 (patch)
tree7526bfbae648a9578d3575e4da4387cfdcea4e62
parent4f766edf1327ad3628c824c3c00f1c1f10c50b38 (diff)
downloadperlweeklychallenge-club-a25424298927e69f5ab123c6dbc02086b4de67f9.tar.gz
perlweeklychallenge-club-a25424298927e69f5ab123c6dbc02086b4de67f9.tar.bz2
perlweeklychallenge-club-a25424298927e69f5ab123c6dbc02086b4de67f9.zip
pwc335 solution in python
-rw-r--r--challenge-335/pokgopun/python/ch-1.py68
-rw-r--r--challenge-335/pokgopun/python/ch-2.py114
2 files changed, 182 insertions, 0 deletions
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()