EBoard 26: Dictionaries and Hash Table

This class will be recorded! Its use is limited to members of the class. Please do not share with others.

Approximate overview

  • General administrative stuff [~10 min]
  • Review of randomness quiz [~10 min]
  • Q&A [~10 min]
  • Quiz [~10 min]
  • Lab [~50 min]

Administrative stuff

Notes and news

  • SoLA 3 arrives tomorrow
  • Tomorrow’s class is optional. I will be here to answer any outstanding questions you might have.
  • I have some students who missed some materials and arranged incredibly late submissions. If you see late due dates, please ignore them.
    • And talk to me if you think there are reasons I should allow you to do additional late submissions. I’d rather that we find a way for you to show your skills.
  • I will not estimate grades for you. You can see how you are doing on Gradescope. (At least I hope you can.)
  • You probably saw a slew of quizzes returned on Sunday. I think I’ve now returned all of the quizzes. The graders are getting to lab writeups and self checks.
  • I have not yet gotten to the tokens. I spent Saturday writing LAs. I spent Sunday grading problems.
  • I’m increasing the default time on quizzes to 8 minutes. I hope that helps.
  • Did anyone notice the subtle change in lab text?

Tutoring reminders

  • Please use our tutors! They like to work.
  • Evening tutoring will be available
    • 8-10 p.m. Sundays through Thursdays
    • 3-5 p.m. Sundays
    • In the “Drop in tutoring” channel on the CS team.
    • Sam will generally not be available during these times.
    • We should have two evening tutors on Wednesdays.
    • Don’t forget to cite the evening tutors (and the individual tutors).

Upcoming activities

Attend (or watch recording within a day or so) and send a one-paragraph reflection asap afterwards.

Only those activities I list count.

  • Noon, TODAY, CS Extras
  • Some time, TODAY, Art Museum Thingy
  • Noon, Wednesday, 9 December 2020: Community Convesation
  • 5:00 p.m. Thursday, 10 December 2020: MIST: The Mathematical Image Synthesis Toolkit. Sam’s research students. (+2 tokens, even if you critique their work)

Upcoming work

Mini Projects

Readings

  • No reading for Tuesday.
  • Reading for Wednesday
    • Trees
    • Wait until after Tuesdays’s class.

Quizzes

  • Quiz today: Pair structures and/or vectors
  • No quiz tomorrow
  • Quiz Wednesday: Hash Tables and Dictionaries

Other

  • SoLA 3 Tuesday (no quiz)

SoLA 3 Notes and Q&A

  • Mentor sessions tonight at 5pm and 9pm (I think).
  • I am happy to set up longer time limits on SoLA 3 if you think it would be helpful (e.g., to relieve anxiety).
    • Please let me know by 8pm tonight what time limit you want (30, 35, 40, 60 are typical).
    • I’ll also set up a sample problem so that you can check that you have the right time limits.
  • Sample questions posted. Answers in mentor sessions, tomorrow’s class, or in the Q&A channel on Teams.
  • There are 20 LAs on SoLA 3. Only do the ones you have not gotten points for.
    • Six from Group 1
    • Seven from Group 2
    • Seven from Group 3
    • I’ve tried to name them clearly.

Q&A

Will you answer a question about the sample problems?

Yes, in class tomorrow.

The mentors will answer some tonight.

I’ll also answer some on Teams.

Will we have to trace through vector code or write procedures?

Yes. Sam added the trace questions because tracing helps you better understand what’s going on, which better prepares you.

Mini-project 4 Notes and Q&A

  • Mini-Project 4 should be returned by the end of the day today.
  • I’ve started adding some more test cases to the instructions so that you can catch some of your own errors.
  • We’re now at the point that I will not accept work that regularly violates the basic stylistic guidelines, particularly with regards to indentation and spacing.
  • Note: When you get this right, you’ll find that your procedures are surprisingly short.
    • My match? is 23 lines of code (plus some comments)
    • One of the nice shorter student versions of match is 34 lines of code (plus some comments), with no helpers.
    • Another of the nice shorter student versions of match is 38 lines of code, including the code in helpers (but not including comments).
    • My find-match is 7 lines of code, using if, and, and or, with no helpers (other than match?)
    • One of the shorter student versions of match is 17 lines of code, none of which has more than four “words” on it, plus some parentheses and brackets. (Yes, that’s short enough.)
    • An even shorter student version is 12 lines long, although the do have five “words” on one line.
  • Please make sure to cite the evening tutors if you use them.
  • Please put all of the tests at the end of the file so it’s easier for the grader to find your code.

