Skip to content

Instantly share code, notes, and snippets.

View whiledoing's full-sized avatar

whiledoing whiledoing

View GitHub Profile
@whiledoing
whiledoing / using-class.py
Last active June 29, 2017 19:46
[python-context-manager] using class and context manager method to implement python context manager #python
class Context(object):
def __init__(self, name):
self.file = open(name)
def __enter__(self):
return self.file
def __exit__(self, ctx_type, ctx_value, ctx_traceback):
self.file.close()
@whiledoing
whiledoing / cVimrc
Last active July 9, 2017 06:17
[chrome-extension-cVimrc] #chrome
set nohud
map h J
map l K
map c x
map C X
set cncpcompletion
let barposition = "bottom"
set typelinkhints
set noautofocus
@whiledoing
whiledoing / repeated-timer-example.py
Last active April 15, 2018 15:12
[python-repeated-timer] repeated timer impl with threading.Timer #python
from threading import _Timer
class RepeatedTimer(_Timer):
"""
See: https://hg.python.org/cpython/file/2.7/Lib/threading.py#l1079
"""
def run(self):
while not self.finished.is_set():
self.finished.wait(self.interval)
self.function(*self.args, **self.kwargs)
@whiledoing
whiledoing / markdown-table-example.md
Last active October 7, 2023 09:34
[markdown-example] markdown example demo #markdown
Left-Aligned Center Aligned Right Aligned
col 3 is some wordy text $1600
col 2 is centered $12
zebra stripes are neat $1
@whiledoing
whiledoing / mac-brew-link-python-workable
Last active December 28, 2019 11:09
[mac operation] mac operation backup #mac #osx
@whiledoing
whiledoing / dijkstra.py
Last active December 28, 2019 11:10 — forked from kachayev/dijkstra.py
[python-dijkstra] Dijkstra shortest path algorithm based on python heapq heap implementation #python #algorithm
from collections import defaultdict
from heapq import *
def dijkstra(edges, f, t):
g = defaultdict(list)
for l,r,c in edges:
g[l].append((c,r))
# dist records the min value of each node in heap.
q, seen, dist = [(0,f,())], set(), {f: 0}
@whiledoing
whiledoing / bst.py
Created June 9, 2018 09:50
[python-BST] binary search tree impl #python
def reversePairs_using_bst_tree(nums):
class Node:
def __init__(self, v, left=None, right=None):
self.val = v
# cnt初始值为1,因为节点本身记录cnt,创建的时候就包含了一次
# cnt记录大于等于v的节点个数
self.cnt = 1
self.left = left
self.right = right
@whiledoing
whiledoing / lru_cache.py
Created June 9, 2018 09:52
[python-LRUCache] LRU implementation #python #datastructure
class LinkedNode:
def __init__(self, value, next):
self.value = value
self.next = next
class LRUCache:
def __init__(self, capacity):
self.head = LinkedNode(None, None)
self.tail = None
@whiledoing
whiledoing / bisect_left.py
Created June 9, 2018 09:54
[python-bisect-left] binary left search implmentation #python #algorithm
def bisect_left(data, v):
left, right = 0, len(data)
while left < right:
mid = (left+right)//2
if v <= data[mid]:
right = mid
else:
left = mid+1
return right
@whiledoing
whiledoing / next_permutation.py
Created June 9, 2018 09:55
[python-next-permutation] next permuation of list of elements #python #algorithm
def next_permutation(nums):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
if not nums: return nums
l = len(nums)
i, j = l - 2, l - 1
while i >= 0 and nums[i] >= nums[i + 1]:
i -= 1