class Sheet:
def __init__(self, width, length, thickness=1, count=0):
self.width, self.length = sorted([width, length])
self.thickness = thickness
self.count = count
@property
def foldable(self):
return self.length >= 2 * self.thickness
def fold_once(self):
if self.foldable:
self.width, self.length = sorted([self.width, self.length/2])
self.thickness *= 2
self.count += 1
return True
else:
return False
def fold(self):
while self.fold_once():
pass
return self
def run_test_cases():
assert Sheet(82, 18).fold().count == 4
assert Sheet(404, 279).fold().count == 6
assert Sheet(10, 26).fold().count == 3
if __name__ == '__main__':
# run_test_cases()
with open('input.txt', 'rt') as fi, open('output.txt', 'wt') as fo:
dimensions = map(float, next(fi).split())
answer = Sheet(*dimensions).fold().count
print(answer, file=fo)
from scipy.optimize import minimize
import numpy as np
def func(x):
return np.abs(np.sin(x)-x)
minimize(func, -1)
fun: 1.3390927663600727e-08
hess_inv: array([[ 109.41685172]])
jac: array([ -9.30984970e-06])
message: 'Optimization terminated successfully.'
nfev: 36
nit: 11
njev: 12
status: 0
success: True
x: array([-0.00431507])
result = minimize(func, -1)
print(result.fun) # => 1.3390927663600727e-08
print(result.x[0]) # => -0.0043150659686
x = -0.00431507
sin(x) - x # => 1.3390965196077853e-08
result = minimize(func, -1, method='Nelder-Mead')
python -m cProfile -s time z:\test.py > z:\profile.txt
ncalls tottime percall cumtime percall filename:lineno(function)
3188 0.609 0.000 1.730 0.001 test.py:71(suggest_coord)
39844 0.422 0.000 0.902 0.000 test.py:164(check_fit_score)
128340 0.223 0.000 0.223 0.000 test.py:254(get_cell)
67237 0.223 0.000 0.338 0.000 test.py:257(check_if_cell_clear)
3188 0.130 0.000 1.063 0.000 test.py:108(sort_coordlist)
64378 0.094 0.000 0.094 0.000 {method 'append' of 'list' objects}
34372/34369 0.052 0.000 0.052 0.000 {built-in method builtins.len}
108 0.034 0.000 0.062 0.001 test.py:23(clear_grid)
3188 0.032 0.000 1.827 0.001 test.py:123(fit_and_add)
3297 0.027 0.000 0.058 0.000 random.py:260(shuffle)
3299 0.023 0.000 0.035 0.000 {method 'sort' of 'list' objects}
2160 0.022 0.000 0.056 0.000 test.py:336(__init__)
1193 0.020 0.000 0.035 0.000 test.py:236(set_word)
try:
...
except SomeException as e:
...
except AnotherException as e:
...
except (ThirdException, FourthException):
...
else:
...
finally:
...
from contextlib import suppress
with suppress(SomeException):
...
try:
...
except SomeException:
pass
list(zip(*filter(any, zip(*filter(any, matrix)))))
def trim(matrix):
fltr = lambda matrix: zip(*filter(any, matrix))
return [list(row) for row in fltr(fltr(matrix))]
matrix = trim(matrix)
from collections import defaultdict
dic = defaultdict(set)
dic[1].add(1)
dic[2].add(2)
dic[1].add(1)
dic[1].add(3)
print(dic)
print(dic[0])
from itertools import count
def lastmax(*sequence):
return max(zip(sequence, count()))
assert lastmax(1, 2, 3) == (3, 2)
assert lastmax(1, 2, 1, 2, 1) == (2, 3)
assert lastmax(1, 2, 3, 4, 5) == (5, 4)
assert lastmax(1, 1, 1, 1, 1) == (1, 4)
def lastmax(*sequence):
return max(zip(sequence, range(len(sequence))))
from operator import itemgetter
def lastmax(*sequence):
return max(enumerate(sequence), key=itemgetter(1, 0))[::-1]
def lastmax(*sequence):
return max({v: i for i, v in enumerate(sequence)}.items())
d = {1: [1, 2, 3, 4], 3: [5, 7, 9], 9: [9, 0, 5, 4, 6]}
while d:
for k, v in d.copy().items():
print(f'{k}:{v.pop(0)}') if v else d.pop(k)
while d:
for key, vals in tuple(d.items()):
try:
print(f'{key}:{vals.pop(0)}')
except IndexError:
del d[key]
from itertools import chain, cycle, zip_longest
def solve(mapping, placeholder=None):
keys = cycle(mapping.keys())
vals = chain(*zip_longest(*mapping.values(), fillvalue=placeholder))
for key, val in zip(keys, vals):
if val is not placeholder:
print(f'{key}:{val}')
solve({1: [1, 2, 3, 4], 3: [5, 7, 9], 9: [9, 0, 5, 4, 6]})
from itertools import repeat, starmap
def solve(mapping):
iterf = lambda key, vals: (repeat(key), iter(vals))
items = tuple(starmap(iterf, mapping.items()))
for _ in max(mapping.values(), key=len):
for ikeys, ivals in items:
try:
print(f'{next(ikeys)}:{next(ivals)}')
except StopIteration:
pass
solve({1: [1, 2, 3, 4], 3: [5, 7, 9], 9: [9, 0, 5, 4, 6]})