Peek does not remove
Updated
The whole point of peek is that the stack is observationally the same after it. If your peek decrements size, it is pop. Tests: size, then peek, then size again.
What to focus on
Pop-while and bracket matching peek first, then pop only when the closer matches. A peek that removes would destroy the next comparison.
Worked examples
unchanged
a = [4, 1, 9], peek then peek- both 9
- size still 3
Result: still [4, 1, 9]
then pop
a = [4, 1, 9], peek then pop- peek 9
- pop 9
- now [4, 1]
Result: remove happens on pop
one item
a = [7], peek then peek- both 7
- size still 1
Result: still [7]
Try it
Write peek(a). a is the stack, written [bottom, ..., top]. Peek, print size, peek again. Size must stay the same until you pop. Pop after peek actually removes the top. On [4, 1, 9] size stays 3 across two peeks, then pop removes 9. Same on [7]: size stays 1.
The stack is non-empty. Values are integers.