Skip to content

Instantly share code, notes, and snippets.

@blueset

blueset/README.md

Last active Oct 5, 2016
Embed
What would you like to do?
UniMelb COMP10001 2015S2 Project 3: My Solution
  • Solutions including challenging questions
  • Questions
def parse(s):
# Split the query by space
l = s.split()
r = []
while l:
# process each keyword from beginning
a = l.pop(0)
# convert numbers to int
if a.isdecimal():
a = int(a)
# process all negative values
if a == "negate":
if r and type(r[-1]) is int:
r[-1] = -r[-1]
else:
# return first error position
# Since the question did not specify how exatly to
# determine it, I took the number of all elements generated
# successfully
return len(r)
else:
r.append(a)
return r
def simple(l):
# add the first number
r = l.pop(0)
while l:
# process the next pair of command
n = l.pop(0)
o = l.pop(0)
if o == "add":
r += n
elif o == "subtract":
r -= n
elif o == "multiply":
r *= n
elif o == "divide":
r /= n
# convert to integer if possible
return int(r) if r == int(r) else r
def full(l):
# list of calculations
d = {
'add': lambda a, b: a + b,
'subtract': lambda a, b: a - b,
'multiply': lambda a, b: a * b,
'divide': lambda a, b: a / b
}
# stack
s = []
while l:
a = l.pop(0)
if a in d:
# pop out elements for calculation
# if found an operator
x = s.pop()
y = s.pop()
s.append(d[a](y, x))
else:
# press elements into stack
s.append(a)
# return the integer value if possible
return int(s[0]) if s[0] == int(s[0]) else s[0]
def full2(l):
"""
Recurssive solution
"""
d = {
'add': lambda a, b: a + b,
'subtract': lambda a, b: a - b,
'multiply': lambda a, b: a * b,
'divide': lambda a, b: a / b
}
# only calculatable if there's more than 2 elements.
if len(l) >= 3:
if l[-1] in d:
# if next set is to be calculated
if l[-2] in d:
a = full2(l[:-1])
# convert result back to a list for concatnation
if type(a) is not list:
a = [a]
l = a + [l[-1]]
# if the further set is to be calculated
if l[-3] in d:
a = full2(l[:-2])
if type(a) is not list:
a = [a]
l = a + [l[-2], l[-1]]
# calculate everything before the current operation
a = full2(l[:-3])
if type(a) is not list:
a = [a]
l = a + [d[l[-1]](l[-3], l[-2])]
# return the list when there's extra stuff
if len(l) != 1:
return l
return int(l[0]) if l[0] == int(l[0]) else l[0]
def full3(l):
"""
Solution with negate function.
Assuming all input is valid.
"""
d = {
'add': lambda a, b: a + b,
'subtract': lambda a, b: a - b,
'multiply': lambda a, b: a * b,
'divide': lambda a, b: a / b
}
s = []
while l:
a = l.pop(0)
if a in d:
x = s.pop()
y = s.pop()
s.append(d[a](y, x))
elif a == "negate":
# added negate operation
s[-1] = -s[-1]
else:
s.append(a)
return int(s[0]) if s[0] == int(s[0]) else s[0]
def full4(l):
"""
Convert to traditional expression, with bracket in every calculation.
"""
ops = ['add', 'subtract', 'multiply', 'divide', 'negate']
if len(l) < 3:
return l
if l[-1] in ops:
if l[-2] in ops:
l = full4(l[:-1]) + [l[-1]]
if l[-3] in ops:
l = full4(l[:-2]) + l[-2:]
l = l[:-3] + ["(%s %s %s)" % (l[-3], l[-1], l[-2])]
return l
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment