higher_func.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. from typing import Callable
  2. def apply(func, objects):
  3. return [func(x) for x in objects]
  4. def compute(func_list, *args):
  5. return [func(x) for func in func_list for x in args]
  6. def compute_new(func_list, *args):
  7. lst = []
  8. value = 0
  9. for x in args:
  10. value = x
  11. for func in func_list:
  12. value = func(value)
  13. lst.append(value)
  14. return lst
  15. # def filter_list(f: Callable, lst: list):
  16. def filter_list(f, lst):
  17. return [value for value in lst if f(value)]
  18. def filter_collection(f, collection):
  19. lst = [value for value in collection if f(value)]
  20. if isinstance(collection, str):
  21. return ''.join(lst)
  22. return type(collection)(lst)
  23. """Для теста"""
  24. def square(num):
  25. return num ** 2
  26. def inc(num):
  27. return num + 1
  28. def dec(num):
  29. return num - 1
  30. def is_even(num):
  31. return num % 2 == 0
  32. def main():
  33. # print(compute([inc, dec, square], 10, 20, 30, 40))
  34. # print(compute_new([inc, square, dec], 10, 20, 30, 40))
  35. # numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  36. # even_numbers = filter_list(is_even, numbers) # берем только четные
  37. # print(even_numbers)
  38. numbers = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
  39. even_numbers = filter_collection(is_even, numbers)
  40. print(even_numbers)
  41. print(filter_collection(lambda x: x not in 'aeiou', 'I never heard those lyrics before'))
  42. if __name__ == '__main__':
  43. main()