Skip to content

Instantly share code, notes, and snippets.

View Hanaasagi's full-sized avatar
🌸
はる

Hanaasagi

🌸
はる
View GitHub Profile
# -*-coding:UTF-8-*-
import six
class Field(object):
def __init__(self, key, default=None):
self.key = key
class BaseMapper(type):
def __new__(cls, name, bases, attrs):
@Hanaasagi
Hanaasagi / Timsort.java
Created May 1, 2017 08:05
Timsort 算法实现 JDK1.7
import java.util.Arrays;
public class TimSortTest {
public static void main(String[] args) {
int arr[] = {3, 6, 8, 9, 15, 13, 11, 7, 42, 58, 100, 22, 26, 39, 38, 43, 50,
70, 46, 10, 36, 56, 58, 56, 59, 10, 71, 89,46, 10, 36, 56, 58, 56, 59, 10, 71, 89};
System.out.println("before sort: " + Arrays.toString(arr));
System.out.println("array length: " + arr.length);
TimSort.sort(arr, 0, arr.length, null, 0, 0);
@Hanaasagi
Hanaasagi / flatten.py
Created May 2, 2017 14:51
flatten a list
def flatten(seq):
l = []
for elt in seq:
t = type(elt)
if t is tuple or t is list:
for elt2 in flatten(elt):
l.append(elt2)
else:
l.append(elt)
return l
@Hanaasagi
Hanaasagi / binary_search.py
Created May 4, 2017 05:08
可以找出元素第一次出现位置的二分查找
def binary_search(l, t):
low, high = -1, len(l)
while low+1 != high:
mid = (low + high) / 2
if l[mid] < t:
low = mid
else:
high = mid
pos = high
if pos >= len(l) or l[pos] != t:
@Hanaasagi
Hanaasagi / max_cont_subseq.md
Created May 4, 2017 06:04
求最大连续子序列和的 N 种方法

求最大连续子序列和的 N 中方法

1) O(n^3)

def max_continuous_subsequence(l):
    maxsofar = 0
    length = len(l)
    for i in range(length):
        for j in range(i, length):
# ip source https://db-ip.com/db/#downloads
import csv
import netaddr
result = []
with open('dbip-country-2017-05.csv', 'r') as rf, \
open('ip-cidr-CN', 'w') as wf:
reader = csv.reader(rf, delimiter=',')
for row in reader:
@Hanaasagi
Hanaasagi / view_pyc_file.py
Created May 23, 2017 04:21
show the bytecode from a pyc file
import platform
import time
import sys
import binascii
import marshal
import dis
import struct
def view_pyc_file(path):
@Hanaasagi
Hanaasagi / tmux-cheatsheet.markdown
Created May 25, 2017 07:59 — forked from ryerh/tmux-cheatsheet.markdown
Tmux 快捷键 & 速查表

Tmux 快捷键 & 速查表

启动新会话:

tmux [new -s 会话名 -n 窗口名]

恢复会话:

tmux at [-t 会话名]
@Hanaasagi
Hanaasagi / lru_cache.py
Created June 23, 2017 04:37
an LRU cache
class LRUCache:
""" LRUCache implemented with HashMap and LinkList
>>> cache = LRUCache(3)
>>> cache.set(1,1)
>>> cache.set(2,2)
>>> cache.set(3,3)
>>> cache
capacity: 3 [(1, 1), (2, 2), (3, 3)]
>>> cache.get(1)
1
@Hanaasagi
Hanaasagi / longestsubstring.py
Created June 25, 2017 07:43
最长子串 可以使用 difflib
def longest_substring(str1, str2):
"""
>>> longest_substring("apple pie available", "apple pies")
'apple pie'
>>> longest_substring("apples", "appleses")
'apples'
>>> longest_substring("bapples", "cappleses")
'apples'
"""
result = ""