| 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 None focus 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 None focus 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 None focus 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 | new_path = list(self.path) |
| 27 | return Zipper(new_focus, new_path) |
| 28 | |
| 29 | # Edge Case: Handle None focus by returning None |
| 13 | 30 | def left(self) -> "Zipper | None": |
| 14 | | pass |
| 15 | | |
| 31 | if self.focus is None or self.focus["left"] is None: |
| 32 | return None |
| 33 | new_path = list(self.path) |
| 34 | new_path.append({ |
| 35 | "parent": self.focus, |
| 36 | "side": "left", |
| 37 | "sibling": self.focus["right"] |
| 38 | }) |
| 39 | return Zipper(self.focus["left"], new_path) |
| 40 | |
| 41 | # Edge Case: Handle None focus by returning self |
| 16 | 42 | def set_left(self, tree: dict | None) -> "Zipper": |
| 17 | | pass |
| 18 | | |
| 43 | if self.focus is None: |
| 44 | return self |
| 45 | new_focus = { |
| 46 | "value": self.focus["value"], |
| 47 | "left": tree, |
| 48 | "right": self.focus["right"] |
| 49 | } |
| 50 | new_path = list(self.path) |
| 51 | return Zipper(new_focus, new_path) |
| 52 | |
| 53 | # Edge Case: Handle None focus by returning None |
| 19 | 54 | def right(self) -> "Zipper | None": |
| 20 | | pass |
| 21 | | |
| 55 | if self.focus is None or self.focus["right"] is None: |
| 56 | return None |
| 57 | new_path = list(self.path) |
| 58 | new_path.append({ |
| 59 | "parent": self.focus, |
| 60 | "side": "right", |
| 61 | "sibling": self.focus["left"] |
| 62 | }) |
| 63 | return Zipper(self.focus["right"], new_path) |
| 64 | |
| 65 | # Edge Case: Handle None focus by returning self |
| 22 | 66 | def set_right(self, tree: dict | None) -> "Zipper": |
| 23 | | pass |
| 24 | | |
| 67 | if self.focus is None: |
| 68 | return self |
| 69 | new_focus = { |
| 70 | "value": self.focus["value"], |
| 71 | "left": self.focus["left"], |
| 72 | "right": tree |
| 73 | } |
| 74 | new_path = list(self.path) |
| 75 | return Zipper(new_focus, new_path) |
| 76 | |
| 77 | # Edge Case: Handle empty path (root node) by returning None |
| 78 | # Edge Case: Handle None focus by returning None |
| 25 | 79 | def up(self) -> "Zipper | None": |
| 26 | | pass |
| 27 | | |
| 80 | if not self.path: |
| 81 | return None |
| 82 | |
| 83 | parent_info = self.path[-1] |
| 84 | parent = parent_info["parent"] |
| 85 | side = parent_info["side"] |
| 86 | sibling = parent_info["sibling"] |
| 87 | |
| 88 | # Reconstruct parent with current focus as child |
| 89 | new_parent = dict(parent) |
| 90 | new_parent[side] = self.focus |
| 91 | |
| 92 | # If we're coming from the left, the sibling in the path is the right child |
| 93 | # If we're coming from the right, the sibling in the path is the left child |
| 94 | if side == "left": |
| 95 | new_parent["right"] = sibling |
| 96 | else: # side == "right" |
| 97 | new_parent["left"] = sibling |
| 98 | |
| 99 | new_path = self.path[:-1] |
| 100 | return Zipper(new_parent, new_path) |
| 101 | |
| 102 | # Edge Case: Handle None focus by returning None |
| 103 | # Edge Case: Handle empty path by returning the focus directly |
| 28 | 104 | def to_tree(self) -> dict: |
| 29 | | pass |
| 105 | if self.focus is None: |
| 106 | return None |
| 107 | |
| 108 | if not self.path: |
| 109 | return self.focus |
| 110 | |
| 111 | # Navigate back to root |
| 112 | current_zipper = self |
| 113 | while True: |
| 114 | up_zipper = current_zipper.up() |
| 115 | if up_zipper is None: |
| 116 | break |
| 117 | current_zipper = up_zipper |
| 118 | |
| 119 | return current_zipper.focus |
| 120 | |
| 121 | # Handled Edge Cases: Empty tree, None focus, empty path (root node) |