Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
tail_and_mutual_recursion_examples
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
SOFT Core
SOFT 161 and 162
tail_and_mutual_recursion_examples
Commits
e4c3b8fa
Commit
e4c3b8fa
authored
Mar 17, 2021
by
Christopher Bohn
Browse files
Options
Downloads
Patches
Plain Diff
Added real, if simple, logic to parse function
parent
764ef2b7
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
MutualRecursion.py
+40
-9
40 additions, 9 deletions
MutualRecursion.py
with
40 additions
and
9 deletions
MutualRecursion.py
+
40
−
9
View file @
e4c3b8fa
def
parse
(
expression
):
# This should have real logic
left_expression
=
"
1
"
operator
=
"
+
"
right_expression
=
"
2
"
import
math
operators
=
{
'
+
'
,
'
-
'
,
'
*
'
,
'
/
'
}
def
parse
(
expression
):
"""
Expects an expression in the form of left-paren left-expression operator right-expression right-paren,
where each left/right expression is either an integer or another expression.
For example,
'
(1+2)
'
or
'
((1+2)-(3*4))
'
"""
if
expression
[
0
]
!=
'
(
'
or
expression
[
-
1
]
!=
'
)
'
:
raise
SyntaxError
(
f
'
Expression must be bounded by parenthesis. $
{
expression
}
does not satisfy.
'
)
full_expression
=
expression
.
replace
(
'
'
,
''
)[
1
:
-
1
]
if
full_expression
[
0
]
!=
'
(
'
:
# the left expression is an integer
index
=
math
.
inf
for
operator
in
operators
:
this_operator_index
=
full_expression
.
find
(
operator
)
if
-
1
<
this_operator_index
<
index
:
index
=
this_operator_index
else
:
parentheses_count
=
1
index
=
1
while
parentheses_count
>
0
:
if
full_expression
[
index
]
==
'
(
'
:
parentheses_count
+=
1
if
full_expression
[
index
]
==
'
)
'
:
parentheses_count
-=
1
index
+=
1
left_expression
=
full_expression
[:
index
]
operator
=
full_expression
[
index
]
right_expression
=
full_expression
[
index
+
1
:]
return
left_expression
,
operator
,
right_expression
def
add
(
left_expression
,
right_expression
):
return
evaluate
(
left_expression
)
+
evaluate
(
right_expression
)
return
int
(
evaluate
(
left_expression
)
)
+
int
(
evaluate
(
right_expression
)
)
def
subtract
(
left_expression
,
right_expression
):
return
evaluate
(
left_expression
)
-
evaluate
(
right_expression
)
return
int
(
evaluate
(
left_expression
)
)
-
int
(
evaluate
(
right_expression
)
)
def
multiply
(
left_expression
,
right_expression
):
return
evaluate
(
left_expression
)
*
evaluate
(
right_expression
)
return
int
(
evaluate
(
left_expression
)
)
*
int
(
evaluate
(
right_expression
)
)
def
divide
(
left_expression
,
right_expression
):
return
evaluate
(
left_expression
)
/
evaluate
(
right_expression
)
return
int
(
evaluate
(
left_expression
)
)
/
int
(
evaluate
(
right_expression
)
)
operator_map
=
{
...
...
@@ -38,4 +66,7 @@ def evaluate(expression):
if
__name__
==
'
__main__
'
:
print
(
evaluate
(
"
1+2
"
))
print
(
f
'
(1 + 2) =
{
evaluate
(
"
(1 + 2)
"
)
}
'
)
print
(
f
'
((1+2) - (3*4)) =
{
evaluate
(
"
((1+2) - (3*4))
"
)
}
'
)
print
(
f
'
(1 - (3 * 4)) =
{
evaluate
(
"
(1 - (3 * 4))
"
)
}
'
)
print
(
f
'
((1+2)-3) =
{
evaluate
(
"
((1+2)-3)
"
)
}
'
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment