Commit 463ba475 authored by Roberts Ivanovs's avatar Roberts Ivanovs
Browse files

Added a "square" operand

parent 3b5bafc3
......@@ -4,6 +4,7 @@ class Node:
"-": (lambda x, y: x - y),
"*": (lambda x, y: x * y),
"/": (lambda x, y: x / y),
"^": (lambda x, y: x ** y),
}
def __init__(self, operator=None, value=None):
......@@ -61,7 +62,7 @@ def extract_number(string):
for ind, letter in enumerate(string):
if letter.isdigit():
result += letter
elif ind == 0 and letter in "+-*/":
elif ind == 0 and letter in Node.ops.keys():
operator = letter
else:
break
......@@ -88,7 +89,7 @@ def calculate(expr, binary_tree=None):
binary_tree.root.add_right(new_number)
root_node = binary_tree.root
else:
if operator not in "*/":
if operator not in "*/^":
binary_tree.add_root(Node(operator=operator), "left")
binary_tree.root.add_right(new_number)
root_node = binary_tree.root
......
......@@ -88,6 +88,31 @@ class TreeTestCalculateFunction(unittest.TestCase):
res = calculate(expression)
self.assertEqual(res.calculate_below(), 32)
def test_sqr(self):
"""
Testing the the calculable result for expression '3^2'
"""
expression = "3^2"
res = calculate(expression)
self.assertEqual(res.calculate_below(), 9)
def test_sqr_negative(self):
"""
Testing the the calculable result for expression '-3^2'
"""
expression = "-3^2"
res = calculate(expression)
self.assertEqual(res.calculate_below(), -9)
def test_sqr_subtraction(self):
"""
Testing the the calculable result for expression '10-3^2'
"""
expression = "10-3^2"
res = calculate(expression)
self.assertEqual(res.operator, "-")
self.assertEqual(res.calculate_below(), 1)
if __name__ == '__main__':
unittest.main()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment