From 6bfd51953d0ae66cd31fcb1ecd53a9a9dbf1cf8c Mon Sep 17 00:00:00 2001
From: Adam Blank <blank@caltech.edu>
Date: Mon, 21 Oct 2024 16:46:23 +0000
Subject: [PATCH] autocommit

---
 09/arithmetic.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 46 insertions(+)
 create mode 100644 09/arithmetic.py

diff --git a/09/arithmetic.py b/09/arithmetic.py
new file mode 100644
index 0000000..c7c08ea
--- /dev/null
+++ b/09/arithmetic.py
@@ -0,0 +1,46 @@
+from termcolor import colored
+
+
+def apply_operation(op: str, a: float, b: float) -> float:
+    if op == '+':
+        return a + b
+    elif op == '-':
+        return a - b
+    elif op == '/':
+        return a / b
+    elif op == '*':
+        return a * b
+
+
+def as_number(x: str) -> float:
+    return float(x)
+
+
+def as_int(x: float) -> int | float:
+    if int(x) == x:
+        return int(x)
+    else:
+        return x
+
+
+def compute_answer(expr: str) -> float:
+    pieces: list[str] = expr.split(" ")
+    while '' in pieces:
+        pieces.remove('')
+
+    if len(pieces) != 3:
+        print("You didn't provide two operands and an operation!")
+
+    a: float = as_number(pieces[0])
+    b: float = as_number(pieces[2])
+
+    op: str = pieces[1]
+    answer: float = apply_operation(op, a, b)
+
+    return as_int(answer)
+
+
+while True:
+    expr: str = input("Enter a mathematical expression: ")
+    ans: float = compute_answer(expr)
+    print(">> " + str(ans))
-- 
GitLab