From c300762ae722050e77da408673d3a2201dd01e60 Mon Sep 17 00:00:00 2001 From: Anoubhav Agarwaal <38296352+anoubhav@users.noreply.github.com> Date: Wed, 5 Sep 2018 11:30:56 +0530 Subject: [PATCH 1/2] 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. --- algorithms/stack/is_sorted.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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)): From c6cd109099784d287331957cf7775b6b92eed6eb Mon Sep 17 00:00:00 2001 From: danghai Date: Fri, 7 Sep 2018 06:00:57 -0700 Subject: [PATCH 2/2] Add test for is_sorted --- tests/test_stack.py | 1 + 1 file changed, 1 insertion(+) 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