Guerrilla Section 5: Object Oriented Programming, Nonlocal & Mutable Trees

Similar documents
OOP, Nonlocal, Trees, LLs, Growth Spring 2019 Guerrilla Section 3: March 16, 2019 Solutions 1 OOP. Questions

Midterm 2 Review Fall 2017 October 13, Lists & Tree Recursion. Instructions

CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters.

MORE SCHEME. 1 What Would Scheme Print? COMPUTER SCIENCE MENTORS 61A. October 30 to November 3, Solution: Solutions begin on the following page.

Structure and Interpretation of Computer Programs Summer 2015 Midterm 2

LINKED LISTS AND MIDTERM REVIEW

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Guerrilla Section 7: Macros, SQL SQL

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

April 2 to April 4, 2018

Structure and Interpretation of Computer Programs Fall 2016 Midterm 2

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

Structure and Interpretation of Computer Programs

TREES AND ORDERS OF GROWTH 7

CS61A Notes 02b Fake Plastic Trees. 2. (cons ((1 a) (2 o)) (3 g)) 3. (list ((1 a) (2 o)) (3 g)) 4. (append ((1 a) (2 o)) (3 g))

CS 61A Discussion 8: Scheme. March 23, 2017

Intro. Scheme Basics. scm> 5 5. scm>

TAIL RECURSION, SCOPE, AND PROJECT 4 11

CSE413 Midterm. Question Max Points Total 100

Structure and Interpretation of Computer Programs

COMPUTER SCIENCE IN THE NEWS. CS61A Lecture 21 Scheme TODAY REVIEW: DISPATCH DICTIONARIES DISPATCH DICTIONARIES 7/27/2012

Structure and Interpretation of Computer Programs

OOP 1 OOP CS 61A GROUP MENTORING. July 19, 2017

Lexical vs. Dynamic Scope

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

INHERITANCE AND NONLOCAL 6

Structure and Interpretation of Computer Programs

SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))

CS61A Midterm 2 Review (v1.1)

CSE341 Spring 2017, Final Examination June 8, 2017

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs Spring 2019 Midterm 2

ITERATORS AND STREAMS 9

TREES AND ORDERS OF GROWTH 8

Lecture #13: Type Inference and Unification. Typing In the Language ML. Type Inference. Doing Type Inference

Structure and Interpretation of Computer Programs Summer 2014 Midterm 1

Structure and Interpretation of Computer Programs

CS 61A, Fall, 2002, Midterm #2, L. Rowe. 1. (10 points, 1 point each part) Consider the following five box-and-arrow diagrams.

Python review. 1 Python basics. References. CS 234 Naomi Nishimura

Structure and Interpretation of Computer Programs

Interpreters and Tail Calls Fall 2017 Discussion 8: November 1, 2017 Solutions. 1 Calculator. calc> (+ 2 2) 4

Why do we need an interpreter? SICP Interpretation part 1. Role of each part of the interpreter. 1. Arithmetic calculator.

CSE341 Spring 2017, Final Examination June 8, 2017

Lecture #15: Generic Functions and Expressivity. Last modified: Wed Mar 1 15:51: CS61A: Lecture #16 1

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs Fall 2015 Midterm 2

Structure and Interpretation of Computer Programs

ormap, andmap, and filter

Structure and Interpretation of Computer Programs Spring 2016 Test 1

Structure and Interpretation of Computer Programs

Fall Semester, The Metacircular Evaluator. Today we shift perspective from that of a user of computer langugaes to that of a designer of

Structure and Interpretation of Computer Programs

User-defined Functions. Conditional Expressions in Scheme

CS558 Programming Languages

61A LECTURE 18 SCHEME. Steven Tang and Eric Tzeng July 24, 2013

CS558 Programming Languages

Sample Final Exam Questions

SCHEME The Scheme Interpreter. 2 Primitives COMPUTER SCIENCE 61A. October 29th, 2012

Lists, Mutability, ADTs, and Trees Spring 2019 Guerrilla Section 2: March 2, Sequences. Questions. lst = [1, 2, 3, 4, 5] lst[1:3]

EXAMINATIONS 2009 MID-TERM TEST. COMP 202 / SWEN 202 Formal Methods of Computer Science / Formal Foundations of Software Engineering WITH ANSWERS

CSE341 Autumn 2017, Final Examination December 12, 2017

