Compsci 201 201 Exam 2 2 Re Review, C , Compar arat ator ors, - - PowerPoint PPT Presentation

compsci 201 201 exam 2 2 re review c compar arat ator ors
SMART_READER_LITE
LIVE PREVIEW

Compsci 201 201 Exam 2 2 Re Review, C , Compar arat ator ors, - - PowerPoint PPT Presentation

Compsci 201 201 Exam 2 2 Re Review, C , Compar arat ator ors, A , APTs Par art 1 1 of of 4 Susan Rodger April 8, 2020 4/8/2020 Compsci 201, Spring 2020 1 U is for Unix Basis for Linux and Android and GNU and


slide-1
SLIDE 1

Compsci 201 201 Exam 2 2 Re Review, C , Compar arat ator

  • rs, A

, APTs Par art 1 1 of

  • f 4

4/8/2020 Compsci 201, Spring 2020 1

Susan Rodger April 8, 2020

slide-2
SLIDE 2

U is for …

  • Unix
  • Basis for Linux and Android and GNU and …
  • User Interface/UI, User Experience/UX
  • User is the heart and soul

4/8/2020 Compsci 201, Spring 2020 2

slide-3
SLIDE 3

Google interface

  • Maris

issa M Mayer

  • Google –employee

number 20

  • Yahoo CEO
  • Lumi Labs

4/8/2020 Compsci 201, Spring 2020 3

slide-4
SLIDE 4

Announcements

  • APT

PT-6 du due T Tuesda day, A April il 7

  • APT

PT-7 o

  • ut

ut t thi his week and nd d due ue A April 1 16 (no note Thu hursday)

  • Assig

ignme ment P t P5 Percolatio tion

  • Code due Thursday, April 9, grace thru April 13
  • Assig

ignme ment P t P6 Huffma man o

  • ut e

end o d of week, k, du due April il 22

  • Discussi

ssion

  • n 12

12 Monda day, A April il 13 13

  • Pre-discussion out at end of week
  • Exam 2

m 2 is A April il 10 10 – Your ur o

  • wn w

n work!

  • APT

PT Q Quiz 2 2 is A April 11 11-15 15 – Your ur o

  • wn w

work!

4/8/2020 Compsci 201, Spring 2020 4

slide-5
SLIDE 5

PfSWoA

  • Exa

Exam 2 Review and and L Logis istics

  • O-Notation, Big-Oh, Recurrences
  • Linked Lists
  • Binary Trees
  • Comparator R

Review

  • API for sorting, solving problem review
  • Helpful on next APTs out

4/8/2020 Compsci 201, Spring 2020 5

slide-6
SLIDE 6

Exam 2

  • Take any t

y time me on F Frida iday A April il 1

  • 10. M

Maybe be earlie lier.

  • Ta

Take on

  • n Gradesc

scop

  • pe, DO

DO NOT OT GO GO TO TO GRADE ADESCOPE UNTI UNTIL YO YOU ARE READY

  • ONCE YOU CLICK ON IT, YOUR TIMER STARTS
  • If

If you canno annot take ke it it on

  • n Frida

iday, April il 10 10

  • Fill out form to tell us why, and the soonest day you

can take it

  • Fill ou
  • ut a ref

eflect ect fo form afte ter taking the he e exam

  • Get 2 extra points
  • Give us feedback

4/8/2020 Compsci 201, Spring 2020 6

slide-7
SLIDE 7

More Exam 2 Details - GradeScope

  • MC,

C, sho hort ans answer, and and s sho hort co code e seg egments.

  • You

u will t ill type in, o in, or clic lick o

  • n

n ans answers

  • Sug

uggest: w writ ite c code o

  • n

n pap aper and and t then hen type it e it in. in.

  • Submi

mit eac each ques uestion as as yo you go.

  • .
  • Lose internet, just connect back in
  • Exam

am is is ab about 1 1 ho hour ur, 15 15 min in

  • Yo

You get an an ex extra 1 ho hour ur, 15 15 min f in for lo logistics

  • Total t

al time i e is 2 2 hr hr, 30 , 30 min in

  • Tho

Those w wit ith h acc accommodations, K Kate w will em ill email

4/8/2020 Compsci 201, Spring 2020 7

slide-8
SLIDE 8

Exam 2 – Honor Code

  • The e

exam is s you

  • ur w

wor

  • rk on
  • nly
  • Use

se book books, op

  • pen notes

es, c code e you h u have w written en

  • Write c

e code e on paper er o

  • nly, t

then t en type e into Gradesc scop

  • pe
  • DO

DO NOT OT write te code de and nd run un it it in in I IntelliJ, Jshel hell or o

  • ther

her comput uter er m means ans

  • DO N

NOT Search on t n the he w web for a ans nswers to problems

  • DO NOT Talk to

lk to any ny human ans a about ut the e e exam am d dur uring the e exam p m perio iod

  • DO

DO NOT OT Ta Talk to to any ny hum humans abou

  • ut th

