I am working on this question posed in a Mathematica course revised in 1998. Here are the instructions:
Poker Hands
You are to define and show working examples of a function "
poker[]
". The function "poker[]
" should take five integers, each between 1 and 13, and output the best poker hand. (True poker experts should note that we are ignoring suits for simplicity.)The possibilities from best to worst are: "Five of a kind", "Four of a kind", "Full house" (three cards of one kind and two of another), "Straight" (five consecutive, distinct cards), "Three of a kind", "Two pair", "Pair", and "High card" (the case in which none of the above apply).
Examples:
poker[1, 2, 1, 2, 1]
should return"full house"
;poker[1, 1, 3, 4, 4]
should return"two pair"
.Hint #1: There are many combinations of two of a kind: the pair might be in the first two cards, the second two cards, the first and third, and so on. To deal with this, consider sorting the five arguments before sending them to a second function with particular pattern definitions.
Hint #2: Use multiple definitions for this second function, along with pattern matching. Here are sample definitions for two hands.
poker[a_,a_,a_,a_,a_]:="Five of a kind" poker[a_,a_,a_,b__]:="Three of a kind" (* the name b is unnessary, although the BlankSequence is not *)
Clear[poker]
poker[0,0,0,0,0];
poker[a_?NumberQ,b_?NumberQ,c_?NumberQ,d_?NumberQ,e_?NumberQ]:=Sort[poker[a,b,c,d,e]]
poker[a_,a_,a_,a_,a_]:="Five of a kind"
poker[b_,a_,a_,a_,a_]:="Four of a kind"
poker[a_,a_,a_,a_,b_]:="Four of a kind"
poker[b_,b_,a_,a_,a_]:="Full House"
poker[a_,a_,a_,b_,b_]:="Full House"
poker[a_,a_,a_,b_,c_]:="Three of a kind"
poker[b_,c_,a_,a_,a_]:="Three of a kind"
poker[b_,a_,a_,a_,c_]:="Three of a kind"
As is evident from the from the error messages and incorrect answers below, I have made a/some mistake(s), even the outputs that correctly identified the "hand" added Sort[]
to the output. I had at first thought that "hands" were only identified when the four or three similar cards (in the case of Full House) were at the beginning of the sort, however that theory was disproved once the Three of a kind hands were inspected. I have not defined the rest of the "hands" because it became evident that I had made a mistake in the definitions I had already written.
poker[3, 3, 3, 3, 3]
(* (prints) $RecursionLimit::reclim: Recursion depth of 1024 exceeded. >>
-> Sort["Five of a kind"] -- correct *)
poker[3, 2, 2, 2, 2]
(* (prints) $RecursionLimit::reclim: Recursion depth of 1024 exceeded. >>
-> Sort["Four of a kind"] -- correct *)
poker[3, 3, 3, 2, 3]
(* (prints) $RecursionLimit::reclim: Recursion depth of 1024 exceeded. >>
-> Sort["Three of a kind"] -- should be "Four of a kind" *)
poker[1, 2, 1, 2, 1]
(* (prints) $RecursionLimit::reclim: Recursion depth of 1024 exceeded. >>
-> Sort["Full House"] -- correct *)
poker[1, 2, 2, 2, 1]
(* (prints) $RecursionLimit::reclim: Recursion depth of 1024 exceeded. >>
-> Sort["Three of a kind"] -- should be "Full House" *)
poker[1, 2, 1, 3, 1]
(* (prints) $RecursionLimit::reclim: Recursion depth of 1024 exceeded. >>
-> Sort["Three of a kind"] -- correct *)
poker[2, 1, 2, 3, 2]
(* (prints) $RecursionLimit::reclim: Recursion depth of 1024 exceeded. >>
-> Sort["Three of a kind"] -- correct *)
poker[3, 3, 1, 3, 2]
(* (prints) $RecursionLimit::reclim: Recursion depth of 1024 exceeded. >>
-> Sort["Three of a kind"] -- correct *)
poker[4, 1, 5, 3, 2]
(* (prints) $RecursionLimit::reclim: Recursion depth of 1024 exceeded. >>
-> poker[1, 2, 3, 4, 5] -- correctly not identified *)
poker
is simply calling itself, which calls itself, etc. You have a rather strange functional syntax. What is theSort
function doing her? $\endgroup$