dimanche 30 avril 2017

Effeciently test ifany three of four numbers are equal?

I am trying to determine a quick way to select sets of numbers (they'll be in arrays). I only need to keep sets where two or more of the numbers are greater than zero; therefore, sets that include zero three times can be discarded. I have come up with the following code but it doesn't have the expected outcomes, any advice would be greatly appreciated.

<?php
function testFour($a, $b, $c, $d)
{
  if(($a + $b + $c + $d) == ($a || $b || $c || $d)) {
      echo $a.", ".$b.", ".$c.", ".$d." => exclude<br>";
    } else {
      echo $a.", ".$b.", ".$c.", ".$d." => keep<br>";
    }
}
echo "<pre>";
testFour(0,0,0,5); // true
testFour(1,0,0,5); // false
testFour(0,4,2,0); // false
testFour(1,1,0,5); // false
testFour(0,2,0,0); // true
testFour(2,3,0,0); // false
echo "</pre>";

Aucun commentaire:

Enregistrer un commentaire