CSC148-Section:L0301

Similar documents
CSC148-Section:L0301

CSC148-Section:L0301/L0401

CSC148-Section:L0301

CSC148-Section:L0301

CSC148-Section:L0301

CSC148-Section:L0301

CSC148 winter stools, names, tracing week 5

CSC148-Section:L0301

CS205: Scalable Software Systems

CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees. CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees 1

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

COMP 202 Recursion. CONTENTS: Recursion. COMP Recursion 1

UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFR08013 INFORMATICS 1 - FUNCTIONAL PROGRAMMING

UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFR08013 INFORMATICS 1 - FUNCTIONAL PROGRAMMING

Introduction to Python and programming. Ruth Anderson UW CSE 160 Winter 2017

CS21: INTRODUCTION TO COMPUTER SCIENCE. Prof. Mathieson Fall 2017 Swarthmore College

Informatics 1 Functional Programming Lecture 4. Lists and Recursion. Don Sannella University of Edinburgh

COMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others

Functional Programming in Haskell Part I : Basics

University of Washington CSE 140 Data Programming Winter Final exam. March 11, 2013

All the operations used in the expression are over integers. a takes a pair as argument. (a pair is also a tuple, or more specifically, a 2-tuple)

CS205: Scalable Software Systems

CS1 Lecture 15 Feb. 18, 2019

CS115 INTRODUCTION TO COMPUTER SCIENCE 1. Additional Notes Module 5

CSE 160 Winter 2017: Final Exam

CSE 115. Introduction to Computer Science I

Practical Questions CSCA48 Week 6

Data Structures and Algorithms 2018

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Week - 03 Lecture - 18 Recursion. For the last lecture of this week, we will look at recursive functions. (Refer Slide Time: 00:05)

Topic 7: Algebraic Data Types

CSC236 Week 4. Larry Zhang

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions

First name (printed): a. DO NOT OPEN YOUR EXAM BOOKLET UNTIL YOU HAVE BEEN TOLD TO BEGIN.

Solving Your Problem by Generalization

CSC 1351: Quiz 6: Sort and Search

List are immutable Lists have recursive structure Lists are homogeneous

If we have a call. Now consider fastmap, a version of map that uses futures: Now look at the call. That is, instead of

More Functions on Lists

Fundamentals of Programming (Python) Getting Started with Programming

CSC148 Week 2. Larry Zhang

3. Priority Queues. ADT Stack : LIFO. ADT Queue : FIFO. ADT Priority Queue : pick the element with the lowest (or highest) priority.

Working with recursion

CSC 120 Computer Science for the Sciences. Week 1 Lecture 2. UofT St. George January 11, 2016

CSCE 314 Programming Languages

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

Shell CSCE 314 TAMU. Higher Order Functions

Lists How lists are like strings

The List Datatype. CSc 372. Comparative Programming Languages. 6 : Haskell Lists. Department of Computer Science University of Arizona

Parallel Programming

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

Comp 311 Functional Programming. Eric Allen, Two Sigma Investments Robert Corky Cartwright, Rice University Sagnak Tasirlar, Two Sigma Investments

Shell CSCE 314 TAMU. Haskell Functions

UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFR08013 INFORMATICS 1 - FUNCTIONAL PROGRAMMING

CS 61B, Spring 1999 MT3 Professor M. Clancy

Recall our recursive multiply algorithm:

CS 1110 Prelim 2 November 6th, 2012

Review for the Final Exam CS 8: Introduction to Computer Science, Winter 2018 Lecture #15

Overview of List Syntax

University of Palestine. Mid Exam Total Grade: 100

Name: utorid: U of T

Haskell Review COMP360

LECTURE 18 AVL TREES

At full speed with Python

Question 1. tmp = Stack() # Transfer every item from stk onto tmp. while not stk.is_empty(): tmp.push(stk.pop())

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

More About Heaps. 14 June 2017 OSU CSE 1

INTERACTION FUNCTIONS

F28PL1 Programming Languages. Lecture 14: Standard ML 4

Computer Science E-119 Fall Problem Set 1. Due before lecture on Wednesday, September 26

Shell CSCE 314 TAMU. Functions continued

An Introduction to MATLAB

COMP 2718: Shell Scripts: Part 1. By: Dr. Andrew Vardy

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

ENGR 101 Engineering Design Workshop

University of Waterloo Department of Electrical and Computer Engineering ECE 250 Data Structures and Algorithms. Final Examination

CSC324- TUTORIAL 5. Shems Saleh* *Some slides inspired by/based on Afsaneh Fazly s slides

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

The Design Recipe Fall 2017

CPSC 217 Midterm (Python 3 version)

