MCPcopy
hub / github.com/keon/algorithms / second_stutter

Function second_stutter

algorithms/stack/stutter.py:43–68  ·  view source on GitHub ↗

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])

Source from the content-addressed store, hash-verified

41
42
43def 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

Callers 1

test_stutterMethod · 0.90

Calls 1

popMethod · 0.45

Tested by 1

test_stutterMethod · 0.72