I currently have a 2 dimensional array that has data like this :
X X X X
X X X X
X X X
X X X X
X X X X
X X X X X
X marks a cell that has data, blank means its empty.
I having been pulling my hair for the last hour trying to figure out how to count what I call “holes”.
It’s basically a cell with empty data between two cells having data.
So by order you can see the cols have respectively 2, 0, 2, 0, 0 holes from left to right.
My function needs to return the total holes, so for this case 4.
Currently I have done this really close but my function is counting the 2 first cells on the 4th col which is wrong and I can’t figure out how to account for that.
Here is my actual code :
public function countHoles(){
$total = 0;
for($i=0; $i<5; $i++){
$counting = false;
$passed = false;
for($j=0; $j<10; $j++){
if(count($this->table[$j][$i])>0){
$passed = true;
}
if($passed && !$counting && count($this->table[$j][$i])==0){
$counting = true;
}
else{
$counting = false;
}
if($passed && $counting){
$total++;
}
}
}
return $total;
}
Your help is appreciated.
I have an answer in javascript, try this:
‘hole’ is the var with the number of holes