This review sheet is intended to help you study for the midterm exam. It is not intended to be an exhaustive list of everything we've covered, but it should help remind you of the big ideas... .
It's a good idea to review your lecture notes and the solutions to your homework problems. The lectures' "quizzes" are especially useful in helping you study for the exam, since they're the style and spirit (roughly) of the questions...
Please remember that you may use at the exam a 8.5 by 11 sheet of self-made notes; two-sided notes are OK.
These don't hope to span all of the topics we've covered, but they are meant to give a sense of the size and scope of possible exam problems.
Racket> (foldr (lambda (X, Y) (+ X Y)) 0 '(1 2 3 4))You should assume that foldr "associates" (orders operations) like this: 1 + 2 + 3 + 4 = 1 + (2 + (3 + (4 + 0))). (Notice that the 0 at the end is the unit for addition.) Write the foldr function from scratch. That is, your implementation my use Racket's list notation and recursion, but no built-in functions. Notice that foldr should not be written exclusively to handle addition! It's first argument can be any function of two variables, its second argument is the unit for that function, and its third argument is a list of elements of the type operated on by the given function.
10
Write a function (closest-sum target L) that takes in a numeric target and a list of numbers L. Then, closest-sum should return the subset of the elements of L whose sum is as close as possible to target.
For example,
Ties may be handled arbitrarily... any best answer is OK.
(closest-sum 19 '(3 4 7 7 22))
'(4 7 7)
Prolog problems
Part A Consider this set of lists:
{ L | L consists of N 0's followed by N 1's, for some N >= 0 }
in other words, the possible values of L are
{ [], [0,1], [0,0,1,1], ... }
Here are some examples of zerones in action:
?- zerones([],0). true ?- zerones([0,0,0,0,0,1,1,1,1,1],N). N = 5 ?- zerones([0,0,0,1,1,1,0],N). false ?- zerones([0,1],2). false ?- zerones(L,N). L = [], N = 0 ; L = [0,1], N = 1 ; ... ?- zerones(L,3). L = [0,0,0,1,1,1]
Part B Write a prolog predicate
subseq( L, M )that is true when the list L is a subsequence of the list M. The idea is that L is a subsequence of M when M contains all of the elements of L in the same order they appear in L, but perhaps M contains some intervening elements, too. You may assume that L and M are both lists. Following are a couple of examples of subseq to clarify its desired behavior:
?- subseq( [1, 2, 3], [0, 1, 5, 3, 2, 4] ). no. ?- subseq( [1, 2, 3], [0, 1, 5, 3, 2, 4, 3] ). yes.
Java problems
Part A Within the OpenList class we wrote for homework, how would you implement the static and nonstatic versions of member, a method that returns true if obj is present in the OpenList and false otherwise. Here are the two method signatures for member:
public static boolean member(Object obj, OpenList L) public boolean member(Object obj)
Part B Write a Java class Vec3 that represents three-dimensional vectors of real values. It should have data members x, y, and z. Include two methods: a constructor and cross, the cross-product operation. Here are their signatures:
public Vec3(double x_in, double y_in, double z_in) public Vec3 cross(Vec3 v)Although the exact workings of the cross product are not really the point here, Wikipedia points out that if a1,a2,a3 and b1,b2,b3 are two three-vectors, their cross product is a2b3 - a3b2, a3b1 - a1b3, a1b2 - a2b1.
Part C In the Java-based parsing assignment, you included the ability to raise a Quantity to a power using the caret operator ^. Explain what changes you would make to the language in order to also enable Python's ** operator to be used to represent taking powers. Consider all three phases of the language: tokenizing, parsing, and evaluation.