Answer to Question #348767 in Python for madhu

Question #348767

given a list of M numbers and a positive integer N, print the sum of numbers whose position in the list is divisible by N .consider that the position of numbers range from 1 to N

INPUT:

the first line contains two space-separated integers N, M

the second line contain M space-separated integers.

output:

print a single integer representing the required sum

explanation:

Sample Output1

Given N=1 , M=7

and the number list = [4,8,6,6,7,9,3]

As every position is divisible by 1,

4+8+6+6+7+9+3=43

so the output should be 43


1
Expert's answer
2022-06-07T08:12:47-0400
N_M_lst = list(input().split())
N = int(N_M_lst[0])
M = int(N_M_lst[1])
number_lst = list(input().split())
number_lst = [int(i) for i in number_lst]
sum = 0
for i in range(M):
    if number_lst[i] % N == 0:
        sum += number_lst[i]
print(sum)

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