home—lectures—recipe—exams—hws—D2L—breeze (snow day)
defining functions
hw02
Due: Sep.07 (Fri) 12:00, hardcopy only: Just problem #1.
Due: Sep.10 (Mon) 12:00, on D2L and hardcopy of all problems.
Your submitted file should be named “hw02.rkt”
.
(To save toner/ink,
your hardcopy can omit the images that are already exactly in this assignment,
and you may print in black-and-white.)
Standard instructions for all homeworks:
-
Your name,
the assignment-number,
and
the URL of this assignment-page
must be in a comment at the start of the file.
-
Any code which results in syntax errors
will generally not earn more than 50%
of the points-possible.
For grading, I will ignore any commented-out code (including commented-out tests).
-
If your hardcopy is more than 1page, it must be stapled.
-
Format well.
(In particular,
each open-paren should be preceded by a space or start-of-line, for now.
E.g. (+ 2 (* 3 4 5)), not ( + 2(* 3 4
5)
).)
- (10pts)
Recall from lecture, we demo'd the steps of writing a simple function,
pizza-area,
which computed the area of a pizza given diameter.
We will write a couple of similar functions.
Of course,
pizzas have topping in the middle, and crust all the way around the rimedge.
(You can think of the full pizza being a disc,
with a smaller pure-topping-covered disc inside of it.)
-
Define a named-constant CRUST-WIDTH, defined to be 2 (inches).
Our favorite pizza place always has this much crust all the way around the edge.
If you like, you can consider this a hard pledge by the pizza-place:
“every pizza we make is guaranteed to have this much crust!”.
-
Some people ignore the crust of a pizza, and only care about
how much topping-covered area they'll be getting when they order a pizza.
Write topping-area,
which returns the amount of pizza covered in topping (in square-inches),
for a given diameter (in inches).
-
Other people love the crust, and only care about how much crust
is on their pizza!
Write crust-area, which returns the amount of exposed crust (in square-inches)
for a pizza of a given diameter (in inches).
For now, your code does not need to give a correct
answer for
pizzas whose diameter is less than twice the CRUST-WIDTH.
However, you should still either:
-
include any restrictions you have
(“pre-conditions”)
about the diameters that your code handles.
Mention these in the function's purpose-statement or signature.
or
- still have test-cases for sensible pizza-diameters
which contain the true, expected answer in the check-expect.
even if those tests don't pass.
Terminology:
Perhaps “desired answer” would be a better term than “expected answer”.
Optional:
If you really want to get your code passing such tests for
diameters less than twice CRUST-WIDTH – again, this is NOT required –
you can either read about cond,
or be clever in using the functions max or min.
- (10pts)
Write the function has-suffix?,
which takes in two strings and returns whether or not the first string
ends in the second string.
Note that this is a computer-sciency definition of “suffix”.
It has nothing to do with syllables or English.
More precisely:
we say that string a has b as a suffix, iff:
there exists a string x such that
(string=? a (string-append x b)).
hint: You can certainly use cond (or if)
if you like, but they're not actually needed; you can instead use a
min or max
in a clever (but standard) way.
`if`:
We will mention if in class on Friday.
Images in racket
To experiment with functions-which-create-and-handle-images in DrRacket,
include (require 2htdp/image) near the top of your file.
Then you should be be able to evaluate each of the following:
(rectangle 80 20 'solid 'blue)
(circle 20 'solid 'red)
(ellipse 80 20 'outline 'orange)
(beside (rectangle 80 20 'outline 'blue)
(circle 20 'solid 'red))
(underlay (rectangle 80 20 'outline 'blue)
(circle 20 'solid 'red))
; Think of 'underlay' and 'beside' as being "addition for images"
; underlay with an offset:
(underlay/offset (rectangle 80 20 'outline 'blue)
0
10
(circle 20 'solid 'red))
; If you want to explore documentation:
; In DrRacket, position the caret on a function-name
; like 'underlay', and hit F1.
|
Test cases for images
For functions that return an image,
your expected-value should either be (a) an expression involving calls to:
the image primitives and/or previously-tested functions,
OR (b) an image-literal
(but, show the expressions you used to create that image-literal).
For example, if the problem were to "create a function that takes in a width, and returns a solid
purple rectangle with the given width and a 16:9 aspect ratio", then the "expected result" part
of the test cases might involve calling the image-library's functions like rectangle:
(check-expect (screen 32) (rectangle 32 18 'solid 'purple))
(check-expect (screen 16) (rectangle 16 9 'solid 'purple))
(check-expect (screen 0) (rectangle 0 0 'solid 'purple))
; Note: for the expected 0x0 rectangle, I could equally well write:
(check-expect (screen 0) empty-image)
|
Or, since DrRacket supports image literals (!), I could copy/paste images as the second argument to check-expects:
-
- (3pts)
Write a function which takes in a size, and returns an image of a brown donut.
(Mmmm, doughnut.)
- (3pts)
Write a function eyes
which takes a size and a color,
and returns two colored rings side-by-side.
- (4pts)
As needed, refactor the above functions so that there is little-to-no repeated code.
(If the underlying helper function has a solid set of tests,
the final function might get by with fewer tests — perhaps even just one.)
Only turn in your refactored versions.
You only need steps 4,5,7,8 of the design recipe (once you decide what
how you'll represent "size", which is a straightforward choice),
and we're only using existing, simple datatypes (so no steps 1-3 or 6).
(0pts — optional!)
Write the function scale-to-width, which takes in two images
and returns the first image,
scaled
so that it has the same
image-width
as the second image.
For example:
-
-
(check-expect (scale-to-width (rectangle 50 20) (square 30))
(rectangle 30 12)).
(Observe that the second image is never used, beyond asking for its width.)
- (10pts)
Consider programs that create meme images, such as:
(check-expect (super-cool-ski-instructor-meme "spend all semester making memes")
)
|
That expected-result was created from the expression:
This code uses a helper scale-to-width;
If you didn't just write it yourself,
you can copy/paste that function from
(here); you'll
want to open that file in DrRacket then copy/paste it near the top of your own hw02.rkt.
(Just cite the URL, as you do for any code taken from the web.)
Note that this code requires internet access.
- Give one more test-case for super-cool-ski-instructor-meme.
- Write the code for super-cool-ski-instructor-meme.
(No additional test-cases required.)
- Write a test case for futurama-fry-meme
in addition to the one shown here:
(check-expect (futurama-fry-meme "the image-library is easy"
"Java's GUI library is hard")
)
|
- Write the code for futurama-fry-meme.
(No additional test-cases required.)
-
Write one additional meme-making function
based on some image from
imgflip.com/memetemplates.
Include 1-2 test cases.
-
We notice that there is a lot of repeated code in our
functions — very ugly.
We will refactor it out.
Task: Write a more general function make-meme, and change
your preceding three functions
so that they are EACH very-short calls to make-meme.
You should not change their behavior at all,
nor change any of the existing test cases.
It is up to you to decide how many parameters (and their type) to provide to
make-meme.
It should be general enough so that it can create almost all standard memes, though
you don't need to worry about having multiple-lines of text at the top or bottom,
nor about having the font size work differently than the examples I provided.
Only submit your refactored versions of the functions for (b),(d),(e) —
you don’t need to (and, shouldn’t) keep the original versions around.
conditions/requirements
In addition to the standard instructions for all hws above,
here are some requirements for our homeworks:
-
For every functions you write,
follow the design recipe,
steps 4,5,7,8.
(You don't need to label each step; just turn in the final result.)
When grading, there are points for having a good purpose-statement,
good tests, etc. In general, test cases alone can be worth nearly half the points.
-
Your programs should be racket’s beginning-student language (for now).
-
Don't repeat code!
For example:
If you find yourself repeating similar calculations
to what’s already in pizza-area, see if
you can avoid the repeated calculations
by calling pizza-area as a helper-function
(perhaps a couple of times).
home—lectures—recipe—exams—hws—D2L—breeze (snow day)