30 Miniproject 3: election processing | 2008-7-21 ~ 2008-7-23 | (1 activity) |
Miniproject 3BackgroundPresidential elections in the United States happen every four years. On November 2 of 2004, for instance, we chose between George Bush and John Kerry for president. The procedure for determining the winner of the election is somewhat complicated, and is described here; in short, the candidate who gets the most popular votes in a particular state earns that state's electoral votes, and whoever gets more than half of all the electoral votes wins the election.AssignmentWrite and test a program to process U.S. presidential election results. The program will include a procedure named winner, whose first argument is a sentence giving the number of electoral votes for each state and whose second is a sentence giving the percentage of popular votes for the Republican and Democratic candidates. (For this assignment, we ignore third-party candidates.) Winner returns either republican, democrat, or #f, depending on who wins the election. Each word in the electoral vote sentence has four characters. The first two are the state abbreviation; the last two are digits that together specify the number of electoral votes for the state. An example is ca55, which says that California has 55 electoral votes. Each word in the vote results sentence has six characters. The first two are the state abbreviation, as with the electoral votes sentence. The next two are the (rounded) percentage of popular votes earned by the Republican candidate; the last two are the percentage of popular votes earned by the Democrat candidate. Some examples from the 2000 election are
Miscellaneous requirementsPut your solution program into a file named winner.scm in your mp3 directory, and your tests (described below) into a file named winner-tests.scm in the same directory. Submit it as detailed below. None of your code may use recursion. Use the higher-order procedures every, keep, and accumulate instead. In general, restrict your use of Scheme to material covered in Simply Scheme chapters 1-10. Your program should include auxiliary procedures to be called from winner; none of your procedures should be a big mess. You will lose points for unnecessarily duplicated code; define procedures with extra placeholders (possibly procedures) to avoid this duplication. Provide names for your procedures and their parameters that clearly indicate their types and use. Also provide comments with your code. Your grade on this assignment will include evaluation of your comments. Each of your procedures must be accompanied with comments that describe each input—is it a sentence, a word, or a number? what kind?— along with the result returned.TestingInclude test expressions in a file named winner-tests.scm. Use the testing library discussed in the previous miniproject (a full writeup of its functionality is available here. There is an additional feature described there: in run-test-cases, you can provide an optional argument to run only some of you test cases. Test winner on races where each candidate wins and where neither candidate wins. Include at least one input where two candidates split all the electoral votes evenly, and another where neither candidate earns half the votes. Also test your auxiliary procedures individually, to provide additional evidence that they work correctly. Any procedure that involves a numeric comparison should be tested with values less than, equal to, and greater than the compared value. Any procedure that includes a keep should be tested at least with one input for which all the elements are kept, another for which nothing is kept, and a third for which some but not all of the elements are kept. (Note that these tests are not guaranteed to remove all your bugs.) |
We will be considering style more strictly than with previous miniprojects, and it will be reflected in your grade.
|
A solution to this project, involving the files winner.scm and winner-tests.scm, is due at 11:59pm on Thursday, July 24th.
To submit, put your files in a directory named mp3 and
submit them in the usual manner (i.e., by typing submit mp3 via unix).
You may, but need not, work in a partnership of two on this project; you and your partner should be in the same lab section. Both partners will receive the same score on the project.
If you work with a partner:
|
31 Quiz 2 During MP3 | (1 activity) |
1.(from Simply Scheme pg 139)
Write compose that takes two functions f and g as arguments. It should return a new function, the composition of its input functions, which computes f(g(x)) when passed the argument x.
>((compose sqrt abs) -25) 5 >(define second (compose first bf)) >(second '(HIGHER ORDER FUNCTION)) ORDER |