computational concepts toolbox
play

Computational Concepts Toolbox Data type: values, literals, Higher - PowerPoint PPT Presentation

Computational Concepts Toolbox Data type: values, literals, Higher Order Functions operations, Functions as Values Expressions, Call Functions with functions as Abstract Data Types argument expression Assignment of


  1. Computational Concepts Toolbox • Data type: values, literals, • Higher Order Functions operations, – Functions as Values • Expressions, Call – Functions with functions as Abstract Data Types argument expression – Assignment of function • Variables values • Assignment Statement • Higher order function David E. Culler patterns • Sequences: tuple, list – Map, Filter, Reduce CS8 – Computational Structures in Data Science • Data structures • Function factories – create • Tuple assignment http://inst.eecs.berkeley.edu/~cs88 and return functions • Call Expressions • Recursion • Function Definition Lecture 7 Statement – Linear, Tail, Tree March 7, 2016 • Conditional Statement • Iteration: list comp, for, while 2/22/16 2 UCB CS88 Sp16 L4 Universality Administrative Issues • Everything that can be computed, can be • Midterm went well (results on gradescope) computed with what you know now. • Well • March 15 12:30 – 3:00 Study session for • or poorly repeat??? • Lab05 today gets you started on ADTs • Maps project out in lieu of homework – Due Sun 3/20 “before break” – Two-week project 2/22/16 3 2/22/16 4 UCB CS88 Sp16 L4 UCB CS88 Sp16 L4

  2. C.O.R.E concepts Creating an Abtract Data Type • Operations Perform useful computations treating objects abstractly as – Express the behavior of objects, invariants, etc C ompute whole values and operating on – Implemented (abstractly) in terms of Constructors and them. Selectors for the object Abstract Data Type • Representation Provide operations on the O perations abstract components that allow – Constructors & Selectors ease of use – independent of – Implement the structure of the object concrete representation. R epresentation Constructors and selectors that • An abstraction barrier violation occurs when a provide an abstract interface to part of the program that can use the higher level a concrete representation functions uses lower level ones instead Execution on a computing E valuation – At either layer of abstraction machine • Abstraction barriers make programs easier to get right, maintain, and modify Abstraction Barrier – Few changes when representation changes 2/22/16 5 2/22/16 6 UCB CS88 Sp16 L4 UCB CS88 Sp16 L4 Examples You have seen Examples You have seen • Lists • Lists – Constructors : • Tuples » list( … ) • Strings » [ <exps>,… ] – Constructors : » [<exp> for <var> in <list> [ if <exp> ] ] » str( … ) – Selectors : <list> [ <index or slice> ] » “<chars>”, ‘<chars>’ – Operations : in, not in, +, *, len, min, max – Selectors : <str> [ <index or slice> ] » Mutable ones too (but not yet) – Operations : in, not in, +, *, len, min, max • Tuples • Range – Constructors : – Constructors : » tuple( … ) » range(<end>), range(<start>,<end>), » ( <exps>,… ) range(<start>,<end>,<step>) – Selectors : <tuple> [ <index or slice> ] – Selectors : <range> [ <index or slice> ] – Operations : in, not in, +, *, len, min, max – Operations : in, not in, len, min, max 2/22/16 7 2/22/16 8 UCB CS88 Sp16 L4 UCB CS88 Sp16 L4

  3. Example ADT: lookup table (lut) lut ADT • Unordered collection of unique key => value • Constructors bindings – lut() - Return an empty lut – “lookup, i.e., ge t, the value associated with a key” – lut_add(lut, key, value) - Return a lut with new key => value binding • Where does this occur? – lut_del(lut, key) - Return a lut without a binding for key – Phonebook • Selectors – Facebook friends – lut_get(lut, key) - Return value in lut bound to key or – Movie listings None if none exists. application – Restaurant ratings – lut_keys(lut) - Return a list of keys for bindings in lut – Roster – lut_values(lut) - Return a list of values for bindings in lut – … lut operations – lut_items(lut) - Return a list of (key, value) for bindings in lut • Operations lut representation http://cs88-website.github.io/assets/slides/adt/lut.py 2/22/16 9 2/22/16 10 UCB CS88 Sp16 L4 UCB CS88 Sp16 L4 lut ADT The Layered Design Process • Constructors • Build the application based entirely on the ADT interface – lut() , lut_add(lut, key, value), lut_del(lut, key) – Operations, Constructors and Selectors • Selectors • Build the operations entirely in ADT – lut_get(lut, key) , lut_keys(lut), lut_values(lut), lut_items(lut) Constructors and Selectors • Operations – Not the implementation of the representation – lut_with_bindings(bindings) - Return a lut of bindings • Build the constructors and selectors on some – lut_len(lut) - Return the number of bindings in lut. concrete representation – lut_print(lut) - Print a representation of bindings in lut. – lut_map_values(lut, fun) – lut_sorted(lut, fun) – lut_update(lut, key, value) – lut_fuzzy_get(lut, fuzz_key, dist_fun) » Return (key, value) for the key closest to fuzz_key under dist_fun. 2/22/16 11 2/22/16 12 UCB CS88 Sp16 L4 UCB CS88 Sp16 L4

  4. An lut application (lut_app.py) Apps (cont) from lut import * New_book = lut_update(phone_book, "Jack Chow", "805-962-0936") phone_book_data = [ ("Christine Strauch", "510-842-9235"), lut_sorted(new_phone_book, lambda k,v:v) ("Frances Catal Buloan", "932-567-3241"), ("Jack Chow", "617-547-0923"), ("Joy De Rosario", "310-912-6483"), ("Casey Casem", "415-432-9292"), ("Lydia Lu", "707-341-1254")] phone_book = lut_with_bindings(phone_book_data) lut_print(phone_book) print("Jack Chows's Number: ", lut_get(phone_book, "Jack Chow")) print("Area codes") http://cs88-website.github.io/assets/slides/adt/lut_app.py area_codes = lut_map_values(phone_book, lambda x:x[0:3]) lut_print(area_codes) 2/22/16 13 2/22/16 14 UCB CS88 Sp16 L4 UCB CS88 Sp16 L4 Apps (cont) Friends App def name_dist(name1, name2): count = max(len(name1),len(name2)) – friend_data = [ ("Christine Strauch", "Jack Chow"), min(len(name1),len(name2)) ("Christine Strauch", "Lydia Lu"), for i in range(min(len(name1), len(name2))): ("Jack Chow", "Christine Strauch"), if (name1[i] != name2[i]): ("Casey Casem", "Christine Strauch"), count += 1 ("Casey Casem", "Jack Chow"), ("Casey Casem", "Frances Catal Buloan"), return count ("Casey Casem", "Joy De Rosario"), ("Casey Casem", "Casey Casem"), ("Frances Catal Buloan", "Jack Chow"), lut_fuzzy_get(phone_book, "Jack", name_dist)) ("Jack Chow", "Frances Catal Buloan"), ("Joy De Rosario", "Lydia Lu"), ("Joy De Lydia", "Jack Chow") ] 2/22/16 15 2/22/16 16 UCB CS88 Sp16 L4 UCB CS88 Sp16 L4

  5. More Friends Above Abstraction Barrier – lut.py def lut_with_bindings(bindings): def make_friends(friends): """Construct lookup table with (key,val) bindings.""” friend_lut = lut() for (der, dee) in friends: new_lut = lut() old_friends = lut_get (friend_lut, der) for k,v in bindings: new_fr = old_friends + [dee] if old_friends is not None new_lut = lut_add(new_lut, k, v) else [dee] return new_lut friend_lut = lut_update (friend_lut, der, new_fr) return friend_lut 2/22/16 17 2/22/16 18 UCB CS88 Sp16 L4 UCB CS88 Sp16 L4 Aside: lambda Lambda Examples • Function expression >>> msort([1,2,3,4,5], lambda x: x ) [1, 2, 3, 4, 5] – “anonymous” function creation – Expression, not a statement, no return or any other statement >>> msort([1,2,3,4,5], lambda x: -x ) [5, 4, 3, 2, 1] lambda <arg or arg_tuple> : <expression using args> >>> msort([(2, "hi"), (1, "how"), (5, "goes"), (7, "I")], lambda x:x[0] ) [(1, 'how'), (2, 'hi'), (5, 'goes'), (7, 'I')] inc = lambda v : v + 1 def inc(v): >>> msort([(2, "hi"), (1, "how"), (5, "goes"), (7, "I")], return v + 1 lambda x:x[1] ) [(7, 'I'), (5, 'goes'), (2, 'hi'), (1, 'how')] >>> msort([(2,"hi"),(1,"how"),(5,"goes"),(7,"I")], lambda x: len(x[1] )) [(7, 'I'), (2, 'hi'), (1, 'how'), (5, 'goes')] http://cs88-website.github.io/assets/slides/adt/mersort.py 2/22/16 19 2/22/16 20 UCB CS88 Sp16 L4 UCB CS88 Sp16 L4

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