Homework 2
Due Date Tuesday 9/22 at 9:00pm (Week 2)
Purpose To write simple functions relating to numbers, strings, images, and conditionals; practice function design, including with unions and structs; and prepare for the exam.
Finger Exercises
Exercise 2 Do these after Wednesday lecture: From HtDP, 64 65 84
You should submit a single .rkt file containing your responses to all graded exercises via the Handin Server. We accept NO email submissions.
You are only allowed to use the language specified at the top of this page: failure to do so will result in a 0.
Graded Exercises
Note: For Exercises 3 to 6, we simply ask you to "write" a function (which means you do not have to follow the design recipe, but you may). From Exercise 7 onwards, we ask you to "design" a function or data (which means you must follow the design recipe).
Exercise 3 Hermione is about to start her first year at the Hogwarts School of Witchcraft and Wizardry, and so she wants to convert her muggle currency (Pounds Sterling) to something more useful, Galleons. While shopping in Diagon Alley, she happens upon a currency-exchange service that provides the following chart...
Notice that there is a flat fee charged for service as well as a conversion rate for each Pound -> Galleon. For example, it takes 8 Pounds to get your first Galleon, but then only 13 to get your second.
Write a function, diagon-convert, to convert from Pounds to Galleons. Write four check-expects: two match the examples described above, one that is shown in the graph, but not described, and one that is a value greater than those shown in the graph. Note: one of these must result in a non-integer for credit.
Hint: it can be useful to write out a table of input/output pairs to help understand these types of mathematical relationships (and these can then provide great tests!)
Exercise 4 For any word of at least two characters that starts with two letters, let’s say that its "encoding" is the uppercase version of the first two letters, followed by a space, and then followed by the number of characters in the word. For example, the encoding of "potter" is "PO 6" and the encoding of "Fun" is "FU 3".
Write a function, encoding, that takes a string as an argument and returns its encoding. You may assume that the argument is a valid word as described above. Write three check-expects: the two examples above, as well as one of your choosing: be creative, but respectful :)
Hint: The String function documentation is a useful place to look for functions that operate on Strings.
Exercise 5 Write the function leap-year? which determines if a natural number represents a leap year in the Gregorian calendar.
Write four check-expects and make sure that once these tests are successful, no parts of your function are highlighted in halloween colors.
Exercise 6 You’ve been tasked to produce an animation of a red ball of radius 25 that moves from top to bottom on a 50 by 250 background, hits the bottom edge of the background, then goes back up, bounces off the top edge and so on, down and up. The background should be yellow whenever the ball is moving down and it should be orange whenever the ball is moving up.
Write a function down-and-up that consumes the amount of time that has elapsed since the start of the animation, in clock ticks, and produces an image of the ball on the appropriate position and backround. Then use (animate down-and-up) to run the animation.
The animation should start with:
with the ball moving down:
until it hits the bottom edge:
and starts moving up:
![]()
eventually hitting the top edge:
and starting to move down again:
![]()
and so on.
Be sure to make use of constants to simplify your code and to have a single point of control over constant values mentioned in the problem statement.
Let’s Shuffle
; A CSG (CoinShuffleGame) is a (make-csg MaybeCoin MaybeCoin MaybeCoin) (define-struct csg [left middle right]) ; and represents the three cups in a coin shuffle game, and what is under them ; A MaybeCoin is one of: ; - #false ; - Number ; and represents either no coin or the coin's monetary value ; A Guess is one of: ; - "left" ; - "middle" ; - "right
Exercise 7 Provide examples and templates for the above data definitions. Recall that when data definitions refer to other complex data types, the template must reflect this by calling the appropriate template.
Exercise 8 Design a function, shuffle-right, which moves all cup values in a CSG to the right. The right cup’s contents should loop back to the left cup.
Exercise 9 Design a function, which given a CSG and a Guess, outputs the monetary value of the guessed cup. No coinage is worth 0.
Exercise 10 Design the function inflation, which given a CSG and a number, adds that monetary value to all of the coins in the cup, and leaves empty cups as is.
Cowabunga!
In many parts of the world, UFOs have been abducting cows. This should not surprise you, as you are a UFO pilot who has been sent on just such a mission. As you have probably been warned, however, the base of your UFO is made of a special alloy that cannot be allowed to come in contact with the ground. Your task, therefore, is to land your UFO atop a cow without crashing on Earth... which, as you may have heard, can turn you into a participant in certain "experiments".
Here’s how the game works:
A cow is placed at an initial position on the bottom of the screen. The cow moves left at a fixed speed until hitting the left side of the screen at which point it changes direction and moves to the right until hitting the right side, and so on.
A UFO is placed at an initial position at the top of the screen. The UFO moves downward at a fixed rate. The UFO can be moved left/right by pressing the left/right arrow keys.
The game is over when the UFO hits the ground or the cow. The game should indicate in some way whether the game was won (UFO hits cow) or lost (UFO hits ground).
In this assignment, we will begin working on some of the data definitions and logic of the game. We will finish it and integrate it into big-bang next assignment.
Exercise 11 Design a data definition for UFOs and Cows. A UFO has an X and a Y coordinate. A Cow has an X coordinate and is either going left or right.
Exercise 12 Design a function that moves a UFO down by some fixed amount.
Exercise 13 Design a function that moves a UFO either left or right by some fixed amount.
Exercise 14 Design a function that moves a Cow in the direction it is going by some fixed amount.
Exercise 15 Design a function that determines if a Cow is at the edge of the screen.
Exercise 16 Design a function that flips the direction of a Cow.
Exercise 17 Design a function that moves a Cow in the direction it is going, unless it at the edge, in which case, the Cow should flip directions, and then move.
Exercise 18 Design a function that determines if a UFO has captured a Cow.
Exercise 19 Design a function that determines if a UFO has crashed on the ground.
Exercise 20 Design a function that determines if the game is over.