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
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
ee26c465
Commit
ee26c465
authored
4 years ago
by
Christopher Bohn
Browse files
Options
Downloads
Patches
Plain Diff
Introduced additional error handling and doctests to parse function
parent
e4c3b8fa
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
+47
-4
47 additions, 4 deletions
MutualRecursion.py
with
47 additions
and
4 deletions
MutualRecursion.py
+
47
−
4
View file @
ee26c465
...
...
@@ -7,10 +7,45 @@ 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))
'
Expressions may have literal sub-expressions
>>>
parse
(
'
(1 + 2)
'
)
(
'
1
'
,
'
+
'
,
'
2
'
)
And
an
expression
may
have
non
-
literal
sub
-
expressions
>>>
parse
(
'
((1+2) - (3*4))
'
)
(
'
(1+2)
'
,
'
-
'
,
'
(3*4)
'
)
Spaces
may
be
included
in
the
expression
>>>
parse
(
'
(1 - (3 * 4))
'
)
(
'
1
'
,
'
-
'
,
'
(3*4)
'
)
But
spaces
are
not
required
>>>
parse
(
'
((1+2)-3)
'
)
(
'
(1+2)
'
,
'
-
'
,
'
3
'
)
The
expression
must
be
enclosed
within
parentheses
>>>
parse
(
'
1+2
'
)
Traceback
(
most
recent
call
last
):
...
SyntaxError
:
Expression
must
be
bounded
by
parenthesis
.
1
+
2
does
not
satisfy
.
And
the
parentheses
must
be
balanced
>>>
parse
(
'
((1+2)
'
)
Traceback
(
most
recent
call
last
):
...
SyntaxError
:
Expression
must
have
balanced
parentheses
.
((
1
+
2
)
does
not
satisfy
.
Finally
,
the
expression
must
have
two
sub
-
expressions
separated
by
one
an
arithmetic
operator
from
{
+
-
*
/
}
>>>
parse
(
'
(3 3)
'
)
Traceback
(
most
recent
call
last
):
...
SyntaxError
:
Expression
must
have
an
operator
(
3
3
)
does
not
satisfy
.
"""
if
expression
==
''
:
raise
SyntaxError
(
'
Vacuous expressions are disallowed.
'
)
if
expression
[
0
]
!=
'
(
'
or
expression
[
-
1
]
!=
'
)
'
:
raise
SyntaxError
(
f
'
Expression must be bounded by parenthesis.
$
{
expression
}
does not satisfy.
'
)
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
...
...
@@ -18,6 +53,8 @@ def parse(expression):
this_operator_index
=
full_expression
.
find
(
operator
)
if
-
1
<
this_operator_index
<
index
:
index
=
this_operator_index
if
index
==
math
.
inf
:
raise
SyntaxError
(
f
'
Expression must have an operator
{
expression
}
does not satisfy.
'
)
else
:
parentheses_count
=
1
index
=
1
...
...
@@ -27,9 +64,15 @@ def parse(expression):
if
full_expression
[
index
]
==
'
)
'
:
parentheses_count
-=
1
index
+=
1
if
index
==
len
(
full_expression
)
and
parentheses_count
>
0
:
raise
SyntaxError
(
f
'
Expression must have balanced parentheses.
{
expression
}
does not satisfy.
'
)
left_expression
=
full_expression
[:
index
]
try
:
operator
=
full_expression
[
index
]
right_expression
=
full_expression
[
index
+
1
:]
except
IndexError
:
raise
SyntaxError
(
f
'
Non-literal expression must have two subexpressions and an operator.
'
f
'
{
expression
}
does not satisfy.
'
)
return
left_expression
,
operator
,
right_expression
...
...
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