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
class Node: | |
def __init__(self, value, next_node=None): | |
self.value = value | |
self.next_node = next_node | |
def get_value(self): | |
return self.value | |
def get_next_node(self): | |
return self.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
class Node: | |
def __init__(self, value, next_node=None, prev_node=None): | |
self.value = value | |
self.next_node = next_node | |
self.prev_node = prev_node | |
def set_next_node(self, next_node): | |
self.next_node = next_node | |
def get_next_node(self): |
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 |
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
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 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
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 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
""" | |
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
""" | |
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: |
OlderNewer