Math Quiz:
The first line input space-seperated integers.
the second line input is positive integer M denoting the number of index ranges
The next M lines contains two space-seperated integers of the range.
explanation: 1 2 2 3 3 3 4 5 6
2
0 2
1 4
output:
1+2+2=5
1+2+2+3+3+3+4=18
input:
1 2 2 3 3 3 4 5 6
2
0 2
1 4
output:
5
18
input:6 6 14 20 8 -2 2 -3
4
1 2
2 5
3 6
0 4
output:
2
2
12
2
Use the following set of codes:
# Sum of ranges
numbers = [int(i) for i in input("Enter a list of numbers separated by a space: ").split()]
index_range = int(input())
range_numbers = []
for i in range(0, index_range):
range_numbers.append(input())
for i in range(0, index_range):
number = range_numbers[i].split(' ')
min_value = int(number[0])
max_value = int(number[1])
sum_result = 0
for j in range(0, len(numbers)):
num = int(numbers[j])
if (num >= min_value) and (num <= max_value):
sum_result += num
print(sum_result)
Comments
Leave a comment