Sunday, February 25, 2018

Python program : Sum of all the perfect squares in a List

Write a function sumsquares(l) that takes as input a list of integers and retuns the sum of all the perfect squares in l.

Here are some examples to show how your function should work.


>>> sumsquares([1,4,9])
14
>>> sumsquares([10,11,12,15])
0
>>> sumsquares([16,20,25,30,625])
666 
 
from math import * 
def sumsquares(l):
  sum=0
  
  for i in l:
    sqr=sqrt(float(i))
    if ((sqr-int(sqr))==0.0) :
      sum=sum + i
  return(int(sum))
 

No comments:

Post a Comment

Sacred Thought

26 April 2024  Dear friends, I write the explanation of two verses of Geets for all of you, I hope you all will like it and benefit from it....