Trying to Find the First Ascending Run in a List P

2019-09-03 02:32发布

Given a list say, [4, 5, 5, 1, 8, 3, 1, 6, 2, 7] I want to be able to find the first ascending run in the list.. I want to return the starting position of the list and how long it lasts. So this list would return position 0 and length is 2 (4,5)

If there isn't an ascending run in the list, return -1.

Below is what I have so far but I can't seem to fix the "list index out of range" error I get when I run the program. I know why I get the error but not sure how I can fix it.

import random

def generate_integer_list (num_integers, low_range, high_range):
    assert num_integers > 0, "Value must be greater than 0"
    assert low_range < high_range, "Value must be less than high_range"

    x_range = range(num_integers) #Create a range for the below for-loop
    l = [] #Create an empty list

    #A for loop that goes on for the amount of integers the user wants and
    #generates a number within the bounds, then adds that number to the list we created above
    for _x in x_range:
        r = random.randint(low_range, high_range)

        l.append(r)

    print (l)

    length = len(l)
    for x in range(length   ):
        t = l[x+1] - l[x]

        if t == -1:
            print (True)
        else:
            print (False)


generate_integer_list (5, 0, 10)

What I'm asking is, how can get this function to find the first ascension and return the location as well as the length

2条回答
何必那么认真
2楼-- · 2019-09-03 02:58

There are three issues with your code:

  1. Instead of testing whether t == -1, you should test if t is positive. In the example you give, for x=0, t would be 1, and thus the first two elements are ascending.
  2. You should print (or return) False outside the loop. This way you'll go through the whole list before deciding if there isn't an ascending run.
  3. Once you find two ascending numbers, you need to start counting how long the run lasts.

Putting this all together:

length = len(l)
run_started = False
for x in range(length-1):
    t = l[x+1] - l[x]
    if t > 0 :
        if run_started:
            run_length += 1
        else:
            run_started = True
            run_length = 2
    else:
        if run_started:
             print True
             print 'Run length:', run_length
             break
if not run_started:
    print False
查看更多
We Are One
3楼-- · 2019-09-03 03:11

This should do it:

def solve(lis):
    run_length = 0
    ind = 0
    for i, (x, y) in enumerate(zip(lis, lis[1:])):
        if run_length and y-x != 1:
            break
        if y-x == 1:
            if not run_length:
                ind = i
            run_length += 1
    if run_length:
        return run_length+1, ind
    return -1

Demo:

>>> solve([4, 5, 5, 1, 8, 3, 1, 6, 2, 7])
(2, 0)
>>> solve([1, 1, 1, 2, 3, 5, 1, 1])
(3, 2)
>>> solve([1, 1, 1, 1])
-1
>>> solve([1, 2, 5, 6, 7] )
(2, 0)
>>> solve([1, 0, -1, -2, -1, 0, 0])
(3, 3)
查看更多
登录 后发表回答