;; ===================================================================================== ;; _____ __ __ ____ _ __ __ ____ ___ ;; / ____/ |/ / __ \/ / / / < / __ < / Spring 2019 Section 54 ;; / / / /|_/ / /_/ / / / /_____/ / / / / / ;; / /___/ / / / ____/ /_/ /_____/ / /_/ / / Assignment 10 ;; \____/_/ /_/_/ \____/ /_/\____/_/ ;; ;; 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 Ten (Bonus Assignment)") (problem "1 (A)") ;; =================================================================== ;; ROLL-N-D-K-VEC ;; =================================================================== ;; Outputs a vector containing N rolls of K-sided dice. ;; ------------------------------------------------------------------- ;; INPUTS: N, a positive integer. ;; K, a positive integer. ;; OUTPUT: A vector of N K-sided die rolls. ;; =================================================================== (define roll-n-d-k (lambda (n k) (let ((acc (make-vector n 0))) (dotimes (i n) (vector-set! acc i (+ 1 (random k)))) 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 vector containing vectors of N rolls of K-sided dice. ;; ------------------------------------------------------------------- ;; INPUTS: VECKY, a vector of lists, each sublist having 2 integers ;; greater than 1. ;; OUTPUT: A vector of vectors, each vector containing the die rolls ;; specified in the input lists. ;; =================================================================== (define roll-em (lambda (vecky) (let ((acc (make-vector (vector-length vecky) 0))) (dotimes (i (vector-length vecky)) (let* ((lst (vector-ref vecky i)) (n (first lst)) (k (first (rest lst)))) (vector-set! acc i (roll-n-d-k n k)))) 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: VECKY, a vector 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 (vecky) (newline) (let ((rolls (roll-em vecky)) (total 0)) (dotimes (i (vector-length vecky)) (let ((sum 0) (n-k (vector-ref vecky i)) (vals (vector-ref rolls i))) (printf "~A --> ~A" n-k vals) (dotimes (j (vector-length vals)) (set! sum (+ sum (vector-ref vals j)))) (printf " sum = ~A "sum) (newline) (set! total (+ total sum)))) 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)))) (problem "2") (problem "A") (define-struct Course (dept number name faculty day hour am-pm length credits)) (define *COURSES* (make-vector 4)) (vector-set! *COURSES* 0 (make-Course 'CSCI 100 "Introduction to Computer Science" "Stephano Maningo" 'Tues 9 'AM 2 3)) (vector-set! *COURSES* 1 (make-Course 'CSCI 224 "Game Programming I" "Mikhail Maus" 'Weds 2 'PM 2 3)) (vector-set! *COURSES* 2 (make-Course 'CSCI 401 "X-Informatics" "Emily Llewellyn" 'Tues 9 'AM 3 3)) (vector-set! *COURSES* 3 (make-Course 'COGS 594 "Advanced Computer Cleverness 3" "Kathryn Janesby" 'Thur 1 'PM 3 4)) (problem "B") ;; =================================================================== ;; FIND-LECS-AT-TIME ;; =================================================================== ;; Outputs a list of symbols representing courses at a specified time. ;; ------------------------------------------------------------------- ;; INPUTS: VECKY, a vector of Courses. ;; HOUR, an integer. ;; AM-PM, a symbol representing AM or PM. ;; OUTPUT: A list of sublists, each sublist containing the Dept. ;; and ID of courses at the given time. The outer list ;; is null if no courses are found at that time. ;; =================================================================== (define find-lecs-at-time (lambda (vecky hour am-pm) (let ((listy '())) (dotimes (i (vector-length vecky)) (let* ((course (vector-ref vecky i)) (time (Course-hour course)) (am-or-pm (Course-am-pm course))) (when (and (= hour time) (eq? am-pm am-or-pm)) (set! listy (cons (cons (Course-dept course) (list (Course-number course))) listy))))) listy))) (tester '(find-lecs-at-time *COURSES* 9 'AM)) (tester '(find-lecs-at-time *COURSES* 3 'PM)) (tester '(find-lecs-at-time *COURSES* 1 'PM)) (problem "(C)") ;; ===================================================================== ;; PRINT-COURSES ;; ===================================================================== ;; Displays information about one or more courses. ;; --------------------------------------------------------------------- ;; INPUTS: VECKY, a vector of Courses. ;; LISTO, a list of sublists, each sublist contains a Dept. ;; Dept. and a Number. ;; OUTPUT: None. ;; SIDE-EFFECT: Uses side-effect printing to display course information. ;; ===================================================================== (define print-courses (lambda (vecky listo) (letrec ((p-c-r (lambda (listy) (cond ((null? listy) (void)) (else (let ((which-dept (first (first listy))) (which-number (first (rest (first listy))))) (dotimes (i (vector-length vecky)) (let* ((course (vector-ref vecky i)) (dept (Course-dept course)) (number (Course-number course))) (when (and (eq? which-dept dept) (= which-number number)) (printf "~A-~A: ~A. (~A.) ~A, ~A ~A. ~A hours. Credits: ~A.\n" dept number (Course-name course) (Course-faculty course) (Course-day course) (Course-hour course) (Course-am-pm course) (Course-length course) (Course-credits course)))))) (p-c-r (rest listy))))))) (p-c-r listo)))) (tester '(print-courses *COURSES* '((CSCI 401) (CSCI 100)))) (tester '(print-courses *COURSES* '((MATH 385)))) (tester '(print-courses *COURSES* (find-lecs-at-time *COURSES* 1 'PM))) (problem "(C) second solution") (define print-courses-2 (lambda (vecky listo) (newline) (while (not (null? listo)) (dotimes (i (vector-length vecky)) (let ((course (vector-ref vecky i))) (when (and (eq? (Course-dept course) (first (first listo))) (eq? (Course-number course) (second (first listo)))) (printf "~A-~A: ~A. (~A.) ~A, ~A ~A. ~A hours. Credits: ~A.\n" (Course-dept course) (Course-number course) (Course-name course) (Course-faculty course) (Course-day course) (Course-hour course) (Course-am-pm course) (Course-length course) (Course-credits course))))) (set! listo (rest listo))))) (tester '(print-courses-2 *COURSES* '((CSCI 401) (CSCI 100)))) (tester '(print-courses-2 *COURSES* '((MATH 385)))) (tester '(print-courses-2 *COURSES* (find-lecs-at-time *COURSES* 1 'PM)))