Home > Software engineering >  Why does my function keep returning True?
Why does my function keep returning True?

Time:04-13

It's probably a simple mistake but I can't see it. I think my for loop in the function makes sense but it keeps returning true....

Write the in_order() function, which has a list of integers as a parameter, and returns True if the integers are sorted (in order from low to high) or False otherwise. The program outputs "In order" if the list is sorted, or "Not in order" if the list is not sorted.

Ex: If the list passed to the in_order() function is [5, 6, 7, 8, 3], then the function returns False and the program outputs:

Not in order

Ex: If the list passed to the in_order() function is [5, 6, 7, 8, 10], then the function returns True and the program outputs:

In order

Note: Use a for loop. DO NOT use sorted() or sort().

def in_order(nums):
    i = 0
    while i < len(nums):
        for x in nums:
            if nums[i] <= nums[i 1]:
                return True
            else:
                return False
        i =1
        
    
if __name__ == '__main__':
    # Test out-of-order example
    nums1 = [5, 6, 7, 8, 3]
    if in_order(nums1):
        print('In order')
    else:
        print('Not in order')
        
    # Test in-order example
    nums2 = [5, 6, 7, 8, 10]
    if in_order(nums2):
        print('In order')
    else:
        print('Not in order')
1: Unit test
0 / 2
Test in_order(nums) returns False, where nums is [5, 6, 7, 8, 3]
Your output
Test failed: in_order() should have returned False because the numbers are not sorted

2: Unit test
2 / 2
Test in_order(nums) returns True, where nums is [5, 6, 7, 8, 10]
Your output
Test passed: in_order() correctly returned True


3: Unit test
3 / 3
Test in_order(nums) returns True, where nums is [5, 5, 6, 6, 6, 7, 8, 10, 10, 10, 10]
Your output
Test passed: in_order() correctly returned True

4: Compare output
0 / 3
Output differs. See highlights below. 
Your output
In order
In order
Expected output
Not in order
In order

CodePudding user response:

You are returning immediately, you need to return False when you have guaranteed the list is out of order, or wait until the whole list has been checked to return True

def in_order(nums):
    i = 0
    # Take 1 so you don't get an index error
    while i < len(nums)-1:
        if nums[i] > nums[i 1]:
            return False
        i  = 1
    return True

You can remove the for loop, as you are already doing your own iteration in the while loop. But if you need a for loop, you can use this:

# Zip method
def in_order(nums):
    l = len(nums)
    # b is the element in front of a
    for a, b in zip(nums[0:l-1], nums[1:l]):
        if a > b:
            return False
    return True

A better alternative is to use:

def in_order(nums):
    """See https://stackoverflow.com/a/3755251/14141223 as well"""
    return all(nums[i] <= nums[i 1] for i in range(len(nums) - 1))

CodePudding user response:

You were returning from the function by checking only two elements. If your first 2 elements are 6 and 5 respectively then your solution would give False.

def in_order(nums):
    return all(
        nums[i] <= nums[i   1]
        for i in range(len(nums) - 1)
    )
        
    
if __name__ == '__main__':
    # Test out-of-order example
    nums1 = [5, 6, 7, 8, 3]
    if in_order(nums1):
        print('In order')
    else:
        print('Not in order')
        
    # Test in-order example
    nums2 = [5, 6, 7, 8, 10]
    if in_order(nums2):
        print('In order')
    else:
        print('Not in order')

Here I've used all() function which returns true when the condition satisfies for all the elements in the array.

You can also rewrite the above more concisely using lambda function.

inorder = lambda nums: all(nums[i] <= nums[i   1] for i in range(len(nums) - 1))

# call inorder
inorder([1, 2, 4, 3])

CodePudding user response:

You can try any of the following functions:

Function 01:

def in_order(nums):
    flag = 0
    if all(nums[i] <= nums[i   1] for i in range(len(nums)-1)):
        flag = 1

    return flag

Function 02:

def in_order(nums):
    i=1
    while i< len(nums):
         if nums[i] < nums[i-1]:
            return False
         i=i 1
    return True

Driver Code:

if __name__ == '__main__':
    # Test out-of-order example
    nums1 = [5, 6, 7, 8, 3]
    if in_order(nums1):
        print('In order')
    else:
        print('Not in order')
        
    # Test in-order example
    nums2 = [5, 6, 7, 8, 10]
    if in_order(nums2):
        print('In order')
    else:
        print('Not in order')

Output:

Not in order
In order
  • Related