Plz help in this code

#1

I’ve been working on this problem in CodeWars and I cannot find where I am going wrong.

import math

def retListSquare(a):
    squares = []
    for i in a:
        squares.append(i*i)
    return squares

def findDivisor(a):
    factors = []
    for i in range(1, a+1):
        if a % i == 0:
            factors.append(i)
    return factors
    
def list_squared(m, n):
	ans = []
	r = range(m, n+1)
	for i in r:
		Sum = sum(retListSquare(findDivisor(i)))
		if int(Sum + 0.5) ** 2 == Sum:
			j = []
			j.append(i)
			j.append(Sum)
			ans.append(j)
	return ans


print(list_squared(42, 250))  # Returns '[]'
#2

How could this condition ever evaluate to true? Since it can’t, ans remains an empty list.
P.S. I love Codewars!

#3

I think this is more like what you’re trying to do. Checking to see if Sum is a perfect square? There may be another way, but this will work:

if math.sqrt(Sum) == int(math.sqrt(Sum)): 

Good luck! :slightly_smiling_face:

#4

if math.sqrt(Sum) % 1 == 0 will do what you want.

Also, to return a list of factors of a number n, you need only range as high as the square root of n.

1 Like
split this topic #5

3 posts were split to a new topic: Segue on a perfect topic

#6

Yeah I was a little confused in the square root thing. Thank mate :+1:

I love CodeWars too! I’m learning Python so trying to solve problems for practice :slight_smile: