Lecture 23
Imperatives, HashMaps, and OOP
Logistics and Week Ahead
Today - Imperative Review / OOP / Hash Structures + MQ16 (in-class)
Next Week (Week 10)
Next Next Week (Week 11 - ThxGiving)
The week ahead the week ahead the week ahead
Week 12 (December 1st)
Reminder: only classes offered by Weinberg Departments have "Reading Week"
Ethics Module 9 - Reflection (Async - For Everyone) - due 12/7 at 11:59pm
Week 13 (Finals) Week
Q4 will be cumulative but still same format and still 49 minutes; if you want to take it with the other section you may, but I need to know in advance (at the latest Friday of Week 12) and you need to verify you don't have an exam conflict!
Quiz 3 - 11/21 - Friday
Some review of imperatives via PollEverywhere will be available via the video recording of today's lecture! You can also see the lecture examples via the purple button on our canvas page.
A whole new approach to program design
Terminology changes across languages*
Throwback Friday
Visualization of Mushroom Kingdom
Looking up a person to the database
; lookup-v1: number database -> employee or false
; find the person in the database via their ssn
; false if not found
(define (lookup-v1 n db)
(cond [(empty? db) false]
[(= n (employee-ssn (first db))) (first db)]
[else (lookup-v1 n (rest db))]))
If we had 100 employees:
Visualization of Mushroom Kingdom
Looking up a person to the database
(define (lookup-v2 n db)
(cond [(empty? db) false]
[(< n (employee-ssn (first db))) false]
[(= n (employee-ssn (first db))) (first db)]
[else (lookup-v2 n (rest db))]))
If we have 100 employees…
Visualization of Mushroom Kingdom
Looking up a person to the database
; lookup-v3: number database -> person or false
; find the person in the database via their ssn
; false if not found
(define (lookup-v3 n db)
(cond [(empty? db) false]
[(= n (employee-ssn (db-node-employee db))) (db-node-employee db)]
[(< n (employee-ssn (db-node-employee db))) (lookup-v3 n (db-node-left db))]
[(> n (employee-ssn (db-node-employee db))) (lookup-v3 n (db-node-right db))]))
Linear Search vs. Binary Search
Mathematical Notation | Class Name | When the input doubles... |
| Constant | Runtime is unchanged ( ... x 1 ) |
| Logarithmic | Runtime increases by a constant |
| Linear | Runtime is doubled ( ... x 2 ) |
| Quasilinear | Runtime is approximately doubled |
| Quadratic | Runtime is quadrupled ( ... x 4 ) |
| Cubic | Runtime is octupled ( ... x 8 ) |
| Exponential | Runtime gets...really big |
Common Big O Classes
Mathematical Notation | Class Name | When the input doubles... |
| Constant | Runtime is unchanged ( ... x 1 ) |
| Logarithmic | Runtime increases by a constant |
| Linear | Runtime is doubled ( ... x 2 ) |
| Quasilinear | Runtime is approximately doubled |
| Quadratic | Runtime is quadrupled ( ... x 4 ) |
| Cubic | Runtime is octupled ( ... x 8 ) |
| Exponential | Runtime gets...really big |
Common Big O Classes
Linear Search
Mathematical Notation | Class Name | When the input doubles... |
| Constant | Runtime is unchanged ( ... x 1 ) |
| Logarithmic | Runtime increases by a constant |
| Linear | Runtime is doubled ( ... x 2 ) |
| Quasilinear | Runtime is approximately doubled |
| Quadratic | Runtime is quadrupled ( ... x 4 ) |
| Cubic | Runtime is octupled ( ... x 8 ) |
| Exponential | Runtime gets...really big |
Common Big O Classes
Binary Search
Key-Value Maps
"Favorite Color" ➡️ "green" or "Worst Color" ➡️ "orange"
Introducing HashMaps!
Using HashMaps
; make-hash : (listof (listof X Y)) -> (hashOf X Y)
; Creates a hash of a list of pairs (lists) by using the first of each
; pair as the Key and the other as the Value
; hash-ref : (hashOf X Y) X T -> Y
; Returns a value (Y) by looking up the key (X) in the hashmap; returns
; the 3rd input (T) if the key isn't in the map.
; hash-set! : (hashOf X Y) X Y -> (void)
; Updates a given key-value pair in a given hashmap
; Effect: modifies the given hash to include / update the given pair
What's a Hash?
Sabrina Carpenter
Taylor Swift
SZA
Rihanna
Keys
Hash function
00
01
02
03
04
…
11
Hashes
10
What's a Hash?
Sabrina Carpenter
Taylor Swift
SZA
Rihanna
Keys
Hash function
00
01
02
03
04
…
11
Hashes
10
COLLISION!
HashMap
Espresso
Sabrina Carpenter
Taylor Swift
SZA
Rihanna
Keys
Hash function
00
01
02
03
04
…
11
Hashes
10
Values
Umbrella
Cruel Summer
Kill Bill
Why? It's all about lookup times.
Espresso
Sabrina Carpenter
Taylor Swift
SZA
Rihanna
Keys
Hash function
00
01
02
03
04
…
11
Hashes
10
Values
Umbrella
Cruel Summer
Kill Bill
Looking up a person to the database
; lookup-v4: number (hashmapOf number person) -> person or false
; find the person in the database via their ssn
; false if not found
(define (lookup-v4 n employee-db-as-hashmap)
(hash-ref employee-db-as-hashmap n #false))
Mathematical Notation | Class Name | When the input doubles... |
| Constant | Runtime is unchanged ( ... x 1 ) |
| Logarithmic | Runtime increases by a constant |
| Linear | Runtime is doubled ( ... x 2 ) |
| Quasilinear | Runtime is approximately doubled |
| Quadratic | Runtime is quadrupled ( ... x 4 ) |
| Cubic | Runtime is octupled ( ... x 8 ) |
| Exponential | Runtime gets...really big |
Common Big O Classes
HashMap
Lookup