diff --git a/algorithms/stack/is_sorted.py b/algorithms/stack/is_sorted.py index 8824dfa95..0a69d83f9 100644 --- a/algorithms/stack/is_sorted.py +++ b/algorithms/stack/is_sorted.py @@ -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)): diff --git a/tests/test_stack.py b/tests/test_stack.py index d04b27f14..78822bc17 100644 --- a/tests/test_stack.py +++ b/tests/test_stack.py @@ -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