cs61amt2_4 CS 61A Midterm #2 ver March 2, 1998 Exam version: A Your name login: cs61a- Discussion section number TA's name

Welcome to CS61A! Last modified: Thu Jan 23 03:58: CS61A: Lecture #1 1

CS Introduction to Computational and Data Science. Instructor: Renzhi Cao Computer Science Department Pacific Lutheran University Spring 2017

What Would Python Print? Tuples, Lists, Dictionaries

Lists, Mutability, ADTs, and Trees Spring 2019 Guerrilla Section 2: March 2, 2019 Solutions. 1 Sequences. Questions. lst = [1, 2, 3, 4, 5] lst[1:3]

SCHEME AND CALCULATOR 5b

In addition to the correct answer, you MUST show all your work in order to receive full credit.

Department of Electrical Engineering and Computer Sciences Fall 2000 Instructor: Dan Garcia CS 3 Final Exam

CONCEPTS OF PROGRAMMING LANGUAGES Solutions for Mid-Term Examination

Welcome to CSC148! Introduction to Computer Science

INTERPRETERS 8. 1 Calculator COMPUTER SCIENCE 61A. November 3, 2016

Lecture #21: Search Trees, Sets. Last modified: Tue Mar 18 18:15: CS61A: Lecture #21 1

ENVIRONMENT DIAGRAMS AND RECURSION 2

Announcement. Overview. LISP: A Quick Overview. Outline of Writing and Running Lisp.

Midterm 1 Review Fall 2017 September 8, Functions. Instructions

STREAMS, ITERATORS, AND BINARY TREES 10

Programming Principles

Structure and Interpretation of Computer Programs

TREES AND ORDERS OF GROWTH 7

Discussion 4. Data Abstraction and Sequences

Transcription:

Guerrilla Section 5: Object Oriented Programming, Nonlocal & Mutable Trees Instructions Form a group of 3-4. Start on Question 1. Check off with a lab assistant when everyone in your group understands how to solve Question 1. Repeat for Question 2, 3, etc. You are not allowed to move on from a question until you check off with a lab assistant. You are allowed to use any and all resources at your disposal, including the interpreter, lecture notes and slides, discussion notes, and labs. You may consult the lab assistants, but only after you have asked everyone else in your group. The purpose of this section is to have all the students working together to learn the material. Object Oriented Programming Question 0 0a) What is the relationship between a class and an ADT? 0b) Define the following: Instance Class Class Attribute Instance Attribute Bound Method

Question 1: What would Python Print? class Foo(): x = 'bam' def init (self, x): self.x = x def baz(self): return self.x class Bar(Foo): x = 'boom' def init (self, x): Foo. init (self, 'er' + x) def baz(self): return Bar.x + Foo.baz(self) foo = Foo('boo') >>> Foo.x >>> foo.x >>> foo.baz() >>> Foo.baz() >>> Foo.baz(foo) >>> bar = Bar('ang') >>> Bar.x >>> bar.x >>> bar.baz()

Question 2: Attend Class class Student: def init (self, subjects): self.current_units = 16 self.subjects_to_take = subjects self.subjects_learned = {} self.partner = None def learn(self, subject, units): print( I just learned about + subject) self.subjects_learned[subject] = units self.current_units -= units def make_friends(self): if len(self.subjects_to_take) > 3: print( Whoa! I need more help! ) self.partner = Student(self.subjects_to_take[1:]) else: print( I m on my own now! ) self.partner = None def take_course(self): course = self.subjects_to_take.pop() self.learn(course, 4) if self.partner: print( I need to switch this up! ) self.partner = self.partner.partner if not self.partner: print( I have failed to make a friend :( ) What Would Python Print? It may be helpful to draw an object diagram (You can draw this however you d like) representing Tim, and all his attributes (be sure to keep track of all partners and their respective attributes). The diagram is not required. >>> tim = Student([ Chem1A, Bio1B, CS61A, CS70, CogSci1 ]) >>> tim.make_friends() >>> print(tim.subjects_to_take) >>> tim.partner.make_friends()

>>> tim.take_course() >>> tim.partner.take_course() >>>tim.take_course() >>> tim.make_friends() STOP! Don t proceed until everyone in your group has finished and understands all exercises in this section, and you have gotten checked off!

Mutable Functions/Nonlocal Question 3: Draw an environment diagram each of the following: 3a) ore = "settlers" def sheep(wood): def ore(wheat): nonlocal ore ore = wheat ore(wood) return ore sheep(lambda wood: ore)("wheat") 3b) aang = 120 def airbend(zuko): aang = 2 def katara(aang): nonlocal zuko zuko = lambda sokka : aang + 4 return aang if zuko(10) == 1: katara(aang + 9) return zuko(airbend) airbend(lambda x: aang + 1)

