CSC148 winter stools, names, tracing week 5

Similar documents
CSC148-Section:L0301

CSC 148 Lecture 3. Dynamic Typing, Scoping, and Namespaces. Recursion

CSC148-Section:L0301

CSC148, Lab #4. General rules. Overview. Tracing recursion. Greatest Common Denominator GCD

CSC148-Section:L0301

CSE : Python Programming

LECTURE 14. Names, Scopes, and Bindings: Scopes

CSC148-Section:L0301/L0401

CSC148-Section:L0301

CSC148-Section:L0301

About Variables in Python F E B 1 1 T H

15-110: Principles of Computing, Spring 2018

CS 164 Handout 16. Final Examination. There are nine questions on the exam, some in multiple parts. You have 3 hours to work on the

CS1 Lecture 5 Jan. 25, 2019

11/2/2017 RECURSION. Chapter 5. Recursive Thinking. Section 5.1

Lecture 24 Tao Wang 1

Practical Questions CSCA48 Winter 2018 Week 11

Recursive Definitions

CSC148-Section:L0301

Last week. Another example. More recursive examples. How about these functions? Recursive programs. CSC148 Intro. to Computer Science

Python Basics. Lecture and Lab 5 Day Course. Python Basics

CSC148 Week 2. Larry Zhang

CMSC 330: Organization of Programming Languages

CS116 - Module 5 - Accumulative Recursion

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 26, 2014

Recursion. CSCI 112: Programming in C

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 27, 2013

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

CS1 Lecture 5 Jan. 26, 2018

Lecture 16: Static Semantics Overview 1

Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems. COMP Week 8 & 9 1

Names, Scopes, and Bindings. CSE 307 Principles of Programming Languages Stony Brook University

Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

GE PROBLEM SOVING AND PYTHON PROGRAMMING. Question Bank UNIT 1 - ALGORITHMIC PROBLEM SOLVING

Names, Scopes, and Bindings. CSE 307 Principles of Programming Languages Stony Brook University

def F a c t o r i a l ( n ) : i f n == 1 : return 1 else : return n F a c t o r i a l ( n 1) def main ( ) : print ( F a c t o r i a l ( 4 ) )

Algorithm. Building blocks of algorithm

Introduction to Problem Solving and Programming in Python.

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

mith College Computer Science Week 12 CSC111 Spring 2018 Dominique Thiébaut

Recursion Chapter 17. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Chapter 4 The If Then Statement

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

Question 1. CSC 148H1 Term test #1 Solutions February [8 marks]

Flow Control. CSC215 Lecture

Overview. Elements of Programming Languages. Objects. Self-Reference

Contents. Slide Set 1. About these slides. Outline of Slide Set 1. Typographical conventions: Italics. Typographical conventions. About these slides

CSC148 Intro. to Computer Science

CSE11 Lecture 20 Fall 2013 Recursion

TAIL RECURSION, SCOPE, AND PROJECT 4 11

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

CS1 Lecture 15 Feb. 18, 2019

CS 415 Midterm Exam Spring SOLUTION

Recursive Thinking. Chapter 8: Recursion. Recursive Definitions. Recursion. Java Software Solutions for AP* Computer Science A 2nd Edition

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

2. Reachability in garbage collection is just an approximation of garbage.

CSC 273 Data Structures

Execution order. main()

CSC148-Section:L0301

CS1 Lecture 22 Mar. 6, 2019

Project 2: Scheme Interpreter

CS558 Programming Languages

Solving problems by recursion

Informal Semantics of Data. semantic specification names (identifiers) attributes binding declarations scope rules visibility

CMSC 150 LECTURE 7 RECURSION

INF 102 CONCEPTS OF PROG. LANGS FUNCTIONAL COMPOSITION. Instructors: James Jones Copyright Instructors.

CS558 Programming Languages

Data Structures And Algorithms

CS61A Notes Week 13: Interpreters

Day 5. COMP1006/1406 Summer M. Jason Hinek Carleton University

Lecture 20. Subclasses & Inheritance

Slide Set 1 (corrected)

