Search code examples
pythonarrayssimilarity

how would I find if numbers in an array are equal to each other in python


If I had an array formed for example [1,2,3,4,5,6], how would I be able to check if 1 and 6 are equal and then 2 and 5 and then 3 and 4 etc without knowing how many numbers in my array that I have? Finding if the array is symmetric

The data set is being generated randomly from different molecules so the data isn't consistent and I am trying to work this out for each molecule.


Solution

  • def checkList(numbers):
        for i in range(len(numbers)//2+1):
            j = len(numbers) -i -1
            print(i,j)
            if i<j:
                if numbers[i] != numbers[j]:
                    return False
        return True
        
    print(checkList([1,2,3,2,1]))
    

    We set two pointers. One is from the start and the other one is from the end. both are moving to the middle while checking each pointer's elements are equal until the middle.