Skip to content

Instantly share code, notes, and snippets.

@EdisonChendi
EdisonChendi / use_traceback.py
Created January 10, 2017 15:15
use traceback module to get more stackinfo when Exception occus
# coding:UTF-8
import sys
import traceback
def a():
print("a")
b()
def b():
@EdisonChendi
EdisonChendi / dot_operator_search_order.py
Created February 3, 2017 16:01
python dot operator search order
# Python . operator search order
# 1. if magic methods are used implicitly, those methods on object's type are used
# 2. __getattributes__(self, attr)
# 3. object's type's __dict__ and it's a data descriptor, return it
# 4. object's __dict__ and it's a data descriptor, return it
# 5. object's __dict__ then object's type's __dict__
# 6. __getattr__
# 7. raise AttributeError
@EdisonChendi
EdisonChendi / 1d_peak_finding.py
Created April 7, 2018 11:45
mit6006_fall2011_lesson1
#coding:UTF-8
from math import inf, floor
from time import time
from random import randint
def linear_find_peak_1d(l, start, end):
minus_inf = -1 * inf
l = [minus_inf] + l[start:end] + [minus_inf]
for i in range(1, len(l)+1):
@EdisonChendi
EdisonChendi / document_distance.py
Last active April 9, 2018 15:02
calculate how different of two docs - word2vec
#coding:UTF-8
s1 = "my name is Hulk"
s2 = "my name is Iron man"
s3 = "your job is save the word"
from collections import defaultdict
import re
import math
from string import whitespace, punctuation, ascii_uppercase
@EdisonChendi
EdisonChendi / insertion_sort.py
Created April 14, 2018 13:05
insertion sort
#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:
@EdisonChendi
EdisonChendi / merget_sort.py
Created April 14, 2018 13:06
merge sort + merge n sorted list with variable length
#coding:UTF-8
import math
def merge_sort(l):
if len(l) <= 1:
return l
mid_idx = (len(l)-0)//2
@EdisonChendi
EdisonChendi / heap_sort.py
Last active April 24, 2018 15:50
heap sort
#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]
@EdisonChendi
EdisonChendi / binary_search_tree.py
Last active May 6, 2018 08:48
binary search tree
#coding:UTF-8
'''
notes on R5
What is a data structure?
Algorithms for you to query update store information
BST
query:
@EdisonChendi
EdisonChendi / counting_sort.py
Last active May 27, 2018 08:46
counting sort in Python
#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):
@EdisonChendi
EdisonChendi / radix_sort.py
Last active May 27, 2018 08:51
radix sort in python
#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