Python | Check whether two lists follow same pattern or not

Given two lists A and B, check if they follow the same pattern or not.
Condition for pattern matching:

  1. Ai > Aj, then Bi > Bj
  2. Ai = Aj, then Bi = Bj
  3. Ai < Aj, then Bi < Bj,
    for all i, j.

Example:

Input:
2 17 100
10 20 50

Output:
YES

Input:
5 7 10 33
10 50 10 45



Output:
NO

Approach:
To check if two lists follow the above pattern. Sort both lists and find the index of previous list element in the sorted list. If indices match, then pattern matched otherwise no pattern matched.

Code : Python program for checking the pattern.

filter_none

edit
close

play_arrow

link
brightness_4
code

# python program for above approach
a = [5, 7, 10, 33]
b = [10, 50, 10, 45]
aa = a.copy()
bb = b.copy()
  
# sorting the list
aa.sort()
bb.sort()
  
for i in range(len(a)-1):
      
    # checking the index are same or not
    if aa[i] < aa[i + 1] and bb[i] < bb[i + 1]:
        if a.index(aa[i])== b.index(bb[i]) and a.index(aa[i + 1]) == b.index(bb[i + 1]):
            flag ="YES"
        else:
            flag ="NO"
            break
      
    elif aa[i] == aa[i + 1] and bb[i] == bb[i + 1]:
        if a.index(aa[i]) == b.index(bb[i]) and a.index(aa[i + 1]) == b.index(bb[i + 1]):
            flag = "YES"
        else:
            flag = "NO"    
            break
      
    else:
        flag = "NO"
        break
      
print(flag)

chevron_right


Output :

NO



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.