Skip to content

Instantly share code, notes, and snippets.

View Edald123's full-sized avatar

Eduardo Ahumada Edald123

  • Mexico
View GitHub Profile
@Edald123
Edald123 / linked_list.py
Last active July 19, 2021 20:43
Linked List in Python
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
@Edald123
Edald123 / doubly_linked_list.py
Last active July 19, 2021 21:01
Doubly linked list in Python
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):
@Edald123
Edald123 / swap_nodes_linked_list.py
Created July 19, 2021 21:05
Swapping elements in a Linked List in Python
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
@Edald123
Edald123 / get_middle_linked_list.py
Created July 19, 2021 21:37
Pointers at different speeds in Python: Find the middle element of a linked list
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 / binary_search.py
Created July 20, 2021 02:40
Binary search in Python
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 / queue.py
Last active July 20, 2021 03:05
Queue in Python
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 / stack_for_hanoi.py
Created July 20, 2021 03:20
Stack in Python
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 / towers_hanoi_project_stack.py
Created July 20, 2021 03:22
Towers of Hanoi project using stacks in Python
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)
@Edald123
Edald123 / hash_map.py
Last active August 3, 2021 03:24
Hash Map in Python using Open Addressing to resolve collisions
"""
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 / recursion.py
Created August 3, 2021 04:38
Recursion in Python
"""
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: