| 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 None when focus is None |
| 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 0 (though this shouldn't happen in normal usage) |
| 7 | 12 | def value(self) -> int: |
| 8 | | pass |
| 9 | | |
| 13 | if self.focus is None: |
| 14 | return 0 |
| 15 | return self.focus["value"] |
| 16 | |
| 17 | # Edge Case: Handle None focus by returning self (no change) |
| 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 None focus or missing 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 = self.path + [{ |
| 33 | "parent": self.focus, |
| 34 | "side": "left", |
| 35 | "sibling": self.focus["right"] |
| 36 | }] |
| 37 | return Zipper(self.focus["left"], new_path) |
| 38 | |
| 39 | # Edge Case: Handle None focus by returning self (no change) |
| 16 | 40 | def set_left(self, tree: dict | None) -> "Zipper": |
| 17 | | pass |
| 18 | | |
| 41 | if self.focus is None: |
| 42 | return self |
| 43 | new_focus = { |
| 44 | "value": self.focus["value"], |
| 45 | "left": tree, |
| 46 | "right": self.focus["right"] |
| 47 | } |
| 48 | return Zipper(new_focus, self.path) |
| 49 | |
| 50 | # Edge Case: Handle None focus or missing right child by returning None |
| 19 | 51 | def right(self) -> "Zipper | None": |
| 20 | | pass |
| 21 | | |
| 52 | if self.focus is None or self.focus["right"] is None: |
| 53 | return None |
| 54 | new_path = self.path + [{ |
| 55 | "parent": self.focus, |
| 56 | "side": "right", |
| 57 | "sibling": self.focus["left"] |
| 58 | }] |
| 59 | return Zipper(self.focus["right"], new_path) |
| 60 | |
| 61 | # Edge Case: Handle None focus by returning self (no change) |
| 22 | 62 | def set_right(self, tree: dict | None) -> "Zipper": |
| 23 | | pass |
| 24 | | |
| 63 | if self.focus is None: |
| 64 | return self |
| 65 | new_focus = { |
| 66 | "value": self.focus["value"], |
| 67 | "left": self.focus["left"], |
| 68 | "right": tree |
| 69 | } |
| 70 | return Zipper(new_focus, self.path) |
| 71 | |
| 72 | # Edge Case: Handle empty path (root node) by returning None |
| 73 | # Edge Case: Handle None focus during reconstruction by creating minimal node |
| 25 | 74 | def up(self) -> "Zipper | None": |
| 26 | | pass |
| 27 | | |
| 75 | if not self.path: |
| 76 | return None |
| 77 | |
| 78 | parent_info = self.path[-1] |
| 79 | parent = parent_info["parent"] |
| 80 | side = parent_info["side"] |
| 81 | sibling = parent_info["sibling"] |
| 82 | |
| 83 | # Reconstruct parent with current focus as child |
| 84 | new_parent = { |
| 85 | "value": parent["value"], |
| 86 | "left": self.focus if side == "left" else sibling, |
| 87 | "right": self.focus if side == "right" else sibling |
| 88 | } |
| 89 | |
| 90 | return Zipper(new_parent, self.path[:-1]) |
| 91 | |
| 92 | # Edge Case: Handle empty path (root node) by returning focus directly |
| 93 | # Edge Case: Handle path with missing focus by reconstructing from path |
| 28 | 94 | def to_tree(self) -> dict: |
| 29 | | pass |
| 95 | if not self.path: |
| 96 | return self.focus |
| 97 | |
| 98 | # Navigate to root by going up and reconstructing |
| 99 | current_zipper = self |
| 100 | while current_zipper.path: |
| 101 | current_zipper = current_zipper.up() |
| 102 | return current_zipper.focus |
| 103 | # Handled Edge Cases: Empty tree, None focus, missing children, root node navigation, path reconstruction |