From 9f4593eb8d2311d2914b0d2d973b0da8596464a1 Mon Sep 17 00:00:00 2001 From: Kar0r404 Date: Thu, 30 Oct 2025 02:04:06 +0200 Subject: [PATCH] complete --- utils.py | 76 ++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 49 insertions(+), 27 deletions(-) diff --git a/utils.py b/utils.py index dd6613d..0f83bd7 100644 --- a/utils.py +++ b/utils.py @@ -4,41 +4,63 @@ complete methods below and add missing methods according to the tests. """ -class Calculator(): - """rounding_digits is a default value by which + + +class Calculator: + """rounding_digits is a default value by which the calculator rounds off numbers 10/3 = 3.3333333333... - if rounding_digits = 2 the answer for this expression is 3.33 + if rounding_digits = 2 the answer for this expression is 3.33 """ + rounding_digits = 0 - - def __init__(self,round_digits): + + def __init__(self, round_digits): self.rounding_digits = round_digits - - def addtion(self,a,b): - return 0 - def subtraction(self,a,b): - return 0 - def multiplication(self,a,b): - return 0 - def division(self,a,b): - return 0 - + + def addtion(self, a, b): + return self._round(a + b, self.rounding_digits) + + def subtraction(self, a, b): + return self._round(a - b, self.rounding_digits) + + def multiplication(self, a, b): + return self._round(a * b, self.rounding_digits) + + def division(self, a, b): + return self._round(a / b, self.rounding_digits) + @property def round_digits(self): return self.rounding_digits - - def set_ound_digits(self,digits): + + def set_round_digits(self, digits): self.rounding_digits = digits - - -class History(): - filepath_ = "" + + @staticmethod + def _round(answer: float, decimal: int | None = None) -> float: + if decimal is not None and isinstance(decimal, int) and decimal > 0: + return round(answer, decimal) + return answer + + +class History: + def __init__(self, filepath="History.txt"): + self.filepath_ = filepath + def __init__(self, filepath): self.filepath_ = filepath - - def save(self,expression:str): - pass - def restore(self)->list[str]: - pass + + def save(self, expression: str): + with open(self.filepath_, "a", encoding="utf-8", errors="ignore") as file: + file.write(expression + "\n") + + def restore(self) -> list[str]: + try: + with open(self.filepath_, "r", encoding="utf-8", errors="ignore") as file: + return [line.strip() for line in file if line.strip()] + except FileNotFoundError: + return [] + def clear(self): - pass \ No newline at end of file + with open(self.filepath_, "w", encoding="utf-8", errors="ignore"): + pass