Message Boards Message Boards

Recognize number string patterns?

GROUPS:

Hey everyone,

I've been wondering if there is a good way to find patterns in Mathematica. I'm aware of the existence of the possibility to use string patterns and related functions to work with patterns, but those don't seem to be the best choice for finding patterns in number strings.

I would love to find the 'underlying' pattern in a number string without knowing a priori what the pattern is myself. Let me make an example: Given the following string of numbers: 01100110011020110, I would like to use a implemented function to get, that the 'underlying' pattern is 0110, because it always repeats itself and the recurrence is only affected by the occuring 2.

Does anyone of you of a function, would allow something like this or could be used for this?

Best, Robert

POSTED BY: Robert Marks
Answer
26 days ago

Ok, here's one attempt. This function looks for patterns (with specified minimum an maximum length) repeated a minimum/maximum number of times:

findRepeatedPatterns[
   string_String,
   minMaxSequenceLength_List,
   minMaxRepititions_List
   ] := findRepeatedPatterns[
   Characters@string,
   minMaxSequenceLength,
   minMaxRepititions
];
findRepeatedPatterns[
  list_List,
  minMaxSequenceLength_List,
  minMaxRepititions_List
] := DeleteDuplicates[
  ReplaceList[
   list,
   {
     Repeated[
      PatternSequence[___, x : Repeated[_, minMaxSequenceLength], ___],
      minMaxRepititions
     ]
   } :> {x}
  ]
];

Look for patterns of at least length 3 repeated at least 3 times:

findRepeatedPatterns["01100110011020110", {3, \[Infinity]}, {3, \[Infinity]}]
Out[2] = {{"0", "1", "1"}, {"0", "1", "1", "0"}, {"1", "1", "0"}}
POSTED BY: Sjoerd Smit
Answer
26 days ago

I discuss this problem in details in the following post:

POSTED BY: Alexey Popkov
Answer
10 days ago

Group Abstract Group Abstract