From 2a1b4301d4c27516b7d4e5ad3bc7783a086911a6 Mon Sep 17 00:00:00 2001 From: fdai7763 Date: Tue, 23 Jan 2024 23:11:35 +0100 Subject: [PATCH] added test for extraction of a positive constant without math operators as well as corresponding functionality --- src/main/py/read_math_function.py | 5 +++++ src/test/py/test_read_math_function.py | 2 ++ 2 files changed, 7 insertions(+) diff --git a/src/main/py/read_math_function.py b/src/main/py/read_math_function.py index e0a7af7..2e7c0fd 100644 --- a/src/main/py/read_math_function.py +++ b/src/main/py/read_math_function.py @@ -52,3 +52,8 @@ def simplify_easy_math_function(type, function): return False if is_string(function) == False or is_allowed_string("c", function) == True: return simplify_constant("c", function) + +def extract_constant(function): + coefficient, constant = function.split('x', 1) if 'x' in function else (function, '') + constant = constant.lstrip('+') + return [constant, f"{coefficient}x"] \ No newline at end of file diff --git a/src/test/py/test_read_math_function.py b/src/test/py/test_read_math_function.py index 70cb55f..7fb7f19 100644 --- a/src/test/py/test_read_math_function.py +++ b/src/test/py/test_read_math_function.py @@ -38,6 +38,8 @@ class read_function(unittest.TestCase): self.assertEqual(simplify_easy_math_function("v", 42), 42) def test_easy_function_string_is_constant(self): self.assertEqual(simplify_easy_math_function("v", "42^(2)+1-3*4/3"), 1761) + def test_extraction_linear_function_constant_without_math_operators_addition(self): + self.assertEqual(extract_constant("42x+5"), ["5", "42x"]) if __name__ == '__main__': unittest.main()