Skip to content

Commit

Permalink
Update is_sorted.py (keon#402)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
anoubhav authored and danghai committed Sep 7, 2018
1 parent 4f8ccdd commit e0cc8c3
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
6 changes: 3 additions & 3 deletions algorithms/stack/is_sorted.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ def is_sorted(stack):
storage_stack = []
for i in range(len(stack)):
if len(stack) == 0:
return True
break
first_val = stack.pop()
if len(stack) == 0:
return True
break
second_val = stack.pop()
if first_val < second_val:
return False
storage_stack.append(first_val)
storage_stack.append(second_val)
stack.append(second_val)

# Backup stack
for i in range(len(storage_stack)):
Expand Down
1 change: 1 addition & 0 deletions tests/test_stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def test_is_sorted(self):
# Test case: bottom [6, 3, 5, 1, 2, 4] top
self.assertFalse(is_sorted([6, 3, 5, 1, 2, 4]))
self.assertTrue(is_sorted([1, 2, 3, 4, 5, 6]))
self.assertFalse(is_sorted([3, 4, 7, 8, 5, 6]))

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

0 comments on commit e0cc8c3

Please sign in to comment.