Given two lists, pair them diagonally, in cross pattern [works for even and equi-length lists].
Input : test_list1 = [4, 5, 6, 2], test_list2 = [9, 1, 4, 7]
Output : [(4, 1), (5, 9), (6, 7), (2, 4)]
Explanation : Crosslinked diagonally, 4->1, 5->9.
Input : test_list1 = [4, 5], test_list2 = [9, 1]
Output : [(4, 1), (5, 9)]
Explanation : Crosslinked diagonally, 4->1, 5->9.
Method #1: Using loop
In this, we iterate through list and test for even or odd index, in case of even index, we pair with next element of other list, or else we pair with previous element of other list. This way cross pattern tuples are formed.
Python3
def crossPair(test_list1, test_list2):
if len (test_list1) ! = len (test_list2):
return - 1
res = []
for idx in range ( len (test_list1)):
if idx % 2 = = 0 :
res.append((test_list1[idx], test_list2[idx + 1 ]))
else :
res.append((test_list1[idx], test_list2[idx - 1 ]))
return res
input_list1 = [ 4 , 5 , 6 , 2 , 8 , 9 ]
input_list2 = [ 9 , 1 , 4 , 7 , 9 , 2 ]
print ( "The original list 1 is : " + str (input_list1))
print ( "The original list 2 is : " + str (input_list2))
print ( "Paired List elements : " , crossPair(input_list1, input_list2))
|
Output:
The original list 1 is : [4, 5, 6, 2, 8, 9] The original list 2 is : [9, 1, 4, 7, 9, 2] Paired List elements : [(4, 1), (5, 9), (6, 7), (2, 4), (8, 2), (9, 9)]
Time complexity: O(n), where n is the length of the input lists.
Auxiliary space: O(n), where n is the length of the input lists.
Method #2: Using list comprehension
Uses similar approach as above method, difference being list comprehension is used as a single linear alternative to solve this problem.
Python3
def crossPair(test_list1, test_list2):
if len (test_list1) ! = len (test_list2):
return - 1
res = [(test_list1[idx], test_list2[idx + 1 ]) if idx % 2 = =
0 else (test_list1[idx], test_list2[idx - 1 ]) for idx in range ( len (test_list1))]
return res
input_list1 = [ 4 , 5 , 6 , 2 , 8 , 9 ]
input_list2 = [ 9 , 1 , 4 , 7 , 9 , 2 ]
print ( "The original list 1 is : " + str (input_list1))
print ( "The original list 2 is : " + str (input_list2))
print ( "Paired List elements : " , crossPair(input_list1, input_list2))
|
Output:
The original list 1 is : [4, 5, 6, 2, 8, 9] The original list 2 is : [9, 1, 4, 7, 9, 2] Paired List elements : [(4, 1), (5, 9), (6, 7), (2, 4), (8, 2), (9, 9)]
Method#3: Using Recursive method.
Python3
def crossPair(test_list1, test_list2, idx = 0 , res = []):
if idx = = len (test_list1):
return res
if idx % 2 = = 0 :
res.append((test_list1[idx], test_list2[idx + 1 ]))
else :
res.append((test_list1[idx], test_list2[idx - 1 ]))
return crossPair(test_list1, test_list2, idx + 1 , res)
input_list1 = [ 4 , 5 , 6 , 2 , 8 , 9 ]
input_list2 = [ 9 , 1 , 4 , 7 , 9 , 2 ]
print ( "The original list 1 is : " + str (input_list1))
print ( "The original list 2 is : " + str (input_list2))
print ( "Paired List elements : " , crossPair(input_list1, input_list2))
|
Output
The original list 1 is : [4, 5, 6, 2, 8, 9]
The original list 2 is : [9, 1, 4, 7, 9, 2]
Paired List elements : [(4, 1), (5, 9), (6, 7), (2, 4), (8, 2), (9, 9)]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #4: Using numpy():
Algorithm:
- Define a function called crossPair that takes in two lists as arguments.
- Create a list comprehension that iterates through the length of the first list using numpy’s arange function
- For each element in the first list, check if its index is even or odd using the modulo operator.
- If the index is even, append a tuple of the current element from the first list and the element at the next index in the second list to a new list.
- If the index is odd, append a tuple of the current element from the first list and the element at the previous index in the second list to the new list.
- Return the new list of paired elements
- Initialize two input lists and print them.
- Call the crossPair function with the two input lists as arguments and print the result.
Python3
import numpy as np
def crossPair(test_list1, test_list2):
return [(test_list1[i], test_list2[i - 1 ] if i % 2 else test_list2[i + 1 ]) for i in np.arange( len (test_list1))]
input_list1 = [ 4 , 5 , 6 , 2 , 8 , 9 ]
input_list2 = [ 9 , 1 , 4 , 7 , 9 , 2 ]
print ( "The original list 1 is : " + str (input_list1))
print ( "The original list 2 is : " + str (input_list2))
print ( "Paired List elements : " , crossPair(input_list1, input_list2))
|
Output:
The original list 1 is : [4, 5, 6, 2, 8, 9]
The original list 2 is : [9, 1, 4, 7, 9, 2]
Paired List elements : [(4, 1), (5, 9), (6, 7), (2, 4), (8, 2), (9, 9)]
Time Complexity: O(n), where n is the length of the input list. This is because the algorithm iterates through each element of the input list once.
Space Complexity: O(n), where n is the length of the input list. This is because the algorithm creates a new list to store the paired elements, which will be the same length as the input list.
Method 5: Using the map function and a lambda function
Step-by-step approach:
- Use the built-in map function to apply a lambda function to the two input lists simultaneously.
- The lambda function should take two arguments, one from each list, and return a tuple with the elements in the cross pattern.
- Use the list function to convert the map object into a list of tuples.
- Return the list of paired elements.
Python3
def crossPair(test_list1, test_list2):
pairs = list ( map ( lambda x, y: (x, test_list2[test_list1.index(x) + 1 ]) if test_list1.index(x) % 2 = = 0 else (x, test_list2[test_list1.index(x) - 1 ]), test_list1, test_list2))
return pairs
input_list1 = [ 4 , 5 , 6 , 2 , 8 , 9 ]
input_list2 = [ 9 , 1 , 4 , 7 , 9 , 2 ]
print ( "The original list 1 is : " + str (input_list1))
print ( "The original list 2 is : " + str (input_list2))
print ( "Paired List elements : " , crossPair(input_list1, input_list2))
|
Output
The original list 1 is : [4, 5, 6, 2, 8, 9]
The original list 2 is : [9, 1, 4, 7, 9, 2]
Paired List elements : [(4, 1), (5, 9), (6, 7), (2, 4), (8, 2), (9, 9)]
Time complexity: O(n), where n is the length of the input lists, as it requires iterating through both lists.
Auxiliary space: O(n) to store the list of paired elements.
Share your thoughts in the comments
Please Login to comment...