compsc sci 201 201 colle lectio ions ns hashing hing o
play

Compsc sci 201 201 Colle lectio ions ns, Hashing hing, O - PowerPoint PPT Presentation

Compsc sci 201 201 Colle lectio ions ns, Hashing hing, O Objects Susan Rodger February 5, 2020 2/5/2020 CompSci 201, Spring 2020 1 Glitchy App? 2/5/2020 CompSci 201, Spring 2020 2 H is for Hashing What better way to have


  1. Compsc sci 201 201 Colle lectio ions ns, Hashing hing, O Objects Susan Rodger February 5, 2020 2/5/2020 CompSci 201, Spring 2020 1

  2. Glitchy App? 2/5/2020 CompSci 201, Spring 2020 2

  3. H is for … • Hashing • What better way to have a bucket list? • Hexadecimal • ABC is 10,11,12 • Base 16 > Base 2? 2/5/2020 CompSci 201, Spring 2020 4

  4. Announcements • Assig ignm nment nt P P2 out l lat ater er t this is w week • APT APT-3 d due ue T Tue ues, F Feb eb 4, 4, Extended t to Thur Thurs F Feb eb 6 6 • Last chance to turn in Friday til 11:59pm • Dis iscussion 5 5 on n Feb eb 10 10 • Prepare for exam • Exam am nex next w week eek, F Feb eb 14 14 2/5/2020 CompSci 201, Spring 2020 5

  5. PFWBVDW • Interfaces aces: L List, S , Set, a , and M Map • When it makes sense to use general type • Empirical and Analytical measures of efficiency • Maps: API and P Problem lem S Solving ving • Keys and Values • Big ig-Oh and and O O-Not otation • Building a mathematical formalism with intuition 2/5/2020 CompSci 201, Spring 2020 6

  6. Midterm Coming Feb 14 • How much ch c code e have you w u written w en with p h paper er a and a writi ting u ute tensil? il? • Tests should measure what you've practiced • Practice writing code on paper! • Midterm r m revie iew a and p d previo ious te tests ts • These are the best practice available • Will practice in Discussion • Logistic tics • Start on time, end on time, accommodations • 1 page front and back of notes you bring and leave 2/5/2020 CompSci 201, Spring 2020 7

  7. Breakfast 201 was yummy! • Wed ed. F Feb eb 5 5 9:30 9:30am • 30 30 minu inutes, d dis iscuss w wha hatever w wit ith h me • Enjo joy br breakfast • More re b bre reakfas asts coming ingl… 2/5/2020 CompSci 201, Spring 2020 8

  8. The hashCode contract • Every o object t has s .hashCode() metho hod • Inherited from Object, but typically overridden • Use @Override and read online • Must r st resp spect . t .equals( s(): I If a.equals(b) ? • a.hashCode() == b.hashCode() • Converse not true! There will be collisions 2/5/2020 CompSci 201, Spring 2020 9

  9. When Strings Collide • Gene enerate strings t tha hat w will c ill coll llid ide • Find ind s suc uch s strings in in the he wild ild String hashCode String hashCode ayay 3009136 buzzards -931102253 ayBZ 3009136 righto -931102253 bZay 3009136 snitz 109586548 bZbZ 3009136 unprecludible 109586548 2/5/2020 CompSci 201, Spring 2020 10

  10. Default: Object.equals, .hashCode When you do not override… • For O Obj bjects ts p and q d q: • p.equals(q) is the same as p == q • Do p and q reference/point to same object • For O Obj bject p t p • p.hashCode() is location in memory of object • Th Thus: i if f p == q then en • p.hashCode() == q.hashCode() 2/5/2020 CompSci 201, Spring 2020 11

  11. Summary: ArrayList and HashSet • Both h hav ave . e .add, .addAll, and mor more • Both iterable: for(Elt e : collection) • Both h h have . .cont ntains lever eraging . .equal uals • HashSet also uses .hashCode to reduce the collection iterated over: locker collisions • Object ect h hygien ene w e when en devel eloping y your ur c classes es • .toString(), .equals(), .hashCode() 2/5/2020 CompSci 201, Spring 2020 13

  12. When Strings Collide https://www.youtube.com/watch?v=HeTShE2PiQI 2/5/2020 CompSci 201, Spring 2020 14

  13. When Strings Collide • Gene enerate strings t tha hat w will c ill coll llid ide • Find ind s suc uch s strings in in the he wild ild String hashCode String hashCode ayay 3009136 buzzards -931102253 ayBZ 3009136 righto -931102253 bZay 3009136 snitz 109586548 bZbZ 3009136 unprecludible 109586548 2/5/2020 CompSci 201, Spring 2020 15

  14. Concept: Inheritance • In Java, e ever ery c class e extend ends O Object ect • Gets methods by default: .toString, .hashCode, .equals, and more • Inherit metho hod + + implement entatio ion • Subclass can n overri rride bas ase c class metho hods • Make .equals work for Point class 2/5/2020 CompSci 201, Spring 2020 16

  15. Work in 201 • How w impo portant a are APT APTs? • How important are APT quizzes? • How important nt a are as e assig ignm nment ents? • Earlier assignments, later assignments? • How im important: r read eadin ing and and W WOTO in in-class • How important is reading? 2/5/2020 CompSci 201, Spring 2020 17

  16. Alphabetical Order • En Encryption? Maybe n not • https://www2.cs.duke.edu/csed/newapt/encryption.html • Think about high-level algorithm • Apply your algorithm to: "pop", "array", "deeds" • Wha hat do w we nee e need to do t to code alg algorithm? • Recall: 'b' + 1 == 'c' • Recall: array['h'] is allowed, 'h' can be index 2/5/2020 CompSci 201, Spring 2020 18

  17. Idea with Encryption APT int[] allcha int lchars = new i int[256] 256]; 96 0 int int nex nextLet is is 'a' 97 0 ‘a’ String ans answer = = ""; ""; 98 0 ‘b’ 0 99 ‘c’ messa ssage i is feed 100 0 ‘d’ ‘e’ 101 0 answer is ans is 0 ‘f’ 102 103 0 ‘g’ ch is ch is 2/5/2020 CompSci 201, Spring 2020 19

  18. How often does a string occur? • Stri rings gs s stored i in Arra rrayList? • Call Collections.frequency(list,word) • If in array a rather than ArrayList? Collections.frequency(Arrays.asList(a), word) ArrayList<String> list is [“cat”, “cat”, “dog”, “fish”, “dog”, “cat”] Collections.frequency(list, “dog”) is Collections.frequency(list, “cat”) is 2/5/2020 CompSci 201, Spring 2020 36

  19. How often does a string occur? • Is Is Collec lectio ions ns.f .freq eque uency ncy ef efficient icient? D Does i it mat atter? • Use Collections.frequency • Can create parallel arrays or use HashMap • Keep count[k] # occurrences of word[k] • Use HashMap if you know that 2/5/2020 CompSci 201, Spring 2020 38

  20. WOTO (correctness counts) http:// //bi bit.ly/2 /201spr pring20-02 0205 05-1 2/5/2020 CompSci 201, Spring 2020 39

  21. Shafi Goldwasser 2012 Turing Award Winner • RCS professor of computer science • at MIT Twice Godel Prize winner • Grace Murray Hopper Award • National Academy • Co-inventor of zero-knowledge • proof protocols Work on what you like, what feels right, I know of no other way to end up doing creative work 2/5/2020 CompSci 201, Spring 2020 40

  22. Why use an interface? 2/5/2020 CompSci 201, Spring 2020 41

  23. What is a Java Interface? • An enforcea ceable a le abstractio ion: n: m metho hods r requir uired ed • Set, Map, List interfaces • Can an im imple lement m more t e tha han o one int ne interface • Can extend only one base-class! • Argua uable le: M Mammal i al is a an inter erface ace • Do NOT inhe T inherit method implementations • Do inherit methods (names, types, etc.) 2/5/2020 CompSci 201, Spring 2020 43

  24. Analogy: Mammals • Drago gon? ? • Mammals • Birth to live young • Hair • Warm-blooded • Lik ike an an int interface! 2/5/2020 CompSci 201, Spring 2020 44

  25. Why use an Interface? • Work w with f h frameworks, e e.g., ., java.ut util il.Collect llection • Iterable, Serializable, and more – use with Java • Arra rrayList, , LinkedLis ist, , TreeS eSet, , Has ashS hSet all all … • .clear(), .contains(o), .addAll(..) , .size(), … .toArray() https:// ://docs cs.o .oracle.co com/en/ /en/java/j /javase/11/ /11/docs cs/ap api/ i/j ava.base/ e/java/ a/ut util/ il/Collec llectio ion.ht n.html 2/5/2020 CompSci 201, Spring 2020 46

  26. There are two kinds … • Ther There ar are 10 10 kind inds o of peo eople in in the he w world ld … … • Those who understand binary and … • Is this funny? • Has ashS hSet/HashMap a and TreeS eSet/TreeMap ap • Tradeoffs in efficiency, organization • LinkedLis ist/Arra rrayList • Tradeoffs in efficiency, organization 2/5/2020 CompSci 201, Spring 2020 47

  27. Link v Array • Get etting ng b between t en two e element ents • Unsnap/Snap v Shift/Insert 2/5/2020 CompSci 201, Spring 2020 48

  28. Preliminaries • List<..> is is an an int interface in in java.ut util il • LinkedList<..> and ArrayList<..> • Implement the interface • Wha hat is is null null? • Variable value • No object referenced 2/5/2020 CompSci 201, Spring 2020 49

  29. Benchmark: Empirical Analysis https://c //course sework.cs.d s.duke.e .edu/2 /201sp spring20/classc sscode/ • • In c class ss ListSplic icer er, m metho hod removeFirst • List<String> parameter • ArrayList<String> argument passed • LinkedList<String> argument passed • Only ly c call L ll List<..> ..> i interface ace m met etho hods • At runtime, call the actual object method • LinkedList.add v ArrayList.add 2/5/2020 CompSci 201, Spring 2020 50

  30. list.remove(0) • Wha hat is is “faster”? L Link inkedList o or Arra rrayList 2/5/2020 CompSci 201, Spring 2020 51

  31. list.remove(0) – where called 2/5/2020 CompSci 201, Spring 2020 52

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend