Python | Check whether two lists follow same pattern or not (original) (raw)
Last Updated : 17 Sep, 2019
Given two lists
A
and
B
, check if they follow the same pattern or not. Condition for pattern matching:
- Ai > Aj, then Bi > Bj
- Ai = Aj, then Bi = Bj
- Ai < Aj, then Bi < Bj, for all i, j.
Example:
**Input:2 17 100 10 20 50Output:YESInput:5 7 10 33 10 50 10 45Output:**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.
Python3 1== `
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)
`
Output :
NO
Similar Reads
- Check if Two Lists Have Same Elements regardless of Order - Python We have multiple ways to check if two lists have the same elements, regardless of their order. The simplest way to check if two lists have the same elements is to sort both lists and then compare them. If both lists have the same elements in the same order after sorting, they are the same.Pythondef 2 min read
- Python program to check whether the values of a dictionary are in same order as in a list Given a dictionary, test if values are in order with list values. Input : test_dict = {"gfg" : 4, "is" : 10, "best" : 11}, sub_list = [4, 10, 11] Output : True Explanation : Values are 4, 10, 11, same as list order. Hence True is returned. Input : test_dict = {"gfg" : 4, "is" : 10, "best" : 11}, sub 6 min read
- Python - Check whether the given List forms Contiguous Distinct Sub-Array or Not You are given an array consisting of elements in the form A1, A2, A3.......An. The task is to find whether the array can be formed as a Contiguous Distinct Sub Array or Not. You need to find whether the array can be converted to contiguous sub-arrays that consist of similar elements and there are a 6 min read
- Python Program to check whether all elements in a string list are numeric Given a list that contains only string elements the task here is to write a Python program to check if all of them are numeric or not. If all are numeric return True otherwise, return False. Input : test_list = ["434", "823", "98", "74"] Output : True Explanation : All Strings are digits.Input : tes 5 min read
- Python - Filter tuple with all same elements Given List of tuples, filter tuples that have same values. Input : test_list = [(5, 6, 5, 5), (6, 6, 6), (9, 10)] Output : [(6, 6, 6)] Explanation : 1 tuple with same elements. Input : test_list = [(5, 6, 5, 5), (6, 5, 6), (9, 10)] Output : [] Explanation : No tuple with same elements. Method #1 : U 4 min read
- Python | Check if string matches regex list Sometimes, while working with Python, we can have a problem we have list of regex and we need to check a particular string matches any of the available regex in list. Let's discuss a way in which this task can be performed. Method : Using join regex + loop + re.match() This task can be performed usi 4 min read
- Python - How to Check if two lists are reverse equal Sometimes, while working with Python lists, we can have a problem in which we need to check if two lists are reverse of each other. This kind of problem can have application in many domains such as day-day programming and school programming. Let's discuss certain ways in which this task can be perfo 6 min read
- Make pair from two list such that elements are not same in pairs - Python We are given two lists, and our task is to create pairs where the elements in each pair must be different. For example, given a = [1, 2, 3] and b = [2, 3, 4], valid pairs would be [(1, 2), (1, 3), (1, 4), (2, 4), (3, 2), (3, 4)], ensuring that no pair has the same elements. Let's explore different m 3 min read
- Check if one dictionary is subset of other - Python Checking if one dictionary is a subset of another involves verifying whether all key-value pairs of the smaller dictionary exist in the larger dictionary with the same values. For example, given two dictionaries a = {'gfg': 1, 'is': 2, 'best': 3, 'for': 4, 'CS': 5} and b = {'gfg': 1, 'is': 2, 'best' 3 min read
- Python | Pair and combine nested list to tuple list Sometimes we need to convert between the data types, primarily due to the reason of feeding them to some function or output. This article solves a very particular problem of pairing like indices in list of lists and then construction of list of tuples of those pairs. Let's discuss how to achieve the 10 min read