lst_keys = ['6.95', '6.95', '6.95', '6.95', '6.96', '6.96', '6.96', '6.96', '6.96', '6.96']
lst_vals = ['0/4', '0/10', '0/6', '0/18', '0/9', '0/24', '0/22', '0/11', '0/14', '0/7']
d = dict()
for key, val in zip(lst_keys, lst_vals):
d.setdefault(key, []).append(val)
print(d)
for key, val in zip(lst_keys, lst_vals):
try:
d[key].append(val)
except KeyError:
d[key] = [val]
from collections import defaultdict
d = defaultdict(list)
for key, val in zip(lst_keys, lst_vals):
d[key].append(val)
from numpy import sqrt
from decimal import Decimal
n = Decimal(100000000000000000000000000000000000000000000000000)
sqrt(n) # Decimal('10000000000000000000000000')
branch = parsed_addr['response']['GeoObjectCollection']['featureMember']['GeoObject']['Point']
branch['pos']
parsed_addr.response.GeoObjectCollection.featureMember.GeoObject.Point.pos
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])