Python

Filipe Martins
Filipe Martins
2,824 Points

Whats is wrong with my code? I have trying to return the sum of the numbers that only exist in both ranges

You’re given two integer ranges x and y as (x1, x2) and (y1, y2). An integer range is a list of numbers. The x range includes every number from x1 to x2 (including x2). Similarly, the y range includes every number from y1 to y2.

A function call of sum_matching_range(1, 10, 5, 20) -> returns 45

To get the answer 45, we summed 5, 6, 7, 8, 9 and 10 since those numbers exist in both ranges (1,10) and (5,20).

def sum_matching_rang (x1, x2, y1, y2):
    x_range = range(x1, x2+1)
    y_range = range(y1, y2+1)
    j = 0
    range_sum = 0

    for i in x_range:
        if x_range[j] == y_range[j]:
            range_sum += x_range[j] + y_range[j]
            j += 1
    return range_sum
print(sum_matching_rang (1, 10, 5, 20)) 

2 Answers

Is this a challenge? If so can you provide a link? Based on your description it looks like you will sum x values if the x value is in the y range. If that is the case then the code could be:

def sum_matching_range(x1, x2, y1, y2):
    x_range = range(x1, x2+1)
    y_range = range(y1, y2+1)
    range_sum = 0

    for i in x_range:
        if i in y_range:
            range_sum += i
    return range_sum
print(sum_matching_range(1, 10, 5, 20)) 

In your code you loop for the number of x items and check if the values are equal at the same position, then only increment j if this is the case. For the test given:

print(sum_matching_rang (1, 10, 5, 20))

the first comparison will be if 1 equals 5. Since this is false nothing is summed and j doesn't increment. Therefore the next test will again be if 1 equals 5, and so on.

Filipe Martins
Filipe Martins
2,824 Points

Perfect! It is a assignment! so easy and I failed :(

Many Thanks for you help!!!

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 24,835 Points

This is a tricky program! And your program is close to a solution. Kris' solution is very elegant.

One thing that is great about Python (well computer languages in general) is there are nearly infinite possibilities in how you could code something and still solve the problem.

I used two approaches based on your code -- slightly different from Kris' approach.

The first version, which is close to your code uses a nested loop approach.

def sum_matching_rang (x1, x2, y1, y2):
    x_range = range(x1, x2+1)
    y_range = range(y1, y2+1)
    range_sum = 0
    # nesting the loops allows us to check ALL values
    for i in x_range:
        for j in y_range:
            # if i and j match then sum the matched value
            if i == j:
                 range_sum += j
    return range_sum
print(sum_matching_rang (1, 10, 5, 20)) 

Another nice approach is using Python set() type and find "set intersection". This allows us to use a single loop to sum the matching numbers.

def sum_matching_rang (x1, x2, y1, y2):
    x_range = range(x1, x2+1)
    y_range = range(y1, y2+1)
    range_sum = 0
    # get numbers that are in BOTH ranges
    matching_numbers = set(x_range).intersection(y_range)
    # calculate sum of matching numbers
    for x in matching_numbers:
        range_sum += x
    return range_sum
print(sum_matching_rang (1, 10, 5, 20)) 

I hope this helps your understanding. Good luck with your Python journey!!