Algorithm for counting number of decks in a given pack of cards

285 viewsProgrammingalgorithms cards deck javascript
0

You are given a pack of cards like
(["9C", "KS", "AC", "AH", "8D", "4C", "KD", "JC", "7D", "9D", "2H", "7C", "3C", "7S", "5C", "6H", "TH"]));
Where C, S, D, H stand for clubs, spade, Diamond and hearts respectively.
A given deck of cards has cards with values 2, 3, 4, 5, 6, 7, 8, 9, T, J, Q, K, A
where T = 10, J = jack, Q = queen, K = King and A = ace
Write a function deckOfCards() which will accept and array of cards as given above
and returns the number of decks present in that card array.

Changed status to publish
0

Below is a simple algorithm in Javascript

// call function here with different test case inputs
document.write("<h5>Total number of decks in the card pack [9C, KS, AC, AH, 8D, 4C, KD, JC, 7D, 9D, 2H, 7C, 3C, 7S, 5C, 6H, TH]: <span>"
+decksOfCards(["9C", "KS", "AC", "AH", "8D", "4C", "KD", "JC", "7D", "9D", 
"2H", "7C", "3C", "7S", "5C", "6H", "TH"]) +'</span></h5><br/>');
 document.write("<h5>Total number of decks in the card pack [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]: <span>"
+decksOfCards(["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"]) +'</span></h5><br/>');
   document.write("<h5>Total number of decks in the card pack [TD, TC, 6H, 5H, KC, JH, QC, 4S, 2S, 8H, JD, 2H, AC, 7D, 6C, 5D, AD, TS, 4D, KH, 3H, 9H, 3S, 2D, 5S, 6S, AH, JS, 6D, 9S, 4C, 7C, 8S, AS, KD, 7S, 4H, KS, 7H, 9D, 8D, 3D, 5C, 9C, QH, JC, 8C, TH, QS, 3C, QD, 2C]: <span>"
+decksOfCards(["TD", "TC", "6H", "5H", "KC", "JH", "QC", "4S", "2S", "8H", "JD", 
"2H", "AC", "7D", "6C", "5D", "AD", "TS", "4D", "KH", "3H", "9H", "3S", "2D", "5S", "6S", 
"AH", "JS", "6D", "9S", "4C", "7C", "8S", "AS", "KD", "7S", "4H", "KS", "7H", "9D", "8D", 
"3D", "5C", "9C", "QH", "JC", "8C", "TH", "QS", "3C", "QD", "2C"])+'</span></h5><br/>');
 document.write("<h5>Total number of decks in the card pack [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, 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, AH]: <span>"
+decksOfCards(["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", "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", "AH"])+'</span></h5><br/>' );
 document.write("<h5>Total number of decks in the card pack [QC, 6D, KD, QS, 7S, 5C, QH, 3H, AD, 9C, 7H,QD, QH, 2S, AH, KS, 5D, QC, AS, 2D, 6C, 8C, 7D, 8H, 9S, 4D, 6H, JC, 3S, 4C, 3C, 7S, KD, 4H, 5C, 7C, AS, 3D, 7D, 2H, 8S, 2H, 8D, 9S, 3C, 5H, KS, 8D, 9H, TH, 2C, JH, 6D, KC, TS, 6S, TD, 4S, 8S, 5S, 8H, TD, 9D, 4H, 6C, 7C, AC, JC, JD, TS, KH, 2C, QD, JS, QS, KH, 8C, 3H, 2D, 2S, 3S, 5H, 4S, 9C, 4D, 6H, JS, TH, 9H, TC, 7H, JH, TC, 6S, AD, AC, JD, 4C, 5D, 9D, KC, 3D]: <span>"
+decksOfCards(["QC", "6D", "KD", "QS", "7S", "5C", "QH", "3H", "AD", "9C", "7H",
"QD", "QH", "2S", "AH", "KS", "5D", "QC", "AS", "2D", "6C", "8C", "7D", "8H", "9S", "4D", 
"6H", "JC", "3S", "4C", "3C", "7S", "KD", "4H", "5C", "7C", "AS", "3D", "7D", "2H", "8S", 
"2H", "8D", "9S", "3C", "5H", "KS", "8D", "9H", "TH", "2C", "JH", "6D", "KC", "TS", "6S", 
"TD", "4S", "8S", "5S", "8H", "TD", "9D", "4H", "6C", "7C", "AC", "JC", "JD", "TS", "KH", 
"2C", "QD", "JS", "QS", "KH", "8C", "3H", "2D", "2S", "3S", "5H", "4S", "9C", "4D", "6H", 
"JS", "TH", "9H", "TC", "7H", "JH", "TC", "6S", "AD", "AC", "JD", "4C", "5D", "9D", "KC", "3D"])+'</span></h5><br/>');
 document.write("<h5>Total number of decks in the card pack [AC, 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, AC, 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, AC, 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]: <span>"
+decksOfCards(["AC", "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", "AC", "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", "AC", "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"])+'</span></h5><br/>');
function decksOfCards(cards) {
  var obj = {'C':{}, 'S':{}, 'D':{}, 'H':{}};
     for (var i = 0; i < cards.length; i++) {
   switch (cards[i][1]) {
    case 'S':
    case 'C':
    case 'D':
    case 'H':
     if (typeof obj[cards[i][1]][cards[i][0]] === 'undefined')
     obj[cards[i][1]][cards[i][0]] = 0;
     obj[cards[i][1]][cards[i][0]]++;
     break;
   }
  }
  
  var min = Number.MAX_SAFE_INTEGER;
  var count = 0;
     for (var key in obj) {
   var arr = [];
   if(Object.keys(obj[key]).length === 13) {
    for (k in obj[key]) {
     arr.push(obj[key][k]);
    }
    min = Math.min(min, Math.min( ...arr ));
    count++;
   }
          }
     return (count && count % 4 === 0)? min : 0;
 }

Changed status to publish
Write your answer.

Categories