61a lecture 22
play

61A Lecture 22 Monday, March 16 Announcements Midterm 2 is on - PowerPoint PPT Presentation

61A Lecture 22 Monday, March 16 Announcements Midterm 2 is on Thursday 3/19 7pm-9pm Topics and locations: http://cs61a.org/exams/midterm2.html Bring 1 hand-written, 2-sided sheet of notes; Two study guides will be provided


  1. 61A Lecture 22 Monday, March 16

  2. Announcements • Midterm 2 is on Thursday 3/19 7pm-9pm § Topics and locations: http://cs61a.org/exams/midterm2.html § Bring 1 hand-written, 2-sided sheet of notes; Two study guides will be provided § Emphasis: mutable data, object-oriented programming, recursion, and recursive data § Review session on Tuesday 5:00pm-6:30pm in 2050 VLSB § Includes content through Friday 3/13 (today is review & examples) • No lecture next Wednesday 3/18 • No discussion sections next Thursday 3/19 or Friday 3/20 • Lecture next Friday 3/20 is a video (but a great one) 2

  3. Linked Lists

  4. Recursive Lists Can Change Attribute assignment statements can change first and rest attributes of a Link The rest of a linked list can contain the linked list as a sub-list >>> s = Link(1, Link(2, Link(3))) >>> s.first = 5 >>> t = s.rest Global frame Rest Rest Rest First First First >>> t.rest = s 2 3 1 s >>> s.first 5 >>> s.rest.rest.rest.rest.rest.first 2 Global frame Rest Rest First First Note: The actual 2 5 s environment diagram is much more complicated. t 4

  5. Environment Diagrams

  6. Go Bears! Global frame def oski(bear): func oski(bear)[parent=G] oski def cal(berk): func λ (ley) [parent=f2] [parent=G] f1: oski nonlocal bear bear func abs(...) [parent=G] if bear(berk) == 0: cal func cal(berk) [parent=f1] Return Value return [berk+1, berk-1] f2: cal [parent=f1] bear = lambda ley: berk-ley berk 2 list return [berk, cal(berk)] 0 1 Return Value 2 return cal(2) f3: cal [parent=f1] list berk 2 oski(abs) 0 1 3 1 Return Value f4: λ [parent=f2] ley 2 [2, [3, 1]] 0 Return Value 6

  7. Objects

  8. Land Owners Instance attributes are found before class attributes; class attributes are inherited class Worker: <class Worker> >>> Worker().work() >>> Worker().work() greeting = 'Sir' 'Sir, I work' � greeting: 'Sir' def __init__(self): self.elf = Worker >>> jack >>> jack def work(self): <class Bourgeoisie> Peon return self.greeting + ', I work' � def __repr__(self): greeting: 'Peon' return Bourgeoisie.greeting >>> jack.work() >>> jack.work() 'Maam, I work' � � jack <Worker> class Bourgeoisie(Worker): greeting = 'Peon' >>> john.work() >>> john.work() elf: def work(self): Peon, I work � greeting: 'Maam' print(Worker.work(self)) 'I gather wealth' � return 'I gather wealth' john <Bourgeoisie> � >>> john.elf.work(john) >>> john.elf.work(john) jack = Worker() 'Peon, I work' elf: � john = Bourgeoisie() jack.greeting = 'Maam' 8

  9. Binary Trees

  10. Morse Code Morse code is a signaling protocol that transmits messages by sequences of signals A: Problem : Implement morse so that decode works correctly B: abcde = {'a': '.-', 'b': '-...', 'c': '-.-.', 'd': '-..', 'e': '.'} C: D: def decode(signals, tree): E: """Decode signals into a letter using a morse code tree. ... � >>> t = morse(abcde) >>> [decode(s, t) for s in ['-..', '.', '-.-.', '.-', '-..', '.']] ['d', 'e', 'c', 'a', 'd', 'e'] """ for signal in signals: def morse(code): if signal == '.': .... tree = tree.left elif signal == '-': tree = tree.right return tree.entry (Demo) 10

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