By using message passing to fuifill the code such that
s = make_stack()
print(s("is_empty")) # True
s("push")(1)
s("push")(2)
print(s("peek")) # [2]
print(str(s("pop"))) # [2]
My code is supposed to fill in the blanks of
def make_stack():
items = []
def oplookup(msg):
if msg == "is_empty":
# blank #
elif msg == "clear":
# blank #
elif msg == "peek":
# blank #
elif msg == "push":
# blank #
elif msg == "pop":
# blank #
else:
raise Exception("stack doesn't" + msg)
return oplookup
I don't understand the tracing of the code. My own trial code is
def make_stack():
items = []
def oplookup(msg):
if msg == "is_empty":=
return True
elif msg == "clear":
return []
elif msg == "peek":
return make_stack.items[-1]
elif msg == "push":
return items.append(msg)
elif msg == "pop":
return items.pop()
else:
raise Exception("stack doesn't" + msg)
return oplookup
Another question is for having s("push") (1), what argument does (1) take? Is it under msg or item?