Answer to Question #320738 in Python for sandhya

Question #320738

SUM OF POWERS:

you are given a list of integers L of size N. write a program to compute the value of SUM

SUM=x1pow1+x2pow2+x3pow3+-----+xNpowN

where xi concatenated with powi is the i th element of the list L and powj is single digit number

input:

The input contains space-separated N integers

Explanation: L=[25]

x1=2 ,pow1=5

SUM=2^5=32

i/p: L=[132,301]

x1=13,pow1=2

x2=30,pow2=1

SUM=13^2 +30^1=199


i/p:25

o/p:32

i/p:132 301

o/p:199




1
Expert's answer
2022-03-30T07:39:31-0400
def sum(L):
    X = []
    pow = []
    for n in L:
        x = n // 10
        X.append(x)
        p = n % 10
        pow.append(p)
    s = sum_pows(X, pow)
    return s

def sum_pows(X, pow):
    s = 0
    for x, p in zip(X, pow):
        s += x**p
    return s

def main():
    line = input()
    L = [int(s) for s in line.split()]
    s = sum(L)
    print(s)

if __name__ == '__main__':
    main()

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