Question 4 Write make_max_finder, which takes in no arguments but returns a function which takes in a list. The function it returns should return the maximum value it s been called on so far, including the current list and any previous list. You can assume that any list this function takes in will be nonempty and contain only non-negative values. def make_max_finder(): """ >>> m = make_max_finder() >>> m([5, 6, 7]) 7 >>> m([1, 2, 3]) 7 >>> m([9]) 9 >>> m2 = make_max_finder() >>> m2([1]) 1 """ STOP! Don t proceed until everyone in your group has finished and understands all exercises in this section, and you have gotten checked off!

Question 5: What Would Scheme Print? (define pie 1) (define (apple pie) (define macaron (+ pie 1)) (define (lemon bar) (set! pie bar) (set! macaron (* bar 2))) (define tart (lambda () (cons pie (cons macaron nil)))) (cons tart (cons lemon nil))) > (define cheese (apple 5)) > (define choco (car cheese)) > (define cake (car (cdr cheese))) > pie > choco > (define taffy (choco)) > (car taffy) > (cdr taffy) > (print (cake 9)) > pie > (define taffy (choco)) > (car taffy) > (cdr taffy)

Question 6 The CS61A TAs are worried that students find calling functions to be too boring. To make things more interesting, they write a higher order function excite that converts boring functions into more exciting functions. Help them complete the definition of excite. It takes in three arguments: 1. boring-fn : a function that takes one argument 2. fun-fn : a function that takes two arguments, the second of which is always an integer 3. n : an integer Once called with these three arguments, excite should return an exciting function, which takes one argument and does the following: Every n -th time the excite function is called, it calls fun_fn with the provided argument and the number of times the excite function has ever been called. It returns the result of this call. Otherwise, calling the excite function should return the value of calling boring_fn with the provided argument. Complete the definition of excite in the provided space below. ; Doctests scm> (define boring (lambda (name) (begin (print name) (print 'walked-the-dog)))) boring scm> (define fun (lambda (name i) (begin (print name) (print 'won) (print i) (print 'new-cars!)))) fun scm> (define great-deal (excite boring fun 2)) great-deal scm> (great-deal 'mitas) mitas walked-the-dog scm> (great-deal 'mitas) mitas won 2 new-cars scm> (great-deal 'mitas) mitas walked-the-dog scm> (great-deal 'mitas) mitas won 4 new-cars

(define (excite boring-fn fun-fn n) (define count ) (define (exciting arg) (if (= ( ) 0) ) Question 7 Implement the func-set function, which returns two functions in a list that together represent a set. Both the add and has functions return whether a value is already in the set. The add function also adds its argument value to the set. You may assign to only one name in the assignment statement. ; Doctests scm> (define result (func-set)) scm> (define add (car result)) scm> (define has (car (cdr result)) scm> (add 1) #f scm> (add 3) #f scm> (list (has 1) (has 2) (has 3) (has 4) (has 5)) (#t #f #t #f #f) scm> (add 3) #t scm> (add 2) #f scm> (list (has 1) (has 2) (has 3) (has 4) (has 5)) (#t #t #t #f #f) (define (func-set) (define items (lambda (x) false)) (define (add y) (define f items) (set! ) ) (cons add (cons nil))) STOP! Don t proceed until everyone in your group has finished and understands all exercises in this section, and you have gotten checked off!

Mutable Trees Question 8 Use following definition of a tree to answer the questions below: class Tree: def init (self, label, branches=[]): self.label = label for branch in branches: assert isinstance(branch, Tree) self.branches = list(branches) def repr (self): if self.branches: branches_str = ', ' + repr(self.branches) else: branches_str = '' return 'Tree({0}{1})'.format(self.entry, branches_str) def is_leaf(self): # a leaf has no branches return len(self.branches) == 0

8a) Define filter_tree, which takes in a tree t and one argument predicate function fn. It should mutate the tree by removing all branches of any node where calling fn on its label returns False. In addition, if this node is not the root of the tree, it should remove that node from the tree as well. def filter_tree(t, fn): """ >>> t = Tree(1, [Tree(2), Tree(3, [Tree(4)]), Tree(6, [Tree(7)])]) >>> filter_tree(t, lambda x: x % 2!= 0) >>> t tree(1, [Tree(3)]) >>> t2 = Tree(2, [Tree(3), Tree(4), Tree(5)]) >>> filter_tree(t2, lambda x: x!= 2) >>> t2 Tree(2) """ if not fn(t.label): else: for : if : else: 8b) Fill in the definition for nth_level_tree_map, which also takes in a function and a tree, but mutates the tree by applying the function to every n th level in the tree, where the root is the 0th level. def nth_level_tree_map(fn, tree, n): """Mutates a tree by mapping a function all the elements of a tree. >>> tree = Tree(1, [Tree(7, [Tree(3), Tree(4), Tree(5)]), Tree(2, [Tree(6), Tree(4)])]) >>> nth_level_tree_map(lambda x: x + 1, tree, 2) >>> tree Tree(2, [Tree(7, [Tree(4), Tree(5), Tree(6)]), Tree(2, [Tree(7), Tree(5)])]) """

STOP! Don t proceed until everyone in your group has finished and understands all exercises in this section, and you have gotten checked off! Extra Challenge Question 9: Photosynthesis 9a) Fill in the methods below, so that the classes interact correctly according to the documentation (make sure to keep track of all the counters!). """ >>> p = Plant() >>> p.height 1 >>> p.materials [] >>> p.absorb() >>> p.materials [ Sugar ] >>> Sugar.sugars_created 1 >>> p.leaf.sugars_used 0 >>> p.grow() >>> p.materials [] >>> p.height 2 >>> p.leaf.sugars_used 1 """ class Plant: def init (self): """A Plant has a Leaf, a list of sugars created so far, and an initial height of 1""" def absorb(self): """Calls the leaf to create sugar"""

def grow(self): """A Plant uses all of its sugars, each of which increases its height by 1""" class Leaf: def init (self, plant): # Source is a Plant instance """A Leaf is initially alive, and keeps track of how many sugars it has created""" def absorb(self): """If this Leaf is alive, a Sugar is added to the plant s list of sugars""" if self.alive: class Sugar: sugars_created = 0 def init (self, leaf, plant): def activate(self): """A sugar is used, then removed from the Plant which contains it""" def repr (self):

return Sugar 9b) ( Optional -- only do if time at the end! ) Let's make this a little more realistic by giving these objects ages. Modify the code above to satisfy the following conditions. See the doctest for further guidance. 1) Every plant and leaf should have an age, but sugar does not age. Plants have a lifetime of 20 time units, and leaves have a lifetime of 2 time units. 2) Time advances by one unit at the end of a call to a plant's absorb or grow method. 3) Every time a leaf dies, it spawns a new leaf on the plant. When a plant dies, its leaf dies, and the plant becomes a zombie plant--no longer subject to time. Zombie plants do not age or die. 4) Every time a generation of leaves dies for a zombie plant, twice as many leaves rise from the organic matter of its ancestors--defying scientific explanation. """ >>> p = Plant() >>> p.age 0 >>> p.leaves [ Leaf ] >>> p.leaves[0].age 0 >>> p.age = 18 >>> p.age 18 >>> p.height 1 >>> p.absorb() >>> p.materials [ Sugar ] >>> p.age 19 >>> p.leaves[0].age 1 >>> p.grow() >>> p.age 20 >>> p.is_zombie True >>>p.leaves [ Leaf, Leaf ]

>>> p.leaves[0].age 0 >>> p.absorb() >>> p.age 20 """ You will only need to make changes to the Plant and Leaf classes. class Plant: def init (self): """A Plant has a Leaf, a list of sugars created so far, and an initial height of 1""" self.materials = [] self.height = 1 def absorb(self): """Calls the leaf to create sugar""" def grow(self): """A Plant uses all of its sugars,each of which increases its height by 1""" for sugar in self.materials: sugar.activate() self.height += 1

def death(self): class Leaf: def init (self, plant): # plant is a Plant instance """A Leaf is initially alive, and keeps track of how many sugars it has created""" self.alive = True self.sugars_used = 0 self.plant = plant def absorb(self): """If this Leaf is alive, a Sugar is added to the plant s list of sugars""" if self.alive: self.plant.materials.append(sugar(self, self.plant)) def death(self): def repr (self): return Leaf