Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
AICogSciFinalProject
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Container Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
mrk022
AICogSciFinalProject
Commits
1e7de1fe
Commit
1e7de1fe
authored
Nov 28, 2019
by
mrk022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
updated progress
parent
9ad86f45
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
252 additions
and
0 deletions
+252
-0
queue.py
queue.py
+100
-0
queue.pyc
queue.pyc
+0
-0
tree.py
tree.py
+152
-0
No files found.
queue.py
0 → 100644
View file @
1e7de1fe
# queue code from http://knuth.luther.edu/~leekent/CS2Plus/chap4/chap4.html
class
Queue
:
def
__init__
(
self
):
self
.
items
=
[]
self
.
frontIdx
=
0
def
__compress
(
self
):
newitems
=
[]
for
i
in
range
(
self
.
frontIdx
,
len
(
self
.
items
)):
newitems
.
append
(
self
.
items
[
i
])
self
.
items
=
newitems
self
.
frontIdx
=
0
def
dequeue
(
self
):
if
self
.
isEmpty
():
raise
RuntimeError
(
"Attempt to dequeue an empty queue"
)
# When queue is half full, compress it. This
# achieves an amortized complexity of O(1) while
# not letting the list continue to grow unchecked.
if
self
.
frontIdx
*
2
>
len
(
self
.
items
):
self
.
__compress
()
item
=
self
.
items
[
self
.
frontIdx
]
self
.
frontIdx
+=
1
return
item
def
enqueue
(
self
,
item
):
self
.
items
.
append
(
item
)
def
front
(
self
):
if
self
.
isEmpty
():
raise
RuntimeError
(
"Attempt to access front of empty queue"
)
return
self
.
items
[
self
.
frontIdx
]
def
isEmpty
(
self
):
return
self
.
frontIdx
==
len
(
self
.
items
)
def
clear
(
self
):
self
.
items
=
[]
self
.
frontIdx
=
0
def
main
():
q
=
Queue
()
items
=
list
(
range
(
10
))
items2
=
[]
for
k
in
items
:
q
.
enqueue
(
k
)
if
q
.
front
()
==
0
:
print
(
"Test 1 Passed"
)
else
:
print
(
"Test 1 Failed"
)
while
not
q
.
isEmpty
():
items2
.
append
(
q
.
dequeue
())
if
items2
!=
items
:
print
(
"Test 2 Failed"
)
else
:
print
(
"Test 2 Passed"
)
for
k
in
items
:
q
.
enqueue
(
k
)
items2
=
[]
while
not
q
.
isEmpty
():
items2
.
append
(
q
.
dequeue
())
if
items2
!=
items
:
print
(
"Test 3 Failed"
)
else
:
print
(
"Test 3 Passed"
)
try
:
q
.
dequeue
()
print
(
"Test 4 Failed"
)
except
RuntimeError
:
print
(
"Test 4 Passed"
)
except
:
print
(
"Test 4 Failed"
)
try
:
q
.
front
()
print
(
"Test 5 Failed"
)
except
RuntimeError
:
print
(
"Test 5 Passed"
)
except
:
print
(
"Test 5 Failed"
)
if
__name__
==
"__main__"
:
main
()
\ No newline at end of file
queue.pyc
0 → 100644
View file @
1e7de1fe
File added
tree.py
0 → 100644
View file @
1e7de1fe
# base tree code from http://knuth.luther.edu/~leekent/CS2Plus/chap6/chap6.html
# changed this code to allow for variable functions, and change infix expressions to postfix
import
queue
class
TimesNode
:
def
__init__
(
self
,
left
,
right
):
self
.
left
=
left
self
.
right
=
right
def
eval
(
self
):
return
self
.
left
.
eval
()
*
self
.
right
.
eval
()
def
inorder
(
self
):
return
"("
+
self
.
left
.
inorder
()
+
" * "
+
self
.
right
.
inorder
()
+
")"
class
PlusNode
:
def
__init__
(
self
,
left
,
right
):
self
.
left
=
left
self
.
right
=
right
def
eval
(
self
):
return
self
.
left
.
eval
()
+
self
.
right
.
eval
()
def
inorder
(
self
):
return
"("
+
self
.
left
.
inorder
()
+
" + "
+
self
.
right
.
inorder
()
+
")"
class
MinusNode
:
def
__init__
(
self
,
left
,
right
):
self
.
left
=
left
self
.
right
=
right
def
eval
(
self
):
return
self
.
left
.
eval
()
-
self
.
right
.
eval
()
def
inorder
(
self
):
return
"("
+
self
.
left
.
inorder
()
+
"-"
+
self
.
right
.
inorder
()
+
")"
class
NumNode
:
def
__init__
(
self
,
num
):
self
.
num
=
num
def
eval
(
self
):
return
self
.
num
def
inorder
(
self
):
return
str
(
self
.
num
)
def
check_if_variables
(
x
,
y
):
if
'x'
in
x
and
'x'
in
y
:
return
"both_vars"
elif
'x'
in
x
or
'x'
in
y
:
return
"one_var"
else
:
return
"both_const"
def
E
(
q
):
if
q
.
isEmpty
():
raise
ValueError
(
"Invalid Prefix Expression"
)
token
=
q
.
dequeue
()
if
token
==
"+"
:
return
PlusNode
(
E
(
q
),
E
(
q
))
if
token
==
"*"
:
return
TimesNode
(
E
(
q
),
E
(
q
))
if
token
==
"-"
:
return
MinusNode
(
E
(
q
),
E
(
q
))
try
:
output
=
NumNode
(
float
(
token
))
except
:
print
(
"Not a valid operation, need variables of like terms"
)
output
=
NumNode
(
float
(
0
))
return
output
OPERATORS
=
set
([
'+'
,
'-'
,
'*'
,
'/'
,
'('
,
')'
])
PRIORITY
=
{
'+'
:
1
,
'-'
:
1
,
'*'
:
2
,
'/'
:
2
}
def
infix_to_prefix
(
formula
):
op_stack
=
[]
exp_stack
=
[]
for
ch
in
formula
.
split
():
if
not
ch
in
OPERATORS
:
exp_stack
.
append
(
ch
)
elif
ch
==
'('
:
op_stack
.
append
(
ch
)
elif
ch
==
')'
:
while
op_stack
[
-
1
]
!=
'('
:
op
=
op_stack
.
pop
()
a
=
exp_stack
.
pop
()
b
=
exp_stack
.
pop
()
exp_stack
.
append
(
op
+
b
+
a
)
op_stack
.
pop
()
# pop '('
else
:
while
op_stack
and
op_stack
[
-
1
]
!=
'('
and
PRIORITY
[
ch
]
<=
PRIORITY
[
op_stack
[
-
1
]]:
op
=
op_stack
.
pop
()
a
=
exp_stack
.
pop
()
b
=
exp_stack
.
pop
()
exp_stack
.
append
(
op
+
" "
+
b
+
" "
+
a
)
op_stack
.
append
(
ch
)
# leftover
while
op_stack
:
op
=
op_stack
.
pop
()
a
=
exp_stack
.
pop
()
b
=
exp_stack
.
pop
()
exp_stack
.
append
(
op
+
" "
+
b
+
" "
+
a
)
print
exp_stack
[
-
1
]
return
exp_stack
[
-
1
]
def
eval_tree
(
expression
):
x
=
NumNode
(
5
)
y
=
NumNode
(
4
)
p
=
PlusNode
(
x
,
y
)
t
=
TimesNode
(
p
,
NumNode
(
6
))
root
=
PlusNode
(
t
,
NumNode
(
3
))
print
(
root
.
eval
())
print
(
root
.
inorder
())
#x = input("Please enter a prefix expression: ")
x
=
infix_to_prefix
(
expression
)
lst
=
x
.
split
()
q
=
queue
.
Queue
()
for
token
in
lst
:
q
.
enqueue
(
token
)
root
=
E
(
q
)
print
(
root
.
eval
())
print
(
root
.
inorder
())
#print(infix_to_prefix("3 + 4 - 6"))
def
split_equation
(
equation
):
expressions
=
equation
.
split
(
"="
)
return
expressions
def
main
():
exp
=
split_equation
(
"3 * 4 - 6=0"
)
eval_tree
(
exp
[
0
])
if
__name__
==
"__main__"
:
main
()
\ No newline at end of file
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