1

I have a list of numbers where in the sum of any two adjacent numbers is a perfect square. The list is x=[1,8,28,21,4,32,17,19,30,6,3,13,12,24]

for i in range(len(x)-1):
    y= x[i]+x[i+1]
    z=y**0.5
    #till here found the square root of the sum of the adjacent numbers in list
    if(z.is_integer==True):
        //code

I want to check the remaining numbers in the list. If all the elements of the list satisfy the condition. Then I want to print the list

The expected output should be

[1,8,28,21,4,32,17,19,30,6,3,13,12,24] satisfies the condition
Guy
  • 46,488
  • 10
  • 44
  • 88
Avinash K
  • 43
  • 5
  • Can you clarify more?, if you already found perfect square, then append X[i], X[i+1] into new list and when for loop ends, print new list. – pkd May 31 '21 at 06:10
  • 1
    `is_integer` is a function, it should be `is_integer()`. – Guy May 31 '21 at 06:22

5 Answers5

2

Maybe something like this? Make function that will be called for list and return True if list satisfies condition and False if it doesn't.

def some_function(nums):
   for i in range(len(nums) - 1):
      y = nums[i] + nums[i + 1]
      z = y ** 0.5
      #till here found the square root of the sum of the adjacent numbers in list
      if z.is_integer() not True:
         # if there is some two numbers that don't meet condition, function will return False
         return False
   return True

You call it like this: meet_condition = some_function(x) After that just check if it's True and if it is print list and appropriate text.

Milos Stojanovic
  • 172
  • 2
  • 11
0

You can use this approach. I am sure there is a better way, with fewer lines of code. Have a blast!

numbers = [1,8,28,21,4,32,17,19,30,6,3,13,12,24]

for i in range(len(numbers)):
    if i < len(numbers) - 1:
        if ((numbers[i] + numbers[i+1]) ** 0.5) % 1 == 0:
            continue
        else:
            print("Does not satisfy condition")
            break
    print(numbers)

Output:

[1, 8, 28, 21, 4, 32, 17, 19, 30, 6, 3, 13, 12, 24]
Cédric
  • 26
  • 5
0
import math

x=[1,8,28,21,4,32,17,19,30,6,3,13,12,24]

b = [x[i] for i in range(0,len(x)-1) if math.sqrt(x[i]+x[i+1]).is_integer()]

if (b[-1] == x[-2]):
    b.append(x[-1])

print(b)

Output : [1, 8, 28, 21, 4, 32, 17, 19, 30, 6, 3, 13, 12, 24]

singhV
  • 157
  • 9
0

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:

  1. Define a method that returns consecutive pairs from the list;
  2. Define a method that checks if the number is a squere;
  3. 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
iGian
  • 11,023
  • 3
  • 21
  • 36
0

Something pythonic should be like this.

import numpy as np
import functools
nums = np.array([1,8,28,21,4,32,17,19,30,6,3,13,12,24])
result = functools.reduce(lambda a,b: a and b, map(lambda x : (x ** 0.5).is_integer(), nums[1:] + nums[:-1]))
M. Zhu
  • 51
  • 4