| 1 | 1 | class Zipper: |
| 2 | | # Tree is a dict with keys "value" (int), "left" (dict or None), "right" (dict or None) |
| 2 | def __init__(self, focus, path): |
| 3 | self.focus = focus |
| 4 | self.path = path |
| 5 | |
| 6 | # Edge Case: Handle empty tree by returning a zipper with focus None and empty path |
| 3 | 7 | @staticmethod |
| 4 | 8 | def from_tree(tree: dict) -> "Zipper": |
| 5 | | pass |
| 6 | | |
| 9 | return Zipper(tree, []) |
| 10 | |
| 11 | # Edge Case: Handle when focus is None by returning None |
| 7 | 12 | def value(self) -> int: |
| 8 | | pass |
| 9 | | |
| 13 | if self.focus is None: |
| 14 | return None |
| 15 | return self.focus["value"] |
| 16 | |
| 17 | # Edge Case: Handle when focus is None by returning self |
| 10 | 18 | def set_value(self, value: int) -> "Zipper": |
| 11 | | pass |
| 12 | | |
| 19 | if self.focus is None: |
| 20 | return self |
| 21 | new_focus = { |
| 22 | "value": value, |
| 23 | "left": self.focus["left"], |
| 24 | "right": self.focus["right"] |
| 25 | } |
| 26 | return Zipper(new_focus, self.path) |
| 27 | |
| 28 | # Edge Case: Handle when focus is None or has no left child by returning None |
| 13 | 29 | def left(self) -> "Zipper | None": |
| 14 | | pass |
| 15 | | |
| 30 | if self.focus is None or self.focus["left"] is None: |
| 31 | return None |
| 32 | new_path = [{ |
| 33 | "from": "left", |
| 34 | "parent": self.focus |
| 35 | }] + self.path |
| 36 | return Zipper(self.focus["left"], new_path) |
| 37 | |
| 38 | # Edge Case: Handle when focus is None by returning self |
| 16 | 39 | def set_left(self, tree: dict | None) -> "Zipper": |
| 17 | | pass |
| 18 | | |
| 40 | if self.focus is None: |
| 41 | return self |
| 42 | new_focus = { |
| 43 | "value": self.focus["value"], |
| 44 | "left": tree, |
| 45 | "right": self.focus["right"] |
| 46 | } |
| 47 | return Zipper(new_focus, self.path) |
| 48 | |
| 49 | # Edge Case: Handle when focus is None or has no right child by returning None |
| 19 | 50 | def right(self) -> "Zipper | None": |
| 20 | | pass |
| 21 | | |
| 51 | if self.focus is None or self.focus["right"] is None: |
| 52 | return None |
| 53 | new_path = [{ |
| 54 | "from": "right", |
| 55 | "parent": self.focus |
| 56 | }] + self.path |
| 57 | return Zipper(self.focus["right"], new_path) |
| 58 | |
| 59 | # Edge Case: Handle when focus is None by returning self |
| 22 | 60 | def set_right(self, tree: dict | None) -> "Zipper": |
| 23 | | pass |
| 24 | | |
| 61 | if self.focus is None: |
| 62 | return self |
| 63 | new_focus = { |
| 64 | "value": self.focus["value"], |
| 65 | "left": self.focus["left"], |
| 66 | "right": tree |
| 67 | } |
| 68 | return Zipper(new_focus, self.path) |
| 69 | |
| 70 | # Edge Case: Handle when path is empty (at root) by returning None |
| 25 | 71 | def up(self) -> "Zipper | None": |
| 26 | | pass |
| 27 | | |
| 72 | if not self.path: |
| 73 | return None |
| 74 | |
| 75 | parent_info = self.path[0] |
| 76 | parent = parent_info["parent"] |
| 77 | from_direction = parent_info["from"] |
| 78 | |
| 79 | if from_direction == "left": |
| 80 | new_parent = { |
| 81 | "value": parent["value"], |
| 82 | "left": self.focus, |
| 83 | "right": parent["right"] |
| 84 | } |
| 85 | else: # from_direction == "right" |
| 86 | new_parent = { |
| 87 | "value": parent["value"], |
| 88 | "left": parent["left"], |
| 89 | "right": self.focus |
| 90 | } |
| 91 | |
| 92 | return Zipper(new_parent, self.path[1:]) |
| 93 | |
| 94 | # Edge Case: Handle when path is empty (at root) by returning focus directly |
| 28 | 95 | def to_tree(self) -> dict: |
| 29 | | pass |
| 96 | if not self.path: |
| 97 | return self.focus |
| 98 | |
| 99 | current = self |
| 100 | while current.path: |
| 101 | current = current.up() |
| 102 | return current.focus |
| 103 | |
| 104 | # Handled Edge Cases: Empty tree, focus is None, no left/right child, path is empty (at root) |