Created
November 7, 2018 18:22
-
-
Save lahaxearnaud/46b42fb4c685226a9d5bf4b149ce7f9c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Services; | |
use Phpml\Math\Distance; | |
class CocktailsDistance implements Distance | |
{ | |
/** | |
* @param array $a | |
* @param array $b | |
* | |
* @return float | |
*/ | |
public function distance(array $a, array $b):float | |
{ | |
// un sans et un avec alcool ? | |
$distance = array_shift($a) !== array_shift($b)?10:0; | |
$count = count($a); | |
for ($i = 0; $i < $count; ++$i) { | |
// +2 de distance par différence de nombre d'ingrédient | |
if ($a[$i] === 0 && $b[$i] !== 0) { | |
$distance += 2; | |
continue; | |
} | |
// +3 si un ingrédient de a n'est pas présent dans le cocktail b | |
$distance += in_array($a[$i], $b, true) ? 0 : 3; | |
} | |
return $distance; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment