Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from typing import Callable
- def make_average(accumulation_period) -> Callable[[float], float]:
- # ваш код
- array = [0] * accumulation_period
- cur_days = 0
- cur_sum = 0
- start = 0
- end = accumulation_period - 1
- def get_avg(delta): #алгоритм пересчёта среднего
- nonlocal array
- nonlocal accumulation_period
- nonlocal cur_days
- nonlocal cur_sum
- nonlocal start
- nonlocal end
- div = accumulation_period
- if (cur_days + 1 < accumulation_period):
- cur_days += 1
- div = cur_days
- cur_sum -= array[start]
- cur_sum += delta
- start = (start + 1) % accumulation_period
- end = (end + 1) % accumulation_period
- array[end] = delta
- res = cur_sum / div
- """
- print("delta", delta)
- print("array", array)
- print("accumulation_period", accumulation_period)
- print("cur_days", cur_days)
- print("cur_sum", cur_sum)
- print("start", start)
- print("end", end)
- print("div", div)
- print("res", res)
- print()
- """
- return res
- return get_avg
- def is_floats_eq(lhs: float, rhs: float, eps: float = 1e-6) -> bool:
- """
- Сравнивает числа с плавающей точкой на равенство с заданной точностью.
- Args:
- lhs: левый аргумент сравнения.
- rhs: правый аргумент сравнения.
- eps: точность. По умолчанию сравнение происходит с точностью до 6 знаков после запятой.
- Returns:
- Булево значение. True, если числа равны, False - иначе.
- """
- return abs(lhs - rhs) < eps
- """
- a = make_average(3)
- delta = [1, 2, 3, 4, 5]
- for i in delta:
- a(i)
- b = make_average(4)
- for i in delta:
- b(i)
- """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement