Recursion is a technique by which a function makes one or more calls to itself during execution, or by which a data structure relies upon smaller instances of the very same type of structure in its representation.
defdraw_line(tick_length,tick_label=''):# Draw one line with given tick length. line ='-'* tich_lengthif tick_label: line+=' '+tick_labelprint(line)defdraw_interval(center_length):# Draw tick interval based upon a central tick length.if center_length >0:# stop when length drops to 0draw_interval(center_length -1)# recursively draw top ticksdraw_line(center_length)# draw center tickdraw_interval(center_length-1)# recursively draw bottom ticksdefdraw_ruler(num_inches,major_length):# Draw English rulerdraw_line(major_length, '0')# draw inch 0 linefor j inrange(1, 1+num_inches):draw_interval(major_length-1)# draw interior ticks for inchdraw_line(major_length, str(j))# draw inch j line and label
Binary Search
Binary Search is the method that used to efficiently locate a target value within a sorted sequence of n elements.(If it is not sorted, we use a sequential search algorithm.)
If the target equals data[mid], then we have found the item we are looking for, and the search terminates successfully.
If target < data[mid], then we recur on the first half of the sequence, that is, on the interval of indices from low to mid-1.
If target > data[mid], then we recur on the second half of the sequence, that is, on the interval of indices from mid+1 to high.
defbinary_search(data,target,low,high):# Return True if target is found in indicated portion of a Python list.# The search only considers the portions from data[low] to data[high] inclusive.if low>high:returnFalse#interval is empty; no matchelse: mid=(low+high)//2if target==data[mid]:# found a matchreturnTrueelif target < data[mid]:# recur on the portion left on the middlereturnbinary_search(data, target, low, mid-1)else:# recur on the portion right of the middlereturnbinary_search(data, target,mid+1,high)
Recursion Run Amok
Fibonacci Numbers
F0β=0F1β=1Fnβ=Fnβ2β+Fnβ1βforn>1
defbad_fibonacci(n):# Return the nth Fibonacci numberif n<=1:return nelse:returnbad_fibonacci(n-2)+bad_fibonacci(n-1)
defgood_fibonacci(n)# Return pair of Fibonacci numbers, F(n) and F(n-1)if n<=1:return(n,0)else: (a,b)=good_fibonacci(n-1)return(a+b,a)
Linear recursion
0 1 1 2
We don't have to use a recursive function twice. Instead of using twice, we just let the return value have pair structure. By doing this, the time complexity is reduced from exponential form to linear form.
Exercises
Linear recursion can be a useful tool for processing a data sequence, such as a Python list.
Q1. Let's compute the sum of a sequence S, of n integers.
deflinear_sum(S,n):# Return the sum of the first n numbers of sequence Sif n==0:return0else:returnlinear_sum(S,n-1)+S[n-1]
S=[4,3,6,2,8], linear_sum(S,5)=23
Q2. Write a short recursive Python function that finds the minimum and maximum values in a sequence without using any loops.
Q3. Give a recursive algorithm to compute the product of two positive integers, m and n, using only addition and subtraction.