Skip to content

Instantly share code, notes, and snippets.

View jasonhuh's full-sized avatar

Jason Huh jasonhuh

View GitHub Profile
@jasonhuh
jasonhuh / Main.java
Created August 3, 2020 16:59
Java Template for Codeforces, Topcoder
// original source: https://gist.github.com/raviagarwal7/e85898a2f9876581f154ee4aa0f98935
import java.io.*;
import java.util.*;
import java.math.*;
public class Main
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer tokenizer=null;
@jasonhuh
jasonhuh / count_pairs_sum_divisible_by_k.py
Created July 5, 2019 12:11
Count pairs in array whose sum is divisible by k
# Original question: https://www.geeksforgeeks.org/count-pairs-in-array-whose-sum-is-divisible-by-k/
# Refactored the code to make it easier to understand
def solve(A, K):
freq = [0]*K # Bucket of remainders
for num in A:
freq[num % K] += 1 # Count of remainders in each bucket
res = 0
for i in range(1 + len(freq)//2):
if i == 0:
@jasonhuh
jasonhuh / string_hash.py
Last active May 13, 2019 12:35
string hash functions
from functools import reduce
def string_hash(s):
BASE, PRIME = 26, 10**9 + 7
return reduce(lambda x, c: (x*BASE + ord(c)) % PRIME, s, 0)
@jasonhuh
jasonhuh / kmp.py
Last active August 8, 2019 00:43
KMP (Knuth Morris Pratt) Algorithm for Pattern Searching
class KMP:
def partial(self, pat):
res = [0]
for i in range(1, len(pat)):
j = res[-1]
while j > 0 and pat[j] != pat[i]:
j = res[j-1]
res.append(j+1 if pat[j] == pat[i] else j)
return res
@jasonhuh
jasonhuh / trie.py
Last active March 12, 2019 12:53
Trie data structure in python
class Node:
"""
Trie Node implementation. Very basic.
"""
def __init__(self, char: str = None):
self.children = []
self.char = char
self.is_end_of_word = False
class Trie:
@jasonhuh
jasonhuh / kth_smallest.py
Last active August 8, 2019 00:49
kth_smallest.py
def kth_smallest(ar, k):
def kth_smallest(lo, hi, k):
# same as k - 1 >= 0 and k - 1 <= hi - lo
if k > 0 and k <= hi - lo + 1:
p = partition(lo, hi)
# Since array is 0-based, subtract 1 from k
if p - lo == k - 1:
return ar[p]
if p - lo > k - 1:
return kth_smallest(lo, p - 1, k)
@jasonhuh
jasonhuh / quick_sort3.py
Created March 5, 2019 13:35
quick sort with random partition where a pivot is on the right side
import random
def quick_sort(ar):
def partition(lo, hi):
pivot, i = ar[hi], lo
for j in range(lo, hi):
if ar[j] <= pivot:
ar[i], ar[j] = ar[j], ar[i]
i += 1
ar[i], ar[hi] = ar[hi], ar[i]
@jasonhuh
jasonhuh / topology_sort.py
Last active March 4, 2019 17:42
Topology sort
from collections import defaultdict
def topology_sort(g, V):
def dfs(v):
visited[v] = True
for u in g[v]:
if not visited[u]:
dfs(u)
stack.insert(0, v) # This is the only difference from a conventional dfs
@jasonhuh
jasonhuh / quick_sort2.py
Last active March 5, 2019 13:26
quick sort where partition is chosen in the middle
def quick_sort(ar):
def partition(lo, hi):
pivot = ar[(lo + hi) // 2]
while lo <= hi:
while ar[lo] < pivot:
lo += 1
while pivot < ar[hi]:
hi -= 1
if lo <= hi:
@jasonhuh
jasonhuh / huffman.py
Created March 2, 2019 21:43
Huffman algorithm
#Source: Python algorithms
import heapq
from itertools import count
def huffman(seq, frq):
num = count()
trees = list(zip(frq, num, seq))
heapq.heapify(trees)
while len(trees) > 1: