This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
flower_definitions = [ | |
['begonia', 'cautiousness'], | |
['chrysanthemum', 'cheerfulness'], | |
['carnation', 'memories'], | |
['daisy', 'innocence'], | |
['hyacinth', 'playfulness'], | |
['lavender', 'devotion'], | |
['magnolia', 'dignity'], | |
['morning glory', 'unrequited love'], | |
['periwinkle', 'new friendship'], |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from linked_list import Node | |
class Stack: | |
def __init__(self, name): | |
self.size = 0 | |
self.top_item = None | |
self.limit = 1000 | |
self.name = name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
NewerOlder