;; ===================================================================================== ;; _____ __ __ ____ _ __ __ ____ ___ ;; / ____/ |/ / __ \/ / / / < / __ < / Spring 2018 Section 04 ;; / / / /|_/ / /_/ / / / /_____/ / / / / / ;; / /___/ / / / ____/ /_/ /_____/ / /_/ / / Assignment 08 ;; \____/_/ /_/_/ \____/ /_/\____/_/ ;; ;; S .d8888. .d88b. db db db d888888b d888888b .d88b. d8b db .d8888. ;; A 88' YP .8P Y8. 88 88 88 `~~88~~' `88' .8P Y8. 888o 88 88' YP ;; M `8bo. 88 88 88 88 88 88 88 88 88 88V8o 88 `8bo. ;; P `Y8b. 88 88 88 88 88 88 88 88 88 88 V8o88 `Y8b. ;; L db 8D `8b d8' 88booo. 88b d88 88 .88. `8b d8' 88 V888 db 8D ;; E `8888Y' `Y88P' Y88888P ~Y8888P' YP Y888888P `Y88P' VP V8P `8888Y' ;; ;; ===================================================================================== (load "asmt-helper.txt") (header "Professor Ellis" "Sample Solutions to Assignment Eight") (problem "1") ;; ============================================================ ;; MAX-STREAK-IN-N-TOSSES-ACC ;; ============================================================ ;; INPUTS: N, a non-negative integer. ;; LAST-TOSS, a non-negative integer (or -1 for ;; the first throw. ;; CURR, a positive integer. ;; MAX, a positive integer. ;; OUTPUT: The length of the highest streak. ;; SIDE-EFFECT: Print out die rolls as we go. ;; ============================================================ (define max-streak-in-n-tosses-acc (lambda (n last-toss curr max) (cond ((= n 0) max) (else (let ((toss (random 2))) (printf "~A " (if (= toss 0) 'H 'T)) (let* ((new-curr (if (= last-toss toss) (+ 1 curr) 1)) (new-max (if (> new-curr max) new-curr max))) (max-streak-in-n-tosses-acc (- n 1) toss new-curr new-max))))))) ;; ============================================================ ;; MAX-STREAK-IN-N-TOSSES ;; ============================================================ ;; INPUTS: N, a non-negative integer. ;; OUTPUT: The length of the highest streak. ;; SIDE-EFFECT: Print out die rolls as we go. ;; ============================================================ (define max-streak-in-n-tosses (lambda (n) (max-streak-in-n-tosses-acc n -1 0 0))) (tester '(max-streak-in-n-tosses 20)) (tester '(max-streak-in-n-tosses 20)) (tester '(max-streak-in-n-tosses 20)) (problem "* 2 *") ;; ============================================================ ;; NUM-TOSSES-UNTIL-REPEAT-HELPER ;; ============================================================ ;; INPUT: LAST, a non-negative integer (or -1 for the ;; first time). ;; COUNT, the number of rolls we've made. ;; OUTPUT: The number of rolls of a 6-sided die until we ;; get the same number twice. ;; SIDE-EFFECT: Print out die rolls as we go. ;; ============================================================ (define num-tosses-until-repeat-helper (lambda (last count) (let ((toss (+ 1 (random 6)))) (printf "~A " toss) (cond ((not (= toss last)) (num-tosses-until-repeat-helper toss (+ 1 count))) (else (printf "--> Got a repeat!") (newline) count))))) ;; ============================================================ ;; NUM-TOSSES-UNTIL-REPEAT ;; ============================================================ ;; INPUT: None. ;; OUTPUT: The number of rolls of a 6-sided die until we ;; get the same number twice. ;; SIDE-EFFECT: Print out die rolls as we go. ;; ============================================================ (define num-tosses-until-repeat (lambda () (num-tosses-until-repeat-helper -1 1))) (tester '(num-tosses-until-repeat)) (tester '(num-tosses-until-repeat)) (tester '(num-tosses-until-repeat)) (problem "3") ;; ============================================================ ;; NUM-TOSSES-UNTIL-THREE-HELPER ;; ============================================================ ;; INPUTS: ANTE, a non-negative integer (or -1 to begin). ;; PREV, a non-negative integer (or -1 to begin). ;; COUNT, a non-negative integer. ;; OUTPUT: The number of tosses taken until we got 3 ;; in a row. ;; SIDE-EFFECT: Print out die rolls until we get 3 in a row, ;; then print out a message. ;; ============================================================ (define num-tosses-until-three-helper (lambda (ante prev count) (let ((roll (+ 1 (random 6)))) (printf "~A " roll) (cond ((and (= roll ante) (= roll prev)) (printf " --> We got three in a row!") (newline) (+ 1 count)) (else (num-tosses-until-three-helper prev roll (+ 1 count))))))) ;; ============================================================ ;; NUM-TOSSES-UNTIL-THREE ;; ============================================================ ;; INPUTS: None. ;; OUTPUT: The number of tosses taken until we got 3 ;; in a row. ;; SIDE-EFFECT: Print out die rolls until we get 3 in a row, ;; then print out a message. ;; ============================================================ (define num-tosses-until-three (lambda () (num-tosses-until-three-helper -1 -1 0))) (tester '(num-tosses-until-three)) (tester '(num-tosses-until-three)) (tester '(num-tosses-until-three)) (problem "4") ;; ============================================================ ;; NUM-TOSSES-UNTIL-THREE-LETREC ;; ============================================================ ;; Prints tosses of a 6-sided die until there are 3 in a row. ;; ------------------------------------------------------------ ;; INPUTS: None. ;; OUTPUT: The number of tosses taken until we got 3 ;; in a row. ;; SIDE-EFFECT: Print out die rolls until we get 3 in a row, ;; then print out a message. ;; ============================================================ (define num-tosses-until-three-letrec (lambda () (letrec ((num-until-three-r (lambda (one two three acc) (cond ((and (= one two) (= one three) (= two three)) (printf "--> We got three in a row!\n") acc) (else (let ((toss (+ 1 (random 6)))) (printf "~A " toss) (num-until-three-r toss one two (+ 1 acc)))))))) (num-until-three-r -1 -2 -3 0)))) (tester '(num-tosses-until-three-letrec)) (tester '(num-tosses-until-three-letrec)) (tester '(num-tosses-until-three-letrec)) (problem "5") ;; ============================================================ ;; SUM-CUBES ;; ============================================================ ;; INPUTS: N, a positive integer ;; OUTPUT: 1 ^ 3 + 2 ^ 3 + ... N ^ 3 ;; ============================================================ (define sum-cubes (lambda (n) (letrec ((sum-cubes-r (lambda (count acc) (if (= 0 count) acc (sum-cubes-r (- count 1) (+ acc (* count count count))))))) (sum-cubes-r n 0)))) (tester '(sum-cubes 3)) (tester '(sum-cubes 4)) (tester '(sum-cubes 37)) (problem "* 6 *") ;; =================================================================== ;; MEAN ;; =================================================================== ;; Computes the mean (average) die roll for a K-sided die over ;; N rolls. ;; ------------------------------------------------------------------- ;; INPUTS: N, a non-negative integer. ;; K, a non-negative integer. ;; OUTPUT: None. ;; SIDE-EFFECT: Displays the running average of the rolls so far. ;; =================================================================== (define mean (lambda (n k) (letrec ((mean-r (lambda (count acc) (cond ((<= count n) (let* ((toss (+ 1 (random k))) (new-acc (+ acc toss)) (mean (/ new-acc (* 1.0 count)))) (printf "Round ~A. Rolled ~A; sum is ~A + ~A = ~A. " count toss acc toss new-acc) (printf "Current mean is ~A.\n" mean) (mean-r (+ count 1) new-acc))) (else (printf "Done.\n")))))) (mean-r 1 0)))) (tester '(mean 10 20)) (tester '(mean 10 6)) (tester '(mean 12 12))