TopcoderArchiveVisit Topcoder
Archive
Algorithm problem

SortCards

Problem statement, definition, constraints, and public examples.

Problem Statement

*** WARNING: Do not leave the competition room while you are competing. If you have a question, use "admins: question" in the chat area. ***

In this problem, you will be sorting playing cards from multiple decks. Each card will have 3 characteristics: value, suit, and deck number. The value of a card can be any one of (in increasing order): 2,3,4,5,6,7,8,9,10,J,Q,K,A. The suit of a card can be any one of (in increasing order): D,C,H,S. The deck number can be any one of (in increasing order): 1,2,3,4,5. In this problem, a card will be denoted by a string formatted as follows (quotes for clarity): "value suit deck number". For example (quotes for clarity), "3 S 4", "A H 5", and "10 D 1" are 3 strings representing cards.

You will be given a String[] cards representing a list of playing cards. Each element of cards will adhere to the card format mentioned above. You will return the same list of cards sorted in increasing order. Cards should be ordered based on their values. If 2 cards have the same value break ties using suit. If two cards have the same value and suit break ties using deck number.

Definition

Class:
SortCards
Method:
sort
Parameters:
String[]
Returns:
String[]
Method signature:
String[] sort(String[] cards)
(be sure your method is public)

Constraints

  • cards will contain between 2 and 50 elements inclusive
  • Each element of cards will contain between 5 and 6 characters inclusive
  • Each element of cards will be formatted as (quotes for clarity) "value suit deck number" where value is one of 2,3,4,5,6,7,8,9,10,J,Q,K,Asuit is one of D,C,H,Sdeck number is one of 1,2,3,4,5There is a single space between value and suitThere is a single space between suit and deck number
  • Each element of cards will contain no leading or trailing whitespace
  • Each element of cards will be distinct

Examples

  1. {"3 C 1","2 C 1"}
    Returns: { "2 C 1",  "3 C 1" }
    The card with value 2 should come before the card with value 3.
  2. {"2 S 1","2 D 1"}
    Returns: { "2 D 1",  "2 S 1" }
    Both cards have equal value, so the card with suit D comes before the card with suit S.
  3. {"10 C 1","2 D 5","2 D 1"}
    Returns: { "2 D 1",  "2 D 5",  "10 C 1" }
    The card with value 10 should be last. The cards with value 2 both have suit D. We break this tie using deck number, so the card with deck number 1 comes before the card with deck number 5.
  4. {"10 C 5","10 D 3","10 C 3","10 C 2","10 C 1"}
    Returns: { "10 D 3",  "10 C 1",  "10 C 2",  "10 C 3",  "10 C 5" }
    Alot of 10s.
  5. {"A D 1","7 C 2","5 H 3","2 S 4","2 D 5"}
    Returns: { "2 D 5",  "2 S 4",  "5 H 3",  "7 C 2",  "A D 1" }
Back to all problems