When a function is called, a frame for it is pushed onto the program's call stack, and when the function returns, its frame is popped. Running main() in the Python program below pushes and pops several frames. Arrange the frames in the order in which they are popped from the call stack, first popped at the top.
def main(): parse() report() def parse(): read_header() read_body() def read_body(): read_line() def read_header(): pass def read_line(): pass def report(): pass main()
Loading ordering steps…
Unlock full course notes by signing up
Evaluating a postfix expression with a stack
A program evaluates the postfix (reverse Polish) expression below by reading tokens from left to right with an initially empty stack. Each number is pushed. Each binary operator pops two values, applies the operator to them, and pushes the result. Division here is exact, with no rounding.
Give the final value left on the stack, as an integer, and the maximum number of elements the stack holds at any moment during the evaluation, as an integer.
Undo and redo with two stacks
A simple text editor implements undo and redo with two stacks. Every typing action appends one character to the end of the document and pushes that character onto the undo stack. The editor follows three rules:
A new typing action pushes onto the undo stack and empties the redo stack.
Undo pops the top of the undo stack, removes that character from the document, and pushes it onto the redo stack. Undo does nothing if the undo stack is empty.
1.
The read_header frame
2.
The read_body frame
3.
The read_line frame
4.
The parse frame
5.
The report frame
6.
The main frame