At prestigious Wossamotta U. in nearby Frostbite Falls, Minnesota, you are hired to write a program to assist the student affairs office as they select roommates who have similar living preferences.
Before students arrive, they are asked survey questions about their preferences in various categories. The answers are placed in a database, in which each student is represented as a two-element list in which the first element is a string giving the student's name and the second element is a list of the student's responses (which can be symbols, numbers, or strings, depending on the nature of the questions asked. This year, the student affairs office is keeping track of only three preferences per student: room temperature, music, and bedtime. So a typical entry in this year's database looks like this:
("Bullwinkle J. Moose" (cool alternative 12))
Given a database entry representing one student and a list of entries that
includes any number of students and their preferences, the roommate-selection procedure is to return a list that contains the
following lists: a list containing those students who match all of the
query student's preferences, a list containing the students who match all
but one of the query student's preferences, ... a list containing all
students who match exactly one of the query student's preferences, and a
list of the students who have no preferences in common with the query
student.
To construct an example, let's define a roster of students, thus:
(define roster
'(("Rocket J. Squirrel" (cool alternative 12))
("Mister Peabody" (cool polka 12))
("Sherman Peabody" (warm alternative 10))
("Boris Badenov" (warm punk 11))
("Natasha Fatale" (warm punk 11))
("Dudley Do-Right" (cool big-band 9))
("Snidely Whiplash" (cool ragtime 11))
("Ray Fenwick" (cool big-band 10))
("Nell Fenwick" (cool big-band 9))))
Here, then, is how we might look for a roommate for Bullwinkle:
> (roommate-selection '("Bullwinkle J. Moose" (cool alternative 12)) roster)
(("Rocket J. Squirrel")
("Mister Peabody")
("Sherman Peabody" "Dudley Do-Right" "Snidely Whiplash" "Ray Fenwick" "Nell Fenwick")
("Boris Badenov" "Natasha Fatale"))
Rocky is the only student on the roster whose preferences match Bullwinkle's exactly, so the first list in the result includes only Rocky. Mr. Peabody's preferences match Bullwinkle's in two positions, so he's on the second list. The third list contains students whose preferences match Bullwinkle's at only one position, and the fourth list those whose preferences don't match Bullwinkle's at all.
Since the office of student affairs plans to use your program in future
years, and the preference questionnaire may well change from year to year,
your roommate-selection procedure should allow for any number
n of preferences, returning a list of n + 1 lists.
It is likely that will also need to define a number of smaller ``helper
procedures'' that roommate-selection calls. It's up to you to
design and write the specifications for these helper procedures.
Make sure to test your program. The on-line document ``Submitting solutions by e-mail'' describes how to prepare and submit your work.
This document is available on the World Wide Web as
http://www.cs.grinnell.edu/~stone/courses/scheme/exercises/roommate-selection.xhtml
created September 12, 2001
last revised September 17, 2001
Ben Gum (gum@cs.grinnell.edu) and John David Stone (stone@cs.grinnell.edu)