Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding the recurring pattern

Let's say I have a number with a recurring pattern, i.e. there exists a string of digits that repeat themselves in order to make the number in question. For example, such a number might be 1234123412341234, created by repeating the digits 1234.
What I would like to do, is find the pattern that repeats itself to create the number. Therefore, given 1234123412341234, I would like to compute 1234 (and maybe 4, to indicate that 1234 is repeated 4 times to create 1234123412341234)

I know that I could do this:

def findPattern(num):
    num = str(num)
    for i in range(len(num)):
        patt = num[:i]
        if (len(num)/len(patt))%1:
            continue
        if pat*(len(num)//len(patt)):
            return patt, len(num)//len(patt)

However, this seems a little too hacky. I figured I could use itertools.cycle to compare two cycles for equality, which doesn't really pan out:

In [25]: c1 = itertools.cycle(list(range(4)))

In [26]: c2 = itertools.cycle(list(range(4)))

In [27]: c1==c2
Out[27]: False

Is there a better way to compute this? (I'd be open to a regex, but I have no idea how to apply it there, which is why I didn't include it in my attempts)

EDIT:

  1. I don't necessarily know that the number has a repeating pattern, so I have to return None if there isn't one.
  2. Right now, I'm only concerned with detecting numbers/strings that are made up entirely of a repeating pattern. However, later on, I'll likely also be interested in finding patterns that start after a few characters:

magic_function(78961234123412341234)

would return 1234 as the pattern, 4 as the number of times it is repeated, and 4 as the first index in the input where the pattern first presents itself

like image 435
inspectorG4dget Avatar asked Nov 02 '14 20:11

inspectorG4dget


1 Answers

(.+?)\1+

Try this. Grab the capture. See demo.

import re
p = re.compile(ur'(.+?)\1+')
test_str = u"1234123412341234"

re.findall(p, test_str)

Add anchors and flag Multiline if you want the regex to fail on 12341234123123, which should return None.

^(.+?)\1+$

See demo.

like image 58
vks Avatar answered Sep 26 '22 08:09

vks