Your objective in this exercise is to determine and report accurate vote totals in an election, starting from the information contained on the ballots cast.
In this election, the members of the Grinnell Public Interest Research Group, a small non-governmental organization, are choosing three new members for the board of directors. Each of the three hundred and twelve GPIRG members has been given a ballot that is printed with her membership number. On this ballot, she is expected to write the first and last names of not more than three candidates for the board. No one is supposed to cast more than one ballot, name more than three candidates on her ballot, or name any candidate more than once.
We'll represent each ballot cast by a list in which the first element is the membership number printed on the ballot and each subsequent element is the name of a candidate. A typical ballot therefore looks like this:
(3825 "William VanDeventer" "Adeline Gill" "Joe Greenwood")
The file /home/stone/courses/scheme/data/exercise-2.ss contains a
Scheme definition in which the name ballots is given to a list
of ballots. It's a long list, since most of the members of GPIRG voted,
but here's the beginning and the end of the definition:
(define ballots
(list (list 6027 "Yu Shen" "Joe Greenwood" "Vincent Kovacs")
(list 7007 "Frances Caltrop" "Mellajean White" "William VanDeventer")
(list 2419 "Carl Swensen" "William VanDeventer" "Yu Shen")
(list 5366 "Joe Greenwood" "William VanDeventer" "Vincent Kovacs")
; ... skipping several ballots here ...
(list 8840 "Carl Swensen" "William VanDeventer" "Jack Timmons")
(list 4057 "Carl Swensen" "Jack Timmons" "William VanDeventer")
(list 5744 "Jack Timmons" "Mellajean White" "Carl Swensen")
(list 9885 "Jack Timmons" "Yu Shen" "Vincent Kovacs")))
Place the command
(load "/home/stone/courses/scheme/data/exercise-2.ss")
in your program so that you can refer to ballots in subsequent
commands.
(Incidentally, ballots is not the record of a real election.
The file /home/stone/courses/scheme/examples/ballot-maker.ss
contains the Scheme program that created most of the data included in
ballots.)
Define a procedure that takes a list of ballots as argument and returns a list of any membership numbers that appear more than once (indicating that someone tried to vote more than once).
Define a predicate that takes a ballot and a list of membership numbers as
arguments and returns #t if the membership number on the
ballot is an element of the given list, #f if it is not.
Define a predicate that takes a ballot as argument and returns
#t if it contains more than three names after the membership
number, #f if it contains three or fewer names.
Define a predicate that takes a ballot as argument and returns
#t if it contains any duplicate names, #f if all
of the names are different.
Using the previous procedures, define a procedure that takes a list of ballots as argument and returns a list of valid ballots, excluding those that contain duplicate names, those that contain more than three names, and those cast by people who attempted to vote more than once.
Define a procedure that takes a list of valid ballots as its argument and returns an association list in which each key is the name of a candidate and the corresponding value is the number of ballots bearing that candidate's name.
Invoke the procedure just defined to determine the vote totals for the
GPIRG election, assuming that each element of ballots
accurately records the information on one of the ballots cast.
This exercise is due at 9 a.m. on Wednesday, February 23, 2000. Here is what you should turn in:
The source code for your program.
The results displayed when the program is run.
Again, you can submit these items either by printing the files and turning in the paper copies or by sending me the files by e-mail.
This document is available on the World Wide Web as
http://www.cs.grinnell.edu/~stone/courses/scheme/exercise-2.xhtml
created February 16, 2000
last revised March 17, 2000