{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

CS61A_fa97_mt2

CS61A_fa97_mt2 - CS 61A Midterm#2 Fall 1997 CS 61A Fall...

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

View Full Document Right Arrow Icon
CS 61A, Fall 1997 Midterm #2 Professor Harvey Problem #1 This problem concerns the tree abstract data type defined by the constructor make-tree, invoked with (make-tree <datum> <children>), and the selectors datum and children. (a) Write the procedure treeify: -The argument to treeify is a nonempty list representing arithmetic computation in infix notation, such as this example: ((3 + 4) * (7 - (2 / 2))) More precisely, a computation is either a number or a list of three elements, in which the first and last are computations and the middle element is a symbol representing an operator (such as +). -The value returned by treeify is a tree in which the branch nodes are operator symbols and the leaf nodes are numbers: * / \ + - / \ / \ 3 4 7 / / \ 2 2 (b) Suppose that the time required for an addition or subtraction is one microsecond, and the time required for a multiplication or a division is five microseconds. + - 1 * / 5 Write a time procedure that takes as its argument a computation tree as produced by treeify, and returns the number of microseconds required to compute the desired value. >(time (treeify '((3 + 4) * (7 - (2 / 2)))) 12 Problem #2 The procedure addup, shown later, takes two arguments: a list of numbers, and a goal number. It returns a list of numbers, a subset of the original list, whose sum is the goal number, or #f if there is no such list. It returns the shortest possible list. CS 61A, Midterm #2, Fall 1997 CS 61A, Fall 1997Midterm #2Professor Harvey 1
Background image of page 1

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

View Full Document Right Arrow Icon
> (addup '(2 6 3 4 5) 10) (4 6) > (addup '(2 3 4 5) 10) (5 3 2)
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}