Skip to content

Instantly share code, notes, and snippets.

Avatar

Eduardo Ahumada Edald123

  • Mexico
View GitHub Profile
@Edald123
Edald123 / hash_table_blossom.py
Created Aug 3, 2021
Hash map using separate chaining to avoid collisions in Python
View hash_table_blossom.py
from linked_list_for_hash import Node, LinkedList
from blossom_lib import flower_definitions
class HashMap:
def __init__(self, size):
self.array_size = size
self.array = [LinkedList() for i in range(size)]
def hash(self, key):
@Edald123
Edald123 / blossom_lib.py
Created Aug 3, 2021
List for test case for separate chaining
View blossom_lib.py
flower_definitions = [
['begonia', 'cautiousness'],
['chrysanthemum', 'cheerfulness'],
['carnation', 'memories'],
['daisy', 'innocence'],
['hyacinth', 'playfulness'],
['lavender', 'devotion'],
['magnolia', 'dignity'],
['morning glory', 'unrequited love'],
['periwinkle', 'new friendship'],
@Edald123
Edald123 / recursion.py
Created Aug 3, 2021
Recursion in Python
View recursion.py
"""
A function that takes an integer as input and returns
the sum of all numbersfrommm the input down to 1.
Runtime in big-O is O(N), since it calls it self
once per function.
"""
def sum_to_one(n):
if n == 1:
@Edald123
Edald123 / hash_map.py
Last active Aug 3, 2021
Hash Map in Python using Open Addressing to resolve collisions
View hash_map.py
"""
Demonstrative implementation of hash tables in python
without using dictionaries:
This implementation uses an "open addressing system"
to resolve collisions.
In open addressing systems, we check the array at the
address given by our hashing function. One of three
things can happen:
@Edald123
Edald123 / towers_hanoi_project_stack.py
Created Jul 20, 2021
Towers of Hanoi project using stacks in Python
View towers_hanoi_project_stack.py
from stack_for_hanoi import Stack
print("\nLet's play Towers of Hanoi!!")
# Create the Stacks
stacks = []
left_stack = Stack("Left")
middle_stack = Stack("Middle")
right_stack = Stack("Right")
stacks.append(left_stack)
View stack_for_hanoi.py
from linked_list import Node
class Stack:
def __init__(self, name):
self.size = 0
self.top_item = None
self.limit = 1000
self.name = name
@Edald123
Edald123 / queue.py
Last active Jul 20, 2021
Queue in Python
View queue.py
from linked_list import Node
class Queue:
def __init__(self, max_size=None):
self.head = None
self.tail = None
self.max_size = max_size
self.size = 0
@Edald123
Edald123 / binary_search.py
Created Jul 20, 2021
Binary search in Python
View binary_search.py
def binary_search(sorted_list, target):
left_pointer = 0
right_pointer = len(sorted_list)
while left_pointer < right_pointer:
# calculate the middle index using the two pointers
mid_idx = (left_pointer + right_pointer) // 2
mid_val = sorted_list[mid_idx]
if mid_val == target:
return mid_idx
@Edald123
Edald123 / get_middle_linked_list.py
Created Jul 19, 2021
Pointers at different speeds in Python: Find the middle element of a linked list
View get_middle_linked_list.py
from linked_list import LinkedList
def find_middle(linked_list):
fast_pointer = linked_list.head_node
slow_pointer = linked_list.head_node
while fast_pointer.value:
fast_pointer = fast_pointer.get_next_node()
if fast_pointer.value:
fast_pointer = fast_pointer.get_next_node()
@Edald123
Edald123 / swap_nodes_linked_list.py
Created Jul 19, 2021
Swapping elements in a Linked List in Python
View swap_nodes_linked_list.py
import linked_list as LinkedList
def swap_nodes(input_list, val1, val2):
print(f'Swapping {val1} with {val2}')
node1_prev = None
node2_prev = None
node1 = input_list.head_node
node2 = input_list.head_node