Created
August 30, 2017 13:24
-
-
Save tioback/932c27bb9a0b5d502b6a63780bd52926 to your computer and use it in GitHub Desktop.
Full Deck Counter
This file contains hidden or 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
package performer.oss.usecases.oss_014; | |
import java.util.*; | |
/** | |
* 2, 3, 4, 5, 6, 7, 8, 9, T, J, Q, K, A | |
The four suits are: | |
S - Spade (♠), C - Club(♣), H - Heart(♥), D - Diamond(♦) | |
The cards are given using their rank followed by their suit: | |
2 of Spades: 2S | |
Ace of Clubs: AC | |
10 of Hearts: TH | |
Write a function that will accept an array of cards and return the number of full decks contained in the array. | |
Examples: | |
a) ["9C", "KS", "AC", "AH", "8D", "4C", "KD", "JC", "7D", "9D", "2H", "7C", "3C", "7S", "5C", "6H", "TH"] -> 0 | |
b) ["2S", "2C", "2D", "2H", "3S", "3C", "3D", "3H", "4S", "4C", "4D", "4H", "5S", "5C", "5D", "5H", "6S", "6C", "6D", "6H", "7S", "7C", "7D", "7H", "8S", "8C", "8D", "8H", "9S", "9C", "9D", "9H", "TS", "TC", "TD", "TH", "JS", "JC", "JD", "JH", "QS", "QC", "QD", "QH", "KS", "KC", "KD", "KH", "AS", "AC", "AD", "AH", "2S", "2C", "2D", "2H", "3S", "3C", "3D", "3H", "4S", "4C", "4D", "4H", "5S", "5C", "5D", "5H", "6S", "6C", "6D", "6H", "7S", "7C", "7D", "7H", "8S", "8C", "8D", "8H", "9S", "9C", "9D", "9H", "TS", "TC", "TD", "TH", "JS", "JC", "JD", "JH", "QS", "QC", "QD", "QH", "KS", "KC", "KD", "KH", "AS", "AC", "AD", "AH", "2S", "2C", "2D", "2H", "3S", "3C", "3D", "3H", "4S", "4C", "4D", "4H", "5S", "5C", "5D", "5H", "6S", "6C", "6D", "6H", "7S", "7C", "7D", "7H", "8S", "8C", "8D", "8H", "9S", "9C", "9D", "9H", "TS", "TC", "TD", "TH", "JS", "JC", "JD", "JH", "QS", "QC", "QD", "QH", "KS", "KC", "KD", "KH", "AS", "AC", "AD"] -> 2 | |
*/ | |
public class DeckCounter { | |
static final List<String> CARDS = Arrays.asList("2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A"); | |
static final List<String> SUITS = Arrays.asList("S", "C", "H", "D"); | |
public static void main(String[] args) { | |
String[] test1 = {"9C", "KS", "AC", "AH", "8D", "4C", "KD", "JC", "7D", "9D", "2H", "7C", "3C", "7S", "5C", "6H", "TH"}; | |
String[] test2 = {"2S", "2C", "2D", "2H", "3S", "3C", "3D", "3H", "4S", "4C", "4D", "4H", "5S", "5C", "5D", "5H", "6S", "6C", "6D", "6H", "7S", "7C", "7D", "7H", "8S", "8C", "8D", "8H", "9S", "9C", "9D", "9H", "TS", "TC", "TD", "TH", "JS", "JC", "JD", "JH", "QS", "QC", "QD", "QH", "KS", "KC", "KD", "KH", "AS", "AC", "AD", "AH", "2S", "2C", "2D", "2H", "3S", "3C", "3D", "3H", "4S", "4C", "4D", "4H", "5S", "5C", "5D", "5H", "6S", "6C", "6D", "6H", "7S", "7C", "7D", "7H", "8S", "8C", "8D", "8H", "9S", "9C", "9D", "9H", "TS", "TC", "TD", "TH", "JS", "JC", "JD", "JH", "QS", "QC", "QD", "QH", "KS", "KC", "KD", "KH", "AS", "AC", "AD", "AH", "2S", "2C", "2D", "2H", "3S", "3C", "3D", "3H", "4S", "4C", "4D", "4H", "5S", "5C", "5D", "5H", "6S", "6C", "6D", "6H", "7S", "7C", "7D", "7H", "8S", "8C", "8D", "8H", "9S", "9C", "9D", "9H", "TS", "TC", "TD", "TH", "JS", "JC", "JD", "JH", "QS", "QC", "QD", "QH", "KS", "KC", "KD", "KH", "AS", "AC", "AD"}; | |
System.out.println(countFullDeck(test1) == 0); | |
System.out.println(countFullDeck(test2) == 2); | |
} | |
private static int countFullDeck(String[] cards) { | |
List<Set<String>> decks = new ArrayList<>(); | |
for (String card : cards) { | |
for (Set<String> deck : decks) { | |
if (deck.contains(card)) { | |
continue; | |
} | |
deck.add(card); | |
break; | |
} | |
Set<String> deck = new HashSet<>(); | |
deck.add(card); | |
decks.add(deck); | |
} | |
int deckCount = 0; | |
for (Set<String> deck : decks) { | |
if (deck.size() == 52) { | |
deckCount++; | |
} | |
} | |
return deckCount; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment