Home > OS >  Looping through a list of integers
Looping through a list of integers

Time:10-28

I need to create a program that that finds and prints the first sequence of 3 integers in a list whose sum is 5 then exits.

input:

def list_function(numbers):

    while numbers:
        for x in numbers[:3]:
            if x != 5:
                print("The sum has not been reached.")
                inputlist.pop(0)
            if x == 5:
                print("The sum has been reached.")
                break
    return x

inputlist = [1, -2, 3, 3, -1, -2, 7, 0, 2]

list_function(inputlist)

output:

The sum has not been reached. (#this is repeated 9 times)

Any ideas what I need to do in order to make this program work properly?

CodePudding user response:

You can iterate upto length of the list subtracted by length of required list plus one, then take the slice from the iterator's number to the number plus the length of the required list, and if the sum is the required total, return the slice

def list_function(numbers, length=3, total=5):
    for i in range(len(numbers)-length 1):
        if sum(numbers[i:i length])== total:
            return numbers[i:i length]
    return []

SAMPLE RUN:

>>> list_function([1, -2, 3, 3, -1, -2, 7, 0, 2])
[3, 3, -1]

CodePudding user response:

There are two issues.

  1. You are not really taking the sum anywhere. Instead you are comparing x directly.
  2. You are not iterating on each triplet. Just the first one.

In the example you gave, if any one of the first 3 is 5 , it will give the infinite output as sum has been reached. Why ? Because state of numbers is always true. This condition while(numbers) is always true since you're never really changing it.

So if you want to print the first triplet whose sum is 5 then this is how it should be done.

def list_function(numbers):
    for i in range(len(numbers)):
        current_sum = 0
        for x in numbers[i:i 3]:
            current_sum  = x
        if current_sum == 5:
            print(numbers[i], numbers[i 1], numbers[i 2])
            break

inputlist = [1, 5, 3, 3, -1, -2, 7, 0, 2]

list_function(inputlist)

This will print 3 3 -1 as the output.

CodePudding user response:

some problem with this implementation is marked in comments

def list_function(numbers):

    while numbers: # infinite loop
        for x in numbers[:3]:  # alway looping over first 3 numbers, add dynamic variable to iterate further
            if x != 5:
                print("The sum has not been reached.")
                inputlist.pop(0)
            if x == 5: # sum is not taken , this will only check number
                print("The sum has been reached.")
                break
    return x

inputlist = [1, -2, 3, 3, -1, -2, 7, 0, 2]

list_function(inputlist)

in the code below, we are looping over indexes of numbers, taking the sum of 3 numbers and checking if it is equal to 5, if it is then we print the numbers and come out of the function using break. if no sequence is found then the else block is run after the loop else block is executed if the loop is finished, if the loops get broken in between then else block will not get executed.

def list_function(numbers):

    for i in range(len(numbers)-2):
        if numbers[i]   numbers[i 1]   numbers[i 2] == 5:
            print(numbers[i],numbers[i 1],numbers[i 2])
            break
    else:
        print("The sum has not been reached.")
    return None

inputlist = [1, -2, 3, 3, -1, -2, 7, 0, 2]

list_function(inputlist)

I hope this helps.

  • Related