2

I have an array that has the following values: 1,1,3,5,1,1,3,5,1,1,3,5,1,1,3,5

You can easily look at this array and see that the pattern 1,1,3,5 repeats 4 times.
How would I make PHP figure this out for me?
Furthermore, my array may have more than one pattern that would need to be found.
For example, if the array were: 1,2,1,2,1,2,4,5,5,5
I would need to get output like "The pattern 1,2 repeats 3 times 4 repeats 1 time and 5 repeats 3 times."
Ultimately what I want to be able to do is upload a CSV file and parse it so that it is read in plain English.

2
  • 7
    Smells of homework. Also, figure it out. Write some pseudocode. Make a flowchart. Read the StackOverflow FAQ - you have no example code! Commented Jan 26, 2012 at 2:39
  • Well, how are you defining your "pattern"? I assume at least two consecutive elements that occurs at least twice? Commented Jan 26, 2012 at 2:40

4 Answers 4

1

Here's what I got:

$arr=array(1,1,3,5,1,1,3,5,1,1,3,5,1,1,3,5);
$p=array();
for($i=0;$i<count($arr);$i++){
    $tmp=$arr[$i].'';
    for($j=$i;$j<count($arr);$j++){
        $tmp.=','.$arr[$j];
        if(isset($p[$tmp])){
            //nope
        }
        else{
            //nice try
        }
    }

}
foreach($p as $key=>$val){
    if($val>1)
    echo "The patter: $key appeared $val times<br>";
}

Well not verbatim. I may have removed two lines and a total of 4 other characters. Good luck finding out what!

Sign up to request clarification or add additional context in comments.

Comments

0

I would take the array and cycle it taking the first n values from it, and look for them in the array, then storing the number of consecutive occurrences, incrementing n and doing it again. So it would take 1 and the occurrences would be one. Then it would take 1,1 and still one. Then 1,1,3 and it would still be one. Then 1,1,3,5 and it's four. At this point it'd keep on taking 1,1,3,5,1 and look for it, giving again one. Since this is less than the last found (four), it would stop, take back the last pattern, store it in another array with the number of repetition and then shift from the array all the repetition of that pattern, the start over again. This is the concept, implementing it, well, I've to work on it. :P

Comments

0

You should implement and modified the algorithm called the turtle and the hare, which give the possibility to detect cycle (http://en.wikipedia.org/wiki/Cycle_detection). By modifying it you could be able to count repetitions of different patterns.

You could also try to generate all sub-sequences of your string representing the concatenation of your array's elements but it is not optimized at all (as soon as the length of your array is high)..

Comments

0

This may help to find all similar patterns.

$arr = array(1,1,3,5,1,1,3,5,1,1,3,5,1,1,3,5,1,1,3,5);
$data = implode('', $arr);  
for($i=0; $i < count($arr)-1; $i++){
$pattern .= $arr[$i];
if (substr_count($data ,$pattern) !=1)
echo $pattern . ' found '.substr_count($data ,$pattern). ' times<br />';
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.