Upcoming SlideShare
×

# Introduction to Recursion (Python)

5,022 views

Published on

A presentation about the ideas of recursion and recursive functions.

This is my lecture presentation during A. Paruj Ratanaworabhan’s basic preparatory programming course for freshmen: Introduction to Programming: A Tutorial for New Comers Using Python

Published in: Technology
8 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
5,022
On SlideShare
0
From Embeds
0
Number of Embeds
22
Actions
Shares
0
136
0
Likes
8
Embeds 0
No embeds

No notes for slide

### Introduction to Recursion (Python)

1. 1. RecursionExtracted from my lecture during A. Paruj Ratanaworabhan’s basic preparatory programming course for freshmen: Introduction to Programming: A Tutorial for New Comers Using Python By Thai Pangsakulyanont Software and Knowledge Engineering Undergraduate Student Kasetsart University
2. 2. http://greatingpoint.co.th/? showpage=product&ﬁle=product_detail&prd_id=1328776222
3. 3. Read this sentence and do what it says twice. http://programmers.stackexchange.com/a/93858
4. 4. n! = ( 1 if n = 0 (n 1)! ⇥ n if n > 0
5. 5. Factorial Example n! = ( 1 if n = 0 (n 1)! ⇥ n if n > 0 3!
6. 6. Factorial Example 3! n! = ( 1 if n = 0 (n 1)! ⇥ n if n > 0 = (3 - 1)! × 3
7. 7. Factorial Example 3! n! = ( 1 if n = 0 (n 1)! ⇥ n if n > 0 = (3 - 1)! × 3 = 2! × 3
8. 8. Factorial Example 3! n! = ( 1 if n = 0 (n 1)! ⇥ n if n > 0 = (3 - 1)! × 3 = 2! × 3 We turn this problem into a smaller problem of same kind. This is called “decomposition.”
9. 9. Factorial Example 3! n! = ( 1 if n = 0 (n 1)! ⇥ n if n > 0 = (3 - 1)! × 3 = 2! × 3 2! = (2 - 1)! × 2
10. 10. Factorial Example 3! n! = ( 1 if n = 0 (n 1)! ⇥ n if n > 0 = (3 - 1)! × 3 = 2! × 3 2! = (2 - 1)! × 2 = 1! × 2
11. 11. Factorial Example 3! n! = ( 1 if n = 0 (n 1)! ⇥ n if n > 0 = (3 - 1)! × 3 = 2! × 3 2! = (2 - 1)! × 2 = 1! × 2 1! = (1 - 1)! × 1
12. 12. Factorial Example 3! n! = ( 1 if n = 0 (n 1)! ⇥ n if n > 0 = (3 - 1)! × 3 = 2! × 3 2! = (2 - 1)! × 2 = 1! × 2 1! = (1 - 1)! × 1 = 0! × 1
13. 13. Factorial Example 3! n! = ( 1 if n = 0 (n 1)! ⇥ n if n > 0 = (3 - 1)! × 3 = 2! × 3 2! = (2 - 1)! × 2 = 1! × 2 1! = (1 - 1)! × 1 = 0! × 1 0! = 1
14. 14. Factorial Example 3! n! = ( 1 if n = 0 (n 1)! ⇥ n if n > 0 = (3 - 1)! × 3 = 2! × 3 2! = (2 - 1)! × 2 = 1! × 2 1! = (1 - 1)! × 1 = 0! × 1 0! = 1 This is called the “base case” where the function does not call itself anymore.
15. 15. Factorial Example 3! n! = ( 1 if n = 0 (n 1)! ⇥ n if n > 0 = (3 - 1)! × 3 = 2! × 3 2! = (2 - 1)! × 2 = 1! × 2 1! = (1 - 1)! × 1 = 0! × 1 0! = 1
16. 16. Factorial Example 3! n! = ( 1 if n = 0 (n 1)! ⇥ n if n > 0 = (3 - 1)! × 3 = 2! × 3 2! = (2 - 1)! × 2 = 1! × 2 1! = (1 - 1)! × 1 = 0! × 1 = 1 × 1 = 1
17. 17. Factorial Example 3! n! = ( 1 if n = 0 (n 1)! ⇥ n if n > 0 = (3 - 1)! × 3 = 2! × 3 2! = (2 - 1)! × 2 = 1! × 2 1! = (1 - 1)! × 1 = 0! × 1 = 1 × 1 = 1 We use the result of smaller problem to find the result of larger problem. This is called “composition”.
18. 18. Factorial Example 3! n! = ( 1 if n = 0 (n 1)! ⇥ n if n > 0 = (3 - 1)! × 3 = 2! × 3 2! = (2 - 1)! × 2 = 1! × 2 = 1 × 2 = 2
19. 19. Factorial Example 3! n! = ( 1 if n = 0 (n 1)! ⇥ n if n > 0 = (3 - 1)! × 3 = 2! × 3 = 2 × 3 = 6.
20. 20. def fac(n): if n == 0: return 1 else: return fac(n - 1) * n print fac(12) Factorial Example
21. 21. Recursive Function • A function that calls itself!
22. 22. Count Down countdown(5) print "happy recursion day"
23. 23. Countdown Algorithm • If I want to countdown from 0, then it’s over! Don’t do anything. • If I want to countdown from N (> 0), then count N, and countdown from N-1.
24. 24. Countdown Example def countdown(n): if n == 0: return print n countdown(n - 1) countdown(5) print "happy recursion day"
25. 25. Example: Euclidean Algorithm • Fast way to ﬁnd a GCD of two numbers.
26. 26. Example: Euclidean Algorithm If you have 2 numbers, then you subtract the smaller number from the larger number, the GCD of these two number stays the same.
27. 27. Example: Euclidean Algorithm 68 119 GCD(68, 119) is 17
28. 28. Example: Euclidean Algorithm 68 119 GCD(68, 51) is still 17 68 51
29. 29. Example: Euclidean Algorithm If you have 2 numbers, then you subtract the smaller number from the larger number, the GCD of these two number stays the same. Keep doing that until the two numbers equal each other, then that number is the GCD.
30. 30. Example: Euclidean Algorithm GCD(68, 119)
31. 31. Example: Euclidean Algorithm GCD(68, 119) = GCD(68, 51)
32. 32. Example: Euclidean Algorithm GCD(68, 119) = GCD(68, 51) = GCD(17, 51)
33. 33. Example: Euclidean Algorithm GCD(68, 119) = GCD(68, 51) = GCD(17, 51) = GCD(17, 34)
34. 34. Example: Euclidean Algorithm GCD(68, 119) = GCD(68, 51) = GCD(17, 51) = GCD(17, 34) = GCD(17, 17)
35. 35. Example: Euclidean Algorithm GCD(68, 119) = GCD(68, 51) = GCD(17, 51) = GCD(17, 34) = GCD(17, 17) = 17
36. 36. Example: Euclidean Algorithm def gcd(a, b): if a < b: return gcd(a, b - a) elif b < a: return gcd(a - b, b) else: return a print gcd(68, 119)
37. 37. Who Says It’s Fast? print gcd(21991144, 21) # = gcd(21991123, 21) # = gcd(21991102, 21) # = gcd(21991081, 21) # = gcd(21991060, 21) # = gcd(21991039, 21) # = gcd(21991018, 21) # = gcd(21990997, 21) # = gcd(21990976, 21)
38. 38. It’s Actually Really Fast Consider: (56, 10) → (46, 10) → (36, 10) → (26, 10) → (16, 10) → (6, 10) 56 % 10 = 6 (the rest of this is for your exercise)
39. 39. Why Recursion? • Sometimes it’s easier to write and read code in recursive form.
40. 40. Why Recursion? • Sometimes it’s easier to write and read code in recursive form. • You can always convert an iterative algorithm to recursive and vice versa.
41. 41. Why Recursion? • Sometimes it’s easier to write and read code in recursive form. • You can always convert an iterative algorithm to recursive and vice versa. (does not mean it’s easy)
42. 42. Iterative Euclidean Algorithm def gcd(a, b): while a != b: if a < b: b = b - a elif b < a: a = a - b return a print gcd(68, 119)
43. 43. Remember? • Decomposition • Base Case • Composition
44. 44. String Reversal • Given a string, I want a reversed version of that string. print reverse("reenigne") # => “engineer”
45. 45. String Reversal • Let’s think recursively! reenigne
46. 46. Decomposition r eenigne
47. 47. Recursive Case r enginee eenigne somehow
48. 48. Composition renginee
49. 49. Composition engineer
50. 50. ? eenigne enginee
51. 51. e eenigne enignee engine enginee
52. 52. Base Case • The reverse of an empty string is an empty string.
53. 53. Recursive Algorithm reverse(“Hello”)
54. 54. Recursive Algorithm reverse(“Hello”) = reverse(“ello”) + “H”
55. 55. Recursive Algorithm reverse(“Hello”) = reverse(“ello”) + “H” reverse(“ello”) = reverse(“llo”) + “e”
56. 56. Recursive Algorithm reverse(“Hello”) = reverse(“ello”) + “H” reverse(“ello”) = reverse(“llo”) + “e” reverse(“llo”) = reverse(“lo”) + “l”
57. 57. Recursive Algorithm reverse(“Hello”) = reverse(“ello”) + “H” reverse(“ello”) = reverse(“llo”) + “e” reverse(“llo”) = reverse(“lo”) + “l” reverse(“lo”) = reverse(“o”) + “l”
58. 58. Recursive Algorithm reverse(“Hello”) = reverse(“ello”) + “H” reverse(“ello”) = reverse(“llo”) + “e” reverse(“llo”) = reverse(“lo”) + “l” reverse(“lo”) = reverse(“o”) + “l” reverse(“o”) = reverse(“”) + “o”
59. 59. Recursive Algorithm reverse(“Hello”) = reverse(“ello”) + “H” reverse(“ello”) = reverse(“llo”) + “e” reverse(“llo”) = reverse(“lo”) + “l” reverse(“lo”) = reverse(“o”) + “l” reverse(“o”) = reverse(“”) + “o” reverse(“”) = “”
60. 60. Recursive Algorithm reverse(“Hello”) = reverse(“ello”) + “H” reverse(“ello”) = reverse(“llo”) + “e” reverse(“llo”) = reverse(“lo”) + “l” reverse(“lo”) = reverse(“o”) + “l” reverse(“o”) = reverse(“”) + “o” = “” + “o” = “o”
61. 61. Recursive Algorithm reverse(“Hello”) = reverse(“ello”) + “H” reverse(“ello”) = reverse(“llo”) + “e” reverse(“llo”) = reverse(“lo”) + “l” reverse(“lo”) = reverse(“o”) + “l” = “o” + “l” = “ol”
62. 62. Recursive Algorithm reverse(“Hello”) = reverse(“ello”) + “H” reverse(“ello”) = reverse(“llo”) + “e” reverse(“llo”) = reverse(“lo”) + “l” = “ol” + “l” = “oll”
63. 63. Recursive Algorithm reverse(“Hello”) = reverse(“ello”) + “H” reverse(“ello”) = reverse(“llo”) + “e” = “oll” + “e” = “olle”
64. 64. Recursive Algorithm reverse(“Hello”) = reverse(“ello”) + “H” = “olle” + “H” = “olleH”
65. 65. Reverse Example def reverse(str): if str == "": return str # or return “” else: return reverse(str[1:]) + str[0] print reverse("reenigne")
66. 66. print sum_digits(314159265) # => 36 Sum of Digits
67. 67. print sum_digits(314159265) # => 36 Sum of Digits • NO LOOPS! • NO STRINGS!
68. 68. Recursive Algorithm • Sum of digits of 0 is 0. • Sum of digits of N > 0: Find last digit + sum of digits except last.
69. 69. Recursive Algorithm • Sum of digits of 0 is 0. • Sum of digits of N > 0: Find last digit + sum of digits except last. N % 10 N // 10
70. 70. Sum of Digits def sum_digits(n): if n == 0: return 0 else: return (n % 10) + sum_digits(n // 10) print sum_digits(314159265)