;;; ======================== ;;; CMPU-145, Spring 2013 ;;; Asmt. 7 Solutions ;;; ======================== (load "asmt7-helper.txt") (header "Asmt. 7" "Solutions!!") ;;; ============================================================ (problem "1A: Randomly generating a full house: uniformly!") ;;; ============================================================ ;;; RAND-FULL-HOUSE ;;; ---------------------------------- ;;; INPUTS: None ;;; OUTPUT: A randomly generated hand of 5 cards (i.e., numbers ;;; from 0 to 51) that has a full house. ;;; Note: No particular full house should be any more likely ;;; than any other! I.e., uniform probability!! (define rand-full-house (lambda () (let* ((three-ranks (rand-choose-k 3 *ranks*)) (rank-for-trips (first three-ranks)) (rank-for-pair (second three-ranks)) (suits-for-trips (rand-choose-k 3 *suits*)) (suits-for-pair (rand-choose-k 2 *suits*))) (list (card-from-rank-and-suit rank-for-trips (first suits-for-trips)) (card-from-rank-and-suit rank-for-trips (second suits-for-trips)) (card-from-rank-and-suit rank-for-trips (third suits-for-trips)) (card-from-rank-and-suit rank-for-pair (first suits-for-pair)) (card-from-rank-and-suit rank-for-pair (second suits-for-pair)))))) (tester '(cards->string (rand-full-house))) (tester '(cards->string (rand-full-house))) (tester '(cards->string (rand-full-house))) (tester '(cards->string (rand-full-house))) (tester '(cards->string (rand-full-house))) (tester '(cards->string (rand-full-house))) ;;; ============================================================ (problem "1B: EVENT E: A hand with an even distribution of suits") ;;; ============================================================ ;;; HAS-EVEN-DISTN? ;;; --------------------------------------------- ;;; INPUT: CARDS, a list of cards (i.e., numbers from 0 to 51) ;;; OUTPUT: #t, if CARDS contains at least one card from each suit. ;;; #f, otherwise. (define has-even-distn? (lambda (cards) (let ((suits (map suit cards))) (and (member 0 suits) (member 1 suits) (member 2 suits) (member 3 suits) #t)))) (define cards1 (rand-poker-hand)) (tester '(cards->string cards1)) (tester '(has-even-distn? cards1)) (define cards2 (rand-poker-hand)) (tester '(cards->string cards2)) (tester '(has-even-distn? cards2)) (define cards3 (rand-poker-hand)) (tester '(cards->string cards3)) (tester '(has-even-distn? cards3)) (define cards4 (rand-poker-hand)) (tester '(cards->string cards4)) (tester '(has-even-distn? cards4)) ;;; ============================================================ (problem "1C: Some probabilities!!") ;;; ============================================================ ;; Number of 5-card hands with even distribution ;; Note: One of the suits must appear twice; all others just once. (define *num-ways-even-distn* (* 4 ;; 4 choices for suit that has two (n-choose-k 13 2) ;; 2 ranks for that suit 13 ;; 13 choices for rank for each of the remaining suits 13 13)) ;; Probability of getting dealt a hand with even distribution (define *prob-even-distn* (/ *num-ways-even-distn* *num-5-card-hands* 1.0)) (tester '*prob-even-distn*) ;; Number of hands with full houses (define *num-full-houses* (* 13 ;; choices for rank of trips 12 ;; choices for rank of pair (n-choose-k 4 3) ;; suits for trips (n-choose-k 4 2) ;; suits for pair )) ;; Probability of getting dealt a full house (define *prob-full-house* (/ *num-full-houses* *num-5-card-hands* 1.0)) ;; Probability of a hand having even distribution given that ;; it is a full house. (define *prob-even-distn-given-full-house* .50 ;; The trips in the full house must use three different suits: call ;; them A, B, and C. So we only need to know the probability that ONE ;; of the pair will have the remaining suit, call it D. ;; Since the suits of the pair have to be different, there are 4-choose-2 ;; ways of picking the suits for the pair: (A B), (A C), (A D), ;; (B C), (B D), (C D). Notice that half of them involve the ;; fourth suit, D. ) ;; Probability of a hand having even distribution AND being a full house! ;; Note: We use: P(E AND F) = P(E|F)*P(F) (define *prob-even-distn-and-full-house* (* *prob-even-distn-given-full-house* *prob-full-house*)) ;; Probability of a hand being a full house given that it has even distn. ;; Note: We use: P(F|E) = P(F AND E) / P(E) = P(E AND F) / P(E) (define *prob-full-house-given-even-distn* (/ *prob-even-distn-and-full-house* *prob-even-distn*)) ;; SHow me! (tester '*prob-even-distn-given-full-house*) (tester '*prob-full-house*) (tester '*prob-full-house-given-even-distn*) (tester '*prob-even-distn-and-full-house*) (tester '(* *prob-even-distn* *prob-full-house*)) (printf "Note that the events are NOT independent, since the last two values are different.~%") ;;; ============================================================ (problem "1D: Using Gen-And-Test") ;;; ============================================================ ;;; This should estimate Prob(Even Distn|Full House) (tester '(gen-and-test 100000 rand-full-house has-even-distn?)) ;;; ============================================================ (problem "1E: Generating random hands with even distribution") ;;; ============================================================ ;;; RAND-EVEN-DISTN ;;; --------------------------- ;;; INPUT: none ;;; OUTPUT: A randomly generated hand that has at least one ;;; card in each suit. ;;; NOTE: Each hand with even distribution should be equally likely. (define rand-even-distn (lambda () (let* (;; Pick the suit that will appear twice (twofer-suit (rand-choose-one *suits*)) ;; The remaining suits will each appear once (other-suits (remove twofer-suit *suits*)) ;; Pick two ranks for the TWOFER_SUIT (ranks-for-twofer (rand-choose-k 2 *ranks*))) ;; Construct the list of five cards (list (card-from-rank-and-suit (first ranks-for-twofer) twofer-suit) (card-from-rank-and-suit (second ranks-for-twofer) twofer-suit) (card-from-rank-and-suit (rand-choose-one *ranks*) (first other-suits)) (card-from-rank-and-suit (rand-choose-one *ranks*) (second other-suits)) (card-from-rank-and-suit (rand-choose-one *ranks*) (third other-suits)))))) (tester '(cards->string (rand-even-distn))) (tester '(cards->string (rand-even-distn))) (tester '(cards->string (rand-even-distn))) ;;; ============================================================ (problem "1F: And using GEN-AND-TEST one more time!") ;;; ============================================================ ;;; This should estimate Prob(FullHouse|Even Distn) (tester '(gen-and-test 1000000 rand-even-distn has-full-house?))