Subject: Eat what you will and still see results

From: Hello Holidays <[email-masked]>
Date: Mon, 17 Nov 2014 07:53:18 -0800

If you're feeling lazy like most of us, perhaps you will find this interesting. perhaps I can provide a bit of significant advice that might make a big difference for you this Winter.
http://www.yourinbettershapenow.com/string/petunias/baizes/unconcerned/denounces/patienter.xhtml

Heck, this even works in your sleep, believe it or not.

Thanks
Linda

-From: Broadcast-BGB Partners. 74 Lancaster Road
Little Rock AR 7 2 2 0 9 Alternatively you can this http://www.yourinbettershapenow.com/weevil/jakes/udders/promptly/sedimentation/graduate.jsp

Have a good one.

6/3/5/7/3/234/6ee73f1d1cc178352379b387d83bc86cb-##~~4435
^^^^645345345345.h.84564a864643f-5490-4a9d-b0c7-bffb4b3da60d

For instance, I have lists:

a[0] = [1, 1, 1, 0, 0]
a[1] = [1, 1, 0, 0, 1]
a[2] = [0, 1, 1, 1, 0]
# and so on
They seem to be different, but if it is supposed that the start and the end are connected, then they are circularly identical.

The problem is, each list which I have has a length of 55 and contains only three ones and 52 zeros in it. Without circular condition, there are 26,235 (55 choose 3) lists. However, if the condition 'circular' exists, there are a huge number of circularly identical lists

Currently I check circularly identity by following:

def is_dup(a, b):
    for i in range(len(a)):
        if a == list(numpy.roll(b, i)): # shift b circularly by i
            return True
    return False
This function requires 55 cyclic shift operations at the worst case. And there are 26,235 lists to be compared with each other. In short, I need 55 * 26,235 * (26,235 - 1) / 2 = 18,926,847,225 computations. It's about nearly 20 Giga!

Is there any good way to do it with less computations? Or any data types that supports circular?

python algorithm
share|improve this question
edited 3 hours ago

asked 9 hours ago

Jeon
32719
  	 	
Just a hunch : I feel that suffix trees might help here. en.wikipedia.org/wiki/Suffix_tree . To build one, see en.wikipedia.org/wiki/Ukkonen%27s_algorithm   Rerito 9 hours ago
  	 	
The ones are in a row? Adjacent indices?   boh 5 hours ago
add a comment
6 Answers
activeoldestvotes
up vote
21
down vote
First of of this can be done in O(n) in terms of the length of the list You can notice that if you will duplicate your list 2 times ([1, 2, 3]) will be [1, 2, 3, 1, 2, 3] then your new list will definitely hold all possible cyclic lists.

So all you need is to check whether your looking list is inside a 2 times of your starting list. In python you can achieve this in the following way (assuming that the lengths are the same).

list1 = [1, 1, 1, 0, 0]
list2 = [1, 1, 0, 0, 1]
print ' '.join(map(str, list2)) in ' '.join(map(str, list1 * 2))
Some explanation about my oneliner: list * 2 will combine a list with itself, map(str, [1, 2]) convert all numbers to string and  ' '.join() will convert array ['1', '2', '111'] into a string '1 2 111' thus Alexes concern about false positives will not hold.

As pointed by some people in the comments, oneliner can potentially give some false positives, so to cover all the possible edge cases:

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
P.S. when speaking about time complexity, it is worth noticing that O(n) will be achieved if substring can be found in O(n) time. It is not always so and depends on the implementation in your language (although potentially it can be done in linear time KMP for example)

share|improve this answer
edited 3 hours ago

answered 9 hours ago

Salvador Dali
14.5k1375126
3	 	
Oh that's a great idea!!! Though if you have data other than 0s and 1s you could have false positives. Imagine list = [1, 111] and list2 = [1, 11]   Adam Smith 9 hours ago 
2	 	
@AdamSmith no, it will work with all the inputs, positive negative, floats. Anything. As you see, I am using ' ' to put space there as well.   Salvador Dali 9 hours ago 
2	 	
this solution needs the two lists to be of the same length. if l1 = [1,2,3], l2 = [1,2,3,1], then there is a false positive   gefei 9 hours ago
2	 	
Just adding padding spaces (1 before and 1 after each string) will do the trick. No need to overcomplicate things with regexes. (Of course I'm assuming we compare lists of the same length)   Rerito 9 hours ago 
1	 	
@AshwiniChaudhary or using string formatting to adjust each element to the same length. I'm writing an answer   Adam Smith 9 hours ago
show 13 more comments
up vote
4
down vote
Repeat the first array, then use the Z algorithm (O(n) time) to find the second array inside the first.

share|improve this answer