1599158640

- Recursion made simple

Anybody reading data structures and algorithm would have come across a topic called “Recursion”. Now here is the best way to understand recursion. Read this ’n’ times until your understanding becomes** “True”**. Haha!! now that’s how simple the recursion is.

Well recursion can be implemented in any language and it has the same outcome. Here i’ll be demonstrating few topics and key concepts on recursion in python🐍. Yes, you read it right one love for python❤.

A function calling itself until a condition is set true or false is called recursive function. The below image shows to a simple function called “recursive_fun” calling itself ’n’ times.

a simple example of recursion

The output of a recursion can be understood by a concept called as tracing which is done to trace the output by a recursive function.

a tracing tree of recursive_fun(forward phase)

the above tracing tree explains a lot about how the repetitive function call takes place to obtain the desired output. Here the recursive_fun is called 3+1 times where for all the 3 times it prints ’n’ but for the last call it fails to satisfy the condition and returns 0. Hence, in any recursion the function call is made n+1 times. Also, on carefully observing the tracing tree we see that the recursive_fun executes print statement before calling itself, this is known as forward phase. A backward phase of recursion is when a function calls itself before executing any statements, i.e all the other statements are executed until there is no further function calls.

#recursive #algorithms #python #recursion #data-structures

1599158640

- Recursion made simple

Anybody reading data structures and algorithm would have come across a topic called “Recursion”. Now here is the best way to understand recursion. Read this ’n’ times until your understanding becomes** “True”**. Haha!! now that’s how simple the recursion is.

Well recursion can be implemented in any language and it has the same outcome. Here i’ll be demonstrating few topics and key concepts on recursion in python🐍. Yes, you read it right one love for python❤.

A function calling itself until a condition is set true or false is called recursive function. The below image shows to a simple function called “recursive_fun” calling itself ’n’ times.

a simple example of recursion

The output of a recursion can be understood by a concept called as tracing which is done to trace the output by a recursive function.

a tracing tree of recursive_fun(forward phase)

the above tracing tree explains a lot about how the repetitive function call takes place to obtain the desired output. Here the recursive_fun is called 3+1 times where for all the 3 times it prints ’n’ but for the last call it fails to satisfy the condition and returns 0. Hence, in any recursion the function call is made n+1 times. Also, on carefully observing the tracing tree we see that the recursive_fun executes print statement before calling itself, this is known as forward phase. A backward phase of recursion is when a function calls itself before executing any statements, i.e all the other statements are executed until there is no further function calls.

#recursive #algorithms #python #recursion #data-structures

1603537200

Recursion is the one idea I constantly use while I solve coding problems. Most of the time I don’t start by thinking “RECURSION WILL SOLVE THIS!”. However recursion just ends up being the logical way to reach an answer. In my professional opinion recursion is the purest form of coding; write a function that will call itself until you get what you want! To implement recursion we will create a helper algorithm. 1) Identify what the smallest input is. 2) Continually break down a larger input into smaller inputs and pass those smaller inputs back into itself until you get the desired answer. 3) Define a “base case” that will stop the Recursion should the answer not be found.

Let’s look at the idea of Recursion first. We are writing code that will execute itself until we get a desired answer or reach a base case. Essentially we are creating a loop. I will illustrate this with pseudo code:

```
for (let recursion =()=>{ …answer? answer = true : false} ; answer === false; recursion())
```

Much like a traditional for loop the above pseudo code will continue while the second condition is true; the recursion will continue until answer === true. At this point the second statement of the for loop is false terminating the loop. Above if answer === false recursion will call itself again. This is the general idea of recursion. This is why creating a base case is essential to prevent an infinite loop. The “answer” we are looking for might not be present causing recursion to run until the sun burns out.

#algorithms #javascript #recursion #tutorial-for-beginners #iteration #recursion-explained #what-is-recursion #programming

1622168113

Acomputer program consists of line-by-line instructions. The computer performs those instructions line-by-line. However, some instructions may be repetitive with a common pattern. Recursion or iteration helps one to write a few lines of codes to perform such repetitive tasks. Suppose a Python list with five-string elements. We wish to print the elements one in a line. This operation needs five lines of codes.

```
flowers = ['lily', 'tulip', 'rose', 'lavender', 'dandelion']
print(flowers[0])
print(flowers[1])
print(flowers[2])
print(flowers[3])
print(flowers[4])
```

Output:

It can be observed that the five lines of codes follow the same pattern. The only difference in each line is the index of the list elements. What if this list contains 100 or 1000 elements? Coding will become a tedious task. These kinds of problems are resolved through either iteration or recursion. Here, the iterative form of the above codes is as follows.

```
for flower in flowers:
print(flower)
```

Output:

These two lines are sufficient to achieve the task even if the list contains a million elements!

#developers corner #backtracking #dynamic programming #factorial #iteration #iterative #python #python programming #quicksort #recursion #recursive #sorting

1625631000

(Previously 002) In the words of Woody: “That’s not Recursion! It’s looping with style!”

Follow Beyond Code:(Learn to Code in 15 Minutes a Day)

Facebook: https://www.facebook.com/beyondcodebootcamp

YouTube: https://www.youtube.com/channel/UC2KJHARTj6KRpKzLU1sVxBA

Twitter: https://twitter.com/_beyondcode

Daily Coding Live Streams:

Facebook: https://facebook.com/coolaj86

YouTube: https://youtube.com/coolaj86

Twitch: https://twitch.tv/coolaj86

Follow Health, Wealth, Commitment

(Daily Lifestyle Chat)

Facebook: https://www.facebook.com/groups/5406824179391158

YouTube: https://www.youtube.com/channel/UCbw2SbqD0OofAEVF_T61wCQ

#recursion #what's recursion

1596198600

Oh my.

Today I spent an hour trying to wrap my head around an elegant recursive function. Recursion is not very easy to understand. I was relieved when I finally understood it. Hopefully, I can shed some light on them and make it easier to understand what they are and what they do. This article is about:

- The recursive factorial function
- How recursive functions operate underneath the code
- Are they better than iterative code that performs the same function?

You may have heard “A recursive function is one that calls itself.”

Take the following code and feel free to code-along:

```
def factorial_recursive(n):
if n == 1:
return n
else:
return n*factorial_recursive(n-1)
```

When you call the recursive function above, giving it an integer, you are returned with the factorial of a number (n!)

A Factorial of a number is the number multiplied by every number below it all the way to the number 1.

For example the factorial of 7:

7!= 7654321 = 5040

We can print the factorial of a number using the function:

```
num = 3
print(f"The factorial of {num} is {factorial_recursive(num)}")
```

That should print “The factorial of 3 is 6”

#recursion #recursive-function #stack #programming #python