Overview. Elements of Programming Languages. Objects. Self-Reference

CPSC 217 Midterm (Python 3 version)

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

Module 06. Topics: Iterative structure in Python Readings: ThinkP 7. CS116 Spring : Iteration

CS 135 Winter 2018 Tutorial 8: Mutual recursion, nested lists, and local. CS 135 Winter 2018 Tutorial 8: Mutual recursion, nested lists, and local 1

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

4.2 Function definitions the basics

Programming Paradigms in Python

More Static Semantics. Static. One-Slide Summary. Lecture Outline. Typing Rules. Dispatch Rules SELF_TYPE

8/5/10 TODAY'S OUTLINE. Recursion COMP 10 EXPLORING COMPUTER SCIENCE. Revisit search and sorting using recursion. Recursion WHAT DOES THIS CODE DO?

introduction to Programming in C Department of Computer Science and Engineering Lecture No. #40 Recursion Linear Recursion

Late Binding; OOP as a Racket Pattern

Exercise 1: Tail Sum of Squares (2 points) Homework 3 READ THIS BEFORE YOU BEGIN:

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 8: SEP. 29TH INSTRUCTOR: JIAYIN WANG

Recursion. Genome 559: Introduction to Statistical and Computational Genomics Elhanan Borenstein

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

COMP 202 Recursion. CONTENTS: Recursion. COMP Recursion 1

Python Boot Camp. Day 3

Project 4 Due 11:59:59pm Thu, May 10, 2012

1 Getting used to Python

Compiler Construction

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

The role of semantic analysis in a compiler

Fundamentals of Programming Session 13

Reading 8 : Recursion

INF 212 ANALYSIS OF PROG. LANGS FUNCTION COMPOSITION. Instructors: Crista Lopes Copyright Instructors.

ENVIRONMENT DIAGRAMS AND RECURSION 2

Transcription:

1/18 CSC148 winter 2014 stools, names, tracing week 5 Danny Heap / Dustin Wehr heap@cs.toronto.edu / dustin.wehr@utoronto.ca BA4270 / SF4306D http://www.cdf.toronto.edu/~heap/148/f13/ February 5, 2014

2/18 Outline tracing... or not prose to (recursive) code memory model name resolution (name lookup) unit testing example

3/18 don't trace too far! def rec_max(l): """ Return the maximum number in possibly nested list of numbers. >>> rec_max([17, 21, 0]) 21 >>> rec_max([17, [21, 24], 0]) 24 >>> rec_max([17, [21, 24], [18, 37, 16], 0]) 37 """ return max([rec_max(x) if isinstance(x, list) else x for x in L]) Recommended: I trace the simplest (non-recursive) case I trace the next-most complex case, plug in known results I same as previous step...

4/18 TMI tracing In contrast to the step-by-step, plus abstraction (previous slide), you could trace this in the visualizer

5/18 getting that recursive insight for Tower of Hanoi In order to implement a function that moves n cheeses from, say, stool 1 to stool 3, we'd rst think of a name and parameters. We can start with movecheeses(n, source, dest), meaning show the moves from source stool to destination stool for n cheeses.

6/18 getting that recursive insight for Tower of Hanoi In order to implement a function that moves n cheeses from, say, stool 1 to stool 3, we'd rst think of a name and parameters. We can start with movecheeses(n, source, dest), meaning show the moves from source stool to destination stool for n cheeses.

7/18 getting that recursive insight for Tower of Hanoi In order to implement a function that moves n cheeses from, say, stool 1 to stool 3, we'd rst think of a name and parameters. We can start with movecheeses(n, source, dest), meaning show the moves from source stool to destination stool for n cheeses.

8/18 stating that recursive insight: The doodling on the previous slide breaks into a pattern. I move all but the bottom cheese from source to intermediate stool (sounds recursive...) I move the bottom cheese from the source to the destination stool (sounds like the 1-cheese move) I move all but the bottom cheese from the intermediate to the destination stool (sounds recursive...) The original problem repeats, except with dierent source, destination, and intermediate stools! New name: movecheeses(n, source, intermediate, destination)

