This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from itertools import combinations | |
s = 'abcdef' | |
set(''.join(c) for i in range(len(s) + 1) for c in combinations(s, i)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import deque | |
d = deque() | |
for i in range(10000): | |
d.append(i) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CustomInt(int): | |
def __call__(self, v): | |
return CustomInt(self + v) | |
def add(v): | |
return CustomInt(v) | |
# add(3)(4)(5) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) | |