________ _______ __ __ _______ ___
/ ____/ |/ / __ \/ / / / < / __ < / Spring 2019 Section 54
/ / / /|_/ / /_/ / / / /_____/ / / / / /
/ /___/ / / / ____/ /_/ /_____/ / /_/ / / Assignment 10
\____/_/ /_/_/ \____/ /_/\____/_/ Thursday, 25th April, 2019
ASSIGNMENT IS DUE AT THE BEGINNING OF THE NEXT CLASS.
=====================================================================================
Before you begin, press CTRL+ALT+T. This will bring up a terminal window, which is
a quick way to access your network directory. Once it's open, type:
mkdir asmts
cd asmts
mkdir asmt10
cd asmt10
When you save your work today (which you should do often), you should make sure that
you save it in the asmts/asmt9 directory. THIS IS IMPORTANT!
-------------------------------------------------------------------------------------
Please provide AT LEAST THREE examples of your function running.
Don't forget to use ASMT-HELPER.TXT.
Don't forget to use the PROBLEM function before each of your solutions.
------------------------------------------------------------------------------------
Problems may be found in the Problems document on the course website.
Remember to provide contracts for your functions even when they are in the
Problems Book.
Please use the PRINT-HW instruction to print out your homework. Full details
are on the Assignments page of the course website.
==========================================================================================
PROBLEMS HIGHLIGHTED WITH ASTERISKS ARE **COACH-FREE PROBLEMS**.
1. A certain popular game uses rolls of combinations of dice to generate random outcomes.
Varying numbers of dice with different sides can be rolled all at once.
*
(a) Define a function called ROLL-N-D-K-VEC which satisfies the following contract:
*
;; ===================================================================
;; 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.
;; ===================================================================
(b) Define a function called ROLL-EM-VEC which satisfies the following contract:
;; ===================================================================
;; 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.
;; ===================================================================
Here are some examples:
> (roll-em #((4 6) (5 20)))
#(#(3 6 3 1) #(19 8 10 3 20))
> (roll-em #((11 8) (4 12)))
#(#(3 5 8 1 6 3 6 1 4 2 7) #(12 3 2 1))
> (roll-em #((20 6)))
#(#(1 3 6 6 6 4 1 1 2 3 4 4 5 2 2 6 3 5 4 5))
>
HINT: You can (and should) use a call to ROLL-N-D-K-VEC in your solution.
You will need to make a vector to hold the vectors of die rolls before you
start rolling dice. (Think 'accumulator'.)
(c) Define a function called ROLL-AND-SUM-DICE, which satisfies the following contract:
;; ===================================================================
;; 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.
;; ===================================================================
Here are some examples:
> (roll-and-sum-dice #((4 6)))
(4 6) --> #(4 2 4 3) sum = 13
13
> (roll-and-sum-dice #((4 6) (22 8)))
(4 6) --> #(4 2 2 4) sum = 12
(22 8) --> #(1 7 6 8 2 6 8 6 5 5 3 8 1 1 7 4 6 3 8 2 ...) sum = 105
117
> (roll-and-sum-dice #((12 4) (8 6) (5 8) (4 12)))
(12 4) --> #(2 2 2 2 4 2 1 4 4 1 1 2) sum = 27
(8 6) --> #(5 5 4 5 1 5 4 2) sum = 31
(5 8) --> #(1 8 2 4 8) sum = 23
(4 12) --> #(10 8 7 12) sum = 37
118
> (roll-and-sum-dice #((14 4) (12 6) (10 8) (8 10) (6 12) (4 20)))
(14 4) --> #(1 3 1 1 4 4 3 1 4 3 3 4 3 3) sum = 38
(12 6) --> #(2 5 4 3 6 4 2 6 3 3 1 4) sum = 43
(10 8) --> #(6 3 3 8 1 2 8 3 3 4) sum = 41
(8 10) --> #(4 3 1 3 1 3 9 1) sum = 25
(6 12) --> #(12 3 3 7 3 5) sum = 33
(4 20) --> #(19 11 8 16) sum = 54
234
> (roll-and-sum-dice #())
0
>
2.
(a) Here are some entries in a fictional course catalogue:
Dept: CSCI
Number: 100
Name: Introduction to Computer Science
Faculty: Stephano Maningo
Day: Tues
Hour: 9
AM-PM: AM
Length: 2
Credits: 3
Dept: CSCI
Number: 224
Name: Game Programming 1
Faculty: Mikhail Maus
Day: Weds
Hour: 2
AM-PM: PM
Length: 2
Credits: 3
Dept: CSCI
Number: 401
Name: X-Informatics
Faculty: Emily Llewellyn
Day: Tues
Hour: 9
AM-PM: AM
Length: 3
Credits: 3
Dept: COGS
Number: 594
Name: Advanced Computer Cleverness 3
Faculty: Kathryn Janesby
Day: Thur
Hour: 1
AM-PM: PM
Length: 3
Credits: 4
Define a data structure called Course to hold this information.
Make a vector called *COURSES* (include the asterisks) to store individual
data structures representing the courses.
You do not have to write a function to do this: just write it directly into the
Definitions, as I have done in class.
(b) Write a function called FIND-LECS-AT-TIME, which fulfills the following contract:
;; ===================================================================
;; 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.
;; ===================================================================
Here are some examples:
> (find-lecs-at-time *COURSES* 9 'AM)
((CSCI 401) (CSCI 100))
> (find-lecs-at-time *COURSES* 1 'PM)
((COGS 594))
> (find-lecs-at-time *COURSES* 10 'AM)
()
>
HINT: DOTIMES is best here.
Use LET* to store each course in the vector, and any information you might
need to use from that course.
Think carefully about how CONS works and how to construct the output list.
(c) Write a function called PRINT-COURSES which fulfills the following contract:
;; =====================================================================
;; 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.
;; =====================================================================
Here are some examples:
> (print-courses *COURSES* '((CSCI 401) (CSCI 100)))
CSCI-401: X-Informatics. (Emily Llewellyn.) Tues, 9 AM. 3 hours. Credits: 3.
CSCI-100: Introduction to Computer Science. (Stephano Maningo.) Tues, 9 AM. 2 hours. Credits: 3.
> (print-courses *COURSES* '((MATH 385)))
> (print-courses *COURSES* (find-lecs-at-time *COURSES* 1 'PM))
COGS-594: Advanced Computer Cleverness 3. (Kathryn Janesby.) Thur, 1 PM. 3 hours. Credits: 4.
>
HINT: You may wish to define a helper function which prints the output.
Consider how this function works. For each class in the input list, we look
through the vector to find a course with matching details, and when we find one
we display its information.
Does it matter if we keep searching through the vector after we find the course
we are looking for? (With this small number of courses, no.)
This is probably best solved using a combination of the following:
LETREC
LET
LET*
DOTIMES
WHEN
This is a fairly complicated problem to solve. If you get the correct solution, using
correct Scheme code, then your answer is correct: there are numerous possible 'right
answers.'
------------------------------------------------------------------------------------------
Don't forget to submit your work using the SUBMIT101 command!
submit101 e-asmt10 asmt10
(If the name of your directory is different from 'asmt10', change 'asmt10' to whatever
the name of your directory is.)
==========================================================================================
==========================================================================================