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
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): |
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
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: |
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 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(): |
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
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) |
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 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 |
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 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() |
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
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: |
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 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): |
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
""" | |
Result: | |
main->sub1 0.10152816772460938 | |
main->sub2 0.20150494575500488 | |
main 0.31107306480407715 | |
main2->sub1 0.30101609230041504 | |
main2->sub2 0.4036266803741455 | |
main2 0.7056293487548828 | |
""" |
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
import tatsu | |
from pprint import pprint | |
import sys | |
import sys | |
from tatsu.codegen import ModelRenderer | |
from tatsu.codegen import CodeGenerator | |
grammar = """ | |
@@grammar::test |
NewerOlder