Finding the recurring pattern

你离开我真会死。 提交于 2019-12-01 04:11:45

问题


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


回答1:


(.+?)\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.




回答2:


One way to find a recurring pattern and number of times repeated is to use this pattern:

(.+?)(?=\1+$|$)  

w/ g option.
It will return the repeated pattern and number of matches (times repeated)
Non-repeated patterns (fails) will return only "1" match
Repeated patterns will return 2 or more matches (number of times repeated).
Demo



来源:https://stackoverflow.com/questions/26703839/finding-the-recurring-pattern

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!