Q&A

Nope

Friday’s quiz on randomness

Select a random number between 80 and 100, inclusive.

People who know random better than Sam wrote:

(define random-grade
  (lambda ()
    (random 80 101)))

That is, random can take two parameters and generate a random integer between the lower bound (inclusive) and the upper bound (exclusive).

Sam didn’t know that there’s a random with two parameters, so he expected something like the following. There are twenty-one different numbers that the procedure might generate.

(define random-grade
  (lambda ()
    (+ 80 (random 21))))

More negative people wrote:

(define random-grade
  (lambda ()
    (- 100 (random 21))))

Q&A

Mini-Project 5

My procedure counts syllables. But it takes lists of characters instead of strings. How do I do that with Jayne Eyre?

You should be splitting Jayne Eyre into a list of words.

And you can then filter that list appropriately.

(define three-syllable words (filter (o (= <> 3) count-syllables) je))

Should we use file->lines or file->words?

I was assuming file->string and then some clever additional work, like string-replace or remove* and then string-split.

(string-replace
 (string-replace (file->string "jayne-eyre.txt") "\r" "")
 "\n
 "")

For “Write a procedure (abab words) that takes as input a corpus (list) of words and generates a “random” quatrain of four lines of four words. The last words of the first and third lines must rhyme, as must the last words of the second and fourth lines.” do we use one corpus or two?

I’d prefer one, which is how I specified it.

If your aesthetics suggest two words, you can write another procedure, (abab2 awords bwords), and then rewrite abab as a call to that.

(define ababa
  (lambda (words)
    (abab2 words words)))

Does it still count as a rhyme if it’s the same word?

Yes. This is experimental code. And what if your word is orange?

Randomness

Pairs and Pair Structures

How do you show a downward arrow in ASCII art?

If the first section has a downward arrow, it’s the first parameter to the cons.

  • If the second section has a downward arrow, it’s the second parameter to the cons.

Vectors

Why use a list over a vector?

Lists are dynamic: You can extend them with cons and shrink them with cdr. That’s often easier/faster than building a new vector.

To change the size of a vector, you need to build a new vector and copy everything over.

Lists are immutable, which leads to some clarity.

Can you talk about why immutability adds to clarity?

> (define vec1 (vector 'a 'b 'c 'd))
> (define lst1 (list 'a 'b 'c 'd))
> (define vec2 vec1)
> (define lst2 lst1)
> ; How do we make `e` the first symbol of `lst1`, replacing 'a.
(cons 'e (cdr lst1))
'(e b c d)
> (define lst1 (cons 'e (cdr lst1)))
> lst1
'(e b c d)
> ; Will that change lst2?
; Is lst2 the same?
; Because lst2 was lst1 but ...
lst2
'(a b c d)
> vec1
'#(a b c d)
> ; How do I set the first element of vec1 to 'e?
(vector-set! vec1 0 'e)
> vec1
'#(e b c d)
> ; Have we changed vec2?  Yes.
> vec2
'#(e b c d)

When you change something in a vector, you may find that something you didn’t expect to change changes.

What happened?

Because lists are immutable, when we rebuilt lst1, we actually built a new list (a new cons at the start, etc.) So lst1 became something different.

However, vec1 and vec2 refer to the same vector. If we change one (vectors are mutable), we change the other.

How can we define a copy of a vector?

With the confusingly named vector-copy.

> (define vec1 (vector 'a 'b 'c 'd))
> (define vec2 (vector-copy vec1))
> (vector-set! vec1 0 'E)
> vec1
'#(E b c d)
> vec2
'#(a b c d)

Is it dangerous to define a vector as the result of applying a procedure to another vector?

It can be. But some procedures return new vectors, and that’s okay.

There’s a reason we don’t have the ! procedures return vectors.

Hash Tables

Will you go over the self-check problems?

Nope. Discuss them with your partners and then call in a staff member.

For the table model, I couldn’t figure out how to make the set-phone! work.

See the prior question.

Miscellaneous

Where should citations go?

I like general citations at the top and specific citations in the body.

I care more about the attempt than the perfection of the attempt.

Is there a line between the two?

Do both!

Quiz

  • You know the drill.

Lab

  • Don’t forget to review the included procedures!