Advertisement
Korotkodul

A. Фиксируем прибыль

Sep 20th, 2024 (edited)
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.99 KB | None | 0 0
  1. from typing import Callable
  2.  
  3.  
  4. def make_average(accumulation_period) -> Callable[[float], float]:
  5.     # ваш код
  6.     array = [0] * accumulation_period
  7.     cur_days = 0
  8.     cur_sum = 0
  9.     start = 0
  10.     end = accumulation_period - 1
  11.     def get_avg(delta): #алгоритм пересчёта среднего
  12.         nonlocal array
  13.         nonlocal accumulation_period
  14.         nonlocal cur_days
  15.         nonlocal cur_sum
  16.         nonlocal start
  17.         nonlocal end
  18.         div = accumulation_period
  19.         if (cur_days + 1 < accumulation_period):
  20.             cur_days += 1
  21.             div = cur_days
  22.         cur_sum -= array[start]
  23.         cur_sum += delta
  24.         start = (start + 1) % accumulation_period
  25.         end = (end + 1) % accumulation_period
  26.         array[end] = delta
  27.         res = cur_sum / div
  28.         """
  29.        print("delta", delta)
  30.        print("array", array)
  31.        print("accumulation_period", accumulation_period)
  32.        print("cur_days", cur_days)
  33.        print("cur_sum", cur_sum)
  34.        print("start", start)
  35.        print("end", end)
  36.        print("div", div)
  37.        print("res", res)
  38.        print()
  39.        """
  40.         return res
  41.     return get_avg
  42.  
  43. def is_floats_eq(lhs: float, rhs: float, eps: float = 1e-6) -> bool:
  44.     """
  45.    Сравнивает числа с плавающей точкой на равенство с заданной точностью.
  46.  
  47.    Args:
  48.        lhs: левый аргумент сравнения.
  49.        rhs: правый аргумент сравнения.
  50.        eps: точность. По умолчанию сравнение происходит с точностью до 6 знаков после запятой.
  51.  
  52.    Returns:
  53.        Булево значение. True, если числа равны, False - иначе.
  54.    """
  55.     return abs(lhs - rhs) < eps
  56.  
  57. """  
  58. a = make_average(3)
  59. delta = [1, 2, 3, 4, 5]
  60. for i in delta:
  61.    a(i)
  62.  
  63. b = make_average(4)
  64. for i in delta:
  65.    b(i)
  66. """
  67.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement