Lecture 23 Slides - Imperatives, OOP, and HashMaps

1 of 27

Lecture 23

Imperatives, HashMaps, and OOP

2 of 27

Logistics and Week Ahead

Today - Imperative Review / OOP / Hash Structures + MQ16 (in-class)

  • Exercise 7 due at 11:59pm (see hints post on edSTEM for…well..hints)
  • Ethics Module 8 Reflections Due Sunday at 11:59pm

Next Week (Week 10)

  • Monday - Q3 Review + MQ17 (in-class)
    • Ex8 released - will be due on Monday of ThxGiving Week at 11:59pm
  • Wednesday - Files and Folders (Pre-Recorded + MQ18) + Tutorial 9
  • Friday - Quiz 3
    • ANU Students, reminder to schedule your Testing Center appointment

Next Next Week (Week 11 - ThxGiving)

  • No in-person events (including Office Hours)
  • Monday - Programming Languages (Pre-Recorded/Asynchronous + MQ19)
    • Ex8 Due - 11:59pm; MQ 19 will be due the following Monday at 11:59pm
  • Wednesday/Friday - University Closed

3 of 27

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

  • Monday - Soft skills, Debugging, Testing
  • Wednesday - Intro to Asteroids (Pre-Recorded) MQ20 + Tutorial 10
  • Friday - Q4 Review + AMA (Exercise 9 Due)

Week 13 (Finals) Week

  • Monday (12/8) - Q4 (Section 2) - 9:00am - TCH Auditorium
  • Wednesday (12/10) - Q4 (Section 1) - 9:00am - TCH Auditorium

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!

4 of 27

Quiz 3 - 11/21 - Friday

  • In-person here in the Auditorium at the time you are officially registered (those with testing accommodations should follow the usual procedures)
    • If you're in the 2pm section, I'm willing to let you take it during the 1pm section but I need to know in advance!
  • Make sure to bring your WildCard to class. You may receive an assigned seat (you'll get a Canvas message).
  • Taken on the Lockdown Browser on your personal computer; details on how to set it up are on our Canvas page as well as on the Quiz 3 page.
  • Very important to be connected to eduroam on Friday.
  • Three Types of Problems (49 minutes):
    • What's the type of this expression?
    • Here's a program; here's what we want to get out of it; here's what we get; fix it
    • Write a valid test for the given function definition
  • Covers everything up to and including Lecture 25 with a focus on imperatives and updated structs
    • You’ll be given glossary entries for specialized functions we learn about for HashMaps and Files/Folders
  • You do NOT need to memorize all of the functions (you get a glossary)
  • You DO need to know the Rules of Execution and Special Forms (which have gotten more extensive in recent weeks)
  • CHARGE YOUR COMPUTER THE NIGHT BEFORE.

5 of 27

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.

6 of 27

A whole new approach to program design

  • It's focused on seeing the world as objects which contain both data and code
    • Data in the form of fields (properties)
    • Code in the form of methods
  • It's commonly referred to as Object-Oriented Programming (OOP)
    • It's worth noting that historically this term has been used to describe a number of different things
      • Specific programming features
      • A programming methodology
      • Specific languages
  • Not all OOP languages implement the same features
    • Java and C# have interfaces
    • C++ has multiple inheritance
    • JavaScript has prototype-based inheritance

7 of 27

Terminology changes across languages*

  • What we call structs are often called classes
  • What we call struct fields/properties are sometimes called attributes or class variables
  • What we call methods are sometimes called class functions
  • One new term for us, a struct instance
    • (define-struct car (mileage)) ; a new struct
    • (define test-car (make-car 1000)) ; a new instance of a struct

8 of 27

Throwback Friday

9 of 27

Visualization of Mushroom Kingdom

10 of 27

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:

  • Best Case: We find the employee right at the beginning of our db – 1 lookup
  • Worst Case: We go through the entire db and don't find them – 101 lookups (n + 1).
  • Average Case: The employee is in the middle of the list - 50 lookups (n / 2).

11 of 27

Visualization of Mushroom Kingdom

12 of 27

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…

  • Best Case: We find the employee right at the beginning of our db – 1 lookup or the ssn is smaller than the one first.
  • Worst Case: We go through the entire db and don't find them – 101 lookups (n + 1).
  • Average Case: The employee is in the middle of the list - 50 lookups (n / 2).

13 of 27

Visualization of Mushroom Kingdom

14 of 27

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))]))

  • Best Case: The employee we want is the root of the tree – 1 lookup.
  • Worst Case: The employee we want is at the bottom of the tree - how many times do we need to divide by 2? (lg n) (this is log base 2). (lg 128) = 7, (lg 256) = 8, (lg 512) = 9…
  • Average Case: The math here is harder…we'll save it for CS 212 and 214!

