Determine if a list is in descending order

前端 未结 7 2489
孤城傲影
孤城傲影 2020-12-03 17:35

I am trying to write a function that will test whether or not a list is in decending order. This is what I have so far, but it doesn\'t seem to be working for all lists. <

相关标签:
7条回答
  • 2020-12-03 18:17

    What you want to do is

    n=len(A)
    for i in range(n - 1):
        if A[i]<=A[i+1]:
            return 'false'
    return 'true
    

    Try to execute your code in your head. At the first iteration, if A[i] is greater than A[i + 1] you return true, else you return false. You never go futher in your list.

    The good solution is to test your condition, and if in false at anytime, return false. But if its right, this doesnt mean that the rest of the list is, and you want to test each of your values.

    And as you test A[i + 1], you dont want to go at the end of your list, but at the n - 1 item of it.

    0 讨论(0)
  • 2020-12-03 18:20

    Instead of using indices, you can iterate over the input:

    def ordertest(iterable):
        it = iter(iterable)
        prev = next(it)
        for e in it:
            if e > prev:
                return False
            prev = e
        return True
    

    Note that it's a bad idea to return the strings 'true' and 'false'. Instead, you can use Python's built-in booleans.

    0 讨论(0)
  • 2020-12-03 18:22

    Here is a concise way to perform this test that uses all():

    def ordertest(A):
        return all(A[i] >= A[i+1] for i in range(len(A)-1))
    

    Examples:

    >>> ordertest([9,8,5,1,4,3,2])
    False
    >>> ordertest([9,8,5,4,3,2,1])
    True
    
    0 讨论(0)
  • 2020-12-03 18:26

    You should rather do the reverse check (As soon as you get A[i] < A[i+1], return false

    def ordertest(A):
        for i in range( len(A) - 1 ):
            if A[i] < A[i+1]:
                return False
            return True
    
    0 讨论(0)
  • 2020-12-03 18:37
    n=len(l)
    c=0
    for i in range(n - 1):
        if l[i]<l[i+1]:
            c+=1
        if c==1:
            break
    if(c==1):
        return False
    return True
    

    To check without any error and to check that too correctly this function using a counter is the best. As soon as the loop runs and the iterate becomes 1 it breaks no more need of searching. it will return false else true. Thank you!

    0 讨论(0)
  • 2020-12-03 18:39

    You can do this easily with a generator expression and the all() builtin:

    all(earlier >= later for earlier, later in zip(seq, seq[1:]))
    

    For example:

    >>> seq = [9, 8, 5, 1, 4, 3, 2] 
    >>> all(earlier >= later for earlier, later in zip(seq, seq[1:]))
    False
    >>> seq = [9, 8, 5, 4, 3, 2] 
    >>> all(earlier >= later for earlier, later in zip(seq, seq[1:]))
    True
    

    This should be nice and fast as it avoids python-side loops, short circuits nicely (if you use itertools.izip() in 2.x), and is nice and clear and readable (avoiding looping over indices, for example).

    Note that a generic solution for all iterators (not just sequences) is possible too:

    first, second = itertools.tee(iterable)
    next(second)
    all(earlier >= later for earlier, later in zip(first, second))
    
    0 讨论(0)
提交回复
热议问题