CSC 151.01, Class 40: Pause for breath
Overview
- Preliminaries
- Notes and news
- Upcoming work
- Extra credit
- Questions
- A few more notes on searching and sorting
- About the final
- About presentations
- Presentation preparation time
News / Etc.
- Project grades delayed.
- I plan to have all your grades to you Monday of finals week.
- We will discuss quiz 13.
- I have a flash drive for your data.
Upcoming work
- Writeup for class 39 due Monday at 10:30 p.m.
- Exercises 4f, 4g, and 4h.
- To: csc151-01-grader@grinnell.edu
- Subject: CSC 151.01 Writeup 39 (YOUR NAMES)
- Exam 4
- Exam due Tuesday
- Cover sheets due Wednesday
- Epilogues due Wednesday.
- No more readings.
- No more quizzes.
Extra credit (Academic/Artistic)
- CS Table Tuesday: Exotic programming languages.
- CS Extras Thursday: Summer opportunities in CS.
Extra credit (Peer)
- Percussion ensemble Tuesday at 7:30 p.m. in Sebring Lewis.
- Musical: Next to Normal Friday at 7:30, Saturday and Sunday at 2:30. Tickets at box office.
Extra credit (Misc)
- Newtown movie Tuesday night.
- Whoops! It conflicts with exam 4.
- If you see the movie, you can have an extra day on Exam 4.
Other good things
- Stay healthy and sane during week 14.
Questions
- How much of the original should I preserve on problem 1?
- As much as possible. For example, your solution to the first problem
should continue to (a) use
letrec, (b) have a tripartitecond, and (c) return the original list when given an index out of bounds. - Any hints on problem 2?
- “Trust the magic recursion fairy.” Use direct recursion. Assume that the tally procedure will succeed on the cdr. Assume that you can use it on any sublist. Work from there.
- Should
vector-filterreturn anything? - Yes. The procedure builds a new vector. It should return that vector.
- Can
vector-filtermodify the original vector? - No.
- Does
next-largerhave to work with vectors that include negative numbers? - Yes.
- Any hints on
next-larger? - Make a list (human, not computer) of the possible cases.
- Make sure you try all of the examples we’ve given you.
- Any hints on flattening trees?
- The problem suggests writing a helper that keeps track of the flattened version of everthing that will appear to the right. That’s a good strategy. Start with the empty list.
- I keep getting the same result when I call
ioe. Is that intentional? - If you use the correct parameters, you should get a variety of results.
Perhaps you should figure outvss!first and see how it callsioe.
A few more notes on searching and sorting
Some reminders
- Goal of searching: Find a “matching” element.
- Goal of sorting: Order the elements.
- Most elements are “compound” - they contain lots of information, not
just the part we search/sort by.
'("Curtsinger" "Charlie" "systemsguy" "1234" "CSC" "Smart" "CSC-151")'#("Rebelsky" "Esssay" "muser" "3210" "CSC" "Annoying")'((lname "Rebelsky") (fname "Sam") (username "rebelsky") ...)
- Each of these elements has a “key” - the thing we search or sort by.
- When searching or sorting, we need to identify how to extract the “key” from a compound object and how to order by those keys once extracted.
- The “key” may differ from problem to problem. Sometimes we’ll use last name, sometimes we’ll use userid, etc.
Quiz 13
- Problems 1 and 2 gave you a slightly different kind of compound object.
Instead of a list of strings, your compound object is just one string.
"Curtsinger Charlie systemsguy 1234"
- How do we extract the last name? Assume we don’t have
string-split. You may, however, usesubstring.- Approach 1: Use
string-refto find the space. (Iterate through the positions until you find the space.) Grab everything before it withsubstring. (Seems good.) - Approach 2: Turn it into a list of characters with
string->list. Use recursion to find the space. Somehow grab the characters up to that point and turn them back into a string. - Approach 3: Turn it into a list of characters with
string->list. Useindex-ofto find the space. Do something sensible.
- Approach 1: Use
- Note: We assume that names don’t have spaces.
- If we have
string-split, the code becomes a bit easier.(car (string-split str " "))
For problem 1a, we thought you might write
(define get-last-name
(lambda (str)
(car (string-split str " "))))
We hoped you would write
(define get-last-name
(o car (r-s string-split " ")))
For problem 1b, we wanted something similar, but with cadr.
For part 2, it was mostly a case of applying what you’d just written.
“Sort the directory in reverse alphabetical order by last name”
(vector-sort! contacts get-last-name string>=?) ; Good
(vector-sort! contacts get-last-name (lambda (a b) (not (string<=? a b))))
; When you forget that string>=? exists
(vector-sort! contacts get-last-name (negate string<=?))
; What you should do when you forget that string>=? exists
“Sort the directory in alphabetical order by last and first name”
(vector-sort! contacts
(lambda (str)
(string-append (last-name str) ", " (first-name str)))
string<=?)
“Assuming the directory is sorted by last name, search for Curtsinger””
(binary-search contacts get-last-name string<=? "Curtsinger")
Thoughts on merging
How does merge work?
- Hypothesis one: Repeatedly takes an element from the second list and puts it in the correct place in the first list.
- Hypothesis two: Repeatedly compare the first value in each list and grab the smaller of the two to put into a new list.
- Although both would be reasonable, we use the second strategy (which ends up being faster).
How many times is merge called in (merge '(1 2 3 4) '(5 6 7 8) <=)?
(merge '(1 2 3 4) '(5 6 7 8))
=> (cons 1 (merge '(2 3 4) '(5 6 7 8)))
=> (cons 1 (cons 2 (merge '(3 4) '(5 6 7 8))))
=> (cons 1 (cons 2 (cons 3 (merge '(4) '(5 6 7 8)))))
=> (cons 1 (cons 2 (cons 3 (cons 4 (merge '() '(5 6 7 8))))))
=> (cons 1 (cons 2 (cons 3 '(4 5 6 7 8))))
=> (cons 1 (cons 2 '(3 4 5 6 7 8)))
=> (cons 1 '(2 3 4 5 6 7 8))
=> '(1 2 3 4 5 6 7 8)
Answer: Five (5)
How many times is merge called in (merge '(1 3 5 7) '(2 4 6 8) <=)?
(merge '(1 3 5 7) '(2 4 6 8))
=> (cons 1 (merge '(3 5 7) '(2 4 6 8)))
=> (cons 1 (cons 2 (merge '(3 5 7) '(4 6 8))))
=> (cons 1 (cons 2 (cons 3 (merge '(5 7) '(4 6 8)))))
=> (cons 1 (cons 2 (cons 3 (cons 4 (merge '(5 7) '(6 8))))))
=> (cons 1 (cons 2 (cons 3 (cons 4 (cons 5 (merge '(7) '(6 8)))))))
=> (cons 1 (cons 2 (cons 3 (cons 4 (cons 5 (cons 6 (merge '(7) '(8))))))))
=> (cons 1 (cons 2 (cons 3 (cons 4 (cons 5 (cons 6 (cons 7 (merge '() '(8)))))))))
=> (cons 1 (cons 2 (cons 3 (cons 4 (cons 5 (cons 6 (cons 7 '(8))))))))
=> ...
=> '(1 2 3 4 5 6 7 8)
Answer: Eight (8)
Observation: merge can give the same results, but take different numbers
of steps. It dependds on how interleaved the two lists are.
About the final
A sample final is posted online.
Optional! If you do better on the final than on one of your exams, it replaces the lowest exam. If you do worse, it gets ignored. YOU DO NOT HAVE TO SHOW UP!
In-class, like a really hard quiz.
Four problems. All mostly correct: A. Three mostly correct: B. Two mostly correct: C. One mostly correct: D. Zero mostly correct: F. Don’t show up: zero.
Closed book, one page of hand-written notes. 8.5x11 or A4 page. Double sided.
You may take the final
- Thursday, Dec 14, 9-noon in Science 3813
- Wednesday, Dec 13, 2pm-5pm in Science 3813
- Friday, Dec 15, 9-noon in Science 3813
We try to make the final so that you can finish it in an hour if you’ve been keeping up with the material.
About presentations
Each group gets five minutes to talk about what they did. Demo, describe, show an algorithm, …
You may use this computer. You may use the whiteboard. You may use your own laptop (with HDMI)
- Slide decks not necessary.