Skip to content

Instantly share code, notes, and snippets.

View DiegoGallegos4's full-sized avatar
🏠
Working from home

Diego Gallegos DiegoGallegos4

🏠
Working from home
View GitHub Profile
@DiegoGallegos4
DiegoGallegos4 / merge_sort.py
Last active April 12, 2019 03:00
Merge Sort
def merge(A, B):
C = []
while len(A) and len(B):
a, b = A[0], B[0]
if a <= b:
C.append(A.pop(0))
else:
C.append(B.pop(0))
while len(A):
C.append(A.pop(0))
@DiegoGallegos4
DiegoGallegos4 / quicksort.py
Last active April 15, 2019 05:59
Divide and Conquer: Quicksort
def partition(A, left, right):
pivot, j = A[left], left
for i in range(left + 1, right + 1):
if A[i] <= pivot:
j += 1
A[i], A[j] = A[j], A[i]
A[left], A[j] = A[j], A[left]
return j
@DiegoGallegos4
DiegoGallegos4 / singly_linked_list.py
Created April 15, 2019 15:08
Singly-Linked List
class Node:
def __init__(self, key, next):
self.key = key
self.next = next
class List:
def __init__(self, head=None):
self.head = head
self.tail = None
class BinaryTree:
def __init__(self, key, left=None, right=None):
self.key = key
self.left = left
self.right = right
def insert_left(self, key):
if self.left:
node = BinaryTree(key)
node.left = self.left
@DiegoGallegos4
DiegoGallegos4 / doubly_linked_list.py
Last active April 17, 2019 23:36
Doubly-Linked List
class Node:
def __init__(self, key, next=None, prev=None):
self.key = key
self.next = next
self.prev = prev
class DoublyLinkedList:
def __init__(self, head=None):
self.head = None
self.tail = None
@DiegoGallegos4
DiegoGallegos4 / lifo_fifo.py
Created April 17, 2019 22:55
Stack and Queue
class Stack:
def pop(self):
pass
def top(self):
pass
def push(self, key):
pass
@DiegoGallegos4
DiegoGallegos4 / dynamic_array.py
Last active April 19, 2019 01:49
Dynamic Array (Vector)
import ctypes
class DynamicArray:
def __init__(self):
self.capacity = 1
self.size = 0
self.array = self._make_array(self.capacity)
def append(self, elt):
if self.size == self.capacity:
@DiegoGallegos4
DiegoGallegos4 / heap.py
Last active April 21, 2019 05:01
Binary Heap
class BinaryMaxHeap:
def __init__(self):
self.size = -1
self.heap = []
def parent(self, i):
return (i - 1) // 2
def left_child(self, i):
return 2 * i + 1
@DiegoGallegos4
DiegoGallegos4 / binary_search_tree.py
Created April 24, 2019 15:14
Binary Search Tree
class TreeNode:
def __init__(self, key, parent=None, left=None, right=None):
self.key = key
self.parent = parent
self.left = left
self.right = right
class BinarySearchTree:
def __init__(self):
self.root = None
@DiegoGallegos4
DiegoGallegos4 / algo.py
Created August 5, 2019 17:44 — forked from howCodeORG/algo.py
howCode's Simple Genetic Algorithm in Python
import random
population = 200
generations = 0
mutation = 0.01
alphabet = "abcdefghijklmnopqrstuvwxyz! "
target = "subscribe to howcode!"
output = ""
data = []