Exercise 162: Here is the function search:
; Number List-of-numbers -> Boolean (define (search n alon) (cond [(empty? alon) false] [else (or (= (first alon) n) (search n (rest alon)))]))
It determines whether some number occurs in a list of numbers. The function may have to traverse the entire list to find out that the number of interest isn’t contained in the list.
Develop the function search-sorted, which determines whether a number occurs in a sorted list of numbers. The function must take advantage of the fact that the list is sorted.
The function we are developing here is quite similar to the last two questions. The only real difference is that we are starting to look at trying to optimise list traversal. This is simple enough to do as the list is already sorted and we can easily check where we are in the list and return as soon as we know the number can't be in the list (as we have gone past the place where the number would have to be).
Firstly we need (or desire) to include racket/base. This will give us access to fprintf - a function that can print out some diagnostic information so that we prove that we are taking advantage of the fact that the list is sorted. This is absolutely not required for the question.
(require racket/base)
; this testlist is just for ease of testing.
(define TESTLIST (list 100 80 45 23 22 20 3 1))
; tests are pretty straight forward - either the number in the list is there or it is not.
(check-expect (search-sorted 100 TESTLIST) true)
(check-expect (search-sorted 1 TESTLIST) true)
(check-expect (search-sorted 50 TESTLIST) false)
This is the search-sorted function. This is fairly straight forward. First I display some information as to what the current state of the list is and where we are at, then I recurse further down the list. If the first item on the list is larger than where we are at, then the list can't contain the n we are searching for and we immediately return false.
; Number List-of-numbers -> Boolean
(define (search-sorted n alon)
(fprintf (current-output-port)
"searching for ~a First of alon: ~a. Alon is ~a \n" n (first alon) alon)
(cond
[(or (empty? alon)
(> n (first alon))) false]
[else (or (= (first alon) n) (search-sorted n (rest alon)))]))
This is the output from running out test cases on the above function. As you can see the first test case (where the n is not present in the list) iterates thru the entire list. For the second test case, as soon as we find the head of the list is less than 50, we stop recursing and return false.
Test Case 1
searching for 100 First of alon is 100. Alon is (100 80 45 23 22 20 3 1)
searching for 1 First of alon is 100. Alon is (100 80 45 23 22 20 3 1)
searching for 1 First of alon is 80. Alon is (80 45 23 22 20 3 1)
searching for 1 First of alon is 45. Alon is (45 23 22 20 3 1)
searching for 1 First of alon is 23. Alon is (23 22 20 3 1)
searching for 1 First of alon is 22. Alon is (22 20 3 1)
searching for 1 First of alon is 20. Alon is (20 3 1)
searching for 1 First of alon is 3. Alon is (3 1)
searching for 1 First of alon is 1. Alon is (1)
searching for 50 First of alon is 80. Alon is (80 45 23 22 20 3 1)
searching for 50 First of alon is 45. Alon is (45 23 22 20 3 1)
searching for 1 First of alon is 100. Alon is (100 80 45 23 22 20 3 1)
searching for 1 First of alon is 80. Alon is (80 45 23 22 20 3 1)
searching for 1 First of alon is 45. Alon is (45 23 22 20 3 1)
searching for 1 First of alon is 23. Alon is (23 22 20 3 1)
searching for 1 First of alon is 22. Alon is (22 20 3 1)
searching for 1 First of alon is 20. Alon is (20 3 1)
searching for 1 First of alon is 3. Alon is (3 1)
searching for 1 First of alon is 1. Alon is (1)
Test Case 2
searching for 50 First of alon is 100. Alon is (100 80 45 23 22 20 3 1)searching for 50 First of alon is 80. Alon is (80 45 23 22 20 3 1)
searching for 50 First of alon is 45. Alon is (45 23 22 20 3 1)
No comments:
Post a Comment