Skip to content

Instantly share code, notes, and snippets.

View wiccy46's full-sized avatar

Jiajun wiccy46

  • Holoplot
  • Berlin
View GitHub Profile
@wiccy46
wiccy46 / init.vim
Created May 7, 2020 16:49
[init.vim]nvim init #vim
if has('win32') || has('win64')
let g:plugged_home = '~/AppData/Local/nvim/plugged'
else
let g:plugged_home = '~/.vim/plugged'
endif
" Plugins List
call plug#begin(g:plugged_home)
" UI related
Plug 'chriskempson/base16-vim'
Plug 'vim-airline/vim-airline'
@wiccy46
wiccy46 / memoization.py
Created May 6, 2020 21:00
[memoization]Memoization decorator #python #optimization
class Memoize:
def __init__(self, func):
self.func = func
self.cache = {}
def __call__(self, arg):
if arg not in self.cache:
self.cache[arg] = self.func(arg)
return self.cache[arg]
@wiccy46
wiccy46 / subsequence.py
Created May 2, 2020 20:45
[subsequence] subsequence of a string using combinations #python
from itertools import combinations
s = 'abcdef'
set(''.join(c) for i in range(len(s) + 1) for c in combinations(s, i))
@wiccy46
wiccy46 / decorator.py
Created April 28, 2020 05:42
[decorator]Python decorator #python
# Use decorator to do something before or after certain function
# E.g. Timer and Certain error check
# importing time module
from time import time
class Timer:
def __init__(self, func):
self.function = func
@wiccy46
wiccy46 / deque.py
Created April 26, 2020 08:42
[deque] Faster array append in python #python #datastructure
from collections import deque
d = deque()
for i in range(10000):
d.append(i)
@wiccy46
wiccy46 / datastruc_coll.py
Last active April 27, 2020 21:15
[data structure] A collection of data structure related snippet #python #datastructure
"""Quick-Union
worst case is still O(N^2) if tree too tall
"""
"""Find root"""
# id is your tree array
def root(i):
while i != id[i]:
id[i] = id[id[i]]; # one pass variant
i = id[i]
return i
@wiccy46
wiccy46 / merge_set.py
Created April 25, 2020 08:13
[merge_set] Union, merge set #python#algorithms
from operator import or_
from functools import reduce # python3 required
reduce(or_, [{1, 2, 3, 4}, {3, 4, 5, 6}])
# set([1, 2, 3, 4, 5, 6])
@wiccy46
wiccy46 / find_primes.py
Last active April 26, 2020 09:16
[find_primes] Several find primes method, Yield, Use sieve of eratosthenes to find primes. #python #math
# Method 1 # sieve of eratosthenes
def primes(n):
i, p, ps, m = 0, 3, [2], n // 2
sieve = [True] * m
while p <= n:
if sieve[i]:
ps.append(p)
for j in range(int((p*p-3)/2), m, p):
sieve[j] = False
i, p = i+1, p+2
@wiccy46
wiccy46 / chain_add.py
Created April 17, 2020 20:41
[chainadd] Chain add function #python #math
class CustomInt(int):
def __call__(self, v):
return CustomInt(self + v)
def add(v):
return CustomInt(v)
# add(3)(4)(5)
@wiccy46
wiccy46 / divisors.py
Created April 17, 2020 20:40
[divisors] Find all divisors of a number, inkl 1 and self. #python #math
def get_divisors(n):
return set(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0)))