DEV Community

Discussion on: Daily Challenge #198 - 21 Blackjack

Collapse
 
vidit1999 profile image
Vidit Sarkar • Edited

C++

int scoreHand(vector<string> cards){
    unordered_map<string,int> scoreCount; // holds the score of each card
    for(int i=2;i<=10;i++){
        scoreCount[to_string(i)] = i;
    }
    scoreCount["A"] = 1; // store the score of A as 1 (minimum)
    scoreCount["J"] = 10;
    scoreCount["K"] = 10;
    scoreCount["Q"] = 10;

    unordered_map<string, int> cardCount; // holds the number of times cards are apperaing
    int score = 0; // minimum score (i.e. score with A as 1)
    for(string s : cards){
        score += scoreCount[s];
        cardCount[s]++;
    }

    // if score is 21 return 21
    if(score == 21)
        return score;

    // if score < 21 try to maximize it
    // but keep it less than or equal to 21
    if(score < 21){
        for(int i=0;i<cardCount["A"];i++){
            if(score > 11){
                return score;
            }
            // add score of A to score
            // score = score - 1 + 11
            score += 10;
        }
    }

    // no need to write the case when score > 21
    // because we are always starting from minimum score possible
    return score;
}

Code can be optimized by caculating scoreCount seperately and then passing
it as an argument in the function.

Collapse
 
vidit1999 profile image
Vidit Sarkar • Edited

Test cases --

cout << scoreHand({"A"}) << "\n";
cout << scoreHand({"5", "4", "3", "2", "A", "K"}) << "\n";
cout << scoreHand({"A", "J"}) << "\n";
cout << scoreHand({"A", "10", "A"}) << "\n";
cout << scoreHand({"5", "3", "7"}) << "\n";
cout << scoreHand({"5", "5", "A"}) << "\n";
cout << scoreHand({"A", "A", "A"}) << "\n";
cout << scoreHand({"A", "A"}) << "\n";
cout << scoreHand({"5", "4", "3", "10", "K"}) << "\n";

Output --

11
25
21
12
15
21
13
12
32