Page 1 of 1

What this function doing ?

Posted: Tue Aug 23, 2011 12:03 am
by kcp88
What this function doing ? Can anyone explain to me ? I'm newbie in PHP . :( Thanks first !!

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

Re: What this function doing ?

Posted: Tue Aug 23, 2011 2:16 am
by twinedev
(Disclaimer, I'm pretty tired right now, so I could of missed something... ;-)

Looks to be a function that will go through an array, and return the maximum number of times that what you are searching for occur next to each other. (see my final notes though...)

ie, passed the array:
c,a,b,b,b,d,a,e,b,b,e,e,f,g,b,b,b,b,b,b,b,b,e,a,c,b,b,b,d,d,c,c

and you pass it you are checking 'b', you would get 8 for the group where 'b' was 8 times in a row.

Here is the same function, written to explain more:

Code: Select all

function countMaxOcc($arySearch, $strMatch) {
	$intMaxTotal = 0; // ($t) contains the highest count found
	$intCurCount = 0; // ($c) Conatins the current count for this matching group
	$bInGroup = TRUE; // ($p) Says you are current in a group (ie, last item also matched)

	foreach($arySearch as $strItem) {
		if ($strItem == $strMatch) {
			// This item matched what we are looking for

			if ($bInGroup) { // ie. did the last item also match?
				$intCurCount++;
			}
			else { // ie. Last item didn't match, so we start at 1
				$intCurCount = 1;
			}

			// Lets the next loop know we matched here, so keep increasing $intCurCount
			$bInGroup = TRUE; 
		}
		else {
			// This item doesn't match what we are looking for

			if ($intCurCount > $intMaxTotal) {
				// The last group's total was higher than any other so far
				$intMaxTotal = $intCurCount;
			}
			else {
				// As a broken out IF, this really isn't needed, but put in to equate other code
				$intMaxTotal = $intMaxTotal;
			}

			// Lets the next loop know the this one didn't match (so $intCurCount restarts at 1)
			$bInGroup = FALSE; 
			
		}
	}

	return $intMaxTotal;
}
My final notes, if I am correct at what this function is supposed to be doing, it is doing it WRONG. The way it is coded, if you pass it this:

a,b,b,b,c,d,e,b,b,b,b,a,c,d,e,b,b,b,b,b,b,b

What do you think you would get? If you are looking for the maximum number of times B occurs in a row, the answer should be 7 for the last group. However the way it is programmed, you would actually get 4, because the loop ends on a match, so it never hits the code to assign $c ($intCurCount in my version) to $t ($intMaxTotal) before exiting the loop and returning it.

-Greg

Re: What this function doing ?

Posted: Tue Aug 23, 2011 5:22 am
by kcp88
Thanks a lot ! :) I think it is correct after i run several tries . BTW I don't understand what is the "?" and "1:1" doing ... , as in below code .

Code: Select all

$c=($p)?$c+1:1;

Re: What this function doing ?

Posted: Tue Aug 23, 2011 9:09 am
by Celauran
kcp88 wrote:Thanks a lot ! :) I think it is correct after i run several tries . BTW I don't understand what is the "?" and "1:1" doing ... , as in below code .

Code: Select all

$c=($p)?$c+1:1;
It's a ternary operator. Could also be written as such:

Code: Select all

if ($p)
{
   $c += 1;
}
else
{
    $c = 1;
}