9/18 write some code! Fill in the three steps from the previous slide, using recursive calls to movecheeses(...) with dierent values for the number of cheeses, the source, destination, and intermediate stools, where appropriate. def move_cheeses(n: int, source: int, intermediate: int, destination: int) -> None: """Print moves to get n cheeses from source to destination, possibly using intermediate""" if n > 1: # fill this in! move_cheeses(?,?,?,?) move_cheeses(?,?,?,?) move_cheeses(?,?,?,?) else: # just 1 cheese --- leave this out for now!

10/18 complete that code! Now, ll in what you do to move just one cheese don't use any recursion! You will be returning a string that species you are moving from source to destination. def move_cheeses(n: int, source: int, intermediate: int, destination: int) -> None: """Print moves to get n cheeses from source to destination, possibly using intermediate""" if n > 1: move_cheeses(n - 1, source, destination, intermediate) move_cheeses(1, source, intermediate, destination) move_cheeses(n - 1, intermediate, source, destination) else: # just 1 cheese --- fill this in now! print(???? )

11/18 complete that code! Now, ll in what you do to move just one cheese don't use any recursion! You will be returning a string that species you are moving from source to destination. def move_cheeses(n: int, source: int, intermediate: int, destination: int) -> None: """Print moves to get n cheeses from source to destination, possibly using intermediate""" if n > 1: move_cheeses(n - 1, source, destination, intermediate) move_cheeses(1, source, intermediate, destination) move_cheeses(n - 1, intermediate, source, destination) else: print(source, "-->", destination)

12/18 python gratication Once you have your code entered into some Python environment, you should run it with a few small values of n. As usual, you can get more intuition about it by tracing examples, working from small to larger n

13/18 how much detail for developers? Enough detail to predict results and eciency of our code more detail than end users, less than compiler/interpreter designers. In Python: I Every name x contains a value id(x) I Every value is a reference to the address of an object

13/18 how much detail for developers? Enough detail to predict results and eciency of our code more detail than end users, less than compiler/interpreter designers. In Python: I Every name x contains a value id(x) I Every value is a reference to the address of an object I Actually, the python docs consider the \address" part an implementation detail, not relevant for developers. Docs for id(): Return the \identity" of an object. This is an integer which is guaranteed to be unique and constant for this object during its lifetime. Two objects with non-overlapping lifetimes may have the same id() value.

14/18 searching for names python looks, in order: I innermost scope (function body usually) local (can also be a list/dictionary comprehension, or a lambda expression) I scopes of enclosing functions nonlocal I global (current module or main ). I built-in names I see scopes and namespaces

15/18 intense example Try running python docs namespace example to check your comfort level This might seem like a very python-specic thing, but in fact every programming language has some standard for name lookup, and they are all fairly similar.

16/18 methods The rst parameter, conventionally called self, is a reference to the instance: >>> class Foo:... def f(self):... return "Hi world!"... >>> x = Foo() Now Foo.f(x) means x.f()

17/18 hunting for a method... Start in the object's nearest subclass and work upwards (through the inheritance hierarchy), for example visualize method

18/18 write unit tests with good coverage for this function def max_nested(l:list) -> int or None : """ REQ : L is a (possibly-nested) list of integers Returns the largest integer in L, or None if L has no integers in it. """ if len(l) == 0: return None maxes_of_parts = [] for x in L: if isinstance(x,int): maxes_of_parts.append(x) else: y = max_nested(x) if y is not None: maxes_of_parts.append(y) return max(maxes_of_parts) review: choosing test cases

18/18 write unit tests with good coverage for this function def max_nested(l:list) -> int or None : """ REQ : L is a (possibly-nested) list of integers Returns the largest integer in L, or None if L has no integers in it. """ if len(l) == 0: return None maxes_of_parts = [] for x in L: if isinstance(x,int): maxes_of_parts.append(x) else: y = max_nested(x) if y is not None: maxes_of_parts.append(y) return max(maxes_of_parts) Did your unit tests find an error in the code?