Skip to content

Commit 4ad5918

Browse files
authored
Update readme files (#185)
* Update data structure readme * Update README.mds
1 parent f98e1c2 commit 4ad5918

7 files changed

Lines changed: 106 additions & 36 deletions

File tree

‎.claude/settings.local.json‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"permissions": {
3+
"allow": [
4+
"Bash(grep *)",
5+
"Bash(ls -1 *.py)"
6+
]
7+
}
8+
}

‎arrays/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ Output: `[1,5]`
408408
def arrays_intersection(arr1: List[int], arr2: List[int], arr3: List[int]) -> List[int]:
409409
intersection = set(arr1)
410410
intersection = intersection.intersection(arr2)
411-
return sorted(list(intersection.intersection(arr3)))
411+
return list(intersection.intersection(arr3))
412412
```
413413

414414
```python

‎binary_tree/README.md‎

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ For every odd-indexed level, all nodes at the level have even integer values in
9595
right). Given the root of a binary tree, return true if the binary tree is even odd, otherwise return false.
9696

9797
```python
98-
def even_odd_tree(root: TreeNode):
98+
def even_odd_tree(root: Optional[TreeNode]):
9999
if not root:
100100
return False
101101

@@ -109,14 +109,10 @@ def even_odd_tree(root: TreeNode):
109109
node = level.popleft()
110110

111111
if even:
112-
if node.val % 2 == 0:
113-
return False
114-
if last_value and node.val <= last_value:
112+
if not validate_even(node, last_value):
115113
return False
116114
else:
117-
if node.val % 2 != 0:
118-
return False
119-
if last_value and node.val >= last_value:
115+
if not validate_odd(node, last_value):
120116
return False
121117

122118
last_value = node.val
@@ -128,6 +124,22 @@ def even_odd_tree(root: TreeNode):
128124
even = not even
129125

130126
return True
127+
128+
129+
def validate_even(node: TreeNode, last_value: Optional[int]) -> bool:
130+
if node.val % 2 == 0:
131+
return False
132+
if last_value and node.val <= last_value:
133+
return False
134+
return True
135+
136+
137+
def validate_odd(node: TreeNode, last_value: Optional[int]) -> bool:
138+
if node.val % 2 != 0:
139+
return False
140+
if last_value and node.val >= last_value:
141+
return False
142+
return True
131143
```
132144

133145
## Has Path Sum

‎data_structures/README.md‎

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
# Data Structures
22

