I'd suggest you review the way you check the integer is a square:
y = 9
z = y ** 0.5
z #=> 3.0
z.is_integer==True #=> False
So, it seems 9
not to be a square.
is_integer
should be is_integer()
without check.
Please refer to this topic for example: Check if a number is a perfect square
For a solution to the question, I propose to split the problem:
- Define a method that returns consecutive pairs from the list;
- Define a method that checks if the number is a squere;
- Put it together using the all(iterable) function.
First the method for getting consecutive elements, in this case is a generator:
def each_cons(iterable, n = 2):
if n < 2: n = 1
i, size = 0, len(iterable)
while i < size-n+1:
yield iterable[i:i+n]
i += 1
Given your list x=[1,8,28,21,4,32,17,19,30,6,3,13,12,24]
, you call it this way:
list(each_cons(x));
# => [[1, 8],[8, 28],[28, 21],[21, 4],[4, 32],[32, 17],[17, 19],[19, 30],[30, 6],[6, 3],[3, 13],[13, 12],[12, 24]]
Then a method for the square, which I've stolen here:
def is_square(apositiveint):
x = apositiveint // 2
seen = set([x])
while x * x != apositiveint:
x = (x + (apositiveint // x)) // 2
if x in seen: return False
seen.add(x)
return True
Finally put all together:
all(is_square(a + b) for a, b in each_cons(x))
#=> True