0

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?

1 Answer 1

1

I think the first issue you have is that you need to return something callable from oplookup, probably a function. The functions all need to manipulate (or test) the items list (which they can access because they are closures).

Here's what that code might look like:

def make_stack():
    items = []
    def oplookup(msg):
        if msg == "is_empty":
            def empty():
                return not items
            return empty
        elif msg == "clear":
            def clear():
                items[:] = [] # slice assignment, to clear in-place
            return clear
        #...

Note that in clear I avoid doing an assignment directly to items because it's in the enclosing scope. In Python 2, it's impossible to reassign items, since it is neither local or global variable. In Python 3, reassignment is possible using the nonlocal keyword. Since I don't know what version of Python you're using, I used a slice assignment which works in both versions.

This style of code is not very Pythonic. A much more natural way to do this would be to make a class with methods (but you'd end up with a slightly different API):

class Stack:
    def __init__(self):
        self.items = []

    def is_empty(self):
        return not self.items

    def clear(self):
        self.items = [] # this time we can replace the list

    def push(self, item):
        self.items.append(item)

    #...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.