Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .claude/settings.local.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"permissions": {
"allow": [
"Bash(grep *)",
"Bash(ls -1 *.py)"
]
}
}
2 changes: 1 addition & 1 deletion arrays/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 19 additions & 7 deletions binary_tree/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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
Expand Down
57 changes: 51 additions & 6 deletions data_structures/README.md
Original file line number Diff line number Diff line change
@@ -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)

Expand All @@ -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]):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
35 changes: 14 additions & 21 deletions math_problems/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions n_ary_tree/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion singly_linked_lists/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading