diff --git a/.claude/settings.local.json b/.claude/settings.local.json new file mode 100644 index 0000000..33b042f --- /dev/null +++ b/.claude/settings.local.json @@ -0,0 +1,8 @@ +{ + "permissions": { + "allow": [ + "Bash(grep *)", + "Bash(ls -1 *.py)" + ] + } +} diff --git a/arrays/README.md b/arrays/README.md index 046db75..1d75346 100644 --- a/arrays/README.md +++ b/arrays/README.md @@ -408,7 +408,7 @@ Output: `[1,5]` def arrays_intersection(arr1: List[int], arr2: List[int], arr3: List[int]) -> List[int]: intersection = set(arr1) intersection = intersection.intersection(arr2) - return sorted(list(intersection.intersection(arr3))) + return list(intersection.intersection(arr3)) ``` ```python diff --git a/binary_tree/README.md b/binary_tree/README.md index 48508c4..d3d9ffa 100644 --- a/binary_tree/README.md +++ b/binary_tree/README.md @@ -95,7 +95,7 @@ For every odd-indexed level, all nodes at the level have even integer values in right). Given the root of a binary tree, return true if the binary tree is even odd, otherwise return false. ```python -def even_odd_tree(root: TreeNode): +def even_odd_tree(root: Optional[TreeNode]): if not root: return False @@ -109,14 +109,10 @@ def even_odd_tree(root: TreeNode): node = level.popleft() if even: - if node.val % 2 == 0: - return False - if last_value and node.val <= last_value: + if not validate_even(node, last_value): return False else: - if node.val % 2 != 0: - return False - if last_value and node.val >= last_value: + if not validate_odd(node, last_value): return False last_value = node.val @@ -128,6 +124,22 @@ def even_odd_tree(root: TreeNode): even = not even return True + + +def validate_even(node: TreeNode, last_value: Optional[int]) -> bool: + if node.val % 2 == 0: + return False + if last_value and node.val <= last_value: + return False + return True + + +def validate_odd(node: TreeNode, last_value: Optional[int]) -> bool: + if node.val % 2 != 0: + return False + if last_value and node.val >= last_value: + return False + return True ``` ## Has Path Sum diff --git a/data_structures/README.md b/data_structures/README.md index 40d75aa..7e75f03 100644 --- a/data_structures/README.md +++ b/data_structures/README.md @@ -1,10 +1,11 @@ # Data Structures -* [Singly Linked List](#singly-linked-list) -* [Doubly Linked List](#doubly-linked-list) -* [Circularly Linked List](#circularly-linked-list) * [Binary Tree](#binary-tree) +* [Circularly Linked List](#circularly-linked-list) +* [Doubly Linked List](#doubly-linked-list) +* [Min Heap](#min-heap) * [Queues](#queue) +* [Singly Linked List](#singly-linked-list) * [Stack](#stack) * [Trie](#trie) @@ -25,8 +26,7 @@ Drawbacks: * Operations take O(N) time (Ex: Search) * Tracking of pointers takes up additional memory - -Node Implementation: +Node Implementation: ```python class SinglyLinkedListNode(Generic[T]): @@ -507,7 +507,6 @@ class StackList(Generic[T]): self.__list.pop(data) ``` - ## Trie 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: self.child_nodes: Dict[str: TrieNode] = dict() ``` +## Min Heap + +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`. + +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. + +```python +class MinHeap: + def __init__(self): + self.list = [] + + def insert(self, val): + self.list.append(val) + self._sift_up(len(self.list) - 1) + + def is_empty(self): + return len(self.list) == 0 + + def size(self): + return len(self.list) + + def values(self): + values = [] + for value in self.list: + values.append(value) + return values + + def _get_parent(self, idx): + return (idx - 1) // 2 + + def _sift_up(self, idx): + current_index = idx + + while current_index > 0: + parent = self._get_parent(current_index) + + if self.list[current_index] < self.list[parent]: + self.list[current_index], self.list[parent] = ( + self.list[parent], + self.list[current_index], + ) + current_index = parent + else: + break +``` + Trie like implementation with a flat dictionary (Hash table) ```python diff --git a/math_problems/README.md b/math_problems/README.md index 84d2878..b2211e6 100644 --- a/math_problems/README.md +++ b/math_problems/README.md @@ -81,36 +81,29 @@ Task Given a year, determine whether it is a leap year. If it is a leap year, return the Boolean True, otherwise return False. -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. - Input Format * the year to test. Output Format -* The function must return a Boolean value (True/False). Output is handled by the provided code stub. - -## Moving Average - -Create a class `MovingAverage` to calculate the average of a max amount of numbers (n). -Class should have a `.next(value: int)` method that returns the average after adding the new value. -The oldest values should be removed when adding new values once the max amount of numbers is reached. +* The function must return a Boolean value (True/False). ```python -class MovingAverage: - - def __init__(self, max_size: int): - self.max_size = max_size - self.queue = deque() - - def next(self, number: int) -> float: - self.queue.append(number) - - if len(self.queue) > self.max_size: - self.queue.popleft() +def leap_year(year: int) -> bool: + leap = False + + if year % 4 == 0: + if year % 100 == 0 and year % 400 == 0: + leap = True + elif year % 100 == 0: + leap = False + else: + leap = True + else: + leap = False - return sum(self.queue) / float(len(self.queue)) + return leap ``` ## Power of Three diff --git a/n_ary_tree/README.md b/n_ary_tree/README.md index 4ed9f16..d01ec0b 100644 --- a/n_ary_tree/README.md +++ b/n_ary_tree/README.md @@ -15,6 +15,7 @@ K L * [Encode Decode](#encode--decode-tree) * [Level Order Traversal](#level-order-traversal) * [Max Depth](#max-depth) +* [NaryTreeNode](#narytreenode) * [Postorder Traversal](#postorder-traversal) * [Preorder Traversal](#preorder-traversal) @@ -140,6 +141,17 @@ def max_depth_bottom_up(root: NaryTreeNode) -> int: return depth ``` +## NaryTreeNode + +The node class used by all N-ary tree implementations. Each node holds a value and an optional list of child nodes. + +```python +class NaryTreeNode: + def __init__(self, value: T, children: Optional[List["NaryTreeNode"]] = None): + self.value: T = value + self.children: Optional[List[NaryTreeNode]] = children +``` + ## Postorder Traversal Recursive diff --git a/singly_linked_lists/README.md b/singly_linked_lists/README.md index b025f4e..c30b170 100644 --- a/singly_linked_lists/README.md +++ b/singly_linked_lists/README.md @@ -8,7 +8,7 @@ * [Intersection Two Linked Lists](#intersection-two-linked-lists) * [Linked List Cycle](#linked-list-cycle) * [Merge K Sorted Lists](#merge-k-sorted-lists) -* [Merge Two Sorted Lists](#) +* [Merge Two Sorted Lists](#merge-two-sorted-lists) * [Odd Even Linked List](#odd-even-linked-list) * [Palindrome Linked List](#palindrome-linked-list) * [Remove Linked List Elements](#remove-linked-list-elements)