Test algo PHP pour candidature stage
Salut tout le monde,
j'ai postulé dans une boîte pour faire du Web et ils m'ont demandé de faire quelques "fonctionnalités" en guise de test. A priori j'ai pas eu trop de problème à les réaliser (j'ai testé ça a l'air de marcher) mais bon j'aimerais votre avis, si possible, pour savoir si c'est optimal, ou si j'ai pas oublié quelque chose... je tiens pas mal à ce stage donc j'aimerais leur envoyer un truc parfait :ccool:
Voici le lien du test :
http://pdfcast.org/pdf/test-php
Ce que j'ai fait pour l'étape 1 :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| <?php
$bestBrands = array(
"Adidas" => 153,
"Converse" => 193,
"Nike" => 124,
"Asics" => 67,
);
function randomBrand($bestBrands)
{
// Vérification que largument soit bien un tableau
if(!is_array($bestBrands))
{
// Si ce nest as le cas, on lève une exception
throw new Exception('Argument non-compatible.');
}
else
{
// Initialisation de la marque qui sera retourné par la fonction
$randomBrand = '';
// Récupération du nombre total de voix pour le sondage
$totalVoices = array_sum($bestBrands);
// Generation dun nombre aléatoire entre 0 et le nombre de voix
$rand = rand(0, $totalVoices);
// Algorithme de recuperation de la marque tirée aléatoirement
$iterator = 0 ;
foreach($bestBrands as $name => $value) {
if ($rand <= $iterator+$value) {
$randomBrand = $name;
break;
}
$iterator+=$value;
}
}
return $randomBrand;
}
try {
$randomBrand = randomBrand($bestBrands);
echo $randomBrand;
} catch (Exception $e) {
echo 'Exception reçue : ', $e->getMessage(), "\n";
}
?> |
Ce que j'ai fait pour l'etape 2:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| <?php
$bestBrands = array(
"Adidas" => 153,
"Converse" => 193,
"Nike" => 124,
"Asics" => 67,
);
function randomBrand($bestBrands)
{
// Vérification que largument soit bien un tableau
if(!is_array($bestBrands))
{
// Si ce nest as le cas, on lève une exception
throw new Exception('Argument non-compatible.');
}
else
{
// Initialisation de la marque qui sera retourné par la fonction
$randomBrand = '';
// Récupération du nombre total de voix pour le sondage
$totalVoices = array_sum($bestBrands);
// Generation dun nombre aléatoire entre 0 et le nombre de voix
$rand = rand(0, $totalVoices);
// Algorithme de recuperation de la marque tirée aléatoirement
$iterator = 0 ;
foreach($bestBrands as $name => $value) {
if ($rand <= $iterator+$value) {
$randomBrand = $name;
break;
}
$iterator+=$value;
}
}
return $randomBrand;
}
function randomList($bestBrands)
{
// Initialisation de la liste à retourner
$randomList = array();
// Initialisation de la liste des marques réduite à chaque tirage
$temporaryList = $bestBrands;
// Vérification que largument soit bien un tableau
if(!is_array($bestBrands))
{
// Si ce nest as le cas, on lève une exception
throw new Exception('Argument non-compatible.');
}
else
{
while(count($temporaryList)>0) {
// Appel de la fonction randomBrand sur la liste
$randomBrand = randomBrand($temporaryList);
// Ajout dans la liste à retourner
array_push($randomList, $randomBrand);
// Suppression de lélement dans la liste temporaire
unset($temporaryList[$randomBrand]);
}
}
return $randomList;
}
try {
$randomList = randomList($bestBrands);
var_dump($randomList);
} catch (Exception $e) {
echo 'Exception reçue : ', $e->getMessage(), "\n";
}
?> |
Un enorme merci d'avance pour votre aide :oops: