CS1 Lecture 15 Feb. 18, 2019

Similar documents
CS1 Lecture 15 Feb. 19, 2018

CS1 Lecture 13 Feb. 13, 2019

Recursion. Recursion [Bono] 1

OVERVIEW. Recursion is an algorithmic technique where a function calls itself directly or indirectly. Why learn recursion?

Data Structures And Algorithms

CS1 Lecture 12 Feb. 11, 2019

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

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

recursive algorithms 1

Module 05: Types of recursion

q To develop recursive methods for recursive mathematical functions ( ).

q To develop recursive methods for recursive mathematical functions ( ).

UNIT 5A Recursion: Basics. Recursion

Announcements. Recursion and why study it. Recursive programming. Recursion basic idea

ENVIRONMENT DIAGRAMS AND RECURSION 2

Environment Diagrams and Recursion Fall 2017 Discussion 2: September 6, 2017 Solutions. 1 More Environment Diagrams

2: Complexity and Recursion

CS1 Lecture 5 Jan. 26, 2018

Reading 8 : Recursion

CS 4349 Lecture September 13th, 2017

CS1 Recitation. Week 2

Two Approaches to Algorithms An Example (1) Iteration (2) Recursion

61A Lecture 4. Monday, September 9

ESc101 : Fundamental of Computing

CS205: Scalable Software Systems

Recursive Definitions

CSE 2123 Recursion. Jeremy Morris

Measuring Efficiency

Solving Recursive Sequences by Iteration

Lecture 13. Call Stacks & Debugging

Lecture 3.4: Recursive Algorithms

Recursive Helper functions

Functions. CS10001: Programming & Data Structures. Sudeshna Sarkar Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

C22a: Problem Solving using Recursion

UNIT 5A Recursion: Basics. Recursion

# track total function calls using a global variable global fibcallcounter fibcallcounter +=1

APCS-AB: Java. Recursion in Java December 12, week14 1

Lecture 26: Sorting CS 1110 Introduction to Computing Using Python

CS1 Lecture 11 Feb. 9, 2018

RECURSION, RECURSION, (TREE) RECURSION! 2

Lecture Notes 4 More C++ and recursion CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson

Physics 2660: Fundamentals of Scientific Computing. Lecture 5 Instructor: Prof. Chris Neu

CS/CE 2336 Computer Science II

RECURSION. Data Structures & SWU Rachel Cardell- Oliver

CS1 Lecture 22 Mar. 6, 2019

Dynamic programming 4/9/18

CS 051 Homework Laboratory #2

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

CMSC 132: Object-Oriented Programming II. Recursive Algorithms. Department of Computer Science University of Maryland, College Park

Recursion. Tracing Method Calls via a Stack. Beyond this lecture...

Recursive Methods and Problem Solving. Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms

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 ) )

Lecture 12: Dynamic Programming Part 1 10:00 AM, Feb 21, 2018

Recursion: Factorial (1) Recursion. Recursion: Principle. Recursion: Factorial (2) Recall the formal definition of calculating the n factorial:

CS1 Lecture 9 Feb. 5, 2018

Lecture 6 CS2110 Spring 2013 RECURSION

CS 380 ALGORITHM DESIGN AND ANALYSIS

Today: Amortized Analysis (examples) Multithreaded Algs.

Algorithmic Methods Tricks of the Trade

Decorators in Python

CS 100: Computability, Python Lists


RECURSION 3. 1 Recursion COMPUTER SCIENCE 61A. June 30, 2016

Using the MIPS Calling Convention. Recursive Functions in Assembly. CS 64: Computer Organization and Design Logic Lecture #10 Fall 2018

Recursion. EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG

Class 6: Efficiency in Scheme

CSE : Python Programming

UNIT 5B Binary Search

CS 171: Introduction to Computer Science II. Quicksort

61A LECTURE 15 MEMOIZATION, RECURSIVE DATA, SETS

Algorithmics. Some information. Programming details: Ruby desuka?

Lecture 8: Conditionals & Control Flow (Sections ) CS 1110 Introduction to Computing Using Python

Admin. How's the project coming? After these slides, read chapter 13 in your book. Quizzes will return

Outline. Simple Recursive Examples Analyzing Recursion Sorting The Tower of Hanoi Divide-and-conquer Approach In-Class Work. 1 Chapter 6: Recursion

MEIN 50010: Python Recursive Functions

Homework 3: Recursion Due: 11:59 PM, Sep 25, 2018

Physics 2660: Fundamentals of Scientific Computing. Lecture 7 Instructor: Prof. Chris Neu

9/16/14. Overview references to sections in text RECURSION. What does generic mean? A little about generics used in A3

ENVIRONMENT DIAGRAMS AND RECURSION 2

CS 211: Recursion. Chris Kauffman. Week 13-1

CS Introduction to Data Structures Week 4, 2017

CS 318 Principles of Operating Systems

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

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

Lecture 7 CS2110 Fall 2014 RECURSION

