为什么我的函数一直返回 True?

Why does my function keep returning True?

这可能是一个简单的错误,但我看不到。我认为我在函数中的 for 循环是有道理的,但它一直返回 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

你马上返回,你需要return False当你确定列表没有顺序时,或者等到整个列表都检查完了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

您可以删除 for 循环,因为您已经在 while 循环中进行了自己的迭代。但是如果你需要一个for循环,你可以使用这个:

# 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

更好的选择是使用:

def in_order(nums):
    """See  as well"""
    return all(nums[i] <= nums[i+1] for i in range(len(nums) - 1))

您通过仅检查两个元素从函数返回。如果您的前 2 个元素分别是 6 和 5,那么您的解决方案将给出 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')

这里我使用了 all() 函数,当条件满足数组中的所有元素时,该函数 returns 为真。

您也可以使用 lambda 函数更简洁地重写上面的代码。

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

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

您可以尝试以下任一功能:

函数 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

函数 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代码:

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')

输出:

Not in order
In order