Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
s8_ivanov_r
AT_binary_tree_calculator
Commits
463ba475
Commit
463ba475
authored
Nov 18, 2019
by
Roberts Ivanovs
Browse files
Added a "square" operand
parent
3b5bafc3
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
28 additions
and
2 deletions
+28
-2
calculator_tree.py
calculator_tree.py
+3
-2
test_calculator_tree.py
test_calculator_tree.py
+25
-0
No files found.
calculator_tree.py
View file @
463ba475
...
...
@@ -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
...
...
test_calculator_tree.py
View file @
463ba475
...
...
@@ -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
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment