Different solution at Pairs Sums Problem

Question about the solution of screen 4 of mission 480

My Code:

# Provided input
test_values = [1, 2, 5]
test_targets = [2, 3, 7, 8]      
# Expected output:
# {2: True, 3: True, 7: True, 8: False}

# Write your code below
def find_sums(values, target_sums):
    sums = {}
    for target in target_sums:
        sums[target] = False
        for i in values:
            for j in values:
                if (i + j) == target:
                    sums[target] = True          
    return sums

print(find_sums(test_values, test_targets))

The answer:

# Provided input
test_values = [1, 2, 5]
test_targets = [2, 3, 7, 8]      
# Expected output:
# {2: True, 3: True, 7: True, 8: False}

# Write your code below
def find_sums(values, target_sums):
    sums = {}
    for target in target_sums:
        sums[target] = False
        for i in range(len(values)):
            for j in range(i, len(values)):
                if values[i] + values[j] == target:
                    sums[target] = True
    return sums

print(find_sums(test_values, test_targets))

The author uses a different approach. Why?

Thanks in advance!

Hey.

I don’t know what problem this is, but I can guess from the example. Hopefully my guess is right.

I don’t really understand your question. Let me turn it back on you. Here’s my solution:

def find_sums(values, target_sums):
    return {
        t: t in map(lambda x: sum(x[0]),
                    zip(__import__("itertools").product(values,
                                                        values)
                       )
                   ) for t in target_sums
    }

Why did you, the author of a solution, use a different one?

1 Like

Hi Bruno,

Thanks for your reply! I am looking to understand the instructions and why the author of the course presented the specific solution he/she did because it was different from mine. You are presenting a whole different solution to this problem which is even less understandable for me. :slight_smile:

@DataBuzzer,

The main difference between your solution and mine is the fact that I iterate over the indexes and you over the values directly:

# Iterate over the indexes
for i in range(len(values)): 

VS

# Iterate over the values
for i in values:

The reason why I iterated over the indexes rather the going directly over the values, is to be able to do the optimization of checking fewer pairs to find the answer.

Note that in, the second for loop, start at index i, not at 0:

for i in range(len(values)):
    for j in range(i, len(values)):
                 # ^
                 # start at i  

We’ve learned this in screen 7 for mission 479.

For example, my solution will check (values[0], values[1]) but never (valur[1], value[0]) while your solution will. However, they both have the same sum so the second check is redundant.

But don’t worry, your solution is totally correct. We’ve learned that this does not change the complexity so it is not a critical optimization.

If you’re not convinced that this reduces the number of pairs that are checked, you can add a print() statement in both solutions and run them.

Loop on values

def find_sums(values, target_sums):
    sums = {}
    for target in target_sums:
        sums[target] = False
        for i in values:
            for j in values:
                print(i, j)   # <- Added print statement to your solution
                if (i + j) == target:
                    sums[target] = True          
    return sums

It printed 36 pairs:

1 1
1 2
1 5
2 1
2 2
2 5
5 1
5 2
5 5
1 1
1 2
1 5
2 1
2 2
2 5
5 1
5 2
5 5
1 1
1 2
1 5
2 1
2 2
2 5
5 1
5 2
5 5
1 1
1 2
1 5
2 1
2 2
2 5
5 1
5 2
5 5

Loop on indexes

def find_sums(values, target_sums):
    sums = {}
    for target in target_sums:
        sums[target] = False
        for i in range(len(values)):
            for j in range(i, len(values)):
                print(values[i], values[j]) # <- Added print statement to my solution
                if values[i] + values[j] == target:
                    sums[target] = True        
    return sums

Mine printed 24 pairs:

1 1
1 2
1 5
2 2
2 5
5 5
1 1
1 2
1 5
2 2
2 5
5 5
1 1
1 2
1 5
2 2
2 5
5 5
1 1
1 2
1 5
2 2
2 5
5 5
2 Likes

If François hadn’t answered, I’d have said that they were basically the same answer, which was what confused me in your question.

It turns that the answers are fundamentally different, but I missed this when I read the solutions, it escaped me.

1 Like

Thanks! Makes sense!

Thanks for replying anyways! Next time I will try to make my question more clear.

1 Like