-2

We have two arrays X and Y of n characters each, where X has n randomly ordered characters, while Y has exactly the same n characters but in different order.

Your task is to find a way to group the cells of the array X and Y into pairs that hold the same character. For example: Input:

X = ['O', 'L', 'M', 'S', 'N', 'J', 'P', 'T', 'I', 'R', 'H', 'G']
Y = ['S', 'N', 'H', 'P', 'T', 'I', 'O', 'R', 'L', 'M', 'G', 'J']

Output:

X[0] with Y[6]
X[1] with Y[8]
X[2] with Y[9]
X[3] with Y[0]

… and so on

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • So you want to loop on array X and for each character ___find its index___ in array Y? – Tomerikoo Aug 07 '21 at 22:59
  • Does this answer your question? [Finding the index of an item in a list](https://stackoverflow.com/questions/176918/finding-the-index-of-an-item-in-a-list) – Tomerikoo Aug 07 '21 at 22:59
  • Please see [How do I ask and answer homework questions?](https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions) – martineau Aug 08 '21 at 01:22

1 Answers1

1

You can iterate through the characters in x and find their index in y

x = ["O", "L", "M", "S", "N", "J", "P", "T", "I", "R", "H", "G"]
y = ["S", "N", "H", "P", "T", "I", "O", "R", "L", "M", "G", "J"]

for i,char in enumerate(x):
    print(f"x[{i}] == y[{y.index(char)}]")

Or if you want to store the pairs in a list you could use this list comprehension:

[(i, y.index(char)) for i, char in enumerate(x)]
Jacob
  • 750
  • 5
  • 19