aboutsummaryrefslogtreecommitdiff
path: root/challenge-335/sgreen/python
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-335/sgreen/python')
-rwxr-xr-xchallenge-335/sgreen/python/ch-1.py37
-rwxr-xr-xchallenge-335/sgreen/python/ch-2.py71
-rwxr-xr-xchallenge-335/sgreen/python/test.py30
3 files changed, 138 insertions, 0 deletions
diff --git a/challenge-335/sgreen/python/ch-1.py b/challenge-335/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..48a7ce776a
--- /dev/null
+++ b/challenge-335/sgreen/python/ch-1.py
@@ -0,0 +1,37 @@
+#!/usr/bin/env python3
+
+import sys
+from collections import Counter
+
+
+def common_characters(word_list: list[str]) -> list[str]:
+ """Find common characters in all words.
+
+ Args:
+ word_list (list[str]): List of words to compare.
+
+ Returns:
+ list[str]: List of common characters sorted alphabetically.
+ """
+ solution = []
+
+ # Turn the words into a frequency dict
+ freq_list = [Counter(word) for word in word_list]
+
+ # Find the minimum frequency of each character across all words
+ for letter in sorted(freq_list[0]):
+ min_freq = min(freq[letter] for freq in freq_list)
+ if min_freq > 0:
+ # ... and add it to the solution
+ solution.extend(letter * min_freq)
+
+ return solution
+
+
+def main():
+ result = common_characters(sys.argv[1:])
+ print(result)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/challenge-335/sgreen/python/ch-2.py b/challenge-335/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..bf9b286364
--- /dev/null
+++ b/challenge-335/sgreen/python/ch-2.py
@@ -0,0 +1,71 @@
+#!/usr/bin/env python3
+
+import sys
+
+
+def check_winner(board: list[list[str]]) -> str | None:
+ """
+ Check the current state of the Tic Tac Toe board for a winner.
+
+ Args:
+ board (list[list[str]]): The current state of the Tic Tac Toe board
+
+ Returns:
+ str | None: 'A' if player A wins, 'B' if player B wins, or None if no winner yet
+ """
+ # Check rows and columns
+ for i in range(3):
+ if board[i][0] == board[i][1] == board[i][2] != '_':
+ return board[i][0]
+ if board[0][i] == board[1][i] == board[2][i] != '_':
+ return board[0][i]
+
+ # Check diagonals
+ if board[0][0] == board[1][1] == board[2][2] != '_':
+ return board[0][0]
+ if board[0][2] == board[1][1] == board[2][0] != '_':
+ return board[0][2]
+
+ return None
+
+
+def find_winner(moves: list[list[int]]) -> str:
+ # Initialize the board
+ board = [['_' for _ in range(3)] for _ in range(3)]
+
+ # Player A starts first
+ current_player = 'A'
+
+ for move in moves:
+ # Check the move is on the board and not already taken
+ if not 0 <= move[0] <= 2 and not 0 <= move[1] <= 2:
+ return "Invalid move (out of bounds)"
+ if board[move[0]][move[1]] != '_':
+ return "Invalid move (already taken)"
+
+ # Place the move on the board
+ board[move[0]][move[1]] = current_player
+
+ # Check for a win
+ result = check_winner(board)
+ if result:
+ return result
+
+ # Switch players
+ current_player = 'B' if current_player == 'A' else 'A'
+
+ # We've made all moves, check for pending or draw
+ return "Pending" if any('_' in row for row in board) else "Draw"
+
+
+def main():
+ # Convert input into integers, and pair them up
+ array = [int(n) for n in sys.argv[1:]]
+ moves = [array[i:i + 2] for i in range(0, len(array), 2)]
+
+ result = find_winner(moves)
+ print(result)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/challenge-335/sgreen/python/test.py b/challenge-335/sgreen/python/test.py
new file mode 100755
index 0000000000..2f4a95cbef
--- /dev/null
+++ b/challenge-335/sgreen/python/test.py
@@ -0,0 +1,30 @@
+#!/usr/bin/env python3
+
+import unittest
+ch_1 = __import__('ch-1')
+ch_2 = __import__('ch-2')
+
+
+class TestClass(unittest.TestCase):
+ def test_ch_1(self):
+ self.assertEqual(ch_1.common_characters(["bella", "label", "roller"]), ["e", "l", "l"])
+ self.assertEqual(ch_1.common_characters(["cool", "lock", "cook"]), ["c", "o"])
+ self.assertEqual(ch_1.common_characters(["hello", "world", "pole"]), ["l", "o"])
+ self.assertEqual(ch_1.common_characters(["abc", "def", "ghi"]), [])
+ self.assertEqual(ch_1.common_characters(["aab", "aac", "aaa"]), ["a", "a"])
+
+ def test_ch_2(self):
+ moves_1 = [[0,0],[2,0],[1,1],[2,1],[2,2]]
+ moves_2 = [[0,0],[1,1],[0,1],[0,2],[1,0],[2,0]]
+ moves_3 = [[0,0],[1,1],[2,0],[1,0],[1,2],[2,1],[0,1],[0,2],[2,2]]
+ moves_4 = [[0,0],[1,1]]
+ moves_5 = [[1,1],[0,0],[2,2],[0,1],[1,0],[0,2]]
+ self.assertEqual(ch_2.find_winner(moves_1), "A")
+ self.assertEqual(ch_2.find_winner(moves_2), "B")
+ self.assertEqual(ch_2.find_winner(moves_3), "Draw")
+ self.assertEqual(ch_2.find_winner(moves_4), "Pending")
+ self.assertEqual(ch_2.find_winner(moves_5), "B")
+
+
+if __name__ == '__main__':
+ unittest.main()