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 unittest | |
import string | |
import collections | |
from pprint import pprint | |
# def print(*args, **kwargs): | |
# pass | |
def make_table(letters, width): | |
table = {} |
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 unittest | |
import re | |
def permutations(ops, len): | |
if len == 1: | |
return [[op] for op in ops] | |
l = [] | |
for i, op in enumerate(ops): | |
for s in permutations(ops, len-1): | |
l.append([op,]+s) |
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
#coding:UTF-8 | |
from collections import defaultdict | |
def find_anagrams(setence): | |
words = set(setence.split()) | |
dd = defaultdict(list) | |
for w in words: # O(nklgk) | |
dd["".join(sorted(w))].append(w) | |
return {k:v for k, v in dd.items() if len(v) > 1} # O(n) |
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
#coding:UTF-8 | |
class Graph: | |
def __init__(self): | |
# use dict to store the graph | |
# the number of keys in self.ves is the number of vertices in the graph | |
# the total number of values(list) is the sum of degree of each vertex | |
self.ves = {} |
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
#coding:UTF-8 | |
from random import randint | |
from functools import partial | |
from counting_sort import counting_sort | |
''' | |
for simplicity: use base 10 | |
b - base | |
k - range of values of keys of the input |
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
#coding:UTF-8 | |
from random import randint | |
from math import inf | |
''' | |
running time: O(n+k) | |
''' | |
def counting_sort(arr, n, k, key=lambda x: x): |
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
#coding:UTF-8 | |
''' | |
notes on R5 | |
What is a data structure? | |
Algorithms for you to query update store information | |
BST | |
query: |
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
#coding:UTF-8 | |
def heapify(arr, i): | |
# O(lgn) | |
left_idx = 2*i+1 | |
if left_idx >= len(arr): | |
return | |
right_idx = left_idx + 1 | |
if right_idx >= len(arr): | |
left = arr[left_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
#coding:UTF-8 | |
import math | |
def merge_sort(l): | |
if len(l) <= 1: | |
return l | |
mid_idx = (len(l)-0)//2 |
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
#codint:UTF-8 | |
def insertion_sort(l): | |
if len(l) <= 1: | |
return l | |
for i in range(1, len(l)): | |
key = l[i] | |
for j in reversed(range(0, i)): | |
c = l[j] | |
if c > key: |
NewerOlder