University of Virginia Computer Science
CS150: Computer Science, Fall 2005

Exam 1 Out: 30 September 2005
Due: Wednesday, 5 October, 1:01PM

Name: _________________________________________________________

Directions

Work alone. You may not discuss these problems or anything related to the material covered by this exam with anyone except for the course staff between receiving this exam and class Wednesday.

Open book. You may use any books you want, lecture notes and slides, your notes, and problem sets. If you use anything other than the course books and notes, cite what you used.

No DrScheme. You may not run DrScheme or use any other Scheme interpreter between now and when you turn in this exam.

Answer well. Answer all questions 1-10, and optionally answer questions 11 and 12. Write your answers on this exam. You should not need more space than is provided to write good answers, but if you want more space you may attach extra sheets. If you do, make sure the answers are clearly marked.

The questions are not necessarily in order of increasing difficulty, so if you get stuck on one question you should continue on to the next question. There is no time limit on this exam, but it should not take a well-prepared student more than an hour or two to complete.

Full credit depends on the clarity and elegance of your answer, not just correctness. Your answers should be as short and simple as possible, but not simpler.

Counting with Procedures

1. Define a procedure counteq that takes two inputs, a list lst and a value val, and produces as output a count of the number of elements in lst that are equal to (use eq? as your comparison function) the value val. For example:
(counteq (list 1 2 3 4 5) 3) should evaluate to 1
(counteq (list 7 7 7) 7) should evaluate to 3
(counteq (list 1 2 3 4 5) 7) should evaluate to 0
(define (counteq lst val)










    )
2. Define a procedure count that takes two inputs, a list lst and a procedure p, and produces as output a count of the number of elements in lst for which p applied to that element is not #f. For example, (count (list 1 2 3 4 5) odd?) should evaluate to 3.









3. Using the count procedure you defined in question 2, define a procedure count3s that takes a list as its input and evaluates to the number of 3 elements in the list. For example, (count3s (list 1 2 3 4 5)) should evaluate to 1. Your count3s procedure should be very short and may not use if.



Language

4. Draw a recursive transition network that describes the same language as the Backus-Naur Form grammar below produces starting from Expression:
Expression ::= ( Expression ExpressionList )
Expression ::= Name
ExpressionList ::= Expression ExpressionList
ExpressionList ::=
The terminals are Name and the parentheses.














Complexity

For the next two questions (5 and 6), you are given two functions, f and g that both take a single, non-negative integer parameter n. Explain which of the following are true:
  1. f is O (g)
  2. f is Ω (g)
  3. f is Θ (g)
In some cases, note that several of these may be true. A good answer will identify which of these are true, and will argue convincingly why those are true but the others are not.

5. f: n; g: n2

6. f: the time it takes to apply the counteq procedure (you defined in question 1) to a list of length n; g: the time it takes to apply the best possible sorting procedure to a list of length n










7. For any two functions f and g is it always the case that at least one of the two possibilities, f is O (g) or g is O (f) is true? Prove or disprove using a precise argument.

8. Describe the complexity of the select-mosaic-tiles procedure from PS1 (provided in mosaic.ss, but simplified slightly here):
(define (select-mosaic-tiles samples tiles color-comparator)
  (map2d (lambda (sample) (tile-name (find-best-match sample tiles
  color-comparator))) samples))

(define (map2d f ll)
  (map (lambda (inner-list) (map f inner-list)) ll))

(define (find-best-match sample tiles color-comparator)
  (if (null? tiles)                         
      (error "No tiles to match!")          
      (if (= (length tiles) 1)              
	  (car tiles)                        
	  (pick-better-match 
	   sample (first tiles) 
	   (find-best-match sample 
			    (cdr tiles) color-comparator)
	   color-comparator))))

(define (pick-better-match sample tile1 tile2 color-comparator)
  (if (color-comparator sample (tile-color tile1) (tile-color tile2))
      tile1
      tile2))

For good credit, your answer must explain your reasoning clearly. Remember to carefully define the meaning of all variables you use in your answer and state all the assumptions you make about procedures used whose code is not shown here.

Trees

9. Using the tree procedures from Class 12 (extended with make-leaf:
   (define (make-tree left el right) (list left el right))
   (define (make-leaf el) (make-tree null el null))
   (define (get-left tree) (first tree))
   (define (get-element tree) (second tree))
   (define (get-right tree) (third tree))
 
define a procedure count-tree that takes two inputs, a tree and a test procedure, and evaluates to the number of elements in the tree for which the test procedure applied to the element does not evaluate to #f. For example,
(count-tree (make-tree (make-leaf 3) 7 (make-tree (make-leaf 8) 9 (make-leaf 3))) even?) should evaluate to 1.


















10. What is the complexity of your count-tree procedure? Explain your reasoning clearly and carefully. (You may assume the tree is well balanced for this question.)

These two questions are optional and worth no credit, but we appreciate your answers.

11. Do you feel your performance on this exam will fairly reflect your understanding of the course material so far? If not, explain why.











12. Do you have any comments about how the course is going so far or suggests for improving the remainder of the course?











"); print ( $res[$first] ) ; print (""); ?>
CS 150: Computer Science
University of Virginia
evans@cs.virginia.edu
Using these Materials