;; ===================================================================================== ;; _____ __ __ ____ _ __ __ ____ ___ ;; / ____/ |/ / __ \/ / / / < / __ < / Spring 2018 Section 04 ;; / / / /|_/ / /_/ / / / /_____/ / / / / / ;; / /___/ / / / ____/ /_/ /_____/ / /_/ / / Assignment 09 ;; \____/_/ /_/_/ \____/ /_/\____/_/ ;; ;; 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 Nine") (problem "1") ;; =================================================================== ;; COUNT-X-IN-N-K ;; =================================================================== ;; Counts the occurrences of rolling X in N rolls of a K-sided die. ;; ------------------------------------------------------------------- ;; INPUTS: X, a positive integer. ;; N, a positive integer. ;; K, a positive integer. ;; OUTPUT: The number of times X occurs in N rolls of a K-sided ;; die. ;; =================================================================== (define count-x-in-n-k (lambda (x n k) (let ((count 0)) (dotimes (i n) (let ((toss (+ 1 (random k)))) (printf "~A " toss) (when (= x toss) (set! count (+ count 1))))) (printf "--> ") count))) (tester '(count-x-in-n-k 1 20 6)) (tester '(count-x-in-n-k 4 20 12)) (tester '(count-x-in-n-k 10 20 20)) (problem "2") ;; ============================================================ ;; NUM-TOSSES-UNTIL-THREE ;; ============================================================ ;; 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 (lambda () (let ((count 0) (last -1) (tosses 0)) (while (< count 3) (set! tosses (+ tosses 1)) (let ((toss (+ 1 (random 6)))) (printf "~A " toss) (if (= last toss) (set! count (+ 1 count)) (set! count 1)) (set! last toss))) (printf "--> HEY! We got three in a row! ") tosses))) (tester '(num-tosses-until-three)) (tester '(num-tosses-until-three)) (tester '(num-tosses-until-three)) (problem "3") ;; ============================================================ ;; CONSEQ-SUMS ;; ============================================================ ;; Outputs a list containing the sums of consecutive pairs of ;; values in LISTY. ;; ------------------------------------------------------------ ;; INPUT: LISTY, a list of numbers. ;; OUTPUT: A list containing the sums of consecutive pairs of ;; values in LISTY. ;; ============================================================ (define conseq-sums (lambda (listy) (cond ((or (null? listy) (null? (rest listy))) listy) (else (let ((acc '())) (while (not (null? (rest listy))) (let ((one (first listy)) (two (first (rest listy)))) (set! acc (cons (+ one two) acc))) (set! listy (rest listy))) (reverse acc)))))) (tester '(conseq-sums '(1 2 3 4 5))) (tester '(conseq-sums '())) (tester '(conseq-sums '(1))) (problem "4 (A)") ;; =================================================================== ;; ROLL-N-D-K ;; =================================================================== ;; Outputs a list containing N rolls of K-sided dice. ;; ------------------------------------------------------------------- ;; INPUTS: N, a positive integer. ;; K, a positive integer. ;; OUTPUT: A list of N K-sided die rolls. ;; =================================================================== (define roll-n-d-k (lambda (n k) (let ((acc '())) (dotimes (i n) (set! acc (cons (+ 1 (random k)) acc))) (reverse acc)))) (tester '(roll-n-d-k 4 6)) (tester '(roll-n-d-k 10 12)) (tester '(roll-n-d-k 8 20)) (problem "4 (B)") ;; =================================================================== ;; ROLL-EM ;; =================================================================== ;; Outputs a list containing lists of N rolls of K-sided dice. ;; ------------------------------------------------------------------- ;; INPUTS: LISTY, a list of lists, each sublist having 2 integers ;; greater than 1. ;; OUTPUT: A list of lists, each vector containing the die rolls ;; specified in the input lists. ;; =================================================================== (define roll-em (lambda (listy) (let ((acc '())) (dotimes (i (length listy)) (let* ((lst (first listy)) (n (first lst)) (k (first (rest lst)))) (set! acc (cons (roll-n-d-k n k) acc))) (set! listy (rest listy))) (reverse acc)))) (tester '(roll-em '((4 6) (10 10) (5 20)))) (tester '(roll-em '((4 6) (10 10) (5 20)))) (tester '(roll-em '((4 6) (10 10) (5 20)))) (problem "4 (C)") ;; =================================================================== ;; ROLL-AND-SUM-DICE ;; =================================================================== ;; Outputs the sum of rolling a LOT of bunches of K-sided dice. ;; ------------------------------------------------------------------- ;; INPUTS: LISTY, a LIST of lists, each sublist having two ;; integers greater than 1. ;; OUTPUT: The sum of all the dice rolled. ;; SIDE-EFFECT: We print the dice rolled, the outcomes, and the sum ;; for each set of dice as we go. ;; =================================================================== (define roll-and-sum-dice (lambda (listy) (newline) (let ((rolls (roll-em listy)) (total 0)) (dotimes (i (length listy)) (let ((sum 0) (n-k (first listy)) (vals (first rolls))) (printf "~A --> ~A" n-k vals) (dotimes (j (length vals)) (set! sum (+ sum (first vals))) (set! vals (rest vals))) (printf " sum = ~A "sum) (newline) (set! total (+ total sum))) (set! listy (rest listy)) (set! rolls (rest rolls))) total))) (tester '(roll-and-sum-dice '((4 6)))) (tester '(roll-and-sum-dice '((4 6) (20 8)))) (tester '(roll-and-sum-dice '((12 4) (8 6) (5 8) (4 12)))) (tester '(roll-and-sum-dice '((14 4) (12 6) (10 8) (8 10) (6 12) (4 20))))