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

Similar documents
LINKED LISTS AND MIDTERM REVIEW

Structure and Interpretation of Computer Programs

SQL AND FINAL REVIEW

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

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

TREE RECURSION AND DATA ABSTRACTION

GENERATORS AND STREAMS

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

Structure and Interpretation of Computer Programs

Abstract Data Types Chapter 1

TREES AND ORDERS OF GROWTH 7

Structure and Interpretation of Computer Programs

Solutions to CS 61A Challenge Problems: Midpoint Review

CS61A Lecture 15 Object Oriented Programming, Mutable Data Structures. Jom Magrotker UC Berkeley EECS July 12, 2012

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

Structure and Interpretation of Computer Programs

COMPUTER SCIENCE IN THE NEWS (TWO YEARS AGO) CS61A Lecture 16 Mutable Data Structures TODAY REVIEW: OOP CLASS DESIGN 7/24/2012

Tree-Structured Data. A tree can contains other trees: [5, [6, 7], 8, [[9], 10]] (+ 5 (- 6 7) 8 (* (- 9) 10))

CS61A Lecture 16 Mutable Data Structures. Jom Magrotker UC Berkeley EECS July 16, 2012

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

TREES AND ORDERS OF GROWTH 8

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

CS 11 python track: lecture 4

Lists, loops and decisions

7. (2 pts) str( str( b ) ) str '4' will not compile (single, double, or triple quotes

Module 10: Imperative Programming, Modularization, and The Future

CS 403/503 Exam 4 Spring 2017 Name

COMPUTER SCIENCE IN THE NEWS. CS61A Lecture 14 Object Oriented Programming TODAY 7/11/2012 WHERE WE WERE: FUNCTIONAL PROGRAMMING

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

Spring 2018 Discussion 5: February 28, Object Oriented Programming

Lecture 7. Memory in Python

OBJECT ORIENTED PROGRAMMING 5

Structure and Interpretation of Computer Programs Spring 2019 Midterm 2

What is an algorithm?

CMSC 201 Spring 2019 Lab 06 Lists

CSC148 Intro. to Computer Science

Final Exam Version A

PREPARING FOR PRELIM 1

Structure and Interpretation of Computer Programs

Python Language Basics II. Georgia Advanced Computing Resource Center University of Georgia Zhuofei Hou, HPC Trainer

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

61A LECTURE 22 TAIL CALLS, ITERATORS. Steven Tang and Eric Tzeng July 30, 2013

CS 1110 Final, May 2017

Structure and Interpretation of Computer Programs

Chapter 2: Objects, classes and factories

CMSC201 Computer Science I for Majors

Assignment 2. CS 234 Fall 2018 Sandy Graham. Create()

fohgp siejt karbl mcqdn

Introduction to programming (LT2111) Lecture 6

CS 1110 Final Exam Solutions May 15th, 2014

FINAL REVIEW 13. December 4, Sample Final Questions


CME 193: Introduction to Scientific Python Lecture 6: Classes and iterators

CS 1110 Final, December 8th, Question Points Score Total: 100

CS 234. Module 3. September 29, CS 234 Module 3 Data structures, ADTs with no order of any kind, Python review 1 / 19

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

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

CS100: CPADS. Decisions. David Babcock / Don Hake Department of Physical Sciences York College of Pennsylvania

LECTURE 6 Python Basics Part 5

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

Structure and Interpretation of Computer Programs Fall 2015 Midterm 2

Special Member Functions

Abstract Data Types. CS 234, Fall Types, Data Types Abstraction Abstract Data Types Preconditions, Postconditions ADT Examples

Advanced topics, part 2

NOTE: All references to Python on this exam mean Python 3, so you should answer accordingly.

RLISTS AND THE ENVIRONMENT MODEL 9

Special Member Functions. Compiler-Generated Destructor. Compiler-Generated Default Constructor. Special Member Functions

CS Lecture 18: Card Tricks. Announcements. Slides by D. Gries, L. Lee, S. Marschner, W. White

Structure and Interpretation of Computer Programs

CS61A Notes Week 13: Interpreters

LINKED LISTS AND MIDTERM REVIEW 6

CS177 Recitation. Functions, Booleans, Decision Structures, and Loop Structures

EXAM PREPARATION SECTION 1

Introduction to: Computers & Programming: Review prior to 1 st Midterm

CS 1110 Final, December 8th, Question Points Score Total: 100

CS 115 Exam 2 (Section 1) Fall 2017 Thu. 11/09/2017

Data Structures (list, dictionary, tuples, sets, strings)

6.01, Spring Semester, 2008 Assignment 3, Issued: Tuesday, February 19 1

RECURSION 7. 1 Recursion COMPUTER SCIENCE 61A. October 15, 2012

CS 115 Exam 1, Fall 2015 Thu. 09/24/2015

Recall. Key terms. Review. Encapsulation (by getters, setters, properties) OOP Features. CSC148 Intro. to Computer Science

Structure and Interpretation of Computer Programs

CSI33 Data Structures

Structure and Interpretation of Computer Programs

CSC148 Week 2. Larry Zhang

Structure and Interpretation of Computer Programs Summer 2014 Midterm 1

Context Free Grammars and Recursive Descent Parsing

PREPARING FOR THE FINAL EXAM

CSI33 Data Structures

Mutation & Data Abstraction Summer 2018 Discussion 4: July 3, Mutable Lists

CS 1110 Final Solutions May 2017

CE151-4-AU UNIVERSITY OF ESSEX. Sample Examination Questions 2011 INTRODUCTION TO PROGRAMMING

Lecture 6. COMP1006/1406 (the OOP course) Summer M. Jason Hinek Carleton University

MITOCW watch?v=flgjisf3l78

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]

