Sunteți pe pagina 1din 4

Recursive Programming in Python

An operation or unit is recursive, when straightforward self-similar process or unit, form part of
its entire constituent. Let's take an example of a large river to understand the concept of
recursion better. The large river can be understood as a large or mainstream fed by a series of
tributaries. Hence contains smaller instances of itself (the tributaries).

Another good example of recursion is the Droste effect in art where a picture can appear within
a picture repeatedly; up to such a point where the smallest picture (within a series of larger
pictures) cannot be further redrawn.

In the subsequent paragraphs, we give a step by step walkthrough of what the fundamental
recursive programming technique entails—backed up by original Python code samples to
illustrate the scrutinized points.

What is recursive programming in Python?


Recursive programming is a powerful problem-solving strategy that defines a concept by using
the definition of itself, leveraged for designing concise and straightforward (briefly and clearly
expressed) algorithms for solving computational problems.

Recursive expressions are abundant in mathematics, where they are often used to describe
properties of functions.

A demonstration of recursion programming thinking


procedure in diagrams
Let us present the mental model of deriving recursive cases using diagrams to bring the concept
closer home.
 The figure above demonstrates that a recursive programming procedure starts by
considering the general instance of a problem, determined by the respective input
parameters. (Top left box).
 To define a recursive case, we choose a decomposition that leads to a smaller, self-
similar instance of the problem (Bottom left box).
 A recursive call to the more straightforward inputs produces the results in the bottom
right box.
 Depending on the nature of the problem, we further derive recursive cases, up to such a
point when we solve the original problem (Top right box).
 Applying recursive methods to the given general instance of a problem would produce a
solution represented in the top right box.

Recursive programming in Python sample code


Using a divide and conquer approach, let us put our general diagram for thinking about
recursive cases in programming in a Python code that computes the maximum value in a list.

Assuming our list l, holds the following values;

l = [9, -7, 1, 3, 7, -2, 0, 5, 6]

def max_list_length(a):

if len(a) == 1:

return a[0]

else:

middle = len(a) // 2

m1 = max_list_length(a[0:middle])

m2 = max_list_length(a[middle:len(a)])

return max(m1, m2)


# Our list

l = [9, -7, 1, 3, 7, -2, 0, 5, 6]

# Function call:

print(max_list_length(l))

Results: 9

This is what happens:

 Our program first decomposes the original list into two self-similar instances of lists.
 The recursive call is then implemented on each of the lists to return the maximum value
in either of the two lists.
 The recursive then returns the maximum value from either of the two maximum values
derived from the two lists.

Advantages of using recursion in programming


Recursive algorithms are much simpler to design and comprehend compared to iterative ones.
This is because they closely resemble a logical approach we would take in solving a problem.
Furthermore, recursive algorithms use a program's stack implicitly to store information. Thus
making the operations carried out on them transparent to the programmer.

Disadvantages of using recursion in programming


Recursive algorithms are not as efficient as their iterative counterparts.

Additionally, recursive programs use more memory compared to iterative programs.

S-ar putea să vă placă și