CS1 Lecture 15 Feb. 19, 2018

Similar documents
CS1 Lecture 15 Feb. 18, 2019

CS1 Lecture 13 Feb. 13, 2019

recursive algorithms 1

Recursion. Recursion [Bono] 1

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

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

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

UNIT 5A Recursion: Basics. Recursion

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

UNIT 5A Recursion: Basics. Recursion

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

Module 05: Types of recursion

CS1 Lecture 5 Jan. 26, 2018

ENVIRONMENT DIAGRAMS AND RECURSION 2

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

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

Data Structures And Algorithms

CS/CE 2336 Computer Science II

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

Counting Problems; and Recursion! CSCI 2824, Fall 2012!

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

Clicker Question What will this print? def blah(x): return x+1 def main(): z = blah(3) print(z) main() A) 3 B) 4 C) 5 D) It causes an error

Recursive Definitions

RECURSION, RECURSION, (TREE) RECURSION! 2

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

1.7 Recursion. Department of CSE

Functions and Recursion

COMP-202: Foundations of Programming. Lecture 13: Recursion Sandeep Manjanna, Summer 2015

EE 368. Weeks 4 (Notes)

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

RECURSION: n. SEE RECURSION 3

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

Algorithmics. Some information. Programming details: Ruby desuka?

Solving Recursive Sequences by Iteration

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

ENVIRONMENT DIAGRAMS AND RECURSION 2

C22a: Problem Solving using Recursion

CS1 Lecture 3 Jan. 18, 2019

2: Complexity and Recursion

RECURSION, RECURSION, (TREE) RECURSION! 3

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

ESc101 : Fundamental of Computing

Identify recursive algorithms Write simple recursive algorithms Understand recursive function calling

Chapter 15: Recursion

ITERATION AND RECURSION 3

RECURSION, RECURSION, (TREE) RECURSION! 3

CS 61A Higher Order Functions and Recursion Fall 2018 Discussion 2: June 26, 2018 Solutions. 1 Higher Order Functions. HOFs in Environment Diagrams

DM550/DM857 Introduction to Programming. Peter Schneider-Kamp

CS1 Lecture 22 Mar. 6, 2019

CS1 Lecture 12 Feb. 11, 2019

CS1 Lecture 5 Jan. 25, 2019

UNIT 5B Binary Search

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 14: Recursion

CSE 2123 Recursion. Jeremy Morris

Math 152: Applicable Mathematics and Computing

CS1 Lecture 3 Jan. 22, 2018

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

CSE 341 Lecture 5. efficiency issues; tail recursion; print Ullman ; 4.1. slides created by Marty Stepp

Lecture 3.4: Recursive Algorithms

CS116 - Module 5 - Accumulative Recursion

CSC236 Week 5. Larry Zhang

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

CSC-140 Assignment 4

CS 61A Higher Order Functions and Recursion Fall 2018 Discussion 2: June 26, Higher Order Functions. HOFs in Environment Diagrams

CSCI 121: Recursive Functions & Procedures

CS1 Lecture 11 Feb. 9, 2018

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

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

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

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

Lecture 7 CS2110 Fall 2014 RECURSION

Recursion. Fundamentals of Computer Science

= otherwise W-4 W-1. Overview. CSE 142 Computer Programming I. Overview. Factorial Function

RECURSION. Data Structures & SWU Rachel Cardell- Oliver

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

Lecture 6 CS2110 Spring 2013 RECURSION

CS205: Scalable Software Systems

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

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

CS3: Introduction to Symbolic Programming. Lecture 5:

34. Recursion. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

CS1 Recitation. Week 2

Recursion CS GMU

Algorithmic Methods Tricks of the Trade

CS 135 Lab Assignments Week 1

Resources matter. Orders of Growth of Processes. R(n)= (n 2 ) Orders of growth of processes. Partial trace for (ifact 4) Partial trace for (fact 4)

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

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

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

CS1 Lecture 4 Jan. 23, 2019

Lecture 6: Recursion RECURSION

CS 310 Advanced Data Structures and Algorithms

cs Java: lecture #6

Recursion (Rosen, 6 th edition, Section 4.3, 4.4)

ECE 2400 Computer Systems Programming Fall 2018 Topic 2: C Recursion

CS 051 Homework Laboratory #2

I2204 ImperativeProgramming Semester: 1 Academic Year: 2018/2019 Credits: 5 Dr Antoun Yaacoub

Data structure and algorithm in Python

CS115 INTRODUCTION TO COMPUTER SCIENCE 1. Additional Notes Module 5

CS61C Machine Structures. Lecture 12 - MIPS Procedures II & Logical Ops. 2/13/2006 John Wawrzynek. www-inst.eecs.berkeley.

Transcription:

CS1 Lecture 15 Feb. 19, 2018 HW4 due Wed. 2/21, 5pm (changed from original 9am so people in Wed. disc. sections can get help) Q2: find *any* solution. Don t try to find the best/optimal solution or all of them Q3: although you cannot use a loop in Q2, you may use a loop in Q3 (but function must also be recursive) Exam 1, Thursday evening, 2/22, 6:30-8:00pm, MacBride Auditorium 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

Discussion of HW4 Q2 ideas Suppose we want to make 87 with coins of 3, 5, 8 Check if you can make 84 with 3, 5, 8 If yes, answer for 87 is a value 3 coin plus coins that make 84 (if check above failed) check if you can make 82 with 3, 5, 8 If yes, answer for 87 is a value 5 coin plus coins that make 82 (if check above failed) check if you can make 79 with 3, 5, 8. If yes, answer for 87 is a value 8 coin plus coins that make 79 If all three checks fail, then 87 cannot be made with 3, 5, 8 Important: each check if is a simple recursive call!

Last time Started recursion (end of Ch 5) Today A few words on Exam 1 More recursion examples

Recursion (end of Ch 5) Very important and useful concept Not just for programming, but math and everyday life, nature, etc. Has undeserved reputation among some people: recursion is bad recursive programs are inefficient Yes, one can write very bad recursive programs but this is true of nonrecursive programs as well. And recursion can be super useful.

Recursion Recursive function: function that contains within its definition calls to itself Consider math s factorial. E.g. 3! = 3 * 2 * 1 You might be used to definition like: n! = n * (n-1) * * 2 * 1 But more precise mathematical definition of factorial function is: factorial(1) = 1 factorial(n) = n * factorial (n-1), for all n > 1 Programming-wise, can very directly translate recursive mathematical definitions into code: def factorial(n): if (n == 1): return 1 else: return n * factorial (n 1) DON T let the function call, factorial(n-1), scare you. It s just a function call. If you draw stack frames like we did in earlier lectures, it all works out fine. DO need to think carefully when writing/analyzing recursive programs though

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

Ch3: Stack frames def countdown(n): if n == 0: print( Blast off! ) else: print(n) countdown(n-1) >>> n = 100 >>> y = 2 >>> countdown(y) 2 countdown: n 0 countdown: n 1 countdown: n 2 1 Blast Off! _main_: n 100 y 2

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 flatten a list E.g. [[[[[3,[2,4]]], 0], [ a ]], 23] -> [3,2,4,0, a, 23] generate a number sequence Towers of Hanoi problem lec14.py, lec15.py ToHcomplete.py

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

Q3 of HW4 Definition of similar: Two items similar if types equal, and If they are lists, they are equal length and their corresponding items are similar Function outline def aresimilar(item1, item2): # check types # 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

Next Time Review for exam, and sample problems