the exam am until t til the exam i is ha handed back

4/8/2020 Compsci 201, Spring 2020 8

slide-9
SLIDE 9

Now let’s review for the exam

4/8/2020 Compsci 201, Spring 2020 9

slide-10
SLIDE 10

Runtime and Other Analysis

  • Asymptotic

ic c comple lexit ity of runtim ime o e or memory

  • Return value: bleem(10)=88, bleem(20)=360
  • 1+2+…+n=O(n2)
  • Run

untim ime and and v val alue

  • sum += 1

4/8/2020 Compsci 201, Spring 2020 10

slide-11
SLIDE 11

Intuition and Reality

  • "f

"fun un" x 4 = 4 = " "funf nfunf unfunf unfun un"

  • https://www.youtube.com/watch?v=VbrEsOLu75c
  • Analyze only in terms of n, say with "fun"
  • It's all about line 34, how much does it matter?
  • 3, 6, 9,

3, 6, 9, 12, 12, …, 3n = , 3n = 3( 3(1 1 + 2 2 + … + n) n) s so b big ig-Oh Oh?

  • What if length of base is m, then big-Oh

4/8/2020 Compsci 201, Spring 2020 12

slide-12
SLIDE 12

Buffering … and faster

  • "f

"fun un" x 4 = 4 = " "funf nfunf unfunf unfun un"

  • It's all about line 41, how much does is matter?
  • 3, 3, 3,

3, 3, 3, … ,3 = ,3 = so 3n and 3n and O(n) n)

  • What if length of base is m, then big-Oh?

4/8/2020 Compsci 201, Spring 2020 13

slide-13
SLIDE 13

WOTO-O Review

http:// //bi bit.ly/2 /201spr pring20-04 0408 08-1

4/8/2020 Compsci 201, Spring 2020 14

slide-14
SLIDE 14

Compsci 201 201 Exam 2 2 Re Review, C , Compar arat ator

  • rs, A

, APTs Pa Part 2 2 of 4 f 4

4/8/2020 Compsci 201, Spring 2020 15

Susan Rodger April 8, 2020

slide-15
SLIDE 15

Recursive Insert and Recurrences

  • Recur

currence nce rela elation f for .ad .add b belo elow? ? (TreeD

reeDem emo.j .java)

  • https://coursework.cs.duke.edu/201spring20/classcode/
  • T(N)is time to add one value to tree of N values, what

is complexity of adding N values one-at-a-time?

4/8/2020 Compsci 201, Spring 2020 16

slide-16
SLIDE 16

Understand and Analyze

  • If tree h

has as N values ues

  • One recursive call: T(n) = T(n/2) + O(1)
  • worst: T(n) = T(n-1) + O(1), solutions?
  • We

e do thi his n n tim

  • imes. U

Upper er b bound und n n lo log n, n, but ut … …

  • log(1) + log(2) + … + log(n) = log(1 ✕2 ✕.. ✕ n)
  • log(n!)
  • n log n
  • Stirling's Formula

4/8/2020 Compsci 201, Spring 2020 19

slide-17
SLIDE 17

Recurrence Relations

Recurrence Algorithm Solution

T(n) = T(n/2) + O(1) binary search O(log n) T(n) = T(n-1) + O(1) sequential search O(n) T(n) = 2T(n/2) + O(1) tree traversal O(n) T(n) = T(n/2) + O(n) quicksort partition O(n) T(n) = 2T(n/2) + O(n) mergesort, quicksort O(n log n) T(n) = T(n-1) + O(n) selection or bubble sort O(n2)

4/8/2020 Compsci 201, Spring 2020 20

No need to derive, remember or look up

slide-18
SLIDE 18

WOTO Midterm Tree Review

http:// //bi bit.ly/2 /201spr pring20-04 0408 08-2

4/8/2020 Compsci 201, Spring 2020 21

slide-19
SLIDE 19

Compsci 201 201 Exam 2 2 Re Review, C , Compar arat ator

  • rs, A

, APTs Pa Part 3 3 of 4 f 4

4/8/2020 Compsci 201, Spring 2020 22

Susan Rodger April 8, 2020

slide-20
SLIDE 20

Dina Katabi

  • Born i

in Syria ia, , Professor at at MIT

  • ACM Hopper Award, ACM Fellow
  • MacArthur Genius Award
  • AI that sees through walls?
  • http://bit.ly/katabi-ted

4/8/2020 Compsci 201, Spring 2020 23

https://www.nytimes.com/2013/10/20/opinion/sunday/dina-katabi.html

When I was a kid, I was, …, very much fascinated by Star Wars, and what fascinated me the most is this notion of the Force, this energy that connects all people and all objects and allows you to feel people that you can't even see. And I remember many nights, I would be sitting at home, just, like, concentrating and focusing, trying to feel the Force, and I didn't feel anything, don't worry.

slide-21
SLIDE 21

Linked List - LowLevelLinkDemo.java

  • Conv

