Stacks and Monotonic Structures
33 / 170Stacks and Monotonic StructuresLesson 3 of 8About 36 min
Evaluating anything with structure: push values, and let each operator consume what it needs.
Both functions evaluate the same subtraction, and they disagree. Type the two values separated by a space.
def careless(tokens): stack = [] for token in tokens: if token == "-": a, b = stack.pop(), stack.pop() stack.append(a - b) else: stack.append(int(token)) return stack[-1] def careful(tokens): stack = [] for token in tokens: if token == "-": b, a = stack.pop(), stack.pop() stack.append(a - b) else: stack.append(int(token)) return stack[-1] print(careless(["10", "3", "-"]), careful(["10", "3", "-"]))Read the constraints first and let them tell you what complexity is expected. Derive the approach, implement it, run the tests, and submit when it passes.