Stutter a stack using an auxiliary queue. Args: stack: A list representing a stack (bottom to top). Returns: The stack with each value duplicated. Examples: >>> second_stutter([3, 7, 1, 14, 9]) [3, 3, 7, 7, 1, 1, 14, 14, 9, 9]
(stack: list[int])
| 41 | |
| 42 | |
| 43 | def second_stutter(stack: list[int]) -> list[int]: |
| 44 | """Stutter a stack using an auxiliary queue. |
| 45 | |
| 46 | Args: |
| 47 | stack: A list representing a stack (bottom to top). |
| 48 | |
| 49 | Returns: |
| 50 | The stack with each value duplicated. |
| 51 | |
| 52 | Examples: |
| 53 | >>> second_stutter([3, 7, 1, 14, 9]) |
| 54 | [3, 3, 7, 7, 1, 1, 14, 14, 9, 9] |
| 55 | """ |
| 56 | queue: collections.deque[int] = collections.deque() |
| 57 | for _ in range(len(stack)): |
| 58 | queue.append(stack.pop()) |
| 59 | for _ in range(len(queue)): |
| 60 | stack.append(queue.pop()) |
| 61 | for _ in range(len(stack)): |
| 62 | queue.append(stack.pop()) |
| 63 | for _ in range(len(queue)): |
| 64 | val = queue.pop() |
| 65 | stack.append(val) |
| 66 | stack.append(val) |
| 67 | |
| 68 | return stack |