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 / trace.py
Last active April 14, 2018 10:12
a performance tracer for Python
"""
Result:
main->sub1 0.10152816772460938
main->sub2 0.20150494575500488
main 0.31107306480407715
main2->sub1 0.30101609230041504
main2->sub2 0.4036266803741455
main2 0.7056293487548828
"""
import tatsu
from pprint import pprint
import sys
import sys
from tatsu.codegen import ModelRenderer
from tatsu.codegen import CodeGenerator
grammar = """
@@grammar::test