0

I need to write a function where, for any list, each element in the list is added together and to see if it equals a constant.

For example:

L = [1, 2, 3, 4]

The sum of each element added together is 1+2=3, 1+3=4, 1+4=5, 2+3=5, 2+4=6, 3+4=7.

And then to test each of these results to a constant eg if the output = C where C = 3, and append the two numbers that were summed to equal the constant to a new list to be printed.

I am not allowed to use indexing here.

So far I am able to add the numbers in pairs along the list:

def prize(L):
    possible = []
    C = 4
    prevprev = L.pop(0)
    prev = L.pop(0)
    print("initial", prevprev, prev)

    for n in L:
        i = prev + prevprev
        prevprev = prev
        prev = L.pop(0)


    if i == C:
        possible.append(i)

    return possible

print("possible ", possible)

But for some reason this misses out the last 2 elements in the list when iterating over it.

Remi Guan
  • 21,506
  • 17
  • 64
  • 87
IceDust
  • 3
  • 2
  • Just a **Tip**: [`L = [1, 2, 3, 4]; sum([sum(i) for i in list(__import__('itertools').combinations(L, 2))])`](https://docs.python.org/3/library/itertools.html#itertools.combinations). – Remi Guan Oct 30 '15 at 03:35
  • Possible duplicate of [Modifying list while iterating](http://stackoverflow.com/questions/1637807/modifying-list-while-iterating) and [How to modify list entries during for loop?](http://stackoverflow.com/questions/4081217/how-to-modify-list-entries-during-for-loop). See also: [Thou shalt not modify a list during iteration](https://unspecified.wordpress.com/2009/02/12/thou-shalt-not-modify-a-list-during-iteration/) – TessellatingHeckler Oct 30 '15 at 03:38
  • @TessellatingHeckler thank you for that last link, that explains why i wasn't getting the desired results :) – IceDust Oct 30 '15 at 04:25
  • @KevinGuan unfortunately we have not been taught about itertools yet, so I'd rather stick with what i know for now, but thank you anyway! – IceDust Oct 30 '15 at 04:25

1 Answers1

2

I couldn't quite follow your code or explanation quite well, but I believe this does what you want. This is done without the itertools module. Using it would produce a more compact code. This is done as an alternative to itertools

lst = [1, 2, 3, 4]

def prize(L):
    pair_list = []
    C = 4

    # Creates a list of tuples (1, 2), (1, 3), etc
    for x in L:
        for y in L:
            # if x != y | To prevent 1 + 1 and 2 + 2 (following example)
            # (y, x) not in pair_list | Prevent inverted pairs
            if x != y and x + y == C and (y, x) not in pair_list:
                pair_list.append((x, y))

    # Return list tuples that add to constant           
    return pair_list

print('Possible', prize(lst))

if you wish to use itertools then you would use

from itertools import combinations

def prize(L):
    C = 4
    return [(x, y) for x, y in combinations(L, 2) if x + y == C]

lst = [1, 2, 3, 4]

print('Possible', prize(lst))
Steven Summers
  • 5,079
  • 2
  • 20
  • 31
  • Thank you so much! This does exactly what I was trying to do. Thank you for the comments as well, that really helps with understanding the code :) – IceDust Oct 30 '15 at 04:18