Math 4242 Fibonacci, Memoization lecture 13

CS1 Lecture 30 Apr. 2, 2018

Instructors: Daniel Deutch, Amir Rubinstein, Teaching Assistants: Amir Gilad, Michal Kleinbort

Tail Recursion. ;; a recursive program for factorial (define fact (lambda (m) ;; m is non-negative (if (= m 0) 1 (* m (fact (- m 1))))))

CS3: Introduction to Symbolic Programming. Lecture 5:

CSI33 Data Structures

RECURSION, RECURSION, (TREE) RECURSION! 3

CS61C Machine Structures. Lecture 3 Introduction to the C Programming Language. 1/23/2006 John Wawrzynek. www-inst.eecs.berkeley.

Recursion. ! When the initial copy finishes executing, it returns to the part of the program that made the initial call to the function.

RECURSION (CONTINUED)

CS 135 Lab Assignments Week 12

CSE 143. Computer Programming II

Lecture 4: Defining Functions

Lecture #5: Higher-Order Functions. A Simple Recursion. Avoiding Recalculation. Redundant Calculation. Tail Recursion and Repetition

UNIT 5C Merge Sort. Course Announcements

Transcription:

CS1 Lecture 15 Feb. 18, 2019 HW4 due Wed. 2/20, 5pm Q2 and Q3: it is fine to use a loop as long as the function is also recursive. Exam 1, Thursday evening, 2/21, 6:30-8:00pm, W290 CB You must bring ID No points in this week s discussion sections. Not required. Consider them just extra office hours to get help with HW4 or other problems. Wednesday: exam review, including sample questions

Last time Recursion Ch 5 Several basic examples Today More recursion examples

(last time) Important rules for recursive functions When writing a recursive function: MUST have base case(s), situations when code does not make recursive call. MUST ensure that recursive calls make progress toward base cases. I.e. you need to convince yourself that recursive call is closer to base case than the original problem you are working on SHOULD ensure you don t unnecessarily repeat work. Ignoring this contributes to recursion s bad reputation. E.g. direct recursive implementation of Fibonacci is extremely and unnecessarily inefficient

(last time) Recursion examples printlist, printlistreverse sumlistitems ispalindrome wasitacaroracatisaw is a palindrome sitonapotatopanotis is a palindrome Fibonacci sequence: 1, 1, 2, 3, 5, 8, fib(0) = 1, fib(1) = 1 fib(n) = fib(n-1) + fib(n-2), for n > 1 An example where naïve direct recursive implementation is impractically slow lec14.py

Simplest direct recursive implementation of Fibonacci violates third rule of designing recursive function: redoes many calculations many times For fib(6), fib(2) called 5 times, fib(1) called 3 times. This can be easily fixed (while maintaining basic recursive structure). Technique is called memoization and just means that once you calculate something store the value in a table. Whenever about to calculated something, check first if value is already in table Thus, after fib(5) calls fib(4), fib(4) s value, 3, is stored in a table. When fib(6) needs the value fib(4), it will be retrieved from the table rather than generating calls to fib(3), fib(2), etc.

flatten a list More recursion examples E.g. [[[[[3,[2,4]]], 0], [ a ]], 23] -> [3,2,4,0, a, 23] generate a number sequence Towers of Hanoi problem Drawing shapes lec15.py ToHcomplete.py

Q2 of HW4 Def of nestingdepth of a list 0 if list has no lists as elements 1 more than maximum nestingdepth among all items of the list that are lists Examples [] -> 0 [[]] -> 1 [[[]]] -> 2 [[[]], 1, 2] -> 2, because [[]] is 1 so the whole list is 1 + 1 = 2 [ [[]], 1, 2, [[[100]]] ] -> 3, because [[]] is 1, [[[100]]] is 2, max is 2, so whole list is 2 + 1 = 3 def nestingdepth(inlist): # initialize a variable for max nesting level seen (among sublists) # iterate over items in inlist # if item is not a list, just skip it # if item is a list, recursively compute its nesting level and # compare that item s nesting level with max seen so far, # updating max if nec # if max has been update in the loop, return 0 # otherwise return 1 more than max

Q3 of HW4 Definition of similar: Two items similar if types equal, or are both numbers If they are lists, they are equal length and their corresponding items are similar Function outline: def aresimilar(item1, item2): # if both items are numbers return? # if types not equal return? # if types equal and items are not lists return? # if items are lists of same length # loop over items of lists, checking similarity # (recursively!) of corresponding items # if some pair is not similar return False # if you complete loop without returning False then all # corresponding pairs are similar so you can return True

Towers of Hanoi solution Rods/towers A, B, C. Goal is to move disks from A to C, one at a time, never allowing larger disk to be on top of smaller disk Algorithm: Move n-1 disks from A -> B (by this algorithm!) Move final disk from A -> C Move n-1 disks from B -> C (by this algorithm!) ToHcomplete.py

Next Time Review for exam, and sample problems