nvert ar array t to link linked-lis list, v , value ues i in same o

  • rder
  • https://course

sework.cs. s.duke.edu/201sp sprin ing20/cla lassc sscode/

  • Cho

hoices: ad add t to front o

  • r bac

ack o

  • f lis

list b bein eing b built uilt

  • If we add to front? loop over array values …
  • If we add to back? Need pointer to front too
  • Invar

ariant iant?

  • last …

4/8/2020 Compsci 201, Spring 2020 24

slide-22
SLIDE 22

Add to Back Invariant

  • Cho

hoices: ad add t to front o

  • r bac

ack o

  • f lis

list b bein eing b built uilt

  • If we add to back? Need pointer to front too
  • Invar

ariant iant

  • last is last node of list with values in range [0,k)
  • True before loop guard first tested? [0,1)
  • Updated in loop?

4/8/2020 Compsci 201, Spring 2020 25

slide-23
SLIDE 23

Linked List Concepts Reviewed

  • Loop wit

ith s h statement lis list = = list.nex .next

  • Recurse

se wit ith c h cal all do doit(lis list.nex next)

  • Mus

ust a assign t to .ne .next f fiel ield t to create a a link linked lis list

  • Can be via node constructor
  • Veri

rify fy c code works: s: em empty lis list and and la last no node o e of lis list

  • void null-pointer dereference

4/8/2020 Compsci 201, Spring 2020 26

slide-24
SLIDE 24

WOTO Link Review

http:// //bi bit.ly/2 /201spr pring20-04 0408 08-3

4/8/2020 Compsci 201, Spring 2020 27

slide-25
SLIDE 25

Compsci 201 201 Exam 2 2 Re Review, C , Compar arat ator

  • rs, A

, APTs Pa Part 4 4 of 4 f 4

4/8/2020 Compsci 201, Spring 2020 28

Susan Rodger April 8, 2020

slide-26
SLIDE 26

Sorting APTs

  • Creating

ing i inne ner/nes /nested c classes es t to help

  • https://www2.cs.duke.edu/csed/newapt/clientlist.html
  • https://www2.cs.duke.edu/csed/newapt/medaltable.html
  • Sorting

ing Map.E .Ent ntry<Key,Value ue> pa pairs

  • https://www2.cs.duke.edu/csed/newapt/sortbyfreqs.html
  • Sim

imple U Use o e of API

  • https://www2.cs.duke.edu/csed/newapt/lengthsort.html

4/8/2020 Compsci 201, Spring 2020 29

slide-27
SLIDE 27

ClientsList APT

  • Nes

ested c cla lass t tha hat c can an b be e compared: P Per erson

  • Two approaches
  • Implement Comparable and a newer approach
  • Solve? Translate, Process, Translate

4/8/2020 Compsci 201, Spring 2020 30

slide-28
SLIDE 28

How to sort: old and new

  • This

This is is t the he OLD LD way to to sort i t in Java

  • Her

ere is is ne newer way, no , no need need f for C Comparable

4/8/2020 Compsci 201, Spring 2020 31

slide-29
SLIDE 29

4/8/2020 Compsci 201, Spring 2020 32

slide-30
SLIDE 30

4/8/2020 Compsci 201, Spring 2020 33

slide-31
SLIDE 31

SortByFreqs, SortedFreqs

  • Can u

n use e TreeMap ap to keep count nt o

  • f each s

ch string ing

  • Map.Entry class is like inner class Person
  • However, does efficiency matter?
  • Suppo

ppose f for SortedF dFreqs qs we create a a sorted s set o

  • f

the u unique s ue string ings, w , why TreeSet?

  • Simply return count of each string

4/8/2020 Compsci 201, Spring 2020 34

slide-32
SLIDE 32

Efficiency for APTs?

  • If we

e ha have M M uniq unique v val alues f from a a set o

  • f N
  • This code is O(MN), we can do better
  • Extract values: TreeMap<String,Integer>

4/8/2020 Compsci 201, Spring 2020 35

slide-33
SLIDE 33

SortByFreqs

  • TreeMap <String,Integer>, keys in

in order er

  • Keys are in sorted order
  • What song will you use for green dance?
  • https://w

://www.youtube be.co com/w /wat atch ch?v=XjVNl VNlG5cZyQ

4/8/2020 Compsci 201, Spring 2020 36

slide-34
SLIDE 34

Using TreeMap and Map.Entry

  • Map.E

.Ent ntry is is lik like a a pair air cla class f for m map aps

  • Use Map.Entry<Key,Value>
  • Getting this right is not pretty

4/8/2020 Compsci 201, Spring 2020 37

slide-35
SLIDE 35

Using Lambda to sort in Java

  • Perha

haps m more i intuit uitiv ive: s sort s string ings u using ng correspond nding ing va value i ue in a map

  • Given string, find value
  • Leverage alphabetical order of strings and

stable sort: "equal" keys don't change order, where equal is count of occurrences from map

4/8/2020 Compsci 201, Spring 2020 39