Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Recursion with Python [Rev]

221 views

Published on

Intro to Python

Published in: Technology
  • Be the first to comment

Recursion with Python [Rev]

  1. 1. RECURSION With Python Made by Bima Sudarsono Adinsa 1906399083 Dennis Al Baihaqi Walangadi 1906400141 Muhammad Faisal Adi Soesatyo 1906293184 Muhammad Ivan Radman 1906399114 With assistance of Mr. Fariz Darari, S.Kom, M.Sc., Ph.D.
  2. 2. Recursion What is recursion? Recursion is a computer programming technique involving the use of procedure, subroutine, function, or algorithm that calls itself either directly or indirectly https://xkcd.com/1516/
  3. 3. RecursionComponents 01 Recursive case Part where the code calls it self Base case Base case is the condition that stops recursion from continuing on forever Recursion consist of 02 ! Recursion without Base case will result Recursion Error When a recursion doesn’t have base case, it will run infinitely and reached maximum depth
  4. 4. Example How a particular problem is solved using recursion? The idea is to represent a problem in terms of one or more smaller problems, and add one or more base conditions that stop the recursion. For example, we can use recursion to solve factorials. Let’s solve 10! 10! means 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 But 9! also means 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 Thus, 10! Can be written as 10 x 9! That means it can be translated to: n! = n x (n-1)! So on python we can write ...
  5. 5. Example def factorial(num): # Base case 0! = 1 if num == 0: return 1 # Recursive case else: return num * factorial(num - 1) Example usage : Factorial in Python Because 0! = 1, and factorial doesn’t have negative value Recursion case, when the number is greater than 0, multiply the number by the number behind it
  6. 6. Example Example usage : Fibonacci in Python Fibonacci is a set of numbers that starts with a one or a zero, followed by a one, and proceeds based on the rule that each number (called a Fibonacci number) is equal to the sum of the preceding two numbers. If the Fibonacci sequence is denoted F(n), where n is the first term in the sequence, the following equation obtains for n = 0, where the first two terms are defined as 0 and 1 by convention: F (0) = 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 ... In some texts, it is customary to use n = 1. In that case, the first two terms are defined as 1 and 1 by default, and therefore: F (1) = 1, 1, 2, 3, 5, 8, 13, 21, 34 … So, in python we can write ...
  7. 7. Example Example usage : Fibonacci in Python def fib(n): # Base case n = 0 or n = 1 if n == 0 or n == 1: return n # Recursive case else: return fib(n - 1) + fib(n - 2) Because based on the theory, fibonacci sequence starts from 0 or 1 if we want to get the value of n, we have to sum the value of n-1 with value of n-2 This algorithm will return a fibonacci number on index n
  8. 8. RecvsIter Recursion vs Iteration ● Advantages ○ It can reduce time complexity ○ It adds clarity and reduce your time to write and debug the code ○ It is better in problem based on tree traversals / structures ● Disadvantages ○ It uses more memory for the stack ○ It can be slow, due to the overhead of maintaining stack
  9. 9. CREDITS: This presentation template was created by Slidesgo, including icons by Flaticon, and infographics & images by Freepik. References ● https://www.geeksforgeeks.org/recursion/ ● https://www.cs.utah.edu/~germain/PPS/Topics/recursion.html ● https://www.khanacademy.org/computing/computer- science/algorithms/recursive-algorithms/a/recursion ● Punch, William F. ,Richard Enbody, Practice of Computing Using Python, 3rd Edition (2017) ● https://medium.com/@williambdale/recursion-the-pros-and-cons-76d32d75973a ● https://stackoverflow.com/questions/5250733/what-are-the-advantages-and- disadvantages-of-recursion ● https://desain.cs.ui.ac.id/static/img/asset/logo.zip

×