Найти в Дзене
📑 Recursive Infinite Generators python-puzzles.blogspot.com/...tml 📑 Task 1) What is the last number the program will print? 2) What object appears when this function is applied 3) Can you write a program for an infinite generator of fibonacci numbers without recursion but limit the number of iterations? def fibonacci_gen(x=0, y=1): yield x yield from fibonacci_gen(y, x + y) fg = fibonacci_gen() for _ in range(10): print(next(fg), end=' ') 📑 Answer 1) 34 2) It is a recursive infinite generator 3) def fibonacci_gen_step(x=0, y=1, steps=7): step = 1; yield x while True: if step == steps: return else: step += 1 x, y = y, x + y yield x fgs1 = fibonacci_gen_step() for _ in range(7): print(next(fgs1), end=' ') print() fgs2 = fibonacci_gen_step() for _ in range(10): try: print(next(fgs2), end=' ') except StopIteration: print(f"\nmaximum number of generator iterations = {_}") break
1 год назад
📑 A Series of Variables python-puzzles.blogspot.com/...tml 📑 Task 1) What will the program print for each case? 2) How to change f_string_y in the program so that variables y0, y1, y2 become global f_string_x = lambda i: \ f'''x{i} = {2 ** i}; print(f"{{x{i}=}}", end=' ')''' for i in range(3): exec(f_string_x(i)) print('x0' in globals()) f_string_y = lambda i: \ f'''y{i} = {2 ** i}; print(f"{{y{i}=}}", end=' ')''' [exec(f_string_y(i)) for i in range(3)] print('y0' in globals()) 📑 Answer 1) x0=1 x1=2 x2=4 True and y0=1 y1=2 y2=4 False 2) f_string_y = lambda i: \ f'''globals()["y{i}"] = {2 ** i}; print(f"{{y{i}=}}", end=' ')''' [exec(f_string_y(i)) for i in range(3)] print('y0' in globals())
1 год назад
📑 Function Dictionary python-puzzles.blogspot.com/...tml 📑 Task 1) What object is created by the first line of the program? 2) Rewrite the function so that the numbers from 0 to 3 are raised to the 2nd power f_dict = lambda n: {_: lambda x, y=_: x ** y for _ in range(n + 1)} for _ in f_dict(n=3): print(_, f_dict(n=3)[_](2)) 📑 Answer 1) The result is a function that creates a dictionary of functions 2) f_dict = lambda n: {_: lambda x, y=_: y ** x for _ in range(n + 1)} for _ in f_dict(n=3): print(_, f_dict(n=3)[_](2))
1 год назад
📑 Copying Lists python-puzzles.blogspot.com/...tml 📑 Task 1) What will the program print? 2) Make a list of identical elements and copy this list so that the result of the last three lines looks different: [[1], [], []] [[2], [], []] list_ = 3 * [[]] list_copy = list_.copy() print(list_, list_copy) list_[0].append(1) list_copy[0].append(2) print(list_, list_copy) 📑 Answer 1) 2 lists of empty lists: [[], [], []] [[], [], []] 2 identical lists of elements: [[1, 2], [1, 2], [1, 2]] [[1, 2], [1, 2], [1, 2]] 2) list_ = [] [list_.append([]) for _ in range(3)] list_copy = [_[:] for _ in list_] print(list_, list_copy) list_[0].append(1) list_copy[0].append(2) print(list_, list_copy)
1 год назад
📑 Continue and Pass python-puzzles.blogspot.com/...tml 📑 Task 1) Will the code print two identical lines? 2) How can you modify the code to avoid using external variables when loading a string argument into the function exec()? def command_string(command): string = ''' for i in range(11): x = i if not i: ''' + command + ''' elif i % 3 == 0: x = 'x' print(x, end=' ') print()''' return string exec(command_string('continue')) exec(command_string('pass')) 📑 Answer 1) The lines will be different: - in the second case, the program will not perform any actions when i = 0, - in the first case, it will skip this iteration 2) def command_string_dict(command): dict_ = {'continue': 'continue', 'pass': 'pass'} string = f''' for i in range(11): x = i if not i: {dict_[command]} elif i % 3 == 0: x = 'x' print(x, end=' ') print()''' return string exec(command_string_dict('continue')) exec(command_string_dict('pass'))
1 год назад
Если нравится — подпишитесь
Так вы не пропустите новые публикации этого канала