Answer to Question #265694 in Python for ganesh

Question #265694

Given a space-separted list of integers as input, write a program to add the elements in the given range and print their sum


You will be given M multiple range queries,where

You should print the sum of numbers that belong to the corresponding range


note: while checking if the number belongs to a range,including the string and numbers of range as well


Input

The first line of input is space-separated integers

The secondline of input is a positive integers M denoting the number of queries

The next M line contain two space-separated integer

Output:

The output should be M line printing the sum of each includes range


sample input 1:

1 2 2 3 3 3 4 5 6

2

0 2

1 4


sample output1:

5

18


sampleInput2:

6 6 14 20 8 -2 2 -3

4

1 2

2 5

3 6

0 4

sampleOutput:

2

2

12

2



1
Expert's answer
2021-11-15T07:38:46-0500
numbers = input().split(" ")
numberQueries=int(input())
rangeNumbers=[]
for i in range(0, numberQueries):
    rangeNumbers.append(input())


for i in range(0, numberQueries):
    num=rangeNumbers[i].split(" ")
    minValue=int(num[0])
    maxValue=int(num[1])
    sumResult=0
    for i in range(0, len(numbers)):
        number=int(numbers[i])
        if(number>=minValue and number<=maxValue):
            sumResult+=number
    print(sumResult)
        
    





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