From 164441a4c9c4a741a0fcecb5ba452b5accdc98dd Mon Sep 17 00:00:00 2001 From: wuliaozhiji Date: Sat, 19 Feb 2022 12:42:11 -0500 Subject: [PATCH] [+] A1 P1.1 insert_move_sequence --- assignments/A2/a2_game_tree.py | 37 ++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/assignments/A2/a2_game_tree.py b/assignments/A2/a2_game_tree.py index b0c0c4a..0ee3e93 100644 --- a/assignments/A2/a2_game_tree.py +++ b/assignments/A2/a2_game_tree.py @@ -135,7 +135,44 @@ class GameTree: ii) First reverse the list, and then use a recursive helper method that calls `list.pop` on the list of moves. Just make sure the original list isn't changed when the function ends! + + >>> game_tree = GameTree() + >>> # Test duplicates: + >>> game_tree.add_subtree(GameTree('a2b3', False)) + >>> game_tree.insert_move_sequence(['a2b3']) + >>> len(game_tree.get_subtrees()) + 1 + >>> game_tree.insert_move_sequence(['c2d3', 'd4d3', 'd2c3']) + >>> game_tree.insert_move_sequence(['c2d3', 'd4d3', 'b1d3']) + >>> len(game_tree.get_subtrees()) + 2 + >>> sub1 = game_tree.find_subtree_by_move('c2d3') + >>> len(sub1.get_subtrees()) + 1 + >>> sub2 = sub1.find_subtree_by_move('d4d3') + >>> len(sub2.get_subtrees()) + 2 + >>> sub3 = sub2.find_subtree_by_move('d2c3') + >>> sub3.get_subtrees() + [] """ + self.insert_move_sequence_helper(moves, 0) + + def insert_move_sequence_helper(self, moves: list[str], index: int) -> None: + # All moves inserted, finish + if index == len(moves): + return + + # Next root already exists + for c in self._subtrees: + if c.move == moves[index]: + c.insert_move_sequence_helper(moves, index + 1) + return + + # Next root doesn't exist + sub = GameTree(moves[index], not self.is_white_move) + self.add_subtree(sub) + sub.insert_move_sequence_helper(moves, index + 1) ############################################################################ # Part 2: Complete Game Trees and Win Probabilities