Skip to content

Instantly share code, notes, and snippets.

l1 = [23.17,3.2,1.22,0.32]
l2 = [7.36,4.16,3.20,1.69,1.28,1.28,0.96,0.96,0.90,0.64,0.64,0.64,0.50,0.50,0.32,0.32,0.32,0.32,0.32,0.32,0.32,0.32,0.32,0.32]
'''
Convert input to ints because of accuracy issue with float arithmetic.
'''
l1 = [round(x * 100) for x in l1]
l2 = [round(x * 100) for x in l2]
def group(l1, l2):
l = [2,3,5,7,9]
N = 13
def solve(l, N):
def helper(l, N, i, ans):
while i < len(l):
ans.append(l[i])
if N == l[i]:
result.append(ans[:])
elif N >= l[i] * 2:
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def __repr__(self):
return "({}, {})".format(self.x, self.y)
def __add__(self, other):
def generator():
import time
class Timer:
def __init__(self):
self.stack = []
def scope(self, name):
if self.stack:
name = self.stack[-1].name + '->' + name
return Scope(name, self)
@czheo
czheo / deco.py
Created March 21, 2019 16:19
decorator in a class
class Klass:
def __init__(self):
self.c = 100
def dump_attr(attr):
def deco(f):
def wrapper(*argv, **kvargv):
print('Value of "%s" is' % attr, getattr(argv[0], attr))
return f(*argv, **kvargv)
return wrapper
def backtrack(lst, target, ans, res):
if len(lst) == len(ans) // 2:
if target == 0:
res.append(ans[:])
else:
n = lst[len(ans) // 2]
ans.append('+')
ans.append(str(n))
backtrack(lst, target - n, ans, res)
ans.pop()
l = [("a", "b"), ("b", "c"), ("e", "f")]
def merge(l):
ret = []
for s in map(set, l):
for x in ret:
if s & x:
x.update(s)
break
else:
def quick_sort(lst):
qs(lst, 0, len(lst) - 1)
def qs(lst, start, end):
if start < end:
pivot = partition(lst, start, end)
qs(lst, start, pivot - 1)
qs(lst, pivot + 1, end)
def partition(lst, start, end):
@czheo
czheo / merge_sort.merge.py
Last active May 11, 2018 06:36
merge sort
def merge(left, right, a):
i = j = k = 0
while i < len(left) and j < len(right):
if left[i] < right[j]:
# min(left) < min(right)
a[k] = left[i]
i += 1
else:
# min(left) >= min(right)
a[k] = right[j]
@czheo
czheo / javascript_tips_1.js
Last active April 14, 2018 11:08
Javascript Tips: 10 Things I Learned from the jQuery Source http://www.youtube.com/watch?v=i_qE1iAmjFg
/* self invoking anon function */
// you may see a wrapper like this
(function (window, document, undefined) {
// code here
})(this, this.document);
/*********** Break Down ************/
// this function invoke itself
(function(){