Docsity

CS 134: Recursion, Slides of Logistics

Partial preview of the text

Download CS 134: Recursion and more Slides Logistics in PDF only on Docsity!

CS 134:

Recursion

Announcements & Logistics

  • (^) Lab 6 is today/tomorrow, due Wed/Thurs 10 pm
    • (^) Analyzes supreme court data
    • (^) Uses dictionaries, tuples, sorting with lambda, plotting, CSV files
    • (^) Review relevant lecture material before lab!
  • (^) HW 6 will be posted on Wednesday
  • (^) Labs 4 and 5 will be returned today
  • (^) Midterm will be returned at the end of class
    • (^) Avg: 82%
    • (^) Please review your exam and come see us with questions!
    • (^) Solutions available on Glow

Do You Have Any Questions?

Where are We Going?

  • (^) First half of CS134: learning all the fundamentals of programming
    • (^) Functions, conditionals, loops, data types
    • (^) Built-in data structures and methods, sorting, plotting
  • (^) Looking ahead to the second half: shift in the course to more algorithmic and conceptual topics, more "computational thinking"
    • (^) Recursion (~1 week)
    • (^) Defining our own data types using classes and objects (~2 weeks)
      • (^) Object oriented programming topics
      • (^) Building our own data types: linked lists
    • (^) How does sorting work / what happens under the hood when Python is sorting?
    • (^) Understanding the basics behind efficient vs inefficient code

Today’s Plan: Intro To Recursion

  • (^) What is recursion?
  • (^) Translating recursive ideas into recursive programs
  • (^) Examining the relation between recursive and iterative programs
    • (^) That is, how do recursive ideas relate to the iterative ideas (for loops, while loops) we’ve covered so far

Recursion In Art and Pop Culture

  • (^) Computer scientists were of course writing nerdy poems about recursion long before it was cool. Great fleas have little fleas upon their backs to bite 'em, And little fleas have lesser fleas, and so ad infinitum. And the great fleas themselves, in turn, have greater fleas to go on; While these again have greater still, and greater still, and so on. — Siphonaptera, A Budget Of Paradoxes by Augustus De Morgan (1874)

Why Learn About Recursion?

  • (^) Recursion is an important problem solving paradigm that can not only

lead to elegant code, it can also be used to do really cool things.

  • (^) By the end of the week, you'll be able to use recursion to draw these

beautiful pictures

Exercise: Forming Base Case & Recursive Rules

  • (^) How would you define the concept of exponentiation an^ as a base case
and a recursive rule (assuming n >= 0 )
  • (^) A recursive definition:
    • (^) Base case: a^0 = 1
    • (^) Recursive rule: an^ = a * an-

Exercise: Forming Base Case & Recursive Rules

  • (^) Similarly, how would you define the concept of factorial n! as a base case
and a recursive rule (assuming n >= 0 )
  • (^) A recursive definition:
    • (^) Base case: 0! = 1
    • (^) Recursive rule: n! = n * (n-1)!

Translating Recursive Ideas To Programs

  • (^) The beauty of recursion is that once you’ve written down your recursive idea, the programming part comes relatively easy
  • (^) Ideally, you spend more time with pen and paper and front-load all your thinking into coming up with an appropriate base case and recursive rule
  • (^) Once you have these two ingredients, the implementation of recursive programs is fairly formulaic

Translating Recursive Ideas To Programs

  • (^) Recursive definition for an:
    • (^) Base case: a^0 = 1
    • (^) Recursive rule: an^ = a * an-

Recursive Functions

  • (^) We have seen many examples of functions calling other functions
  • (^) A recursive function is a function that calls itself
  • (^) Similar to recursive definitions, all recursive functions consist of one or more base cases and a set of recursive rules that successively simplify the problem until we approach one of the base cases
  • (^) It’s important that the recursive rule eventually takes you towards one of the base cases, else we end up with the recursion equivalent of an infinite loop
  • (^) We will compare recursive implementations to iterative implementations in the next lecture, but for now let’s see what infinite recursion looks like and take a deeper look into how recursion works

Infinite Recursion

  • (^) Recursive definition for an:
    • (^) Base case: a^0 = 1
    • (^) Recursive rule: an^ = a * an-
  • (^) This gives us the message RecursionError: maximum depth exceeded in comparison — notice we are no longer simplifying the problem in our recursive rule.
  • (^) What does this error mean?
  • (^) So far, we’ve simply believed in the magic of recursion (sometimes folks even explicitly make reference to the existence of a recursion fairy) but let’s take a closer look at what goes on in recursive function calls.
  • (^) Recursive definition of countdown:
    • (^) Base case: n = 0 , do nothing
    • (^) Recursive rule: print(n), call countDown(n-1)

Understanding Recursive Functions

  • (^) It is possible to simplify our function by omitting the base case
  • (^) The following two versions are equivalent
  • (^) However, in recursion, we prefer that you write the base case explicitly for pedagogical reasons (for now) - (^) Bad things happen if you forget the base case.... (we'll see)

Side Note: Implicit Base Case

def countDown(n): '''Version 2''' if n > 0 : print(n) countDown(n-1) def countDown(n): '''Version 1''' if n < 1: pass # do nothing else: print(n) countDown(n-1)