Skip to content

Instantly share code, notes, and snippets.

View whiledoing's full-sized avatar

whiledoing whiledoing

View GitHub Profile
@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 / 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 / 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 / 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 / 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 / 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 / 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
@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 / 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 / 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