Problem Set 2
Purpose The purpose of this problem set is to internalize design skills. Topically, the problem set focuses on functions that process structures and arbitrary itemizations of data.
Finger Exercises HtDP/2e: : 65, 68, 69, 86-90, 94, 102, 106, 107, 109, 113, 115-118, 121, 123, 124, 125, 127, 128
Solutions must be submitted to Blackboard by 7:00pm on Thursday, May 19th.
The solutions to all of the following problems should be contained in one .rkt file.
You MUST follow the Design Recipe when you develop functions in this problem set. Name your functions using the names given below. Do include templates for your functions (you only need one template per data definition).
Late homework and email submissions are not accepted
Problem 1
The Center for Disease Control collects information on patient visits to health care providers for influenza-like illness (ILI) to estimate the level of flu activity.
The percentage of patient visits to healthcare providers for ILI reported each week is weighted on the basis of state population. This percentage is compared each week with the national baseline of 2.4%. The baseline is the mean percentage of patient visits for ILI during non-influenza weeks for the previous three seasons plus two standard deviations.
The activity levels correspond to the number of standard deviations away from the mean the percent of visits due to ILI is each week. There are 10 activity levels classified as minimal (levels 1-3), low (levels 4-5), moderate (levels 6-7), and high (levels 8-10). An activity level of 1 corresponds to values that are below the mean, level 2 corresponds to an ILI percentage less than 1 standard deviation above the mean, level 3 corresponds to ILI more than 1, but less than 2 standard deviations above the mean, and so on, with an activity level of 10 corresponding to ILI 8 or more standard deviations above the mean.
Develop a program called flu-activity that consumes a flu activity level and produces its classification.
For instance (flu-activity 6) produces "moderate"
Problem 2
Consider the following structure definitions:
(define-struct painting (artist year style))
(define-struct album (title singer genre))
(define-struct senator (state party))
(define-struct person (name mother father height))
(define-struct shoe (style brand color))
What are the names of the constructors, predicates and selectors that each of the structures adds to Racket?
Provide data definitions for the structure definitions above. Make appropriate assumptions about what data goes with each field.
Develop templates for functions that consume the structures above.
Provide a structure type definition and a data definition for representing 4-digit PIN numbers. A PIN number consists of digits represented with a number from 0 to 9.
Design the function difference. It consumes two representations of PIN numbers and creates a new PIN number from the differences. For each position in the PIN, it subtracts the lesser number from the greater. If the two are the same the new digit is 0. Note: The problem statement mentions two different tasks: one concerning PINs and one concerning digits. This suggests that you design two functions.
Provide a structure type definition and a data definition for representing points in time since midnight. A point in time consists of three numbers: hours, minutes and seconds.
Design a function time->seconds which consumes instances of a point in time and produces the number of seconds that have passed since midnight.
Design a function advance-time which consumes instances of a point in time and produces the point in time one second later.
Design a function time->text which consumes instances of a point in time and renders the time to look like the display of a common alarm clock. Hint: a text is an image, not a string, but you will need a string version of the time, too. See HelpDesk for more on the text function.
The drawing of a chameleon in Section 5.11 of HtDP/2e is a transparent image. To insert it into DrRacket, copy and save the image from HtDP/2e and insert it with the "Insert Image" menu item. Define the image with the name CHAM. Using this instruction preserves the transparency of the drawing’s pixels.
When a partly transparent image is combined with a colored shape, say a rectangle, the image takes on the underlying color. In the chameleon drawing, it is actually the inside of the animal that is transparent; the area outside is solid white. Try out this expression in your DrRacket, using (require 2hdtp/image) :
(overlay CHAM (rectangle (image-width cham) (image-height cham) "solid" "red"))
Design a world program that has the chameleon continuously walking back and forth across the screen (you should rotate the image 90 degrees so it is facing right). It starts walking from left to right and when it reaches the right end of the screen, it flips horizontally and begins walking from right to left and switches direction again when it reaches the left boundary.
Of course, like all chameleon’s, ours can change color, too: the key "r" turns it red, "b" blue, and "g" green.
Start with a data definition, VCham, for representing chameleons. You will need to represent the position, color and direction of the chameleon in your definition.
Here is a wish list for the functions you will need:
draw-cham – renders the world as a chameleon facing left or right
next-cham – compute the new location of the chameleon in one clock tick from now
change-cham – changes the color of the chameleon if the "r", "g" or "b" key is pressed