Documente Academic
Documente Profesional
Documente Cultură
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.
Recursive expressions are abundant in mathematics, where they are often used to describe
properties of functions.
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)])
# Function call:
print(max_list_length(l))
Results: 9
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.