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:
- I don't necessarily know that the number has a repeating pattern, so I have to return
None
if there isn't one. - 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
Try this. Grab the capture. See demo.
Add anchors and flag
Multiline
if you want the regex to fail on12341234123123
, which should returnNone
.See demo.
One way to find a recurring pattern and number of times repeated is to use this pattern:
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