Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
prm2
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Monitor
Incidents
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
Martin Mareš
prm2
Commits
ebcfffb7
Commit
ebcfffb7
authored
3 months ago
by
Martin Mareš
Browse files
Options
Downloads
Patches
Plain Diff
Výrazy: Řešení pár úkolů z cvičení
parent
368b5057
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
08-vyrazy/objektove2-reseni.py
+140
-0
140 additions, 0 deletions
08-vyrazy/objektove2-reseni.py
with
140 additions
and
0 deletions
08-vyrazy/objektove2-reseni.py
0 → 100755
+
140
−
0
View file @
ebcfffb7
#!/usr/bin/python3
#
# Naše objektové řešení obsahovalo spoustu duplicitního kódu: speciálně
# všechny binární operátory měly identické metody __init__ a velmi podobné
# metody __str__ a eval.
#
# Zde se duplicit zbavíme tím, že je přesuneme do společného předka:
# třídy BinaryNode, která popisuje chování obecného binárního operátoru.
# Každý konkrétní operátor si pak pouze dodefinuje, jak se jmenuje a jaké
# funkci odpovídá.
#
class
Node
:
def
eval
(
self
):
raise
NotImplementedError
()
def
depth
(
self
):
raise
NotImplementedError
()
def
to_postfix
(
self
,
dest
):
raise
NotImplementedError
()
class
NumNode
(
Node
):
def
__init__
(
self
,
x
):
self
.
value
=
x
def
__str__
(
self
):
return
str
(
self
.
value
)
def
eval
(
self
):
return
self
.
value
def
depth
(
self
):
return
0
def
to_postfix
(
self
,
dest
):
dest
.
append
(
str
(
self
.
value
))
class
BinaryNode
(
Node
):
op_name
=
'
?
'
def
__init__
(
self
,
left
,
right
):
self
.
left
=
left
self
.
right
=
right
def
__str__
(
self
):
return
'
(
'
+
str
(
self
.
left
)
+
self
.
op_name
+
str
(
self
.
right
)
+
'
)
'
def
eval
(
self
):
return
self
.
eval_op
(
self
.
left
.
eval
(),
self
.
right
.
eval
())
def
eval_op
(
self
,
x
,
y
):
raise
NotImplementedError
()
def
depth
(
self
):
return
max
(
self
.
left
.
depth
(),
self
.
right
.
depth
())
+
1
def
to_postfix
(
self
,
dest
):
self
.
left
.
to_postfix
(
dest
)
self
.
right
.
to_postfix
(
dest
)
dest
.
append
(
self
.
op_name
)
class
UnaryNode
(
Node
):
op_name
=
'
?
'
def
__init__
(
self
,
arg
):
self
.
arg
=
arg
def
__str__
(
self
):
return
'
(
'
+
self
.
op_name
+
str
(
self
.
arg
)
+
'
)
'
def
eval
(
self
):
return
self
.
eval_op
(
self
.
arg
.
eval
())
def
eval_op
(
self
,
x
):
raise
NotImplementedError
()
def
depth
(
self
):
return
self
.
arg
.
depth
()
+
1
def
to_postfix
(
self
,
dest
):
self
.
arg
.
to_postfix
(
dest
)
dest
.
append
(
self
.
op_name
)
class
NegNode
(
UnaryNode
):
op_name
=
'
N
'
def
eval_op
(
self
,
x
):
return
-
x
class
AddNode
(
BinaryNode
):
op_name
=
'
+
'
def
eval_op
(
self
,
x
,
y
):
return
x
+
y
class
SubNode
(
BinaryNode
):
op_name
=
'
-
'
def
eval_op
(
self
,
x
,
y
):
return
x
-
y
class
MulNode
(
BinaryNode
):
op_name
=
'
*
'
def
eval_op
(
self
,
x
,
y
):
return
x
*
y
class
DivNode
(
BinaryNode
):
op_name
=
'
/
'
def
eval_op
(
self
,
x
,
y
):
return
x
//
y
x
=
AddNode
(
NegNode
(
MulNode
(
NumNode
(
3
),
NumNode
(
4
))),
SubNode
(
NumNode
(
5
),
NumNode
(
6
)))
print
(
x
)
print
(
x
.
eval
())
print
(
x
.
depth
())
dest
=
[]
x
.
to_postfix
(
dest
)
print
(
"
"
.
join
(
dest
))
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