Skip to content

Instantly share code, notes, and snippets.

@EdisonChendi
EdisonChendi / graph_bfs.py
Last active June 10, 2018 09:29
graph + breadth first search
#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 = {}
@EdisonChendi
EdisonChendi / programmation_efficace_2_1.py
Created July 29, 2018 13:07
find all anagrams in a sentence
#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)
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)
@EdisonChendi
EdisonChendi / 1320_minimum_distance_to_type_a_word_using_two_fingers.py
Created March 11, 2020 14:51
leetcode algorithms 1320 - minimum_distance_to_type_a_word_using_two_fingers
import unittest
import string
import collections
from pprint import pprint
# def print(*args, **kwargs):
# pass
def make_table(letters, width):
table = {}