<?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