Recurse function in python

Recurse function in python

Standard Problems on Recursion

  • Program for Tower of Hanoi Algorithm
  • Time Complexity Analysis | Tower Of Hanoi (Recursion)
  • Find the value of a number raised to its reverse
  • Recursively remove all adjacent duplicates
  • Print 1 to n without using loops
  • Print N to 1 without loop
  • Sort the Queue using Recursion
  • Reversing a queue using recursion
  • Mean of array using recursion
  • Binary to Gray code using recursion
  • Sum of natural numbers using recursion
  • Delete a linked list using recursion
  • Product of 2 Numbers using Recursion
  • Decimal to binary number using recursion
  • Sum of array elements using recursion
  • How to Sort a Stack using Recursion
  • Reverse a Doubly linked list using recursion
  • Programs for Printing Pyramid Patterns using Recursion
  • DFS traversal of a Tree
  • Length of longest palindromic sub-string : Recursion
  • Count Set-bits of number using Recursion
  • Print reverse of a string using recursion
  • Print Fibonacci Series in reverse order using Recursion
  • Java Program to Reverse a Sentence Using Recursion
  • Program for length of a string using recursion
  • Sum of digit of a number using recursion
  • Program to calculate value of nCr using Recursion
  • Find geometric sum of the series using recursion
  • Bottom View of a Binary Tree using Recursion
  • Convert a String to an Integer using Recursion
  • Tail recursion to calculate sum of array elements.

Practice Sets on Recursion

  • Write an Interview Experience
  • Share Your Campus Experience
  • Introduction to Recursion – Data Structure and Algorithm Tutorials
  • What is Recursion?
  • Difference between Recursion and Iteration
  • Types of Recursions
  • Finite and Infinite Recursion with examples
  • What is Tail Recursion
  • What is Implicit recursion?
  • Why is Tail Recursion optimization faster than normal Recursion?
  • Recursive Functions
  • Difference Between Recursion and Induction
Читайте также:  Php unserialize from file

Standard Problems on Recursion

  • Program for Tower of Hanoi Algorithm
  • Time Complexity Analysis | Tower Of Hanoi (Recursion)
  • Find the value of a number raised to its reverse
  • Recursively remove all adjacent duplicates
  • Print 1 to n without using loops
  • Print N to 1 without loop
  • Sort the Queue using Recursion
  • Reversing a queue using recursion
  • Mean of array using recursion
  • Binary to Gray code using recursion
  • Sum of natural numbers using recursion
  • Delete a linked list using recursion
  • Product of 2 Numbers using Recursion
  • Decimal to binary number using recursion
  • Sum of array elements using recursion
  • How to Sort a Stack using Recursion
  • Reverse a Doubly linked list using recursion
  • Programs for Printing Pyramid Patterns using Recursion
  • DFS traversal of a Tree
  • Length of longest palindromic sub-string : Recursion
  • Count Set-bits of number using Recursion
  • Print reverse of a string using recursion
  • Print Fibonacci Series in reverse order using Recursion
  • Java Program to Reverse a Sentence Using Recursion
  • Program for length of a string using recursion
  • Sum of digit of a number using recursion
  • Program to calculate value of nCr using Recursion
  • Find geometric sum of the series using recursion
  • Bottom View of a Binary Tree using Recursion
  • Convert a String to an Integer using Recursion
  • Tail recursion to calculate sum of array elements.

Practice Sets on Recursion

Источник

Python Recursion

Recursion is the process of defining something in terms of itself.

A physical world example would be to place two parallel mirrors facing each other. Any object in between them would be reflected recursively.

Python Recursive Function

In Python, we know that a function can call other functions. It is even possible for the function to call itself. These types of construct are termed as recursive functions.

Читайте также:  Nginx разные версии php

The following image shows the working of a recursive function called recurse .

Python Recursive Function

Following is an example of a recursive function to find the factorial of an integer.

Factorial of a number is the product of all the integers from 1 to that number. For example, the factorial of 6 (denoted as 6!) is 1*2*3*4*5*6 = 720 .

Example of a recursive function

def factorial(x): """This is a recursive function to find the factorial of an integer""" if x == 1: return 1 else: return (x * factorial(x-1)) num = 3 print("The factorial of", num, "is", factorial(num))

In the above example, factorial() is a recursive function as it calls itself.

When we call this function with a positive integer, it will recursively call itself by decreasing the number.

Each function multiplies the number with the factorial of the number below it until it is equal to one. This recursive call can be explained in the following steps.

factorial(3) # 1st call with 3 3 * factorial(2) # 2nd call with 2 3 * 2 * factorial(1) # 3rd call with 1 3 * 2 * 1 # return from 3rd call as number=1 3 * 2 # return from 2nd call 6 # return from 1st call

Let’s look at an image that shows a step-by-step process of what is going on:

Factorial by a recursive method

Our recursion ends when the number reduces to 1. This is called the base condition.

Every recursive function must have a base condition that stops the recursion or else the function calls itself infinitely.

The Python interpreter limits the depths of recursion to help avoid infinite recursions, resulting in stack overflows.

By default, the maximum depth of recursion is 1000 . If the limit is crossed, it results in RecursionError . Let’s look at one such condition.

def recursor(): recursor() recursor()
Traceback (most recent call last): File "", line 3, in File "", line 2, in a File "", line 2, in a File "", line 2, in a [Previous line repeated 996 more times] RecursionError: maximum recursion depth exceeded

Advantages of Recursion

  1. Recursive functions make the code look clean and elegant.
  2. A complex task can be broken down into simpler sub-problems using recursion.
  3. Sequence generation is easier with recursion than using some nested iteration.

Disadvantages of Recursion

  1. Sometimes the logic behind recursion is hard to follow through.
  2. Recursive calls are expensive (inefficient) as they take up a lot of memory and time.
  3. Recursive functions are hard to debug.

Table of Contents

Источник

Оцените статью