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

Function first_is_consecutive

algorithms/stack/is_consecutive.py:20–48  ·  view source on GitHub ↗

Check if a stack has consecutive integers using an auxiliary stack. Args: stack: A list representing a stack (bottom to top). Returns: True if the values are consecutive from bottom to top. Examples: >>> first_is_consecutive([3, 4, 5, 6, 7]) True

(stack: list[int])

Source from the content-addressed store, hash-verified

18
19
20def first_is_consecutive(stack: list[int]) -> bool:
21 """Check if a stack has consecutive integers using an auxiliary stack.
22
23 Args:
24 stack: A list representing a stack (bottom to top).
25
26 Returns:
27 True if the values are consecutive from bottom to top.
28
29 Examples:
30 >>> first_is_consecutive([3, 4, 5, 6, 7])
31 True
32 >>> first_is_consecutive([3, 4, 6, 7])
33 False
34 """
35 storage_stack: list[int] = []
36 for _ in range(len(stack)):
37 first_value = stack.pop()
38 if len(stack) == 0:
39 return True
40 second_value = stack.pop()
41 if first_value - second_value != 1:
42 return False
43 stack.append(second_value)
44 storage_stack.append(first_value)
45
46 for _ in range(len(storage_stack)):
47 stack.append(storage_stack.pop())
48 return True
49
50
51def second_is_consecutive(stack: list[int]) -> bool:

Callers 1

test_is_consecutiveMethod · 0.90

Calls 1

popMethod · 0.45

Tested by 1

test_is_consecutiveMethod · 0.72