diff --git a/src/main/py/read_math_function.py b/src/main/py/read_math_function.py index 00a9327..dfed4b6 100644 --- a/src/main/py/read_math_function.py +++ b/src/main/py/read_math_function.py @@ -1,9 +1,9 @@ -def read_constant(type, value): +def simplify_constant(type, value): if type != "c": return False if is_string(value): if is_allowed_string(type, value): - return calculate_string(value) + return calculate_constant_string(value) else: return False return value @@ -18,7 +18,7 @@ def is_allowed_string(type, value): return all(character.isdigit() or character in allowed for character in value) -def calculate_string(string): +def calculate_constant_string(string): string = string.replace('^', '**') try: return eval(string) diff --git a/src/test/py/test_read_math_function.py b/src/test/py/test_read_math_function.py index 0fe03d2..9f20e1a 100644 --- a/src/test/py/test_read_math_function.py +++ b/src/test/py/test_read_math_function.py @@ -4,9 +4,9 @@ from src.main.py.read_math_function import * class read_function(unittest.TestCase): def test_constant_without_calculation(self): - self.assertEqual(read_constant("c", 42), 42) + self.assertEqual(simplify_constant("c", 42), 42) def test_constant_no_c(self): - self.assertEqual(read_constant("e", "42*e^x"), False) + self.assertEqual(simplify_constant("e", "42*e^x"), False) def test_constant_isstring_true(self): self.assertEqual(is_string("42*e^x"), True) def test_constant_isstring_false(self): @@ -16,13 +16,13 @@ class read_function(unittest.TestCase): def test_constant_isallowed_string_false(self): self.assertEqual(is_allowed_string("c", "42^(2)+1-3*4/3x"), False) def test_constant_calculate_without_pow(self): - self.assertEqual(calculate_string("42+1-3*4/3"), 39) + self.assertEqual(calculate_constant_string("42+1-3*4/3"), 39) def test_constant_calculate_with_pow(self): - self.assertEqual(calculate_string("42^(2)+1-3*4/3"), 1761) + self.assertEqual(calculate_constant_string("42^(2)+1-3*4/3"), 1761) def test_constant_calculate_string_decision(self): - self.assertEqual(read_constant("c", "42^(2)+1-3*4/3"), 1761) + self.assertEqual(simplify_constant("c", "42^(2)+1-3*4/3"), 1761) def test_constant_string_not_allowed_decision(self): - self.assertEqual(read_constant("c", "42^(2)+1-3*4/3x"), False) + self.assertEqual(simplify_constant("c", "42^(2)+1-3*4/3x"), False) if __name__ == '__main__': unittest.main()