Linked lists and the List class

INTRODUCTION TO HASKELL

CSc 110, Autumn Lecture 26: Assertions. Adapted from slides by Marty Stepp and Stuart Reges

isinstance and While Loops

Note that pcall can be implemented using futures. That is, instead of. we can use

Questions? Static Semantics. Static Semantics. Static Semantics. Next week on Wednesday (5 th of October) no

BRONX COMMUNITY COLLEGE of the City University of New York DEPARTMENT OF MATHEMATICS AND COMPUTER SCIENCE. Sample Final Exam

Practical Questions CSCA48 Winter 2018 Week 11

Introduction to Computer Science II (CSI 1101)

Hints for Exercise 4: Recursion

CSE548, AMS542: Analysis of Algorithms, Fall 2012 Date: October 16. In-Class Midterm. ( 11:35 AM 12:50 PM : 75 Minutes )

CS Lecture 19: Loop invariants

CS Introduction to Programming Fall 2016

CSC324 Functional Programming Typing, Exceptions in ML

6.046 Recitation 1: Proving Correctness Bill Thies, Fall 2004 Outline

ITI Introduction to Computing II

Recommendation: Play the game and attempt to answer the questions yourself without looking at the answers. You ll learn much less if you just look at

ITI Introduction to Computing II

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

Sorting and Selection

Transcription:

Slides adapted from Professor Danny Heap course material winter17 CSC148-Section:L0301 Week#6-Wednesday Instructed by AbdulAziz Al-Helali a.alhelali@mail.utoronto.ca Office hours: Wednesday 11-1, BA2230. 1

Outline Recursion Quick review of recursion and the template for writing recursive functions Implement recursive functions 2

Template for structural recursion recursion when input is a recursive structure: Base case if input cannot be decomposed into recursive sub-structures, you have a base case and you directly return a result without recursion General case if input can be decomposed into recursive sub-structures, solve them recursively and combine the result(s) 3

Template Use List comprehensions if isinstance(list_, list):#general case return helper_function([your_rec_func(x) for x in list_]) else:#base case return list_ do something with base input 4

functional if <expression 1> if <condition> else <expression 2> False [flatten(x) if isinstance(x,list) else [x] for x in list_] True expression 1 is evaluated if condition is True expression 2 is evaluated if condition is False 5

def gather_lists(list_: Union[List, object]) -> List[object]: Return the concatenation of the sublists of list_. >>> list_ = [[1, 2], [3, 4]] >>> gather_lists(list_) [1, 2, 3, 4] >>> list_ = [[1,[ -1,0],2], [3, 4]] >>> gather_lists(list_) [1, -1, 0, 2, 3, 4] # special form of sum for "adding" lists return sum([gather_lists(x) if isinstance(x, list) else [x] for x in list_], []) 6

def gather_lists(list_: Union[List, object]) -> List[object]: Return the concatenation of the sublists of list_. >>> list_ = [[1, 2], [3, 4]] >>> gather_lists(list_) [1, 2, 3, 4] >>> list_ = [[1,[ -1,0],2], [3, 4]] >>> gather_lists(list_) [1, -1, 0, 2, 3, 4] # special form of sum for "adding" lists return sum([gather_lists1(x) for x in list_], []) if isinstance(list_, list) else [list_] 7

def list_over(obj: Union[list, str], n: int) -> List[str]: Return a list of strings of length greater than n in obj, or sublists of obj, if obj is a list. If obj is a string of length greater than n, return a list containing obj. Otherwise return an empty list. >>> list_over("five", 3) ['five'] >>> list_over("five", 4) [] >>> list_over(["one", "two", "three", "four"], 3) ['three', 'four'] if isinstance(obj, list): return sum([list_over(x,n) for x in obj], []) else: return [obj] if len(obj)>n else [] 8

def list_over(obj: Union[list, str], n: int) -> List[str]: Return a list of strings of length greater than n in obj, or sublists of obj, if obj is a list. If obj is a string of length greater than n, return a list containing obj. Otherwise return an empty list. >>> list_over("five", 3) ['five'] >>> list_over("five", 4) [] >>> list_over(["one", "two", "three", "four"], 3) ['three', 'four'] if isinstance(obj, list): return sum([list_over(x,n) for x in obj], []) else: return [obj] if len(obj)>n else [] 9

def list_even(obj: Union[list, int]) -> List[int]: Return a list of all even integers in obj, or sublists of obj, if obj is a list. If obj is an even integer, return a list containing obj. Otherwise return en empty list. >>> list_even(3) [] >>> list_even(16) [16] >>> list_even([1, 2, 3, 4, 5]) [2, 4] >>> list_even([1, 2, [3, 4], 5]) [2, 4] >>> list_even([1, [2, [3, 4]], 5]) [2, 4] 10

