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()