12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- 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 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 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]))
- if __name__ == '__main__':
- main()
|