PREPARING FOR THE FINAL EXAM

Structure and Interpretation of Computer Programs

Transcription:

OOP CS 6A GROUP MENTORING July 9, 07 OOP. (H)OOP Given the following code, what will Python output for the following prompts? class Baller: all_players = [] def init (self, name, has_ball = False): self.name = name self.has_ball = has_ball Baller.all_players.append(self) def pass_ball(self, other_player): if self.has_ball: self.has_ball = False other_player.has_ball = True return True else: return False class BallHog(Baller): def pass_ball(self, other_player): return False >>> alex = Baller('Alex', True) >>> mitas = BallHog('Mitas') >>> len(baller.all_players) >>> Baller.name >>> len(mitas.all_players)

GROUP TUTORING HANDOUT 7: OOP Page >>> alex.pass_ball() >>> alex.pass_ball(mitas) >>> alex.pass_ball(mitas) >>> BallHog.pass_ball(mitas, alex) >>> mitas.pass_ball(alex) >>> mitas.pass_ball(mitas, alex). Write TeamBaller, a subclass of Baller. An instance of TeamBaller cheers on the team every time it passes a ball. class TeamBaller( ): >>> mitas = BallHog('Mitas') >>> cheerballer = TeamBaller('Chris', has_ball=true) >>> cheerballer.pass_ball(mitas) Yay! True >>> cheerballer.pass_ball(mitas) I don't have the ball False def pass_ball(, ): CS6A Summer 08: Alex Stennet and Chris Allsman, with content by Jennie Chen and

GROUP TUTORING HANDOUT 7: OOP Page 3 3. Lets use OOP to help us implement our good friend, the ping-pong sequence! As a reminder, the ping-pong sequence counts up starting from and is always either counting up or counting down. At element k, the direction switches if k is a multiple of 7 or contains the digit 7. The first 30 elements of the ping-pong sequence are listed below, with direction swaps marked using brackets at the 7th, 4th, 7th, st, 7th, and 8th elements: 3 4 5 6 [7] 6 5 4 3 [0] [3] 0 [-] 0 3 4 [5] [4] 5 6 Assume you have a function has seven(k) that returns True if k contains the digit 7. >>> tracker = PingPongTracker() >>> tracker = PingPongTracker() >>> tracker.next() >>> tracker.next() >>> tracker.next() class PingPongTracker: def init (self): self.current = 0 self.index = self.add = True def next(self): CS6A Summer 08: Alex Stennet and Chris Allsman, with content by Jennie Chen and

GROUP TUTORING HANDOUT 7: OOP Page 4 4. Flying the coop What would Python display? Write the result of executing the code and the prompts below. If a function is returned, write Function. If nothing is returned, write Nothing. If an error occurs, write Error. class Bird: def init (self, call): self.call = call self.can_fly = True def fly(self): if self.can_fly: return "Don't stop me now!" else: return "Ground control to Major Tom..." def speak(self): print(self.call) class Chicken(Bird): def speak(self, other): Bird.speak(self) other.speak() class Penguin(Bird): can_fly = False def speak(self): call = "Ice to meet you" print(call) andre = Chicken("cluck") gunter = Penguin("noot") CS6A Summer 08: Alex Stennet and Chris Allsman, with content by Jennie Chen and

GROUP TUTORING HANDOUT 7: OOP Page 5 >>> andre.speak(bird("coo")) >>> andre.speak() >>> gunter.fly() >>> andre.speak(gunter) >>> Bird.speak(gunter) CS6A Summer 08: Alex Stennet and Chris Allsman, with content by Jennie Chen and

GROUP TUTORING HANDOUT 7: OOP Page 6 Mutable Trees Now that we know how to create objects using Python s class system, we have a new way of implementing some of the ADTs we saw earlier in the course. This allows us to reassign attributes of that object any time we want! Here s an example of implementing trees using a class. class Tree: def init (self, label, branches=[]): self.label = label self.branches = branches def is_leaf(self): return not self.branches Here s how we might use this class: >>> t = Tree(, [Tree()]) = >>> t.branches = t.branches + [Tree(3)] >>> [b.label for b in t.branches] [, 3] >>> t.branches[].is_leaf() True 5. Implement tree_sum which takes in a Tree object and replaces the root value with the sum of all the values in the tree. tree_sum should also return the new root value. def tree_sum(t): >>> t = Tree(, [Tree(, [Tree(3)]), Tree(4)]) >>> tree_sum(t) 0 0 >>> t.branches[0].label 5 >>> t.branches[].label 4 CS6A Summer 08: Alex Stennet and Chris Allsman, with content by Jennie Chen and

GROUP TUTORING HANDOUT 7: OOP Page 7 6. DoubleTree hired you to architect one of their hotel expansions! As you might expect, their floor plan can be modeled as a tree and the expansion plan requires doubling each node (the patented double tree floor plan). Here s what some sample expansions look like: Before After 3 3 3 Fill in the implementation for double_tree. def double_tree(t): Given a tree, mutate it such that each entry appears twice. >>> t = Tree() >>> double_tree(t) >>> t.branches[0].label CS6A Summer 08: Alex Stennet and Chris Allsman, with content by Jennie Chen and