Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
CM-GP
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD 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
Admin message
Setting up 2FA is now mandatory for all users.
Show more breadcrumbs
Senne Deproost
CM-GP
Commits
922075ec
Commit
922075ec
authored
8 months ago
by
Denis Steckelmacher
Browse files
Options
Downloads
Patches
Plain Diff
Print postfix programs in infix form. This looks way more explainable.
parent
42a77ab8
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
postfix_program.py
+27
-20
27 additions, 20 deletions
postfix_program.py
with
27 additions
and
20 deletions
postfix_program.py
+
27
−
20
View file @
922075ec
...
...
@@ -77,9 +77,8 @@ class Program:
value
=
np
.
random
.
normal
(
loc
=
mean
,
scale
=
math
.
exp
(
log_std
))
#
Execute the token
#
Literal, push it
if
value
>=
0.0
:
# Literal, push it
if
do_print
:
stack
.
append
(
str
(
value
))
else
:
...
...
@@ -95,7 +94,7 @@ class Program:
# Silently ignore input variables beyond the end of inp
if
do_print
:
stack
.
append
(
f
'
x
{
input_index
}
'
)
stack
.
append
(
f
'
x
[
{
input_index
}
]
'
)
else
:
if
input_index
<
len
(
inp
):
stack
.
append
(
inp
[
input_index
])
...
...
@@ -106,26 +105,34 @@ class Program:
operator_index
=
-
value
-
1
operator
=
OPERATORS
[
operator_index
]
if
do_print
:
stack
.
append
(
operator
.
name
)
else
:
if
operator
.
function
is
None
:
# End of program
break
if
operator
.
function
is
None
:
# End of program
break
# Pop the operands
operands
=
[]
# Pop the operands
operands
=
[]
for
index
in
range
(
operator
.
num_operands
):
if
len
(
stack
)
==
0
:
# If the stack is empty, "pop" a 1 from it.
# 1 is neutral to mul, can be used for div, does something to add and sub
operand
=
1.0
else
:
operand
=
stack
.
pop
()
for
index
in
range
(
operator
.
num_operands
):
if
len
(
stack
)
==
0
:
# If the stack is empty, "pop" a 1 from it.
# 1 is neutral to mul, can be used for div, does something to add and sub
operand
=
1.0
else
:
operand
=
stack
.
pop
()
operands
.
append
(
operand
)
operands
.
append
(
operand
)
if
do_print
:
# Put a string representation of the operator on the stack
if
len
(
operands
)
==
1
:
result
=
f
"
{
operator
.
name
}
(
{
operands
[
0
]
}
)
"
elif
len
(
operands
)
==
2
:
result
=
f
"
(
{
operands
[
0
]
}
{
operator
.
name
}
{
operands
[
1
]
}
)
"
elif
len
(
operands
)
==
3
:
result
=
f
"
(
{
operands
[
0
]
}
?
{
operands
[
1
]
}
:
{
operands
[
2
]
}
)
"
stack
.
append
(
result
)
else
:
# Run the operator and get the result back
result
=
operator
.
function
(
*
operands
)
stack
.
append
(
result
)
...
...
@@ -137,5 +144,5 @@ class Program:
if
__name__
==
'
__main__
'
:
print
(
Program
([
5.0
,
1.0
,
-
2
1
.0
,
-
2.0
]).
run_program
([
3.14
,
6.28
],
do_print
=
True
))
print
(
Program
([
5.0
,
1.0
,
-
2
.0
,
-
5.0
,
18.0
,
0.0
,
-
8
.0
,
-
2.0
]).
run_program
([
3.14
,
6.28
],
do_print
=
True
))
print
(
Program
([
-
17.0
,
0.0
]).
run_program
([
0.0
],
do_print
=
False
))
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