Fundamentals of CS I (CS151 2001S)

[Current]
[Discussions]
[Glance]
[Honesty]
[Instructions]
[Links]
[News]
[Search]
[Syllabus]
**Primary**

[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Quizzes]
[Readings]
[Reference]
**Sets**

[Blackboard]
[Scheme Report]
[SamR's Schedule]
[Rebelsky/Fall 2000]
[Walker/Fall2000]
[Stone/Spring2000]
**Links**

a. If you have not done so already, please scan the reading on searching. In particular, you should look at the sample procedures.

b. Make a copy of the library file that contains the code from that reading.

c. Start DrScheme.

a. Using `linear-search-list`

, search for the letter a in various
lists of characters. (It's probably easiest to create a list of characters
with `string->list`

.)

b. Using `linear-search-vector`

, search for the letter
`#\a`

in various
vectors of characters.

c. Develop some tests for `search-list-for-keyed-value`

.

Write a procedure that takes a predicate and vector as parameters
and, using `linear-search-vector`

as a helper, finds a
value in the vector that matches the predicate or returns #f if no
such value exists. (Like `linear-search-vectors`

, this
searches vectors; unlike `linear-search-vector`

(and like
`linear-search-list`

) this returns a matching value, rather
than an index.

Define and test a Scheme procedure,
`(search-file `

that reads in Scheme values from a given input
port, applying a specified test to each one. When it finds a value that
passes the test, it should return that value; if it gets the end-of-file
object before finding a value that passes the test, it should call the
*pred?* *port*)`error`

procedure to print an appropriate diagnostic.

In the accompanying library file,
you'll find `lastnames`

, a vector of the last names of
people in this class.

Call the `binary-search`

procedure with appropriate arguments
to determine the position of your surname in this vector.

You probably want the `get-key`

procedure to resemble

(lambda (name) name)

If you look at the library file,
you'll find a list of student records called `students`

.

Write a procedure, `(lookup-student `

that
calls the *lastname*)`binary-search`

procedure with appropriate arguments
and returns the information for the appropriate student.

Add calls to `display`

and `newline`

to the
definition of `binary-search`

, so that it prints out the values
of `lower-bound`

and `upper-bound`

each time the
kernel procedure is called. How many recursive calls are made as
binary search finds your surname in the list? How many are made in the
course of an unsuccessful search for the surname `"Stone"`

?

The *divide-and-conquer* principle can be applied in other
situations. For example, we can apply it to a guessing game
in which one player, A, selects a number in the range from 1
to some value and the other player, B, tries to guess it by asking yes-or-no
questions of the form ``Is your number less than *n*?'' (putting
in specific values for *n*). The most efficient strategy for B to
use is repeated bisection of the range within which A's number is known
to lie.

Write a Scheme procedure that takes the part of B in this game. Your
procedure should take the maximum possible value as a parameter. When
invoked, it should print out a question of the specified form and read in
the user's response (presumably, the symbol `yes`

or the symbol
`no`

), then repeat the process until the range of possible
values has been narrowed to contain only one number. The procedure should
then display and identify that number. A sample run might look like this:

>(guessing-game 100)Is your number less than 51?yesIs your number less than 26?noIs your number less than 38?noIs your number less than 44?noIs your number less than 47?yesIs your number less than 45?noIs your number less than 46?noSince your number is less than 47 but not less than 46, it must be 46.

Tuesday, 14 November 2000

- Created. Some problems were taken from
`http://www.cs.grinnell.edu/~stone/courses/scheme/searching-methods.html`

- This version is available at
`Labs/searching.html`

.

Wednesday, 18 April 2001

- Updated formatting.
- This version is available at
`Labs/searching.html`

.

[Current]
[Discussions]
[Glance]
[Honesty]
[Instructions]
[Links]
[News]
[Search]
[Syllabus]
**Primary**

[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Quizzes]
[Readings]
[Reference]
**Sets**

[Blackboard]
[Scheme Report]
[SamR's Schedule]
[Rebelsky/Fall 2000]
[Walker/Fall2000]
[Stone/Spring2000]
**Links**

**Disclaimer**:
I usually create these pages on the fly. This means that they
are rarely proofread and may contain bad grammar and incorrect details.
It also means that I may update them regularly (see the history for
more details). Feel free to contact me with any suggestions for changes.

This page was generated by Siteweaver on Thu May 3 23:08:03 2001.

This page may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2001S/searching.html`

.

You may validate
this page's HTML.

The source was last modified Wed Apr 18 16:23:08 2001.