Skip to content

Instantly share code, notes, and snippets.

@padawin
Last active August 29, 2015 14:04
Show Gist options
  • Save padawin/35eb92698a6215707614 to your computer and use it in GitHub Desktop.
Save padawin/35eb92698a6215707614 to your computer and use it in GitHub Desktop.
Missing cards in deck
<?php
function findMissingCards($deck)
{
// The order is not the actual order of the cards in the deck, but the alpha order
// because I didn't implement the sort method here, but used a native one
$colors = array('C', 'P', 'Q', 'T');
$values = array(2, 3, 4, 5, 6, 7, 8, 9, 10, 'V', 'D', 'R', 'A');
$missingCards = array();
if (is_string($deck)) {
$deck = explode(' ', $deck);
}
else if (!is_array($deck)) {
throw new InvalidArgumentException("invalid deck value");
}
$deck = array_flip($deck);
foreach ($colors as $color) {
foreach ($values as $value) {
if (!isset($deck[$color . $value])) {
$missingCards[] = $color . $value;
}
}
}
// custom sort to do
sort($missingCards);
return $missingCards;
}
$deck = array(
"C2", "C3", "C4", "C8", "C9", "CV", "CD", "CR", "CA",
"P2", "P3", "P4", "P5", "P6", "P8", "P9","PV", "PD", "PR", "PA",
"Q2", "Q3", "Q4", "Q5", "Q6", "Q7", "Q8", "QV", "QD", "QR", "QA",
"T2", "T3", "T9", "T10", "TV", "TD", "TR", "TA"
);
$missing = findMissingCards($deck);
var_dump(count($missing), count($deck), count($missing) + count($deck));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment