Skip to content

Instantly share code, notes, and snippets.

View whiledoing's full-sized avatar

whiledoing whiledoing

View GitHub Profile
@whiledoing
whiledoing / qsort.py
Created June 9, 2018 09:57
[python-quick-sort] quick sort implmentation #python #algorithm
def qsort(nums):
qsort_impl(nums, 0, len(nums)-1)
return nums
def qsort_impl(nums, start, end):
if start >= end: return
pivot = partition(nums, start, end)
qsort_impl(nums, start, pivot-1)
qsort_impl(nums, pivot+1, end)
@whiledoing
whiledoing / tree_travel.py
Created June 9, 2018 09:58
[python-tree-travel-use-stack] #python #algorithm
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
def front_travel(root):
if not root: return
s = [root]
while s:
@whiledoing
whiledoing / merge_sort.py
Last active December 28, 2019 11:11
[python-merge-sort] merge sort template #python #algorithm
def reversePairs_using_merge_sort(nums):
n = len(nums)
buffer = [None] * n
# 最本质的方法其实应该想到使用merge_sort,因为天然的思考过程可以考虑到分为两个部分
# reversePairs的个数等于左边reverse的和右边reverse的,然后加上合并过程中reverse的
# 这和merge_sort的过程不谋而和。实现的过程中,控制左边变量i的移动,每次找到一个i
# 不停的移动p指针找到符合要求节点的上限,使用j指针找到比i节点小的节点上限,合并和
# 计算reverse pair过程重叠,非常nice的solution
def merge_sort_impl(l, r):
@whiledoing
whiledoing / priority_dict.py
Created June 13, 2018 01:27
[python-heap-with-updated-dict] python heap dict which can update heap in O(1), use redundant element for laze evaluation #python #datastructure
# copyed from http://code.activestate.com/recipes/522995-priority-dict-a-priority-queue-with-updatable-prio/
from heapq import heapify, heappush, heappop
class priority_dict(dict):
"""Dictionary that can be used as a priority queue.
Keys of the dictionary are items to be put into the queue, and values
are their respective priorities. All dictionary methods work as expected.
The advantage over a standard heapq-based priority queue is
that priorities of items can be efficiently updated (amortized O(1))
@whiledoing
whiledoing / log.py
Created December 9, 2019 07:13
[python-logging] python logging example #python
# logging_example.py
import logging
# Create a custom logger
logger = logging.getLogger(__name__)
# Create handlers
c_handler = logging.StreamHandler()
f_handler = logging.FileHandler('file.log')
@whiledoing
whiledoing / ipython-clip-magic.py
Last active January 22, 2020 09:28
[ipython-notebook] ipython related #python
"""
Add copy to clipboard from IPython!
To install, just copy it to your profile/startup directory, typically:
~/.ipython/profile_default/startup/
Example usage:
%clip hello world
# will store "hello world"
@whiledoing
whiledoing / memo.py
Created December 28, 2019 11:09
[python-memo-decorator] python memo func call result decorator #python #util
from functools import wraps
def memoize(function):
memo = {}
@wraps(function)
def wrapper(*args):
try:
return memo[args]
except KeyError:
rv = function(*args)
@whiledoing
whiledoing / data-reader.py
Last active January 21, 2020 09:41
[pandas-snippet] pandas snippets #python #pandas #datascience
from pandas_datareader import data
goog = data.DataReader('GOOG', start='2014', end='2020', data_source='yahoo')
goog.columns = goog.columns.str.lower()
goog = goog.asfreq('D', method='bfill')
@whiledoing
whiledoing / contour-example.py
Last active January 22, 2020 09:34
[matplotlib-snippets] matplotlib snippets #matplotlib #python #visualization
def f(x, y):
return np.sin(x) ** 10 + np.cos(10+x*y) + np.cos(x)
x = np.linspace(0, 5, 50)
y = np.linspace(0, 5, 50)
X, Y = np.meshgrid(x, y)
Z = f(X, Y)
# draw contour
contour = plt.contour(X, Y, Z, 3, colors='black')
@whiledoing
whiledoing / heatmap-witth-rotation.py
Last active January 21, 2020 09:41
[seaborn-snippets] seaborn snippets #python #seaborn #visualization
from sklearn.datasets import load_digits
digits = load_digits()
# split data
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(digits.data, digits.target, shuffle=True)
# use Gaussian Naive Bayes
from sklearn.naive_bayes import GaussianNB
model = GaussianNB()