Change language

Python | Check if two lists match in a circle

| | |

Examples :

` Input: list1 = [10, 10, 0, 0, 10] list2 = [10, 10, 10, 0, 0] Output: Yes Explanation: yes they are circularly identical as when we write the list1 last index to second last index, then we find it is circularly same with list1 Input: list1 = [10, 10, 10, 0, 0] list2 = [1, 10, 10, 0, 0] Output: No `

Method 1. Using list traversal

Using traversing , we have to double the given list. Check for any x (0 "= n) for any x + n and compare with list2 to see if list1 and list2 are the same, if both are the same then list2 is cyclically identical. Check this property using two loops. The first loop will run from 0 to len (list1) and then check if the index (x to x + n) is the same as list2, if yes then returns true, otherwise returns false.

Python implementation of the above approach:

 ` # Python program to check if two ` ` # lists circularly identical ` ` # using a workaround `   ` # function to check circular identity or not ` ` def ` ` circularly_identical (list1, list2): `   ` # doubling list ` ` list3 ` = ` list1 ` ` * ` ` 2 `   ` # crawl list twice1 ` ` for ` ` x ` ` in ` ` range ` ` (` ` 0 ` `, ` ` len ` ` (list1)): ` ` z ` ` = ` ` 0 `    ` # check if list2 == list1 briefly ` ` for ` ` y ` ` in ` ` range ` ` (x, x ` ` + ` ` len ` ` (list1)): ` ` if ` ` list2 [z] ` ` = ` ` = ` ` list3 [y]: ` ` z ` ` + ` ` = ` ` 1 ` ` else ` `: ` ` break `   ` # if all n elements are the same in a circle ` ` if ` ` z ` ` = ` ` = ` ` len ` ` (list1): ` ` return ` ` True `    ` return ` ` False ` ` `  ` `  ` `  ` # driver code ` ` list1 ` ` = ` ` [` ` 10 ` `, ` ` 10 ` `, ` ` 0 ` `, ` ` 0 ` `, ` ` 10 ] `` list2 = [ 10 , 10 , 10 , 0 , 0 ] list3 = [ 1 , 10 , 10 , 0 , 0 ]     # check list 1 and list 2 if < / code> (circularly_identical (list1, list2)): print " Yes "  else : print "No"   # check list 2 and list 3 if (circularly_identical (list2, list3)): print " Yes "  else : print "No"  `

Exit :

` Yes No `

C tutorial 1: using the map () function

Using the Python built-in function map (), we can do this in one step where we need to map list2 to a string and then see if it exists when mapping a double from list1 (2 * list1) to on another line.

Below is the Python implementation of the above approach:

 ` # Python program to check if two ` ` # the lists are circularly identical ` ` # using the map function `   ` # function to check circular identity or not ` ` def ` ` circularly_identical (list1, list2): ` ` `  ` ` ` return ` ` ( ` ` ’’ ` `. join (` ` map ` ` (` ` str ` `, list2)) ` ` in ` ` ’’ ` `. join (` ` map ` ` (` ` str ` `, list1 ` ` * ` ` 2 ` `))) `     ` # driver code ` ` list1 ` ` = ` ` [` ` 10 ` ` , ` ` 10 ` `, < / code> 0 , 0 , 10 ] `` list2 = [ 10 , 10 , 10 , 0 , 0 ] list3 = [ 1 , 10 , 10 , 0 , 0 ]      # check list 1 and list 2 if (circularly_identical (list1, list2)): print "Yes"   else : print "No"   # check list 2 and list 3 if (circularly_identical (list2, list3)): print "Yes"   else : `` print < code class = "string"> "No" `

Exit:

` Yes No `