Last active
August 29, 2015 14:19
-
-
Save gmsantos/a40d46a02b3ea2027728 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 | |
/** | |
* Description of Ovni | |
* | |
* @author Gabriel Machado | |
*/ | |
class Ovni { | |
protected $cometaGrupo = []; | |
public function __construct($cometaGrupo) { | |
$this->cometaGrupo = $cometaGrupo; | |
} | |
public function analisarGrupos() { | |
foreach ($this->cometaGrupo as $cometa => $grupo) { | |
if ($this->restoDiv45($cometa) === $this->restoDiv45($grupo)){ | |
echo "Grupo $grupo, all aboard!!" . PHP_EOL; | |
} else { | |
echo "Grupo $grupo nao sera levado" . PHP_EOL; | |
} | |
} | |
} | |
private function restoDiv45($palavra) { | |
return $this->calcularProduto($palavra) % 45; | |
} | |
private function calcularProduto($palavra) { | |
$produto = 1; | |
$letras = str_split($palavra); | |
foreach ($letras as $letra){ | |
$produto *= $this->ascii2number($letra); | |
} | |
return $produto; | |
} | |
private function ascii2number($letra) { | |
return ord(strtoupper($letra)) - 64; | |
} | |
} | |
$lista = [ | |
'Halley' => 'Amarelo', | |
'Encke' => 'Vermelho', | |
'Wolf' => 'Preto', | |
'Kushida' => 'Azul' | |
]; | |
$ovni = new Ovni($lista); | |
$ovni->analisarGrupos(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment