Answer to Question #320725 in Python for sandhya

Question #320725

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




1
Expert's answer
2022-03-30T07:39:24-0400

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)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS