Python program to test for Non-neighbours in List
Given two elements, write a Python program to check that they don’t occur as their neighbors in the list.
Examples:
Input : test_list = [3, 7, 2, 1, 4, 5, 7, 9], i, j = 7, 4
Output : True
Explanation : 7 doesn't occur are 4's neighbour.
Input : test_list = [3, 7, 2, 1, 4, 5, 7, 9], i, j = 5, 4
Output : False
Explanation : 5 occurs are 4's neighbour.
Method #1: Using loop
In this, we check for next and previous elements to not be ‘i’ or ‘j’ while iteration of list.
Python3
test_list = [ 3 , 7 , 2 , 1 , 4 , 5 , 7 , 9 ]
print ( "The original list is : " + str (test_list))
i, j = 7 , 4
res = True
for idx in range ( 1 , len (test_list) - 1 ):
if test_list[idx] = = i:
if test_list[idx - 1 ] = = j or test_list[idx + 1 ] = = j:
res = False
break
print ( "Are i, j Non-neighbours' : " + str (res))
|
Output
The original list is : [3, 7, 2, 1, 4, 5, 7, 9]
Are i, j Non-neighbours' : True
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2 : Using all() function
This is a one-liner approach to solving this problem. In this, we perform the task of checking for all elements for neighbors using all() in the generator expression in all() functions.
Python3
test_list = [ 3 , 7 , 2 , 1 , 4 , 5 , 7 , 9 ]
print ( "The original list is : " + str (test_list))
i, j = 7 , 4
res = all (test_list[idx - 1 ] ! = j and test_list[idx + 1 ] ! =
j for idx in range ( 1 , len (test_list) - 1 ) if test_list[idx] = = i)
print ( "Are i, j Non-neighbours' : " + str (res))
|
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3 : Using find() method
Python3
test_list = [ 3 , 7 , 2 , 1 , 4 , 5 , 7 , 9 ]
print ( "The original list is : " + str (test_list))
i, j = 7 , 4
x = list ( map ( str ,test_list))
y = "*" .join(x)
a = str (i) + "*" + str (j)
b = str (j) + "*" + str (i)
res = True
if y.find(a)! = - 1 or y.find(b)! = - 1 :
res = False
print ( "Are i, j Non-neighbours' : " + str (res))
|
Output
The original list is : [3, 7, 2, 1, 4, 5, 7, 9]
Are i, j Non-neighbours' : True
Time Complexity: O(N), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(N), where n is the number of elements in the list “test_list”.
Method 4: Using slicing and set operations
Steps:
- Find the indices of i and j in the list using the index() method.
- Create two slices of the list:
- Slice from the start to the index of i.
- Slice from the index of j to the end.
- Create a set by concatenating the two slices and check if j is present in the set.
- If j is not present in the set, then return True else return False.
Python3
test_list = [ 3 , 7 , 2 , 1 , 4 , 5 , 7 , 9 ]
print ( "The original list is : " + str (test_list))
i, j = 7 , 4
i_idx = test_list.index(i)
j_idx = test_list.index(j)
slice1 = set (test_list[:i_idx])
slice2 = set (test_list[j_idx + 1 :])
res = j not in slice1.union(slice2)
print ( "Are i, j Non-neighbours' : " + str (res))
|
Output
The original list is : [3, 7, 2, 1, 4, 5, 7, 9]
Are i, j Non-neighbours' : True
Time complexity: O(N)
Auxiliary space: O(N)
Last Updated :
04 May, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...