Message Boards Message Boards

GROUPS:

Recognize number string patterns?

Posted 5 months ago
510 Views
|
2 Replies
|
3 Total Likes
|

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

2 Replies

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"}}

I discuss this problem in details in the following post:

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract