recursion. what is recursion? “a description of something that refers to itself is called a...

14
Recursion

Upload: jesse-ellis

Post on 12-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Recursion. What is recursion? “A description of something that refers to itself is called a recursive definition.” In mathematics, certain recursive definitions

Recursion

Page 2: Recursion. What is recursion? “A description of something that refers to itself is called a recursive definition.” In mathematics, certain recursive definitions

What is recursion?

• “A description of something that refers to itself is called a recursive definition.”

• In mathematics, certain recursive definitions are used all the time.

• The classic recursive example in mathematics is the definition of factorial.

Page 3: Recursion. What is recursion? “A description of something that refers to itself is called a recursive definition.” In mathematics, certain recursive definitions

The Factorial of a Number

• we define the factorial of a value like this:• n! = n(n - 1)(n – 2)...(1)• For example, we can compute• 5! = 5(4)(3)(2)(1)• Looking at the calculation of 5!, you will notice

something interesting. If we remove the 5 from the front, what remains is a calculation of 4!. In general,

• n! = n(n - 1)!

Page 4: Recursion. What is recursion? “A description of something that refers to itself is called a recursive definition.” In mathematics, certain recursive definitions

Continued…

• 4! = 4(3!) = 4(3)(2!) = 4(3)(2)(1!) =4(3)(2)(1)= 24

• You can see that the recursive definition is not circular because each application causes us to request the factorial of a smaller number. Eventually we get down to 0, which doesn't require another application of the definition. This is called a base case for the recursion.

Page 5: Recursion. What is recursion? “A description of something that refers to itself is called a recursive definition.” In mathematics, certain recursive definitions

Characteristics

• All good recursive definitions have these key characteristics:

• 1. There are one or more base cases for which no recursion is required.

• 2. All chains of recursion eventually end up at one of the base cases.

Page 6: Recursion. What is recursion? “A description of something that refers to itself is called a recursive definition.” In mathematics, certain recursive definitions

Continued…

• The simplest way to guarantee that these two conditions are met is to make sure that each recursion always occurs on a smaller version of the original problem. A very small version of the problem that can be solved without recursion then becomes the base case. This is exactly how the factorial definition works.

Page 7: Recursion. What is recursion? “A description of something that refers to itself is called a recursive definition.” In mathematics, certain recursive definitions

The Factorial Function

• If we write factorial as a function, the recursive definition translates directly into code.

def fact(n):if n == 1:

return 1 BASE CASE! Stops calling itselfelse:return n * fact(n-1)

Page 8: Recursion. What is recursion? “A description of something that refers to itself is called a recursive definition.” In mathematics, certain recursive definitions

Reverse String Recursion

Using recursion reverse a string. A simple example would be to change “cat” into “tac”.

Remember, recursion is more easily carried out when thinking of a way to reduce the info passed back into the function.

That way, eventually we reach the base case.How would this work in this problem?

Page 9: Recursion. What is recursion? “A description of something that refers to itself is called a recursive definition.” In mathematics, certain recursive definitions

Algorithm!

Pseudocode:1. Get the last letter2. Return that letter + the word minus that last

letter.3. When there is only 1 letter left return it!4. That’s it!!!

Page 10: Recursion. What is recursion? “A description of something that refers to itself is called a recursive definition.” In mathematics, certain recursive definitions

The Function

function reverse(word:String):String{if(word.length==1)

return word;BASE CASE!else

return word.charAt(word.length-1)+reverse(word. substr(0,word.length-1));

}

Page 11: Recursion. What is recursion? “A description of something that refers to itself is called a recursive definition.” In mathematics, certain recursive definitions

Exercises

• 1. The Fibonacci sequence is the sequence of numbers 1; 1; 2; 3; 5; 8; … . It starts with two 1s and successive numbers are the sum of the previous two. Create a recursive function to calculate the sum of given set of terms in a fibonacci series. Ex. Fibo(4)=7

• 2. Write and test a recursive function max to find the largest number in a list(array of numbers).

• 3. Write a recursive function that detects whether a word is a palindrome.

Page 12: Recursion. What is recursion? “A description of something that refers to itself is called a recursive definition.” In mathematics, certain recursive definitions

BONUS Computer scientists and mathematicians often use numbering systems

other than base 10. Write a program that allows a user to enter a number and a base and then prints out the digits of the number in the new base. Use a recursive function baseConversion(num,base) to print the digits.Hint: Consider base 10. To get the rightmost digit of a base 10 number,simply look at the remainder after dividing by 10. For example, 153%10 is 3. To get the remaining digits, you repeat the process on 15, which is just153=10. This same process works for any base. The only problem is thatwe get the digits in reverse order (right to left). Write a recursive function that first prints the digits of num=base and thenprints the last digit, namely num%base. You should put a space betweensuccessive digits, since bases greater than 10 will print out with multicharacter digits. For example, baseConversion(245, 16) should print 155.

Page 13: Recursion. What is recursion? “A description of something that refers to itself is called a recursive definition.” In mathematics, certain recursive definitions

BONUS

• Write a recursive function to print out the digits of a number in English.

• For example, if the number is 153, the output should be .One Five Three..

• See the hint from the previous problem for help on how this might be done.

Page 14: Recursion. What is recursion? “A description of something that refers to itself is called a recursive definition.” In mathematics, certain recursive definitions

Reference

https://fbeedle.com/python/99-6ch13.pdf