15 of 27

Linear Search vs. Binary Search

16 of 27

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

17 of 27

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

18 of 27

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

19 of 27

Key-Value Maps

  • In real life, we often find the need to store information in Key-Value Pairs

"Favorite Color" ➡️ "green" or "Worst Color" ➡️ "orange"

  • When we have a bunch of these pairs, we call it a Key-Value Map

  • Some examples…
    • A dictionary where the key is the word we wish to lookup and the definition is the associated value
    • In a translation dictionary, the key might be the word in English and the value might be the same word in Portuguese
    • At a bank the key might be your account number and the value is your balance
    • On CAESAR, the key might be your StudentID# and the value might be your transcript info

20 of 27

Introducing HashMaps!

  • A HashMap is a specialized data structure that stores data using "associations"
    • In other words, it's built to store data as Key-Value Pairs and is an example of a Key-Value Map
    • It works very similarly to the Dictionary data type in Python (though with different syntax)
  • Like lists, it's a data structure that is automatically built into ASL (unlike trees which we defined as inductive data structures).
    • You can read all of the formal documentation here, but we really only want to know about three functions that I'll show in the following slides

  • Note for people continuing to 214: technically the HashMap built into ASL is actually a HashTable.

21 of 27

Using HashMaps

  • We need a constructor to create them:

; 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

  • We need an accessor to lookup a value using some key:

; 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.

  • We need a mutator to update the values stored in a key:

; 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

22 of 27

What's a Hash?

  • A hash (short for hash function) is formally a mathematical function that can be used to map data of some arbitrary size to a fixed-size value space.
  • In simpler terms, it's a function that can map any given input (say a key) to a particular bucket (or "hash") in memory

Sabrina Carpenter

Taylor Swift

SZA

Rihanna

Keys

Hash function

00

01

02

03

04

11

Hashes

10

23 of 27

What's a Hash?

Sabrina Carpenter

Taylor Swift

SZA

Rihanna

Keys

Hash function

00

01

02

03

04

11

Hashes

10

COLLISION!

  • A hash (short for hash function) is formally a mathematical function that can be used to map data of some arbitrary size to a fixed-size value space.
  • In simpler terms, it's a function that can map any given input (say a key) to a particular bucket (or "hash") in memory

24 of 27

HashMap

  • So a HashMap uses a hash function to map keys to hashes which are assigned particular values

Espresso

Sabrina Carpenter

Taylor Swift

SZA

Rihanna

Keys

Hash function

00

01

02

03

04

11

Hashes

10

Values

Umbrella

Cruel Summer

Kill Bill

25 of 27

Why? It's all about lookup times.

  • To lookup in a list, we have to look through the list one thing at a time
  • To lookup in a tree, we have to traverse the branches one level at a time
  • To lookup in a HashMap, what do we have to do?

Espresso

Sabrina Carpenter

Taylor Swift

SZA

Rihanna

Keys

Hash function

00

01

02

03

04

11

Hashes

10

Values

Umbrella

Cruel Summer

Kill Bill

26 of 27

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))

  • Best Case: Just the cost of the hash function.
  • Expected Worst Case: Assuming there aren't any collisions, just the cost of the hash function.
  • Average Case: Just the cost of the hash function.

27 of 27

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