mt2_sol - M IDTERM E XAMINATION S OLUTIONS 2 COMPUTER...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: M IDTERM E XAMINATION S OLUTIONS 2 COMPUTER SCIENCE 61A Department of Electrical Engineering and Computer Sciences University of California, Berkeley July 29, 2010 Throughout the exam, we used comment codes. In other words, if you see something like C or written on your exam, it does not indicate your letter grade, but instead you should look up what that comment means on this solution document! 2 Vector Shuffling A useful approach in vector-based questions is to employ a helper procedure that keeps track of our current position in the vector, as we process the vector. Our solution was (define (knuth-shuffle! vect) (define (knuth-shuffle-helper vect i) (let ((vect-length (vector-length vect))) (if (= i vect-length) okay (begin ;; Choose a random position to swap with. (let ((j (+ i (random (- vect-length i))))) ;; Remember the element at that position. (let ((old (vector-ref vect j))) ;; Swap the two elements (vector-set! vect j (vector-ref vect i)) (vector-set! vect i old))) (knuth-shuffle-helper vect (+ i 1)))))) ;; Call the helper procedure. (knuth-shuffle-helper vect 0)) Grading : 2 points if your solution is correct; 1 point if your solution has the idea, such as saving data and performing a swap, but generating a random number from 0 to the length, as opposed to from j to the length; 0 points for an incorrect solution. Comments : : Always generates random numbers that span the length of the vector, rather than from i to the end. 1 1 Box and Pointers +---+---+ +---+---+ a ->| | | --+-+XX>| | | / | +-+-+---+ | +-+-+---+ +-> 7 v | x 5 v v 2 +---+---+ +---->| | | / | | +-+-+---+ | | | v | 1 | | +---+---+ +---+---+ | c ->| | | --+---->| | | --+---+ | +-+-+---+ +-+-+---+ | | | v | +-------+ 2 | | +----------------------+ v +---+---+ +---+---+ b-x>| | | --+---->| | | / | | +-+-+---+ +-+-+---+ | | | | v v | 3 4 | +->+---+---+ +---+---+ | | | --+--->| | | / | +-+-+---+ +-+-+---+ +->6 | x | v v 4 6 Grading : 2 points, 1 point each for each box-and-pointer final result. Half-a-point off if the list c was deep copied: remember that the append procedure only copies the backbone of the first list. Comments : : Box-and-pointer diagram matches incorrect output. 3 Object-Oriented Programming This was an above-the-line OOP question, but the main idea of the question was remembering that lists are simply pairs, whose second elements are also lists. In this manner, the list class could be defined as (define-class (list car cdr) (method (map fn) (instantiate list (fn car) (ask cdr map fn)))) 2 This works because the cdr of a list is also a list, so we can trust that passing it the map message would pro- duce another list, with the function fn mapped to each element of the cdr . The final piece of the puzzle was the empty-list class, which was simply (define-class (empty-list) (method (map fn) self)) A common mistake was to instead define the empty-list class as (define-class (empty-list) (method (map fn) ())) but this reflected a domain-and-range confusion. One example where this would not work isbut this reflected a domain-and-range confusion....
View Full Document

Page1 / 8

mt2_sol - M IDTERM E XAMINATION S OLUTIONS 2 COMPUTER...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online