def list_even(obj: Union[list, int]) -> List[int]: Return a list of all even integers in obj, or sublists of obj, if obj is a list. If obj is an even integer, return a list containing obj. Otherwise return en empty list. >>> list_even(3) [] >>> list_even(16) [16] >>> list_even([1, 2, 3, 4, 5]) [2, 4] >>> list_even([1, [2, [3, 4]], 5]) [2, 4] if isinstance(obj, list): return sum([list_even(x) for x in obj], []) else: return [obj] if obj%2==0 else [] 11

def count_all(obj: Union[list, object]) -> int: Return the number of elements in obj or sublists of obj if obj is a list. Otherwise, if obj is a non-list return 1. >>> count_all(17) 1 >>> count_all([17, 17, 5]) 3 >>> count_all([17, [17, 5], 3]) 4 if isinstance(obj, list): return sum([count_all(x) for x in obj]) else: return 1 12

def count_all(obj: Union[list, object]) -> int: Return the number of elements in obj or sublists of obj if obj is a list. Otherwise, if obj is a non-list return 1. >>> count_all(17) 1 >>> count_all([17, 17, 5]) 3 >>> count_all([17, [17, 5], 3]) 4 if isinstance(obj, list): return sum([count_all(x) for x in obj]) else: return 1 13

To save space, let us denote: count_all as ca so that count_all([1,2] is ca([1,2])) >>>ca([5, [3, [1,2,[2],3], 1], 4, 6]) 9 # answer is 9 ca( [5,, 4, 6 ] sum( [ 1, 6, 1,1 ] ) ca( [3,, 1 ] ) sum( [ 1, 4, 1 ] ) ca( [1,2,,3] ) sum( [ 1, 1, 1, 1] ) ca( [ca(2)] ) sum( ) [ 1 ] 14

def count_even(obj: Union[list, int]) -> int: Return the number of even numbers in obj or sublists of obj if obj is a list. Otherwise, if obj is a number, return 1 if it is an even number and 0 if it is an odd number. >>> count_even(3) 0 >>> count_even(16) 1 >>> count_even([1, 2, [3, 4], 5]) 2 if isinstance(obj, list): return sum([count_even(x) for x in obj]) else: return 1 if obj%2==0 else 0 15

def count_even(obj: Union[list, int]) -> int: Return the number of even numbers in obj or sublists of obj if obj is a list. Otherwise, if obj is a number, return 1 if it is an even number and 0 if it is an odd number. >>> count_even(3) 0 >>> count_even(16) 1 >>> count_even([1, 2, [3, 4], 5]) 2 if isinstance(obj, list): return sum([count_even(x) for x in obj]) else: return 1 if obj%2==0 else 0 16

def count_above(obj: Union[list, int], n: int) -> int: Return tally of numbers in obj, and sublists of obj, that are over n, if obj is a list. Otherwise, if obj is a number over n, return 1. Otherwise return 0. >>> count_above(17, 19) 0 >>> count_above(19, 17) 1 >>> count_above([17, 18, 19, 20], 18) 2 >>> count_above([17, 18, [19, 20]], 18) 2 17

def count_above(obj: Union[list, int], n: int) -> int: Return tally of numbers in obj, and sublists of obj, that are over n, if obj is a list. Otherwise, if obj is a number over n, return 1. Otherwise return 0. >>> count_above(17, 19) 0 >>> count_above(19, 17) 1 >>> count_above([17, 18, [19, 20]], 18) 2 if isinstance(obj, list): return sum([count_above(x,n) for x in obj]) else: return 1 if obj>n else 0 18

def max_length(obj: Union[list, object]) -> int: Return the maximum length of obj or any of its sublists, if obj is a list. otherwise return 0. >>> max_length(17) 0 >>> max_length([1, 2, 3, 17]) 4 >>> max_length([1,[[1, 2, 3,2, 3,2, 3,2, 3], 4, [4, 5]]]) 9 if isinstance(obj, list): t= [max_length(x) for x in obj] return max(t) if max(t)>len(t) else len(t) else: return 0 19

def max_length(obj: Union[list, object]) -> int: Return the maximum length of obj or any of its sublists, if obj is a list. otherwise return 0. >>> max_length(17) 0 >>> max_length([1, 2, 3, 17]) 4 >>> max_length([1,[[1, 2, 3,2, 3,2, 3,2, 3], 4, [4, 5]]]) 9 if isinstance(obj, list): t= [max_length(x) for x in obj] return max(t) if max(t)>len(t) else len(t) else: return 0 20