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))]