Answer to Question #348013 in Python for venu

Question #348013

(SIR/MADAM please answer to this question because i'm not getting the desire output)

Nth number sum:

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

sample Output2

Given N=4 , M=13

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

the position 4 , 8 and 12 divisible by N,

the output should be 4+9+1=14

so the output should be 14


1
Expert's answer
2022-06-06T08:57:42-0400
entered_NM = input("Enter N and M separated by a space: ").split()
NM_list = list(map(int, entered_NM))

entered_list = input("Enter a list of M items separated by a space: ").split()
test_list = list(map(int, entered_list))

a = []
for i in range(0, len(test_list)):
    if (i + 1) % NM_list[0] == 0:
        a.append(test_list[i])

print(sum(a))

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