| 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 input 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 (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 | 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 | |
| 34 | # Create a breadcrumb to store current node's info for going back up |
| 35 | breadcrumb = { |
| 36 | "parent": { |
| 37 | "value": self.focus["value"], |
| 38 | "right": self.focus["right"] |
| 39 | }, |
| 40 | "side": "left" |
| 41 | } |
| 42 | |
| 43 | new_path = [breadcrumb] + list(self.path) |
| 44 | return Zipper(self.focus["left"], new_path) |
| 45 | |
| 46 | # Edge Case: Handle None focus by returning self (no change) |
| 16 | 47 | def set_left(self, tree: dict | None) -> "Zipper": |
| 17 | | pass |
| 18 | | |
| 48 | if self.focus is None: |
| 49 | return self |
| 50 | new_focus = { |
| 51 | "value": self.focus["value"], |
| 52 | "left": tree, |
| 53 | "right": self.focus["right"] |
| 54 | } |
| 55 | new_path = list(self.path) |
| 56 | return Zipper(new_focus, new_path) |
| 57 | |
| 58 | # Edge Case: Handle None focus by returning None |
| 19 | 59 | def right(self) -> "Zipper | None": |
| 20 | | pass |
| 21 | | |
| 60 | if self.focus is None or self.focus["right"] is None: |
| 61 | return None |
| 62 | |
| 63 | # Create a breadcrumb to store current node's info for going back up |
| 64 | breadcrumb = { |
| 65 | "parent": { |
| 66 | "value": self.focus["value"], |
| 67 | "left": self.focus["left"] |
| 68 | }, |
| 69 | "side": "right" |
| 70 | } |
| 71 | |
| 72 | new_path = [breadcrumb] + list(self.path) |
| 73 | return Zipper(self.focus["right"], new_path) |
| 74 | |
| 75 | # Edge Case: Handle None focus by returning self (no change) |
| 22 | 76 | def set_right(self, tree: dict | None) -> "Zipper": |
| 23 | | pass |
| 24 | | |
| 77 | if self.focus is None: |
| 78 | return self |
| 79 | new_focus = { |
| 80 | "value": self.focus["value"], |
| 81 | "left": self.focus["left"], |
| 82 | "right": tree |
| 83 | } |
| 84 | new_path = list(self.path) |
| 85 | return Zipper(new_focus, new_path) |
| 86 | |
| 87 | # Edge Case: Handle empty path (root node) by returning None |
| 88 | # Edge Case: Handle None focus by returning None |
| 25 | 89 | def up(self) -> "Zipper | None": |
| 26 | | pass |
| 27 | | |
| 90 | if self.focus is None or len(self.path) == 0: |
| 91 | return None |
| 92 | |
| 93 | breadcrumb = self.path[0] |
| 94 | parent = breadcrumb["parent"] |
| 95 | side = breadcrumb["side"] |
| 96 | |
| 97 | # Reconstruct parent with current focus as the appropriate child |
| 98 | if side == "left": |
| 99 | new_parent = { |
| 100 | "value": parent["value"], |
| 101 | "left": self.focus, |
| 102 | "right": parent["right"] |
| 103 | } |
| 104 | else: # side == "right" |
| 105 | new_parent = { |
| 106 | "value": parent["value"], |
| 107 | "left": parent["left"], |
| 108 | "right": self.focus |
| 109 | } |
| 110 | |
| 111 | new_path = list(self.path[1:]) |
| 112 | return Zipper(new_parent, new_path) |
| 113 | |
| 114 | # Edge Case: Handle None focus by returning None |
| 28 | 115 | def to_tree(self) -> dict: |
| 29 | | pass |
| 116 | if self.focus is None: |
| 117 | return None |
| 118 | |
| 119 | # Navigate up to the root to get the complete tree |
| 120 | current = self |
| 121 | while True: |
| 122 | parent = current.up() |
| 123 | if parent is None: |
| 124 | break |
| 125 | current = parent |
| 126 | return current.focus |
| 127 | |
| 128 | # Handled Edge Cases: Empty tree input, None focus, empty path (root node) |