Skip to content

Instantly share code, notes, and snippets.

@Tetsuya3850
Tetsuya3850 / lru_cache.py
Created April 4, 2018 01:20
Last Recently Used Cache with Python
from collections import defaultdict
class DoubleNode:
def __init__(self, data=0, next_node=None, prev_node=None):
self.data = data
self.next = next_node
self.prev = prev_node
class Cache:
def __init__(self):
@Tetsuya3850
Tetsuya3850 / dijkstra.py
Last active March 31, 2018 06:12
Dijkstra’s Shortest Path Algorithm with Python
from collections import defaultdict
from heapq import heappush, heappop
class WeightedVertex:
def __init__(self, name):
self.name = name
self.adjacent = []
def addEdge(self, vertex, weight):
self.adjacent.append((vertex, weight))
@Tetsuya3850
Tetsuya3850 / rabinkarp.py
Last active March 31, 2018 02:35
Rabin-Karp Substring Search with Python
def rapin_karp(pat, txt):
M = len(pat)
N = len(txt)
p = 0
t = 0
h = 1
d = 256
q = 101
for i in range(M - 1):
@Tetsuya3850
Tetsuya3850 / hashtable.py
Last active December 30, 2022 18:35
Hash Table with Python
class HashNode:
def __init__(self, key, value):
self.next = None
self.key = key
self.value = value
class HashTable:
def __init__(self):
self.table = [None] * 101
@Tetsuya3850
Tetsuya3850 / llist.py
Created March 29, 2018 04:06
Linked List with Python
class Node:
def __init__(self, data=0, next_node=None):
self.data = data
self.next = next_node
class LinkedList:
def __init__(self):
self.head = None
def appendleft(self, new_data):
@Tetsuya3850
Tetsuya3850 / dllist.py
Last active October 27, 2020 00:47
Doubly Linked List with Python
class DoubleNode:
def __init__(self, data=0, next_node=None, prev_node=None):
self.data = data
self.next = next_node
self.prev = prev_node
class DoublyLinkedList:
def __init__(self):
self.head = None
self.tail = None
@Tetsuya3850
Tetsuya3850 / graph.py
Last active March 31, 2018 04:00
Directed Graph with Python
from collections import defaultdict, deque, Counter
class Vertex:
def __init__(self, name):
self.name = name
self.adjacent = []
def addEdge(self, vertex):
self.adjacent.append(vertex)
@Tetsuya3850
Tetsuya3850 / bst.py
Created March 29, 2018 02:01
Binary Search Tree with Python
class BinaryTreeNode:
def __init__(self, data=None, left=None, right=None):
self.data = data
self.left = left
self.right = right
class BinarySearchTree:
def __init__(self):
self.root = None
@Tetsuya3850
Tetsuya3850 / trie.py
Last active March 29, 2018 02:02
Trie with Python
class TrieNode:
def __init__(self):
self.children = [None] * 26
self.end = False
self.prefix = 0
def has_no_children(self):
for child in self.children:
if child:
return False
@Tetsuya3850
Tetsuya3850 / heap.py
Last active March 29, 2018 02:02
Min Heap with Python
class MinHeap:
def __init__(self):
self.heap = []
def peek(self):
if not self.heap:
return False
return self.heap[0]
def push(self, data):