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]})
sequence = [2, 3, 7, 8, 0]
# reversed возвращает итератор (не массив)
for item in reversed(sequence):
print(item)
# слайс возвращает новый список (массив)
for item in sequence[::-1]:
print(item)
from itertools import count
def subsequences(sequence):
start = 0
expected = count(sequence[start])
last_index = len(sequence) - 1
for end, val in enumerate(sequence):
flag_val = val == next(expected)
flag_end = end == last_index
if flag_val:
if flag_end:
yield sequence[start:]
continue
yield sequence[start:end]
if flag_end:
yield sequence[end:]
continue
expected = count(sequence[end+1])
start = end
def solve(sequence):
return max(subsequences(sequence), key=len)
if __name__ == '__main__':
sequence = [0, 2, 3, 6, 7, 8, 9, 10, 13, 14, 15, 16, 20]
print(solve(sequence)) # => [6, 7, 8, 9, 10]
from operator import sub
from itertools import groupby
def solve(sequence, step=1):
pairs = zip(sequence[:-1], sequence[1:])
criteria = lambda pair: abs(sub(*pair)) == step
grouped = groupby(pairs, key=criteria)
subsequences = (tuple(group) for flag, group in grouped if flag)
result, extra = zip(*max(subsequences, key=len))
return result + extra[-1:]
if __name__ == '__main__':
sequence = [0, 2, 3, 6, 7, 6, 7, 8, 9, 10, 13, 14, 15, 16, 20]
print(solve(sequence)) # => (6, 7, 6, 7, 8, 9, 10)
result = '0' in input().split()
result = not all(
map(int, input().split())
)
from itertools import repeat
def ask(question, expected=int):
return expected(input(question))
def ask_quantity():
return ask('Введите количество чисел: ')
def ask_number():
return ask('Введите число: ')
def call(func):
return func()
def solve():
return not(all(map(call, repeat(ask_number, ask_quantity()))))
if __name__ == '__main__':
print(f'Результат: {solve()}')
print(min(map(int, input().split()), key=lambda x: (not x % 2, x)))
from difflib import get_close_matches as gcm
model = 'A 170 Classic - 7/2004 - 85Kw'.upper()
model_list = map(str.upper, [
'A 170 CDI CAT ELEGANCE',
'A 170 CDI CAT CLASSIC',
'A 170 CDI CAT AVANTGARDE',
])
result = gcm(model, model_list, n=1, cutoff=0.5)[0]
print(result) # => 'A 170 CDI CAT CLASSIC'
from difflib import SequenceMatcher as SM
s1 = 'A 170 Classic - 7/2004 - 85Kw'.upper()
s2 = 'A 170 CDI CAT CLASSIC'.upper()
SM(isjunk=None, a=s1, b=s2, autojunk=True).ratio() # => 0.52
name = 'toster'
option1 = f"CREATE TABLE '{name}' ..."
option2 = "CREATE TABLE '{0}' ...".format(name)
option3 = "CREATE TABLE '{name}' ...".format(name='toster')
option4 = "CREATE TABLE '{name}' ...".format(**{'name': 'toster'})
def flatten(sequence):
for item in sequence:
try:
yield from item # Или yield from flatten(item) для более общего случая
except TypeError:
yield item
from itertools import chain
def flatten(sequence):
list_of_lists = ([item] if type(item) is int else item for item in sequence)
return list(chain.from_iterable(list_of_lists))
flatten([1, 2, 3, [4, 5, 6], 7, 8])
[int(e) for e in re.findall('\d+', str(a))]