CS 1321 FINAL EXAM PRACTICE PROBLEMS
FALL SEMESTER 2002
These review problems are meant as a study guide, to direct your
questions and preparation, not as a comprehensive overview of the
course. They may or may not be more painful than real test questions.
They DO NOT reflect the style or format of the final exam in any way.
Studying this document does not mean that you have covered and studied
all the material required in full, or that you have seen the full
range of topics possible on the exam.
Seeing a problem here also does not necessarily mean that it could
appear on the final, it just means that it is a good practice
problem for the final. Do not panic if you see some new functions.
YOU SHOULD NOT try to complete this entire review document - just
focus on concepts that you need extra practice with. If you are able
to answer the problems, you will be better-prepared for a final exam
question.
YOU SHOULD review the following:
o The HtDP text
o lecture slides
o homeworks from this semester
o homeworks from previous semesters
o Labs from this semester
o practice tests from both this and previous semesters
in order to gain a complete overview of the course content.
Note that some topics covered in previous semesters may not have been
covered this semester, and vice versa.
In addition to these problems, it would greatly benefit you to know
the Data Analyses and Templates for complex data, covered in the
Design Recipe Guide. These are very helpful for:
o knowing what you're doing when given some complex data
o saving yourself time on a tougher problem
o knowing what to do when you don't know how to start a problem
o partial credit if your solution isn't perfect
INSTRUCTIONS:
-------------
1. Find a scary set of questions
2. Read all about that topic in the materials above
3. THEN try to answer the questions
4. Discuss your solution(s) with others
SOLUTIONS
---------
An official solutions page will not be posted, therefore your
best bet is to discuss the problems with other students, TAs, and post
& discuss your solutions on the newsgroups. Be patient with TAs who
may have extremely busy finals week schedules (due to grading etc).
This review sheet is an excellent collaboration exercise - use it to
your advantage.
Good luck!
PROBLEMS
NOTES: USE DR. SCHEME'S "PRETTY BIG MODE" FOR ALL QUESTIONS
* Stars indicate challenging/optional problems
CS VOCAB
1. (a) What is atomic data?
(b) What is complex data?
(c) What is a function?
(d) How is a procedure different from a function?
(HINT: What is a side-effect?)
(e) What is a program? How is a program different from
functions or procedures?
(f) What is an algorithm?
(h) What is abstraction? (very important!)
(i) What is scope?
(j) What is syntax?
(k) What are semantics?
RECURSION/ITERATION VOCAB
2. (a) What are three requirements of a recursive algorithm?
1. ___________________________
2. ___________________________
3. ___________________________
(b) What is iteration? How is it different from recursion?
(c) What do the following types of recursion mean:
o tail
o generative
o structural
o augmenting
o mutual
(d) Which of these types contradict each other? How and why?
(e) Which types complement each other (i.e. which types may be used
together, or may appear in one function? For instance, is it
possible to have a tail-recursive and mutually recursive
function? What about a generative and structural recursive function?)
SCHEME VOCAB
3. (a) What does the word cond stand for? Why?
(b) What does the word cons stand for? Why?
(c) What is the significance of the lambda symbol? *
HINT: You know it as the all-too-familiar Dr. Scheme logo.
SCHEME SYNTAX
4. (a) What character delimits comments in Scheme?
(b) Is it required to have two of these before a comment?
(c) What character delimits a symbol in Scheme?
(d) What character delimits a string in Scheme?
(e) What are three ways of writing a list in Scheme?
(f) What is a shorthand for writing vectors in Scheme? *
(g) What is a shorthand for writing booleans in Scheme? *
(h) What is the difference between how you write let and
local definitions?
5. What is the difference between:
(a) (define something ...)
and: (define (something ...) ...)
(b) (define 'pi 3.14),
(define pi 3.14)
and: (define "pi" 3.14)
(c) Without typing anything into Dr. Scheme, take a wild guess at
which of the three definitions in (b) will execute correctly.
(NOTE: Read Chapter 5 in the textbook if you got it wrong.)
TESTS
6. Given the following problem on a homework:
Write a function that calculates whether a given year between 1
and 2000 is a leap year. A leap year is any year divisible by
4. Years divisible by 100 are not leap years unless they
are divisible by 400. Return false if you are given a number
outside the valid range.
WAIT - do not write the code for this! That's not what this
question is about.
Suppose you were a TA, writing an autograder to test student's
submitted solutions to this problem. Think about the various tests
you would need to check the students' code:
(a) How many categories of input are there? (That is, give the
optimum number of test cases you would need to test all the
possible input this function should/shouldn't handle.) *
(b) Now list the examples/tests you would use to thoroughly
test the solutions, and which category (from above) they
belong to.
HINT: Think about valid/invalid ranges, boundary conditions,
all types of leap and non-leap years. Even so, if you are
coming up with more than 10 test cases, you are duplicating
a lot of categories - try to be economic, but cover everything.
7. Given the same problem as above:
(a) Write the solution using a cond statement.
(b) Write the solution using ONLY boolean logic and/or/not.
NO cond is allowed!
NOTE: You may use the built-in remainder function to test
divisibility. You should also use your test cases from above to
check your code thoroughly.
STRUCTURES
8. Given the following completely bizarre structure definitions:
(define-struct can-can (can can-can cancun))
(define a-can-can (make-can-can 'can 'can 'can))
(a) Try your very best to list the following items created by that
bizzarre define-struct command:
(i) the constructor
(ii) the selectors
(iii) the predicate
(b) Show how to extract all three 'can symbols from the defined
a-can-can constant. Use three separate one-line commands,
ON PAPER, before testing your code in Dr. Scheme.
(c) Show how to use the predicate to return a true value,
then show how it could return a false value. Use any
example you can create.
NESTED STRUCTURES
9. Given the following structures:
(define-struct point (x y z))
(define-struct cube (p1 p2 p3 p4 p5 p6 p7 p8))
(define my-cube (make-cube ... ... ...))
where each "p" inside the cube is actually a
point structure,
Write the following ON PAPER, before testing them in Dr. Scheme:
(a) show how to extract p3 from my-cube, in one line of Scheme code
(b) show how to extract the x-coordinate of p7 from my-cube, also
with only one line of Scheme code - assuming that p7 is a
point structure
HINT: This is easy to get backwards, so test your solutions!
CONTRACTS
10. Carefully write a Contract for each of the following: (Know that
some are much trickier than they look - if you are not sure, then
ask someone to check your solutions!)
(a) sqr: returns the square of a number
(b) symbol=?: compares two symbols for equality
(c) string?: checks if an unknown piece of data is a string
(d) posn-x: extracts the x-coordinate of a posn
(e) BST-search: checks if a string exists inside a BST of
strings (i.e. whether it is found or not).
(f) length: counts the number of items in a list
(g) append: attaches two lists of Scheme data end-to-end
(h) map: applies a function to each item in a list
(i) filter: applies a predicate to each item in a list, then
removes all items for which the predicate is false
returning a list of ONLY the items for which the
predicate is true. (CORRECTED)
(j) vector-set!: changes a specified index in the vector
(k) display: displays any item to the screen
(l) collect-garbage: runs the Scheme garbage collector
try it: (collect-garbage)
(m) flipflop: takes a list which contains lists of numbers,
returns a vector, containing vectors of numbers
(n) squish: takes two vectors of any two types
of data, and a function (which consumes an item
from each vector and produces a number,
squish only returns a number
(o) d/dx: takes a function f(x) (which takes in a number
and returns a number) and returns its derivative
f'(x), which is also a function that takes a
number and returns a number (don't worry about the
Calculus, just read the problem carefully)
(p) ;; weirdness: _____ _____ _____ _____ --> _____ *
;; This function may take in other functions! Read carefully:
(define (weirdness A B C D)
(local [
;; A constant ...
(define current-limit (* 3 D))
]
(cond [(empty? C) empty]
[(B (first C) current-limit)
(cons (A (first C))
(weirdness A B (rest C) current-limit) )]
[else (weirdness A B (rest C) D)] )))
HINTS: o You may need to run these or research them to figure out
the precise Contract - many are built into Dr. Scheme.
o Remember to be as general as possible, but at the same time
specific where only specific data types will work.
o Remember to use the "new" notations, including functions
as parameters, listof, vectorof, BSTof, etc..
LISTS
11. Given the following list:
(define mylist (cons 1 (cons 2 (cons 3 empty))))
Show how to extract each of the following from mylist,
using only combinations of first & rest,
in ONE line of code per part:
(a) extract the 1
(b) extract everything after the 1
(c) extract the 2
(d) extract everything after the 2
(e) extract the 3
(f) extract the empty at the end of the list
Again - you must extract the values *from* the given list, one
line of code per part. For example, to answer part (f) I could
simply write:
empty
but that empty *does not come from the list*. It is wrong.
NOTE: Do you see why recursion is perfect for automating this process?
If not, try doing this for (list 1 2 3 4 5); it gets tedious... ;)
12. Trace the following code ON PAPER, showing each step: *
(yes, the final result spells something...)
(append (reverse (cons (first '(S C H E M E))
(rest (rest '(A B C))) ))
(map add1
(first (list (reverse (list 0 1 2 0))
(reverse (list 0 1 2 1))) ))
(list (first (rest '("Russian" "is" 'so 'confusing)) ))
(filter string? '(This question is "craaazy!" man!)) )
MAJOR HINT: Think like math - the innermost parentheses get
evaluated first, then the result will replace the
original expression.
13. Given a list of numbers, such as:
(list 1 2 3 4 5)
write a function that will cube each number in the list, to
produce:
(list 1 8 27 64 125)
You must use the following variations: (each of them should simply
take in the user's list, and nothing else)
(a) Write it using augmenting recursion and one or more
local helper functions. Name the main function
cube-aug.
(b) Write it using tail recursion and one or more local
helper functions. NONE of the functions you write can
be augmenting recursive at any point! Name the main
function cube-tail.
HINT: Telltale signs of augmenting recursion - if you
are applying some operation *to* a recursive call, then
that operation will have to wait outside the recursive
call, until that recursion is finished. This will
build a stack of waiting operations: augmenting.
Instead, move the operation *into* the recursive
call, applying it to the extra parameter/accumulator.
Then the operation is done *before* the recursive
call, so nothing is left building up ouside of it.
On most finals if you use ANY augmentation
in your solution to a tail recursive problem,
your solution will not receive any credit.
(c) Write it using a do loop, and one or more local helper
functions. You may not use recursion at any point!
name the main function cube-loop.
(d) Write it WITHOUT a local helper, using only the
built-in functions map and lambda. Name
this variation cube-map.
HINT: There is no built-in cube function for numbers;
perhaps one of your helper functions could abstract this
operation?
14. Write the Scheme code for filter and map.
Some examples of how the built-in map & filter behave:
(map sqr '(1 2 3 4 5)) --> '(1 4 9 16 25)
(map (lambda (x) (+ x 5)) '(10 10 10)) --> '(15 15 15)
(filter (lambda (x) (symbol=? x 'a))
'(a b c a b c a b c a b c)) --> '(a a a a)
HINT: Take a look at the Contracts and descriptions for these two
functions, in the CONTRACTS problem above. Also, the HtDP
text has a description of both (although they give an
incorect Contract in the chart!).
15. Write a function that will find the smallest odd number in
a list of any data.
(smallest-odd '(a b c
-4 -2 0 1 2 3
"HEY" THIS IS "tricky")) --> 1
If the list is empty, or contains no odd numbers, you may return
zero.
Write this using any technique you like (try tail recursion or
a loop). Be sure to also write out a Contract.
HINT: You will need to make sure you are looking at a number,
before you try to check if it is odd or not. There is a
number? predicate in Scheme that will help you.
BOXES & POINTERS
16. Given the boxes & pointers diagram shown in Figure 3.3:

Fig. 3.3
Write out the (HINT: nested!) list that this represents, using:
(a) cons cell notation: (cons ...)
(b) list notation: (list ...)
(c) quote notation: '(... ...)
17. Given the following crazy nested lists, draw the boxes & pointers: *
(a) '(((1 2) 3 (4)) 5 6 (7 8 () (())) 9)
(b) (list (list 1 2) 3 (list (list 4) 5) 6 empty)
(c) (cons 1 (cons (cons 2 (cons empty empty)) (cons 3 empty)))
NESTED LISTS
18. The following is a problem found in summer semester's homework 5:
(meaning that you can look up the solution, once you have tried it!)
Write a function that will search for a symbol in a nested list of
symbols, and replace every occurrence of it with a new
symbol, given by the user.
Here are some examples:
> (replace 'Junk ;; <-- old symbol
'Gold ;; <-- new symbol
empty) ;; <-- a nested list (well, an empty one...)
empty
> (replace 'C++ 'Scheme (list 'a (list 'b 'c) 'd))
(list 'a (list 'b 'c) 'd) ;; <-- nothing matches
> (replace 'deadlines 'blah
(list 'deadlines
(list 'deadlines 'deadlines)
(list 'deadlines 'deadlines 'deadlines)
(list (list 'deadlines) 'and 'more 'deadlines)))
(list 'blah
(list 'blah 'blah)
(list 'blah 'blah 'blah)
(list (list 'blah) 'and 'more 'blah)))
HINT: You may also find the nested list Template in the Design
Recipe Guide helpful.
19. (a) Write a map function that will work on nested lists. It
should do exactly what the regular map function does, but
it should apply that to all the inner lists as well. name
your function nested-map.
(b) Do the same for filter, producing nested-filter.
(c) Do the same for reverse. This is tricky, as it
requires you to reverse each inner list as you reverse the main
list. Thus, given:
'(Scheme! (in (list nested)) a ((reversed) I) Look)
your nested-reverse function should produce:
'(Look (I (reversed)) a ((nested list) in) Scheme!)
20. A classic final-exam problem: write flatten, a function
that will take a nested list such as this:
'(a b (c d (e) ()) f (g ()))
and remove all the nesting (including the nested empty lists),
to produce:
'(a b c d e f g)
a plain list, with all the data from the original list, in the
same order.
HINT: The built-in append should greatly help here.
21. Write a function that will check if two nested lists of strings
are exactly alike. This means each nested inner list will
also need to be alike. Name this function nested-equals?. *
Your function should use only string=? to compare the two
lists. Do not use the built-in equal? predicate, or any
of its relatives.
22. Given this sequence of numbers:
(define mylist (list 1 2 3 4 5 ...))
Write one-line map commands that will take in mylist
and produce the following:
(a) (list #t #f #t #f #t...)
(b) (list 1 4 9 16 25 ...)
(c) (list 5 6 7 8 9 10 ...)
(d) (list (vector 1)
(vector 2)
(vector 3)
(vector 4)
(vector 5)
... ... ...)
(e) (list '(1) '(2) '(3) '(4) '(5) ...)
(f) (list (vector 1)
(vector 2 2)
(vector 3 3 3)
(vector 4 4 4 4)
(vector 5 5 5 5 5)
... ... ...)
MULTIPLE LISTS
23. Given two lists of the same length, write a function that will
apply some operation to the lists, one pair of items at a time,
producing a new list of the results.
For instance:
(double-map +
'(10 20 30) ; adding the two lists:
'(4 5 6)) --> '(14 25 36)
(double-map -
'(10 10 10) ; subtracting the two lists:
'(3 3 3)) --> '(7 7 7)
(double-map symbol=?
'(a b c d) ; comparing the two lists:
'(a b d d)) --> '(#t #t #f #t)
It is important that you try to write a Contract for this problem.
NOTE: This is actually the same behavior as the built-in
map (go ahead and try it on two lists), but do not
use map in your solution.
TREE VOCAB
24. (a) What is an n-ary tree?
(b) What is a binary tree?
(c) What is a traversal? Explain the three types.
(d) What is the difference between a traversal and a search?
(d) What is a node?
(e) What is a parent node? A child node?
(e) What is a leaf node? Can it have a parent? Can it have a child?
(f) What is the root? Can the root be a leaf? Can the root have a parent?
(h) What is a subtree?
TREES
26. Take a look at the five diagrams in Figure 7.29 below:

Fig. 7.29
Answer the following, and most importantly answer WHY:
(a) Which of the diagrams are trees?
(b) Which of the trees are binary trees?
(c) Which of the binary trees are also search trees? (tricky)
(d) Which of the trees are full & balanced? *
(e) Which of the trees are also graphs? (careful!)
(f) Which of the trees can also be considered lists?
Also answer the following ON PAPER:
(g) What are the PRE, IN, and POST order traversals,
for each diagram which is a valid binary tree?
(h) Use Scheme code to represent each valid binary tree in
the figure, using the following node structure:
(define-struct node (data left right))
HINT: Each tree should begin with: (make-node ...)
(i) Insert the numbers 7.5, 4.2, 1.5 into each valid BST
in the diagram.
NOTE: This is trickier than it looks, but you do not have
to write the code for it - just draw the resulting
tree.
(j) After inserting the numbers listed in part (i) above,
delete the root node of each resulting BST.
Be sure to use the algorithm covered in lecture, and again
only draw the result, do not write Scheme code.
BINARY TREES
27. Write a function that will change a binary tree into a nested
list. Here is how the conversion will work:
(convert-BT false) ; empty tree
empty ; empty list
(convert-BT (make-node 2 false false)) ; data, left, right
(list 2 empty empty) ; first, second, third
(convert-BT
(make-node 1 (make-node 2 false false)
(make-node 3 false false)) ; a full tree
(list 1 (list 2 empty empty)
(list 3 empty empty)) ; a nested list
28. (a) Write a swap-node function which will swap the left &
right subtrees of a binary tree node. That is, given:
(make-node 1 ; data
(make-node 2 false false) ; left
(make-node 3 false false)) ; right
it should return a new node with the same data as the
original, but with its left and right branches swapped:
(make-node 1 ; data stays the same
(make-node 3 false false) ; left = old right
(make-node 2 false false)) ; right = old left
What is the Contract for this function?
HINT: It does not take in a BT or a BST. Why? *
(b) WITHOUT using the swap-node function as a helper, write
reverse-BT, a function that will flip the left and
right sides of an entire tree, recursively. That is, given
the following: (which happens to also be a BST)
100
/ \
30 155
/ \ / \
12 42 130 178
it will completely reverse the left & right branches of each
node in the tree, until the result becomes:
100
/ \
155 30
/ \ / \
178 130 42 12
Notice that all the relationships between nodes are the same,
just mirror-image reversed.
BINARY SEARCH TREES
29. Write the Scheme code that will search for a string inside a BST
of strings. Let the user pass in the target string, then the BST.
Return true if the string is found, false if it is not.
Your BST will be organized alphabetically, defined as either:
1. false, if the tree contains nothing, OR:
2. (define-struct node (data left right))
You MUST use a binary search - eliminating one side of the
tree at each step.
Recall that any two strings, str1 and str2, can be easily compared
alphabetically, using:
(string<? str1 str2)
(string>? str1 str2)
(string=? str1 str2)
HINT: This would be a good time to look up the Data Analysis and
Templates for BST processing. Also, what is the Contract
for this problem, using (BSTof ...) notation?
30. Suppose you had the following BST of strings, organized
alphabetically:
"love "
/ \
/ \
"I " "trees!"
/
/
"processing "
If you could "append" the strings in alphabetical order, you'd
get a single string with the sentence: "I love processing
trees!". We're not sure why you would ever want to see this
sentence, but suppose for a minute that you did. It so happens
that there is a function:
(string-append str1 str2 ...)
which will append as many strings together as you like.
Write the Scheme function that will take any BST of strings (not
just the example above), and append all the strings together in
alphabetical order.
If the tree is false, return an empty string, which can
be written as a pair of quotes with nothing inside:
""
HINT: This is a certain type of traversal, the code for
which you have seen before in several places.
N-ARY TREES
31. You've noticed a foreign student named Rotcod Emehcs keeps track
of all his friends using a Scheme program, instead of an address
book. You're dying of curiosity because your Scheme final is
coming up and you need to know how to do this too, so you ask him
how he does it. He explains: each person is represented by
the following structure:
(define-struct person (name age friends))
;;
;; where: (a) name is a string containing the person's full name
;; (b) age is a number
;; (c) friends is a (listof persons)
Each person P will have a list of friends:
P1
.--.---.--.^.-.--.--.--.
/ / / / | \ \ \ \
P2 P3 P4 P5 P6 P7 P8 P9 P10
And each person in the list may have more friends, and they may
have more friends, and so on and so forth. Each person may have
as many friends as they wish.
The only trouble with this is that it's nested and messy, and he
would like your help to be able to make a clean list of all his
friends' names, as well as their friends, and their friends'
friends, etc. instead of looking at the n-ary tree.
(a) Write a program that will take in a person, and make a
single flat list of that person's name, as well as the names of
all of that person's friends, their friends, etc. etc..
(b) Write a program that will take in a person, and only
display that person's name, age, and then do the same for
all the other names in the tree - but not return anything.
Be sure to print a (newline) after each person's info.
(c) Write a program that will count the number of people in
this n-ary tree, including the person who is at the root.
HINT: Each of these programs can be done using a pair of
mutually recursive functions (remember the CS vocab
at the top of this review sheet?).
GRAPH VOCAB
32. (a) What is a vertex?
(b) What is an edge?
(c) What is an adjacency?
(d) What is a cycle?
(e) What is a weighted graph?
(f) What is a directed graph?
(g) What do BFS and DFS stand for?
(h) What is the difference between stack and a queue?
(i) What are the BFS & DFS algorithms, and which
data structure (stack or queue) does each of them use?
GRAPHS
33. Take a moment to study Table 3.44(f) below:

Table 3.44(f)
Answer the following, and again be sure to answer WHY:
(a) Which of these diagrams are graphs?
(HINT: Look closely, they may be tough to guess!)
(b) Which of the graphs are directed?
(c) Which of the graphs are weighted?
(d) Which of the graphs are also trees? (WHY?)
(e) Perform a numerical/alphabetical order BFS and a DFS on
each graph, starting with nodes A or 1, as appropriate.
(BONUS: *** Extra points for knowing Russian alphabetical order! *** ;)
Be sure to keep track of a stack or queue of "nodes
to be visited" at each step. If you do this, it will be
possible to easily trace the final path taken
through the graph, to reach the final letter or number.
Write the final paths taken through the graphs.
(f) Of the searches in part (e), which type of search -
BFS or DFS - is fastest (takes the least amount of steps)?
Is this result true in general? *
VECTORS
34. (a) What are the two big differences between lists and vectors?
(b) Write the code that will access the last element in a list.
(c) Now write *one line* of code that will do the same for a vector.
Which one is more efficient, in terms of the number of steps?
(d) Write the code that will change the last element in a vector v
to be a new item called X. (Comment this out)
35. Create the following vectors, all of length N, using
either make-vector or build-vector, depending on the
complexity of the problem:
(define N ...) ; <-- try any natural number here
(a) (vector 0 0 0 0 ...)
(b) (vector 'a 'a 'a ...)
(c) (vector 1 2 3... N-2 N-1 N) ; notice it starts with 1
(d) (vector N N-1 N-2 ... 3 2 1) ; backwards from N to 1
(d) (vector 2 4 6 8 ...)
(e) (vector #t #f #t #f ...)
(f) (vector 1 8 27 64 125 ...)
(g) (vector '(0) '(1) '(2) '(3) ...)
(h) (vector
(vector 0 0 0 0 ...)
(vector 1 1 1 1 ...)
(vector 2 2 2 2 ...)
... ... ...
... ... ...
(vector N N N N ...)) ; an NxN matrix
(i) (vector
(vector)
(vector 1)
(vector 2 2)
(vector 3 3 3)
(vector 4 4 4 4)
... ... ... ... ...
... ... ... ... ... ...
(vector N N N N N N ... ... ...)) ; a "pyramid" matrix
ITERATION
36. Use a do loop to figure out all the integers between 1 and 2000
which are leap years, put them into a large list, and return them.
Leap years, again, are divisible by 4, but not by 100. However,
numbers divisible by 400 are leap years.
You may wish to cons numbers to the front of the list - then
reverse at the end. Using append at each step will take you a
long while...
ITERATION & VECTORS
37. Write your own version of build-vector, using a do
loop. Once you are finished:
(build-vector 10 (lambda (i) i))
(my-build-vector 10 (lambda (i) i))
both of these should produce exactly the same output, for all
test cases.
38. Write vector-diff which will take in two vectors of the
same length, compare them item by item, and use display to
print out each pair of items that are different. Also, display
the index numbers at which you spotted any differences.
Use the built-in equal? predicate to compare the items, no
matter what type of data they are.
For instance:
> (vector-diff (vector "a" "b" "c")
(vector "a" "b" "c"))
should print nothing.
> (vector-diff (vector 1 2 3 4 5) ; v1
(vector 1 2 2 4 "blargh")) ; v2
should print something along the lines of:
DIFF AT INDEX: 2 v1: 3 v2: 2
DIFF AT INDEX: 4 v1: 5 v2: blargh
Again, you should only display, not return anything.
o What is the Contract?
o Based on the Contract, is this a function or procedure?
39. Define vector-map, which works just like the regular
map for lists, but works on a vector.
(a) Use a do loop and make-vector in your solution.
(b) Use build-vector and a lambda in your solution.
MATRICES
40. Write a function that will retrieve the middle element from a matrix,
assuming that the matrix has a middle element - that is,
the number of rows and columns is odd.
HINT: Do you seriously need a loop for this? I don't think so.
It can be done in one very messy and long line of code,
or more elegantly using a sequence of local variables,
defined one after another.
41. Using nested do loops, write a function that takes a number and a
matrix and multiplies each entry in the matrix by that number.
Call your function scalar-mult.
For example:
> (scalar-mult 5 (vector (vector 1 1 1)
(vector 1 0 1)
(vector 1 1 1) ))
(vector
(vector 5 5 5)
(vector 5 0 5)
(vector 5 5 5))
> (scalar-mult 0 (vector (vector 1 2 3)
(vector 2 4 6)
(vector 9 9 9) ))
(vector
(vector 0 0 0)
(vector 0 0 0)
(vector 0 0 0))
42. (a) Write matrix-ref, which works just like vector-ref,
but extracts a value from a matrix instead. You will need the
user to plug in two indices, i, j for the row and
column. Retrieve the j'th element from the i'th vector inside
the matrix.
(b) Write matrix-set!, which should work just like
vector-set! except for the differences outlined in part (a).
Your function should let the user change the element at row i,
column j, to some new value (which the user will also plug in).
NOTE: You may assumer that the indices i, j will be valid.
43. Using matrix-ref and matrix-set! as helper
functions, write matrix-add, which will add two matrices
together, item by item. The result will be a matrix of numbers.
Store the results inside the first matrix. This means the
numbers in the first matrix should be completely replaced by
the results. Return nothing.
NOTE: You may assume the matrices are of the same size. You may
not assume that the matrices are square.
DATA CONVERSION
44. Write a function called spoof! that will "magically" change a
vector into a list, and a list into a vector.
To do this you will need to create two local helper functions:
listify - changes a list into a vector
vectorify - changes a vector into a list
BOTH of them must use a do loop
If the user passes in a list, you must call vectorify on it to
convert it into a vector. If the user passes in a vector, you
must call listify on it to convert it into a list.
You must copy everything element by element from one format to
the other, and keep the same ordering. You may use functions like
length and reverse for the lists, if necessary, at the very
beginning or end of the processing.
You should write a Contract for all three functions.
If the user passes in something that is not a list or a vector,
you may make it "disappear" by returning: (void) in parentheses.
Some example calls:
> (spoof! (vector 1 2 3 4))
(list 1 2 3 4)
> (spoof! (list 'a 'b 'c 'd))
(vector 'a 'b 'c 'd)
> (spoof! 'NotAVector)
<-- disappears! because of (void)
> (spoof! "Definitely not a list...")
<-- also disappears!
NOTES: You may not make use of the built-in functions list->vector
or vector->list.
You'll need to make use of the predicates list?, vector?
Consider how the functions make-vector or build-vector may
assist you in the problem.
BIG O NOTATION
45. Consider the following data structures, of size N:
1. list
2. vector
3. sorted list
4. sorted vector
5. binary tree
6. binary search tree
7. full & balanced binary search tree
8. NxN matrix
For each, what is the order of efficiency (Big-O) of performing the
following operations, where applicable:
(a) retrieving the first/topmost item
(b) retrieving the last/bottom item
(c) retrieving an item from somewhere in the middle
(d) inserting a new item (without destroying the data organization)
(e) traversing all items
(f) finding the minimum item
(g) finding the median item
(h) searching for a specific item
(careful! binary search on a sorted vector is possible)
46. Know the following Big-O measures:
(a) mergesort & quicksort
(b) insertion sort
OO VOCAB
47. (a) What is state?
(b) What does the constructor do?
(c) What does a query/accessor do?
(d) What does a mutator/modifier do?
(e) What does a service manager do?
(f) What is encapsulation?
(g) What is inheritance? Polymorphism?
(h) What is the difference between an object and a class?
(i) What is the difference between a structure and an object?
(i) What function is necessary for creating a lexical closure?
LAB VOCAB
48. (a) What is netiquette?
(b) According to netiquette, your sig should be no longer than ...? *
(c) To remove a file in UNIX, use: ...?
(d) To make a new directory in UNIX, use: ...?
(e) To move from one directory to another (change directories), use: ...?
(f) To bring up the manual page about "cat", use: ...?
(g) The display command in MATLAB is: ...?
(h) The grid squares in an Excel spreadsheet are called: ...?
(i) I had the most trouble with this UNIX command: ___________.
(HINT: Fill in the blank. Go study it in Lab 2.)