Fifth Homework

This assignment is due at the start of lab on Monday, October 26.

(1) A fresh copy of the midterm is available here. Answer all the questions correctly, including the extra credit, and turn your answers in. For any problem that you got full credit for on the actual midterm, you can simply write "I got full credit for this the first time around."

We're not trying to make anyone relive unpleasant memories; the midterm scores were generally good. But everyone needs to know how to do all the problems on the midterm, and you need to test yourself to make sure that's true for you. It's fine if you've discussed the midterm in section, but you should try to answer each question without consulting your notes or anyone else. You're not forbidden from asking your classmates for help, but this isn't a pair-programming activity; you need to be confident that you can do each of these problems yourself.

Turn in your original graded midterm to the TA in section along with the correctly answered copy; try to do this by Monday.

(2) Chapter 14 introduces trees. Do exercises 14.1.3, 14.1.4, and 14.1.5. Read through Chapters 15 (which covers "mutually referential" data, where an X may contain some Ys and a Y may contain some Xs) and 16 (which talks about iterative design).

(3) Copy into DrScheme the sorting code from Thursday's class (you can get it in the course Email archive on EEE if you don't still have the message). The function i-sort uses an algorithm called "insertion sort"—we take each item and insert it into its correct place on the list of what we've already sorted.

Write the function i-sort-descending, which is like i-sort except that it produces its result in descending order, highest to lowest. (Note that having both functions involves a lot of duplicate code; later on, we'll learn how to avoid that.)

Rerun the timing we did in class on a 10,000-item list; see how long it takes on your machine. You can do this with the expression (time (local ((define SNL (i-sort NL)))"done")): NL is a 10,000-item list like the one we defined in the code from class; i-sort sorts it; to keep from displaying the whole sorted list, we put the call to i-sort into a local definition whose body just returns the string "done" (when it's finished evaluating the definition), and we enclose the whole thing with the time function, which counts the processor time and actual time the task takes (in thousandths of a second). You need to be using Intermediate Student Scheme for the time function.

The Binary Search Tree (BST) code we wrote in class included the functions BST-insert, BST->list, and list->BST. We didn't write an explicit function BST-sort, but you can do it now by combining those components as we did in class.

;; BST-sort: list-of-numbers -> list-of-numbers
;; Return the input list with all its elements in ascending order.
(check-expect (BST-sort empty) empty)
(check-expect (BST-sort (list 1)) (list 1))
(check-expect (BST-sort (list 1 2 3 4 5)) (list 1 2 3 4 5))
(check-expect (BST-sort (list 5 4 3 2 1)) (list 1 2 3 4 5))
(check-expect (BST-sort (list 1 3 5 2 4 6)) (list 1 2 3 4 5 6))

Rerun the timing we did in class on the 10,000-item list using BST-sort. Then rerun the timing on the 100,000-item list (with BST-sort; don't do it with the insertion sort algorithm unless you have something else to do while you're waiting!). Try BST-sort on a 1,000,000-item list. (If you try to sort a million-item list using insertion sort, it might take your computer a couple of days. Later on, we'll see how to analyze and predict this.)

(4) Chapter 15 of the HtDP text covers data structures that are "mutually recursive"—an X might contain some Ys, and Ys in turn could contain Xs. A related example appears below; read it over so you understand how it works.

;; Example of nested lists (lists containing other lists)
;; Data definition:  A book is either
;; -- empty, or
;; -- (cons symbol book), where a symbol represents a word, or
;; -- (cons book book)
;; So this is a book:
(define TTC '(A Tale of Two Cities
                 (It was the best of times)
                 (It was the Dover road)))
;; This gives us a hierarchical (tree-shaped) organization;
;; each nested list could be a chapter, which itself could
;; contain lists for each section or paragraph.
;; But suppose we want to count the words in this book.
;; word-count:  book -> number
;; Return number of words in book
(check-expect (word-count TTC) 16)
(check-expect (word-count empty) 0)
;; We can't just say (length TTC); that gives us the number
;; of elements at the top level (7 in this case), counting
;; each nested list as a single item.  Instead, we need to
;; look into each nested list.  We can write the code for 
;; this by following the data definition, which has three
;; parts:  The list is empty, the first item is a symbol,
;; or the first item is a list:
(define word-count
  (lambda (B)
      ((empty? B) ...)
      ((symbol? (first B)) ...)
      (else ...))))
;; If the book is empty, it has zero words.
;; If the first item is a word (a symbol), add 1 to the
;; number of words in the rest of the book.
;; If the first item is a book (a list), add the number
;; of words in that book to the number of words in the 
;; rest of the book
(define word-count
  (lambda (B)
      ((empty? B) 0)
      ((symbol? (first B)) (add1 (word-count (rest B))))
      (else (+ (word-count (first B)) (word-count (rest B)))))))
;; Notice that there's a recursive call in this code
;; at exactly the same places as "book" appears in the
;; data definition above.
(check-expect (word-count TTC) 16)
(check-expect (word-count empty) 0)

(5) Chapter 16 goes through a longer example of the development process (using file management). It's also another illustration of defining and processing tree-shaped structures.

(6) Chapter 17 talks about handling functions that process two different kinds of complex data. Do exercises 17.1.1, 17.1.2, 17.2.1, 17.2.2, and 17.3.1.

(7) Intermezzo 3 (Chapter 18) covers local definitions, which we saw in the restaurants program. Do exercises 18.1.1, 18.1.2, 18.1.3, 18.1.5, and 18.1.15.

Based in part on ICS H21assignments and exams by David G. Kay from Fall 2001; modified by David G. Kay, Fall 2004, Fall 2005, Fall 2008, Fall 2009.

David G. Kay, kay@uci.edu
Friday, October 23, 2009 9:05 PM