Skip to content

Commit e0cc8c3

Browse files
anoubhavdanghai
authored andcommitted
Update is_sorted.py (keon#402)
* Update is_sorted.py 1)The first 2 return statements have been changed to break statements. This ensures that the code after the first for loop is executed. Previously it would never execute the latter part. 2)The second append statement should add elements to stack and not storage_stack. Try the test case [3,4,7,8,5,6]. The program returns True. It should return False. * Add test for is_sorted
1 parent 4f8ccdd commit e0cc8c3

2 files changed

Lines changed: 4 additions & 3 deletions

File tree

algorithms/stack/is_sorted.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ def is_sorted(stack):
1313
storage_stack = []
1414
for i in range(len(stack)):
1515
if len(stack) == 0:
16-
return True
16+
break
1717
first_val = stack.pop()
1818
if len(stack) == 0:
19-
return True
19+
break
2020
second_val = stack.pop()
2121
if first_val < second_val:
2222
return False
2323
storage_stack.append(first_val)
24-
storage_stack.append(second_val)
24+
stack.append(second_val)
2525

2626
# Backup stack
2727
for i in range(len(storage_stack)):

tests/test_stack.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def test_is_sorted(self):
2525
# Test case: bottom [6, 3, 5, 1, 2, 4] top
2626
self.assertFalse(is_sorted([6, 3, 5, 1, 2, 4]))
2727
self.assertTrue(is_sorted([1, 2, 3, 4, 5, 6]))
28+
self.assertFalse(is_sorted([3, 4, 7, 8, 5, 6]))
2829

2930
def test_remove_min(self):
3031
# Test case: bottom [2, 8, 3, -6, 7, 3] top

0 commit comments

Comments
 (0)