Python Tasks & ML | Задачи по питону и машинному обучению
9.33K subscribers
27 photos
1 file
36 links
Algorithms, functions, classes, regular expressions, iterators, generators, OOP, exceptions, NumPy, pandas, scikit-learn
https://telega.in/c/python_tasks

Questions — @dina_ladnyuk
Download Telegram
Что выведет код?

from contextlib import contextmanager

@contextmanager
def simple_context():
print("enter")
yield
print("exit")

with simple_context():
print("inside")
Что выведет код?

 echo():
value = yield "start"
yield value

g = echo()
print(next(g))
print(g.send("hello"))
Что выведет код?

def flatten(lst):
for item in lst:
if isinstance(item, list):
yield from flatten(item)
else:
yield item

print(list(flatten([1, [2, [3, 4]], 5])))
Что выведет код?

from itertools import tee

it = iter([10, 20, 30])
a, b = tee(it)

print(next(a), next(b), list(a), list(b))
Что выведет код?

with open("data.txt", "w", encoding="utf-8") as f:
f.write("apple\n")
f.write("banana\n")
f.write("cherry\n")

def read_lines():
for line in open("data.txt"):
yield line.strip()

lines = read_lines()
print(next(lines))
print(next(lines))
print(next(lines))
Что выведет код?

from itertools import groupby

data = ['a', 'a', 'b', 'b', 'b', 'a']
groups = [(k, list(g)) for k, g in groupby(data)]

print(groups)