A function should either do something (command) or answer something (query) — never both.
# dirty — modifies AND returns a value in the same function
def solve(stack, value):
stack.append(value)
return len(stack) # query mixed with commandRefactor into two separate functions:
push(stack, value) — command: pushes value, returns Nonesize(stack) — query: returns the current length, no mutationsolve(stack, value, 'push') dispatches to push · solve(stack, 'size') dispatches to size.
Sample tests