from typing import Callable def apply(func, objects): return [func(x) for x in objects] def compute(func_list, *args): return [func(x) for func in func_list for x in args] def compute_new(func_list, *args): lst = [] value = 0 for x in args: value = x for func in func_list: value = func(value) lst.append(value) return lst # def filter_list(f: Callable, lst: list): def filter_list(f, lst): return [value for value in lst if f(value)] def filter_collection(f, collection): lst = [value for value in collection if f(value)] if isinstance(collection, str): return ''.join(lst) return type(collection)(lst) def aggregation(func, sequence): new_list = [] foo = func(sequence[0], sequence[1]) new_list.append(foo) for x in range(2, len(sequence)): foo = func(foo, sequence[x]) new_list.append(foo) return new_list def aggregation_2(func, sequence): new_list = [] foo = func(sequence[0], sequence[1]) new_list.append(foo) for x in range(2, len(sequence)): foo = func(foo, sequence[x]) new_list.append(foo) return new_list[-1] def aggregation_3(func, sequence, initial=None): new_list = [] if initial != None: foo = initial for x in sequence: foo = func(foo, x) else: foo = func(sequence[0], sequence[1]) for x in range(2, len(sequence)): foo = func(foo, sequence[x]) return foo """Для теста""" def square(num): return num ** 2 def inc(num): return num + 1 def dec(num): return num - 1 def is_even(num): return num % 2 == 0 def get_add(x, y): return x + y def get_max(x, y): return max(x, y) def get_product(x, y): return x * y def main(): # print(compute([inc, dec, square], 10, 20, 30, 40)) # print(compute_new([inc, square, dec], 10, 20, 30, 40)) # numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # even_numbers = filter_list(is_even, numbers) # берем только четные # print(even_numbers) # numbers = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) # even_numbers = filter_collection(is_even, numbers) # print(even_numbers) # print(filter_collection(lambda x: x not in 'aeiou', 'I never heard those lyrics before')) # print(aggregation(get_add, [5, 2, 4, 3, 5])) # print(aggregation_2(get_max, [1, 4, 5, 7, 6, 5, 8, 10, 5])) # print(aggregation_2(get_add, [5, 2, 4, 3, 5])) # print(aggregation_3(lambda x, y: x + y, [4, 5, 6], initial=100)) print(aggregation_3(get_product, [2, 5, 10, 1, 2])) if __name__ == '__main__': main()