jackson

Finished
11class 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
37 @staticmethod
48 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
712 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
1018 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
1329 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
1639 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
1950 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
2260 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
2571 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
2895 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)
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.