Searching

Course links

Exercise 1

Develop a Scheme procedure that searches a list for an element that meets a specified test, returning that element if it is successful and returning the symbol nonesuch if it is unsuccessful.

Exercise 2

Develop a Scheme procedure that takes a vector vec of vectors as argument and determines whether any of its elements is a vector consisting entirely of zeroes. If vec has an all-zero element, the procedure should return the position of that element within vec; otherwise, it should return -1.

Exercise 3

Here is a definition that makes class-roster a name for a vector of strings containing the surname of everyone in this class. The vector has been sorted into alphabetical order.

(define class-list
  '#("Bakyu" "Benness" "Brunner" "Chamberlain" "Falcon" "Furuta" "Hecker"
  "Herrington" "Manfredi" "Morrison" "Pecsok" "Poulin" "Rapp" "Romero"
  "Shadel" "Sims"))

Call the binary-search procedure, with appropriate arguments, to determine the position of your surname in this vector.

Exercise 4

This exercise is best done in pairs.

One way to get a feel for search algorithms is to play a guessing game in which one player, A, selects a number in the range from 1 to 100 and the other player, B, tries to guess it by asking yes-or-no questions of the form ``Is your number strictly less than n?'' (putting in specific values for n). Player B could follow a linear-search strategy by asking, ``Is your number less than 100? Is your number less than 99? Is your number less than 98?'' and so on. The first time player A answers ``no,'' player B knows that A's number is the last one he has mentioned.

How would player B proceed if she followed a binary-search strategy? How many questions, at most, would B need to ask in order to identify player A's number?

Exercise 5

Develop a Scheme procedure that takes as arguments a real number point and a vector vec of real numbers that has been sorted into ascending order, and returns the number of elements of vec that are less than or equal to point.