3-
* [Singly Linked List](#singly-linked-list)
4-
* [Doubly Linked List](#doubly-linked-list)
5-
* [Circularly Linked List](#circularly-linked-list)
63
* [Binary Tree](#binary-tree)
4+
* [Circularly Linked List](#circularly-linked-list)
5+
* [Doubly Linked List](#doubly-linked-list)
6+
* [Min Heap](#min-heap)
77
* [Queues](#queue)
8+
* [Singly Linked List](#singly-linked-list)
89
* [Stack](#stack)
910
* [Trie](#trie)
1011

@@ -25,8 +26,7 @@ Drawbacks:
2526
* Operations take O(N) time (Ex: Search)
2627
* Tracking of pointers takes up additional memory
2728

28-
29-
Node Implementation:
29+
Node Implementation:
3030

3131
```python
3232
class SinglyLinkedListNode(Generic[T]):
@@ -507,7 +507,6 @@ class StackList(Generic[T]):
507507
self.__list.pop(data)
508508
```
509509

510-
511510
## Trie
512511

513512
A Trie is a special form of a Nary tree. Typically, a trie is used to store strings. Each Trie node represents a string
@@ -632,6 +631,52 @@ class TrieNode:
632631
self.child_nodes: Dict[str: TrieNode] = dict()
633632
```
634633

634+
## Min Heap
635+
636+
A Min Heap is a complete binary tree where every parent node is smaller than or equal to its children. The smallest element is always at the root. It is commonly backed by an array where for a node at index `i`, its parent is at `(i - 1) // 2` and its children are at `2i + 1` and `2i + 2`.
637+
638+
New values are inserted at the end of the array and then "sifted up" — repeatedly swapped with their parent until the heap property is restored.
639+
640+
```python
641+
class MinHeap:
642+
def __init__(self):
643+
self.list = []
644+
645+
def insert(self, val):
646+
self.list.append(val)
647+
self._sift_up(len(self.list) - 1)
648+
649+
def is_empty(self):
650+
return len(self.list) == 0
651+
652+
def size(self):
653+
return len(self.list)
654+
655+
def values(self):
656+
values = []
657+
for value in self.list:
658+
values.append(value)
659+
return values
660+
661+
def _get_parent(self, idx):
662+
return (idx - 1) // 2
663+
664+
def _sift_up(self, idx):
665+
current_index = idx
666+
667+
while current_index > 0:
668+
parent = self._get_parent(current_index)
669+
670+
if self.list[current_index] < self.list[parent]:
671+
self.list[current_index], self.list[parent] = (
672+
self.list[parent],
673+
self.list[current_index],
674+
)
675+
current_index = parent
676+
else:
677+
break
678+
```
679+
635680
Trie like implementation with a flat dictionary (Hash table)
636681

637682
```python

‎math_problems/README.md‎

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -81,36 +81,29 @@ Task
8181

8282
Given a year, determine whether it is a leap year. If it is a leap year, return the Boolean True, otherwise return False.
8383

84-
Note that the code stub provided reads from STDIN and passes arguments to the is_leap function. It is only necessary to complete the is_leap function.
85-
8684
Input Format
8785

8886
* the year to test.
8987

9088
Output Format
9189

92-
* The function must return a Boolean value (True/False). Output is handled by the provided code stub.
93-
94-
## Moving Average
95-
96-
Create a class `MovingAverage` to calculate the average of a max amount of numbers (n).
97-
Class should have a `.next(value: int)` method that returns the average after adding the new value.
98-
The oldest values should be removed when adding new values once the max amount of numbers is reached.
90+
* The function must return a Boolean value (True/False).
9991

10092
```python
101-
class MovingAverage:
102-
103-
def __init__(self, max_size: int):
104-
self.max_size = max_size
105-
self.queue = deque()
106-
107-
def next(self, number: int) -> float:
108-
self.queue.append(number)
109-
110-
if len(self.queue) > self.max_size:
111-
self.queue.popleft()
93+
def leap_year(year: int) -> bool:
94+
leap = False
95+
96+
if year % 4 == 0:
97+
if year % 100 == 0 and year % 400 == 0:
98+
leap = True
99+
elif year % 100 == 0:
100+
leap = False
101+
else:
102+
leap = True
103+
else:
104+
leap = False
112105

113-
return sum(self.queue) / float(len(self.queue))
106+
return leap
114107
```
115108

116109
## Power of Three

‎n_ary_tree/README.md‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ K L
1515
* [Encode Decode](#encode--decode-tree)
1616
* [Level Order Traversal](#level-order-traversal)
1717
* [Max Depth](#max-depth)
18+
* [NaryTreeNode](#narytreenode)
1819
* [Postorder Traversal](#postorder-traversal)
1920
* [Preorder Traversal](#preorder-traversal)
2021

@@ -140,6 +141,17 @@ def max_depth_bottom_up(root: NaryTreeNode) -> int:
140141
return depth
141142
```
142143

144+
## NaryTreeNode
145+
146+
The node class used by all N-ary tree implementations. Each node holds a value and an optional list of child nodes.
147+
148+
```python
149+
class NaryTreeNode:
150+
def __init__(self, value: T, children: Optional[List["NaryTreeNode"]] = None):
151+
self.value: T = value
152+
self.children: Optional[List[NaryTreeNode]] = children
153+
```
154+
143155
## Postorder Traversal
144156

145157
Recursive

‎singly_linked_lists/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* [Intersection Two Linked Lists](#intersection-two-linked-lists)
99
* [Linked List Cycle](#linked-list-cycle)
1010
* [Merge K Sorted Lists](#merge-k-sorted-lists)
11-
* [Merge Two Sorted Lists](#)
11+
* [Merge Two Sorted Lists](#merge-two-sorted-lists)
1212
* [Odd Even Linked List](#odd-even-linked-list)
1313
* [Palindrome Linked List](#palindrome-linked-list)
1414
* [Remove Linked List Elements](#remove-linked-list-elements)

0 commit comments

Comments
 (0)