Найти в Дзене
📑 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 год назад
📑 The special variable "_" python-puzzles.blogspot.com/...tml 📑 Task 1) What properties of the “_” character do we observe in code cells? 2) How can you restore the properties of this special variable? 10 * 10 print(100 + _) 10 + _ print(100 + _) 10 + _ print(_, __, ___) _, __, ___ = range(3) 10 + _ print(_) print(_ * __ * ___) print(_ * __ * '___') 📑 Answer 1) "_" is a special variable that represents the last returned value and reassigning "_" convert it into an usual variable 2) # from builtins import * # or exit from the session import os os._exit(00) 10 * 10 print(100 + _) 10 + _
1 год назад
📑 Exploration of the Function locals() python-puzzles.blogspot.com/...tml 📑 Task 1) What will the program print? 2) How can we change the 3rd line of the program so that the function takes elements of the same dictionaries as arguments but the program prints out only keys of these dictionaries? def local_args(*args): return locals() print(local_args(1, 2) == locals()['local_args'](1, 2)) print(local_args({'a': 1, 'b': 2}, {'b': 3})['args']) 📑 Answer 1) Two rows: True and ({'a': 1, 'b': 2}, {'b': 3}) 2) def local_args(*args): return locals() print(local_args(*{'a': 1, 'b': 2}, *{'b': 3})['args'])
1 год назад
Генерация серии изображений с помощью ИИ Базовая фотография одна
1 год назад
📑 Dictionary Comprehensions python-puzzles.blogspot.com/...tml 📑 Task 1) What two numbers will the program print? 2) Replace the first five lines of the program with a single line containing a dictionary comprehension dict_ = {} for i in range(3): dict_[i] = i dict_[0] += 1 dict_ = {**dict_, **{i: i ** 2 for i in range(3, 5)}} print(sum(dict_), sum(dict_.values())) 📑 Answer 1) 10 29 The first number is the sum of the keys of the resulting dictionary The second number is the sum of its values 2) dict_ = {i: i ** ((i + 1) // 2) for i in range(5)} print(sum(dict_), sum(dict_.values()))
1 год назад
📑 Boolean Values of Objects python-puzzles.blogspot.com/...tml 📑 Task 1) What will be printed 2) Explain the result with a code snippet, then remove one punctuation character so the program result would be 0 x = 0, print(1 if x and 1 else 0) 📑 Answer 1) 1 2) x = 0, print(type(x), bool(x), bool(x and 1)) print(1 if x and 1 else 0) x = 0 print(type(x), bool(x), bool(x and 1)) print(1 if x and 1 else 0)
1 год назад
📑 Nested Functions for Polynomials python-puzzles.blogspot.com/...tml 📑 Task 1) What will be the result of the calculations? 2) Can you write this code in a form of an anonymous function? 3) Can you make this function more universal so that it builds a polynomial of any degree using arguments as coefficients? def polynomials(a, b, c): def polynomial(x): return a * x**2 + b * x + c return polynomial polynomials(polynomials(1, 1, 1)(0), 1, 1)(0) 📑 Answer 1) 𝑝𝑜𝑙𝑦𝑛𝑜𝑚𝑖𝑎𝑙𝑠(1,1,1)(0) builds the function 𝑓(𝑥)=𝑥2+𝑥+1 and finds its value 𝑓(𝑥)=1 at 𝑥=0 twice So the program will print out 1 2) polynomials_lambda = \ lambda a, b, c: lambda x: a * x**2 + b * x + c polynomials_lambda(polynomials_lambda(1, 1, 1)(0), 1, 1)(0)
1 год назад
Возрастные изменения в понимании ИИ Генерация изображений с помощью алгоритма ToonMe
1 год назад