TopCoder problem "TheBlackJackDivOne" used in SRM 448 (Division I Level One)

Problem Statement


John and Brus are training for a card game tournament. There they will be playing Black Jack. Black Jack is played using a standard deck containing 52 distinct cards. Each card can be represented as a two-character string where the first character is the rank ('2' - '9' for ranks Two through Nine, 'T' for Ten, 'J' for Jack, 'Q' for Queen, 'K' for King and 'A' for Ace) and the second character is the suit ('S' for Spades, 'C' for Clubs, 'D' for Diamonds and 'H' for Hearts). For example, the Jack of Spades can be represented as "JS" and the Nine of Hearts as "9H".

Each time a player receives a card from the deck, his score increases by the card's value. Ranks Two through Ten have values of 2 - 10, respectively. Jacks, Queens and Kings each have a value of 10, and Aces have a value of 11.

Brus randomly shuffles the full card deck and gives John one or more cards from the top of the deck. You are given a String[] cards, where each element represents a single card given to John in this initial step. John will then take extra cards from the top of the deck, one by one, until his score is greater than or equal to 21. Return the expected number of extra cards that John will take.



Method signature:double expected(String[] cards)
(be sure your method is public)


-The returned value must be accurate to within a relative or absolute value of 1E-9.


-cards will contain between 1 and 50 elements, inclusive.
-Each element of cards will contain exactly two characters, where the first character is '2'-'9', 'T', 'J', 'Q', 'K' or 'A', and the second character is 'S', 'C', 'D' or 'H'.
-All elements of cards will be distinct.


Returns: 2.105854341736695
{"KD", "8S"}
Returns: 1.08
John will take the second extra card only if the first one is Two.
{"KD", "2S", "2C", "2D", "2H"}
Returns: 1.0
The same situation, but here there are no Twos left in the deck.
{"AS", "KS", "9S", "JC", "2D"}
Returns: 0.0
Here John's score is already more than 21.

Problem url:

Problem stats url:




PabloGilberto , ivan_metelsky , Chmel_Tolstiy

Problem categories:

Recursion, Search