Created
February 20, 2017 11:23
-
-
Save localheinz/5ff9a31e190c66fa15ef5707a450578c to your computer and use it in GitHub Desktop.
Full Decks Solution (PHP)
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 | |
declare(strict_types=1); | |
final class FullDeck | |
{ | |
public function cards(): array | |
{ | |
static $cards; | |
if (null === $cards) { | |
$ranks = ['2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A']; | |
$suits = ['S', 'C', 'H', 'D']; | |
$cards = []; | |
foreach ($ranks as $rank) { | |
foreach ($suits as $suit) { | |
$cards[] = $rank . $suit; | |
} | |
} | |
} | |
return $cards; | |
} | |
} |
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 | |
declare(strict_types=1); | |
final class FullDecks | |
{ | |
/** | |
* @var FullDeck | |
*/ | |
private $fullDeck; | |
public function __construct(FullDeck $fullDeck) | |
{ | |
$this->fullDeck = $fullDeck; | |
} | |
/** | |
* @param string[] $cards | |
* | |
* @return int | |
*/ | |
public function __invoke(array $cards): int | |
{ | |
$count = 0; | |
while (\count($cards)) { | |
foreach ($this->fullDeck->cards() as $card) { | |
$key = \array_search( | |
$card, | |
$cards, | |
true | |
); | |
if (false === $key) { | |
return $count; | |
} | |
unset($cards[$key]); | |
} | |
++$count; | |
} | |
return $count; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment