Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Résolution et tests unitaire du challenge MONSTERS_3 avec les Collect…
…ions 4 objets créés : - FoodType (enum) - Monster - MonstersLines - MonstersLinesWC (Without Collection) Tests unitaires réalisés avec Pest + readme + composer.json pour intégrer le package collections
- Loading branch information
1 parent
4d3cf99
commit 475cb81
Showing
8 changed files
with
235 additions
and
2 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Challenges\MONSTERS_3; | ||
|
||
enum FoodType: string | ||
{ | ||
case FRUITS = 'F'; | ||
case GRASS = 'G'; | ||
case ROCK = 'R'; | ||
case WOOD = 'W'; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace Challenges\MONSTERS_3; | ||
|
||
class Monster | ||
{ | ||
public function __construct( | ||
public readonly string $name, | ||
public readonly FoodType $foodType, | ||
public readonly int $weight, | ||
) {} | ||
|
||
public static function createFromText(string $informations): Monster | ||
{ | ||
$data = explode(':', $informations); | ||
|
||
return new self( | ||
name: $data[0], | ||
foodType: FoodType::from($data[1]), | ||
weight: (int) $data[2] | ||
); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Challenges\MONSTERS_3; | ||
|
||
class MonstersLines | ||
{ | ||
/** | ||
* @var Monster[] | ||
*/ | ||
private array $monsters = []; | ||
|
||
public function addMonsterFromText(string $informations): void | ||
{ | ||
$this->monsters[] = Monster::createFromText($informations); | ||
} | ||
|
||
public function getMonstersFromFoodType(FoodType $foodType, int $number = 3): string | ||
{ | ||
// Gestion du tri, par défaut, dans l'ordre croissant, décroissant pour ROCK et WOOD | ||
$sort = 'sortBy'; | ||
if ($foodType === FoodType::ROCK || $foodType === FoodType::WOOD) { | ||
$sort = 'sortByDesc'; | ||
} | ||
|
||
return collect($this->monsters) | ||
->filter(function(Monster $item) use ($foodType) { | ||
return $item->foodType === $foodType; | ||
}) | ||
->$sort('weight') | ||
->slice(0, $number) | ||
->implode('name', '-'); | ||
} | ||
|
||
public function getAllFirstThree(): string | ||
{ | ||
return collect(FoodType::cases()) | ||
->map( fn(FoodType $foodType) => $this->getMonstersFromFoodType($foodType, 3) ) | ||
->implode('-'); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Challenges\MONSTERS_3; | ||
|
||
class MonstersLinesWC | ||
{ | ||
/** | ||
* @var Monster[] | ||
*/ | ||
private array $monsters = []; | ||
|
||
public function addMonsterFromText(string $informations): void | ||
{ | ||
$this->monsters[] = Monster::createFromText($informations); | ||
} | ||
|
||
public function getMonstersFromFoodType(FoodType $foodType, int $nbMonsters = 3): string | ||
{ | ||
// 1. Initialisation d'un tableau vide | ||
$monsters = []; | ||
|
||
// 2. Je filtre en selon le type de nourriture | ||
foreach ($this->monsters as $monster) { | ||
if ($monster->foodType === $foodType) { | ||
$monsters[] = $monster; | ||
} | ||
} | ||
|
||
// 3. Je trie dans un sens ou dans l'autre, selon le type de nourriture | ||
// grâce à la fonction usort, en comparant les poids | ||
if ($foodType === FoodType::FRUITS || $foodType === FoodType::GRASS) { | ||
usort($monsters, function (Monster $m1, Monster $m2) { | ||
return $m1->weight <=> $m2->weight; | ||
}); | ||
} else { | ||
// Sens inverse | ||
usort($monsters, function (Monster $m1, Monster $m2) { | ||
return $m2->weight <=> $m1->weight; | ||
}); | ||
} | ||
|
||
// 4. Je garde les N premiers monstres | ||
$monsters = array_slice($monsters, 0, $nbMonsters); | ||
|
||
// 5. J'extraie les noms des monstres | ||
$names = array_column($monsters, 'name'); | ||
|
||
// 6. Je lie les noms avec un "-" | ||
return implode('-', $names); | ||
} | ||
|
||
public function getAllFirstThree(): string | ||
{ | ||
// 0. Je précise l'ordre attendu | ||
$foodTypes = [ | ||
FoodType::FRUITS, | ||
FoodType::GRASS, | ||
FoodType::ROCK, | ||
FoodType::WOOD | ||
]; | ||
|
||
// 0. J'initialise un tableau vide en vue de mon implode final | ||
$result = []; | ||
|
||
// 1. Je parcours chaque type pour récupérer les 3 premiers monstres | ||
foreach ($foodTypes as $foodType) { | ||
$result[] = $this->getMonstersFromFoodType($foodType, 3); | ||
} | ||
|
||
// 2. Je lie les éléments avec un "-" | ||
return implode('-', $result); | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
use Challenges\MONSTERS_3\FoodType; | ||
use Challenges\MONSTERS_3\MonstersLines; | ||
use Challenges\MONSTERS_3\MonstersLinesWC; | ||
|
||
test('Tri des monstres FRUITS', function(string $className) { | ||
|
||
$monsters = ['Drex29:F:16', 'Traz65:W:74', 'Blit86:R:27', 'Brux18:F:83', 'Cobi82:R:72', 'Brux61:F:56', 'Draz68:G:97']; | ||
|
||
$monsterLine = new $className; | ||
|
||
foreach ($monsters as $monsterInformations) { | ||
$monsterLine->addMonsterFromText($monsterInformations); | ||
} | ||
|
||
expect($monsterLine->getMonstersFromFoodType(FoodType::FRUITS, 3))->toBe('Drex29-Brux61-Brux18'); | ||
})->with([ | ||
[MonstersLinesWC::class], | ||
[MonstersLines::class] | ||
]); | ||
|
||
|
||
test('Tri des monstres ROCK', function(string $className) { | ||
|
||
$monsters = ['Drex29:R:16', 'Traz65:W:74', 'Blit86:R:27', 'Brux18:F:83', 'Cobi82:R:72', 'Brux61:F:56', 'Draz68:G:97']; | ||
|
||
$monsterLine = new $className; | ||
|
||
foreach ($monsters as $monsterInformations) { | ||
$monsterLine->addMonsterFromText($monsterInformations); | ||
} | ||
|
||
expect($monsterLine->getMonstersFromFoodType(FoodType::ROCK, 3))->toBe('Cobi82-Blit86-Drex29'); | ||
})->with([ | ||
[MonstersLinesWC::class], | ||
[MonstersLines::class] | ||
]); | ||
|
||
test('Jeu de données complet', function (string $className) { | ||
|
||
// Données issues de Tainix directement -------- | ||
$monsters = ['Zela56:W:49', 'Fluz56:W:40', 'Cobi12:R:63', 'Truz57:F:69', 'Chir23:F:18', 'Moxa15:R:33', 'Vrip15:W:55', 'Moxa72:W:54', 'Trex43:W:23', 'Spro49:R:75', 'Spro80:G:36', 'Brop35:W:60', 'Draz89:R:15', 'Flix26:F:97', 'Plor65:F:21', 'Fero70:G:51', 'Gloz30:R:59', 'Flix61:G:24', 'Vlaz59:R:90', 'Drin34:R:64', 'Chir96:R:99', 'Moxa73:W:50', 'Zorp31:F:74', 'Fluz86:W:12', 'Vlox73:G:34']; | ||
$result = 'Chir23-Plor65-Truz57-Flix61-Vlox73-Spro80-Chir96-Vlaz59-Spro49-Brop35-Vrip15-Moxa72'; | ||
// --------------------------------------------- | ||
|
||
$monsterLine = new $className; | ||
|
||
foreach ($monsters as $monsterInformations) { | ||
$monsterLine->addMonsterFromText($monsterInformations); | ||
} | ||
|
||
expect($monsterLine->getAllFirstThree())->toBe($result); | ||
})->with([ | ||
[MonstersLinesWC::class], | ||
[MonstersLines::class] | ||
]); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
use Challenges\MONSTERS_3\FoodType; | ||
use Challenges\MONSTERS_3\Monster; | ||
|
||
test('Parsing et création d\'un Monster', function() { | ||
|
||
$informations = 'Luxo18:F:33'; | ||
|
||
$monster = Monster::createFromText($informations); | ||
|
||
expect($monster->name)->toBe('Luxo18'); | ||
expect($monster->foodType)->toBe(FoodType::FRUITS); | ||
expect($monster->weight)->toBe(33); | ||
}); |