Anyone know what this function doing ?

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
kcp88
Forum Newbie
Posts: 8
Joined: Mon Aug 22, 2011 11:51 pm

Anyone know what this function doing ?

Post by kcp88 »

Anyone know what this function doing ? Can explain to me ? :(

Code: Select all

function countMaxOcc($tpp, $s){
   $t=0;$c=0;$p=true;
   foreach($tpp as $n){
      if($n==$s){
         $c=($p)?$c+1:1;
         $p=true;
      }else{
         $t=($c>$t)?$c:$t;
         $p=false;
      }
   }
   return $t;
}
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Anyone know what this function doing ?

Post by social_experiment »

Code: Select all

<?php
function countMaxOcc($tpp, $s){
   // set variables
   $t=0;$c=0;$p=true;
   // $tpp should be an array, foreach loops through each
   // value in an array
   foreach($tpp as $n){
      // if the value $n is equal to the value of $s
      if($n==$s){
          // if $p is true, $c is incremented by 1, if $p is false, $c = 1
         $c=($p)?$c+1:1;
         // set $p to true
         $p=true;
      // if $n is not equal to $s
      }else{
         // if $c is greater than $t, $t is equal to the value of $c, if not, $t's value
         // is equal to the value in $t.
         $t=($c>$t)?$c:$t;
         // $p is set to false
         $p=false;
      }
   }
   // return the value of $t
   return $t;
}
?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Anyone know what this function doing ?

Post by Jonah Bron »

In English, it counts the number of times a given value occurs consecutively in an array.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Anyone know what this function doing ?

Post by Jonah Bron »

And it could be better written as this:

Code: Select all

function countMaxOcc($list, $value) {
    $finalMax = 0;
    $currentMax = 0;
    foreach ($list as $listItem) {
        if ($listItem == $value) {
            $finalMax = max($finalMax, ++$currentMax);
        } else {
            $currentMax = 0;
        }
    return $finalMax;
}
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Anyone know what this function doing ?

Post by social_experiment »

Maybe add a check to see if $list is an array?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Anyone know what this function doing ?

Post by Benjamin »

Here's another way:

Code: Select all

function countMaxOcc($array, $value) {
    $n = array_count_values($array);
    return array_key_exists($value, $n) ? $n[$value] : 0;
}
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Anyone know what this function doing ?

Post by Jonah Bron »

I should have seen that one coming :)
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Anyone know what this function doing ?

Post by Benjamin »

Yeah.. I hear that. :drunk:
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Anyone know what this function doing ?

Post by Benjamin »

Actually, what I wrote won't work. I thought it was counting the total occurrences, when in fact it's counting consecutive occurrences.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Anyone know what this function doing ?

Post by Jonah Bron »

Yeah, that's what it's supposed to do. You're so good, you wrote the right thing and didn't even know it :)
Post Reply