This assignment includes only one animation problem, so you should be able to write all of your answers in one Definitions window. As before, your Interactions window should still show me the results of test cases on functions by themselves (before you use them in an animation).
For all the programming assignments, be sure to follow the design recipe. Write your function contract, examples, and function definition in the Definitions Window, save it to a file, and e-mail me this file. Also test your program: since you've already included examples in the Definitions window, you should be able to hit the Run button and see all the results (along with what you said they "should be"). Save the resulting Interactions window to a text file and e-mail me this file too. Be sure to choose meaningful names for functions and parameters, and watch for opportunities to re-use functions you, I, or the textbook have already written.
Also turn in a log of how many errors of different kinds you encountered in the assignment, with brief comments describing each one ("mismatched parentheses" is self-explanatory, but more complex errors might need more description). You may do this using the PSP forms, or simply by keeping track in a text file or on paper and turning it in.
Imagine a program written to help a professor keep track of student grades. For now, grades will be stored as a list of strings, one for each student. Develop the following functions:
pass-everybody
, which takes in a list of strings
representing letter grades ("A", "B", etc.) and
returns the same list, in the same order, but with all the
"F"'s replaced with "D"'s. Use the templates
for functions that take in and return lists.
substitute-string
, which takes in a string (old),
another string (new), and a list of strings, and returns the list of
strings in the same order but with the old string replaced wherever
it appears with the new string.
pass-everybody-again
, which does the exact same
job as pass-everybody
, but by using
substitute-string
. You should be able to write the
whole function in two short lines.
remove-fails
, which takes in a list of strings
representing letter grades (as before) and returns the same list
with the "F"'s simply removed (not replaced). That is,
the resulting list may be shorter than the list you started
with.
This isn't a realistic way to keep track of grades. Define
a data type student
which contains a string
(name), a number (id), and another string (letter-grade). As usual,
provide a data definition in English, a define-struct, examples of the
new data type, contracts for the functions that come for free with it,
and input and output templates.
Now develop the following functions that operate on lists of students:
count-failing-students : student-list -> number
pass-all-students : student-list -> student-list
remove-failing-students : student-list -> student-list
sort-students-by-id : student-list -> student-list
sort-students-by-grade : student-list -> student-list
string<?
function, which you can look up in the DrScheme
Help Desk.Develop the following functions on natural numbers:
copies : natural-number object -> list
(copies 3 "hello") "should be" (cons "hello" (cons "hello" (cons "hello" empty)))
count-down : nat-num -> list-of-numbers
(count-down 4) "should be" (cons 4 (cons 3 (cons 2 (cons 1 (cons 0 empty)))))
Recall the animation exercise from homework 4 about a list of shapes, which were defined to be either circles or rectangles. Let's add motion to that animation:
Develop a function move-shapes
which takes in a list of shapes and two numbers (dx and
dy), and moves every shape by that much. (Hint:
re-use a function from homework 3!)
Modify your handle-key
function
from homework 4 so that when the user presses the up, down, left, or
right arrow key, all the shapes move a few pixels in that
direction. (The c
, r
, and -
keys should still work as they did in homework 4.)
Error log: /30
(I'm not grading on how many or how few errors you encountered,
only on whether you recorded them adequately.)
Function or struct name | Contract | Examples | Definition | Test runs |
---|---|---|---|---|
pass-everybody function |
/5 | /5 | /10 | /5 |
substitute-string function |
/5 | /5 | /10 | /5 |
pass-everybody-again function |
/5 | /5 | /10 | /5 |
remove-fails function |
/5 | /5 | /10 | /5 |
student struct |
data def'n /5 | define-struct /5 | contracts /5 | templates /10 |
count-failing-students function |
/5 | /5 | /10 | /5 |
pass-all-students function |
/5 | /5 | /10 | /5 |
remove-failing-students function |
/5 | /5 | /10 | /5 |
sort-students-by-id function |
/10 | /10 | /20 | /10 |
sort-students-by-grade function |
/10 | /10 | /20 | /10 |
copies function |
/5 | /5 | /10 | /5 |
count-down function |
/5 | /5 | /10 | /5 |
move-shapes function |
/5 | /5 | /10 | /5 |
handle-key function |
/5 | /5 | /10 | /5 |
Following directions | /10 |
Writing contracts from word problems | /10 |
Choosing examples | /10 |
Choosing names | /10 |
Coding | /10 |
Code re-use and function composition | /10 |