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 send me this file (or use the "Handin" button). Be sure to test your functions in one of the following ways:
Be sure to choose meaningful names for functions and parameters, and watch for opportunities to re-use functions you (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. You may do this using the PSP forms, or simply by keeping track in a text file and turning it in.
This assignment is to be done in pairs, just like homework 2 and 3 (but with a different partner).
Do problems 5.1.2, 5.1.3, 5.1.5, and supplementary problem 5.3 (the one that's a modification of problem 5.1.2).
Note on problem 5.1.2: Students are sometimes confused by what check-guess is supposed to do. For the moment, ignore the stuff about "two players" and "random numbers"; just write a function that takes in two numbers and compares them, as directed, and test it yourself as usual. Once it has passed your tests, load the guess.ss teachpack, as directed in the problem, hit "Execute", and test it further by typing
(guess-with-gui check-guess)in the interactions window as directed. The teachpack will provide the "target", you'll guess the "guess", the function you wrote will compare them, and if your function works correctly, you should be able to win the game.
Note on problem 5.1.3: This function is extremely easy -- two or three short lines -- if you re-use previously-defined functions! If you need to re-use a function you wrote for a previous homework assignment, copy and paste it into the definitions window for this assignment.
Note on problem 5.1.5: This should be tested the same way as problem 5.1.2: once you've written a function that takes in four colors and compares them, as directed, try your own test cases as usual. Then load the master.ss teachpack, type
(master check-color)into the interactions window, and if you've written your function correctly, you should be able to win the game.
Three candidates (Anne, Bob, and Charlie) are running for mayor
of Schemeville, which, by court order, has a new computerized voting system.
Develop a function named
"who-won
" which takes in three numbers (the
number of votes for Anne, the number of votes for Bob, and the number of
votes for Charlie, respectively) and returns a string indicating the
name of the person who won -- either "Anne", "Bob", or "Charlie".
For example,
(who-won 7 4 5) "should be" "Anne" (who-won 5 11 36) "should be" "Charlie"For now, you may assume that there are no ties. For extra credit, write a version that reports ties for first place with the string "tie".
(who-won 6 8 8) "should be" "tie"
Another part of the computerized voting system:
develop a function named
"count-votes-4
" which takes in five
strings. The first is the name of a candidate; the other four are
votes which might or might not be for that candidate. The function
should return how many of them are for that candidate.
For example,
(count-votes-4 "Anne" "Bob" "Charlie" "Bob" "Bob") "should be" 0 (count-votes-4 "Anne" "Bob" "Charlie" "Anne" "Charlie") "should be" 1 (count-votes-4 "Bob" "Bob" "Charlie" "Charlie" "Bob") "should be" 2 (count-votes-4 "Charlie" "Charlie" "Charlie" "Charlie" "Bob") "should be" 3 (count-votes-4 "Bob" "Bob" "Bob" "Bob" "Bob") "should be" 4Hint: write an auxiliary function that takes two strings and returns either a 0 (if they don't match) or a 1 (if they do).
Finally, develop a function named
"4-votes->winner
" that takes in four strings
representing votes, and tells which person won. You may assume that the
only candidates are "Anne", "Bob", and "Charlie", and you may assume
that there are no ties. For example,
(4-votes->winner "Anne" "Charlie" "Charlie" "Bob") "should be" "Charlie" (4-votes->winner "Charlie" "Anne" "Anne" "Anne") "should be" "Anne"Hint: This should be quite easy by re-using previous functions.
Note: Obviously, it's a pain passing around four votes as parameters to everything, and even worse if you had more votes or more candidates. We'll learn better ways to handle this, and how to handle any names rather than only Anne, Bob, and Charlie, in a few weeks.
Error log: /15
Function name | Contract | Examples | Definition | Test results |
---|---|---|---|---|
check-guess
|
/5 | /5 | /10 | /5 |
check-guess3 |
/5 | /5 | /10 | /5 |
check-color |
/5 | /5 | /10 | /5 |
modified check-guess |
/5 | /5 | /10 | /5 |
who-won , basic version |
/10 | /10 | /20 | /10 |
who-won , extra credit |
/5 | /10 | /5 | |
count-votes-4 |
/10 | /10 | /20 | /10 |
4-votes->winner |
/5 | /5 | /10 | /5 |
Following directions | /20 |
Writing contracts from word problems | /20 |
Choosing examples | /20 |
Choosing names | /20 |
Coding | /20 |
Code re-use and choice of auxiliaries | /20 |