1 / 15

Day 2 – Lesson 9 Iteration: Recursion

Day 2 – Lesson 9 Iteration: Recursion. Python Mini-Course University of Oklahoma Department of Psychology. Lesson objectives. Define recursion and state why recursion is useful Use recursive functions Use checkpoints to debug programs. Recursion.

Download Presentation

Day 2 – Lesson 9 Iteration: Recursion

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Day 2 – Lesson 9Iteration: Recursion Python Mini-Course University of Oklahoma Department of Psychology Python Mini-Course: Day 3 - Lesson 9

  2. Lesson objectives • Define recursion and state why recursion is useful • Use recursive functions • Use checkpoints to debug programs Python Mini-Course: Day 3 - Lesson 9

  3. Recursion • Not only can functions call other functions (composition), they can also call themselves • This is called recursion • Recursion is a powerful method that is a key component to functional programming Python Mini-Course: Day 3 - Lesson 9

  4. Example: Blastoff def countdown(n): if n <= 0: print 'Blastoff!' else: print n countdown(n-1) countdown(10) Python Mini-Course: Day 3 - Lesson 9

  5. What's happening here? Base class Python Mini-Course: Day 3 - Lesson 9

  6. Base classes • All recursive functions must have a base class • What happens otherwise? • Try this: def recurse(): recurse() recurse() Python Mini-Course: Day 3 - Lesson 9

  7. Fibonacci sequence fibonacci(0) = 0 fibonacci(1) = 1 fibonacci(n) = fibonacci(n−1) + fibonacci(n−2) Python Mini-Course: Day 3 - Lesson 9

  8. Fibonacci sequence def fibonacci(n): if n == 0: return 0 elif n == 1: return 1 else: return fibonacci(n-1) \ + fibonacci(n-2) Python Mini-Course: Day 3 - Lesson 9

  9. Debugging with checkpoints • If a function is not working check: • Preconditions • Arguments being passed into the function • Postconditions • Operations in the function itself • The way the function is being used Python Mini-Course: Day 3 - Lesson 9

  10. Testing preconditions • Add a (temporary) print statement to the beginning of the function • Use type-checking functions Python Mini-Course: Day 3 - Lesson 9

  11. Fibonacci sequence def fibonacci(n): print n, type(n) … fibonacci(-2) fibonacci(0.4) Python Mini-Course: Day 3 - Lesson 9

  12. Fibonacci sequence def fibonacci(n): if (not type(n) == int) or n < 0: print "Fibonacci is only \ defined for non-negative \ integers" return … fibonacci(-2) Python Mini-Course: Day 3 - Lesson 9

  13. Testing postconditions • Use same technique of adding (temporary) print statements at critical points in the function • For large programs or functions, use the half-splitting technique • Check beginning, middle, end • Find the half with a problem • Recurse Python Mini-Course: Day 3 - Lesson 9

  14. Next session • More on loops and conditional execution • Including the while loop • Handling strings and text Python Mini-Course: Day 3 - Lesson 9

  15. Suggested exercises • Exercise 6.5 – The Ackermann function • Exercise 6.8 – Euclidean algorithm Python Mini-Course: Day 3 - Lesson 9

More Related