From 3150fa828e08ebe73e7bc8ee99707a49bc057676 Mon Sep 17 00:00:00 2001 From: fdai7763 Date: Tue, 23 Jan 2024 23:23:27 +0100 Subject: [PATCH] added test for splitting linear function without added constant as well as corresponding functionality --- src/main/py/read_math_function.py | 4 +++- src/test/py/test_read_math_function.py | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/py/read_math_function.py b/src/main/py/read_math_function.py index 8155947..ea86ed6 100644 --- a/src/main/py/read_math_function.py +++ b/src/main/py/read_math_function.py @@ -56,5 +56,7 @@ def simplify_easy_math_function(type, function): def extract_constant(function): coefficient, constant = function.split('x', 1) if 'x' in function else (function, '') - constant = constant.lstrip('+') + if constant == "": + constant = constant + "+0" + constant = constant.lstrip("+") return [constant, f"{coefficient}x"] diff --git a/src/test/py/test_read_math_function.py b/src/test/py/test_read_math_function.py index 9c7ff35..d2ff9e2 100644 --- a/src/test/py/test_read_math_function.py +++ b/src/test/py/test_read_math_function.py @@ -42,6 +42,8 @@ class read_function(unittest.TestCase): self.assertEqual(extract_constant("42x+5"), ["5", "42x"]) def test_extraction_linear_function_constant_without_math_operators_subtraction(self): self.assertEqual(extract_constant("42x-5"), ["-5", "42x"]) + def test_extraction_linear_function_no_constant(self): + self.assertEqual(extract_constant("42x"), ["0", "42x"]) if __name__ == '__main__': unittest.main()