lllumineux

Untitled

Dec 1st, 2020
529
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from typing import Union
  2.  
  3.  
  4. class AverageCounter:
  5.     def __init__(self):
  6.         self._elements_count = 0
  7.         self._elements_sum = 0
  8.  
  9.     def add(self, number: int) -> None:
  10.         self._elements_count += 1
  11.         self._elements_sum += number
  12.  
  13.     def reset(self) -> None:
  14.         self._elements_count = 0
  15.         self._elements_sum = 0
  16.  
  17.     @property
  18.     def average(self) -> Union[int, float]:
  19.         if self._elements_count > 0:
  20.             return self._elements_sum / self._elements_count
  21.  
  22.  
  23. def main():
  24.     average_counter = AverageCounter()
  25.  
  26.     average_counter.add(1)
  27.     average_counter.add(2)
  28.     average_counter.add(3)
  29.     print(average_counter.average)
  30.  
  31.     average_counter.reset()
  32.     print(average_counter.average)
  33.  
  34.  
  35. if __name__ == "__main__":
  36.     main()
  37.  
RAW Paste Data