How do I find out if the two lists are the same when they are represented as circular lists?
a[0] = [1, 1, 1, 0, 0]
a[1] = [1, 1, 0, 0, 1]
a[2] = [0, 1, 1, 1, 0]
These three are not the same list when viewed from each index, but they are all the same when viewed from a circular list.
What comes in as input can be the same list or a different list, but I don't know how to distinguish it.
What I'm thinking is
I wrote pop-append in 2 because numpy.roll() said it would do it for me.
The list length is about 1000, so it takes a long time just to return False <
def is_dup(a, b):
for i in range(len(a)):
if a == list(numpy.roll(b, i)):
return True
return False
I looked it up and they said this is how it works. If the length is the same and the content that connects one side is on the other side, it can be judged as circular.For example, add two a's to make it look like this.
1*11001*1100
You just have to see if there's a b in it's The code is as follows.
a = [1, 1, 1, 0, 0]
b = [1, 1, 0, 0, 1]
c = [0, 1, 1, 1, 0]
def isCircular(arr1, arr2):
if len(arr1) != len(arr2):
return False
str1 = ' '.join(map(str, arr1))
str2 = ' '.join(map(str, arr2))
if len(str1) != len(str2):
return False
return str1 in str2 + ' ' + str2
print(isCircular(a,b))
print(isCircular(b,c))
© 2024 OneMinuteCode. All rights reserved.