As a kid, I was never a fan gambling away my hard-earned allowance. Heck, even playing poker with M&M’s meant that maybe — just maybe — I’ll walk away with less chocolate than I came to the table with, and that was an anxiety worth not experiencing. Fortunately, I’ve since come to my senses, but I’ll never forget the game my risk-averse friends and I would play when we came across a deck of playing cards.

While most kids reached in their pockets for coins to ante up, we’d pulled the face and joker cards out of the deck, shuffle the rest, and deal out six cards, face-up, in the middle of the table with one of the cards a few inches from the rest.

8♥   5♦   2♣   10♥   5♣       3♠

Our goal was to race to see who could make a mathematical equation using only the four basic arithmetic expressions and parenthesis.

5♣ - (8♥ * 5♦ / 10♥) + 2♣   =   3♠

The first to solve (which, almost never was me) kept the five cards as points, and play continued until we ran out of cards.

We’d also play another variant called 24, where the five cards would have to come to 24. For example...

8♥   5♦   2♣   10♥   5♣   =   24

... would become...

(5♦ + 5♣ - 8♥) * (2♣ + 10♥)   =   24

These games (which I’ve since learned are called Krypto and 24) were – and still are – a lot of fun.

Bring Your Own Code

Your exercise for the day: play Krypto and 24. More specifically, write a program to play the games for you.

  • The input should be:
    • Easy - six integers, the first five of which are between 1 and 10, and the sixth which is between 1 and 10 or is 24
    • Hard - an integer array of unknown length and an integer that is a solution of the array
  • The output should be:
    • Easy - a string representing a single solution to the input
    • Medium - a string array representing all possible solutions to the input
    • Hard - a string array representing non-duplicated solutions to the input; a solution is considered duplicate if the differences are only associative or commutative

For bonus points, create a function that mimics a game; i.e., starting with 40 cards, removing 5, shuffling again, etc.