[Solved] Code: How can I go over a list of numbers and print the number of consecutive increases in Python? [closed]


There are 4 things I’d like to mention. Here’s some code and its output:

def srk_func(words):
    current = []
    lastc = []
    for x in words:
        if len(current) == 0: 
                current.append(int(x))
        elif len(current) == 1:
                if current[0] < int(x):
                        current.append(int(x))
                else:          
                        if len(current) >= len(lastc):
                                lastc = current
                        current[:] = []
                        current.append(int(x))
        elif len(current) >= 2:
                if current[-1] < int(x):
                        current.append(int(x))
                else:          
                        if len(current) >= len(lastc):
                                lastc = current
                        elif len(current) < len(lastc):
                                current[:] = []
                        current[:] = []
                        current.append(int(x))
    return lastc

def jm_func(words):
    current = []
    lastc = []
    for w in words:
        x = int(w)
        if not current:
            # this happens only on the first element
            current = [x]
            continue
        if x > current[-1]:
            current.append(x)
        else:
            # no increase, so current is complete
            if len(current) >= len(lastc):
                lastc = current
            current = [x]
    # end of input, so current is complete
    if len(current) >= len(lastc):
        lastc = current
    return lastc

tests = """\
    1
    1 5
    5 1
    1 5 7
    7 5 1
    1 5 7 0
    1 5 7 0 3
    1 5 7 0 2 4 6 8
    1 3 5 7 9 11 0 2
    """

for test in tests.splitlines():
    wds = test.split()
    print wds
    print srk_func(wds)
    print jm_func(wds)
    print

8<--------------------------------------------------

['1']
[]
[1]

['1', '5']
[]
[1, 5]

['5', '1']
[1]
[1]

['1', '5', '7']
[]
[1, 5, 7]

['7', '5', '1']
[1]
[1]

['1', '5', '7', '0']
[0]
[1, 5, 7]

['1', '5', '7', '0', '3']
[0, 3]
[1, 5, 7]

['1', '5', '7', '0', '2', '4', '6', '8']
[0, 2, 4, 6, 8]
[0, 2, 4, 6, 8]

['1', '3', '5', '7', '9', '11', '0', '2']
[0, 2]
[1, 3, 5, 7, 9, 11]

[]
[]
[]

Topic 1: Test your code.

Topic 2: Redundancy: Your code for len(current) == 1 is functionally identical to your code for len >= 2, and the latter is bloated by having the following unnecessary two lines:

  elif len(current) < len(lastc):
        current[:] = []

You can combine the two cases; see my version.

Topic 3: It often happens in this kind of algorithm where you are processing input and keeping some “state” (in this case, current and lastc) that you can’t immediately pack up and go home when you reach the end of the input; you need to do something with that state.

Topic 4: This is going to get technical, but it’s a common trap for new Python players.

>>> current = [1, 2, 3, 4, 5]
>>> lastc = current # lastc and current refer to THE SAME LIST; no copying!
>>> print current
[1, 2, 3, 4, 5]
>>> print lastc
[1, 2, 3, 4, 5]
>>> current[:] = [] # The list to which current refers is cleared
>>> print current
[]
>>> print lastc # lastc refers to the same list
[]

It’s better to just assign the name current to a new list; see my code.

Topic 5 (bonus extra): Consider using 4-space indentation, not 8. Causing both the horizontal and vertical scroll-bars to appear in an SO question or answer == FAIL 🙂

1

solved Code: How can I go over a list of numbers and print the number of consecutive increases in Python? [closed]