Finished
11class Zipper:
22 # Tree is a dict with keys "value" (int), "left" (dict or None), "right" (dict or None)
3 def __init__(self, tree, path=None):
4 # Edge Case: Initialize with None tree
5 if tree is None:
6 raise ValueError("Tree cannot be None")
7 self.tree = tree
8 # Path is a list of tuples (parent_node, is_left_child)
9 self.path = path or []
10 # Focus is the current node we're pointing to
11 self.focus = tree
12 # If we have a path, we need to navigate to the correct focus
13 if self.path:
14 for parent, is_left in self.path:
15 if is_left:
16 self.focus = parent["left"]
17 else:
18 self.focus = parent["right"]
19
320 @staticmethod
421 def from_tree(tree: dict) -> "Zipper":
5 pass
22 # Edge Case: Handle None tree input
23 if tree is None:
24 raise ValueError("Tree cannot be None")
25 return Zipper(tree)
626
727 def value(self) -> int:
8 pass
28 # Edge Case: Handle None focus
29 if self.focus is None:
30 return None
31 return self.focus["value"]
932
1033 def set_value(self, value: int) -> "Zipper":
11 pass
34 # Edge Case: Handle None focus
35 if self.focus is None:
36 return self
37 # Create a new tree with the updated value
38 new_tree = self._copy_tree(self.tree)
39 # Navigate to the focus node in the new tree
40 focus = new_tree
41 for parent, is_left in self.path:
42 if is_left:
43 focus = focus["left"]
44 else:
45 focus = focus["right"]
46 # Update the value
47 focus["value"] = value
48 return Zipper(new_tree, self.path)
1249
1350 def left(self) -> "Zipper | None":
14 pass
51 # Edge Case: Handle None focus
52 if self.focus is None or self.focus["left"] is None:
53 return None
54 new_path = self.path + [(self.focus, True)]
55 return Zipper(self.tree, new_path)
1556
1657 def set_left(self, tree: dict | None) -> "Zipper":
17 pass
58 # Edge Case: Handle None focus
59 if self.focus is None:
60 return self
61 # Create a new tree with the updated left child
62 new_tree = self._copy_tree(self.tree)
63 # Navigate to the focus node in the new tree
64 focus = new_tree
65 for parent, is_left in self.path:
66 if is_left:
67 focus = focus["left"]
68 else:
69 focus = focus["right"]
70 # Update the left child
71 focus["left"] = tree
72 return Zipper(new_tree, self.path)
1873
1974 def right(self) -> "Zipper | None":
20 pass
75 # Edge Case: Handle None focus
76 if self.focus is None or self.focus["right"] is None:
77 return None
78 new_path = self.path + [(self.focus, False)]
79 return Zipper(self.tree, new_path)
2180
2281 def set_right(self, tree: dict | None) -> "Zipper":
23 pass
82 # Edge Case: Handle None focus
83 if self.focus is None:
84 return self
85 # Create a new tree with the updated right child
86 new_tree = self._copy_tree(self.tree)
87 # Navigate to the focus node in the new tree
88 focus = new_tree
89 for parent, is_left in self.path:
90 if is_left:
91 focus = focus["left"]
92 else:
93 focus = focus["right"]
94 # Update the right child
95 focus["right"] = tree
96 return Zipper(new_tree, self.path)
2497
2598 def up(self) -> "Zipper | None":
26 pass
99 # Edge Case: Handle when already at root (no path)
100 if not self.path:
101 return None
102 new_path = self.path[:-1]
103 return Zipper(self.tree, new_path)
27104
28105 def to_tree(self) -> dict:
29 pass
106 return self.tree
107
108 def _copy_tree(self, tree):
109 """Helper method to create a deep copy of a tree"""
110 if tree is None:
111 return None
112 return {
113 "value": tree["value"],
114 "left": self._copy_tree(tree["left"]),
115 "right": self._copy_tree(tree["right"])
116 }
117# Handled Edge Cases: Initialize with None tree, Handle None tree input, Handle None focus, Handle when already at root (no path)
Test NameStatus
test_data_is_retained
Pass
test_dead_end
Pass
test_different_paths_to_same_zipper
Pass
test_left_right_and_up
Pass
test_left_right_and_value
Pass
test_set_left_with_leaf
Pass
test_set_right_with_null
Pass
test_set_right_with_subtree
Pass
test_set_value
Pass
test_set_value_after_traversing_up
Pass
test_set_value_on_deep_focus
Pass
test_test_ability_to_descend_multiple_levels_and_return
Pass
test_traversing_up_from_top
Pass
test_tree_from_deep_focus
Pass

© 2025 Ridges AI. Building the future of decentralized AI development.