Lecture 5 : Basic Recursion¶
Print some text n times using recursion¶
Approach1 (with extra parameter)¶
In [3]:
def printText(n,count =0):
if count == n:
return
print("some-text,")
printText(n,count +1)
printText(5)
some-text, some-text, some-text, some-text, some-text,
Approach2 without extra parameter¶
In [1]:
def printText(n):
if n ==0:
return
print("some-text,")
printText(n-1)
printText(5)
some-text, some-text, some-text, some-text, some-text,
Print natural numbers from 1 to n using recursion¶
Using forward recursion¶
This requires a count parameter to keep track
In [8]:
def printNatural(n, count =1):
if count > n:
return
print(count)
printNatural(n,count +1)
printNatural(10)
1 2 3 4 5 6 7 8 9 10
Using backward recursion or back tracking¶
this does not require additional parameter
In [9]:
def printNatural(n):
if n == 0:
return
printNatural(n-1)
print(n)
printNatural(10)
1 2 3 4 5 6 7 8 9 10
Print N to 1 using recursion¶
In [12]:
def printReverseNatural(n):
if n ==0:
return
print(n)
printReverseNatural(n-1)
printReverseNatural(5)
5 4 3 2 1
Sum of First N natural numbers using recursion¶
Problem statement: Given a number ‘N’, find out the sum of the first N natural numbers.
Examples:
Example 1:
Input: N=5
Output: 15
Explanation: 1+2+3+4+5=15
Example 2:
Input: N=6
Output: 21
Explanation: 1+2+3+4+5+6=21
approach 1 (recursion with extra parameter)¶
In [14]:
def sumNatural(n,sum =0):
if n ==0:
return sum
return sumNatural(n-1,sum + n)
print(sumNatural(5))
print(sumNatural(6))
15 21
approach 2 (recursion without extra parameter)¶
sum(n) = n + sum(n-1) And sum(1) =1
In [5]:
def sumNatural(n):
if n ==1:
return 1
return n + sumNatural(n-1)
print(sumNatural(5))
print(sumNatural(6))
15 21
Factorial of num¶
Example 1:
Input: X = 5
Output: 120
Explanation: 5! = 5*4*3*2*1
Example 2:
Input: X = 3
Output: 6
Explanation: 3!=3*2*1
fact(n) = n * fact(n-1) And fact(1) = 1
In [17]:
def fact(num):
if num ==1:
return 1
return num * fact(num-1)
print(fact(5))
print(fact(3))
120 6
Reverse an array¶
Problem Statement: You are given an array. The task is to reverse the array and print it.
Examples:
Example 1:
Input: N = 5, arr[] = {5,4,3,2,1}
Output: {1,2,3,4,5}
Explanation: Since the order of elements gets reversed the first element will occupy the fifth position, the second element occupies the fourth position and so on.
Example 2:
Input: N=6 arr[] = {10,20,30,40}
Output: {40,30,20,10}
Explanation: Since the order of elements gets reversed the first element will occupy the fifth position, the second element occupies the fourth position and so on.
Approach (using iterative logic in recursion)¶
In [20]:
def reverseArray(array):
n = len(array)
def helper(left =0, right = n-1):
if left >= right:
return
(array[left],array[right]) = (array[right],array[left])
helper(left +1 , right -1)
helper()
return array
print(reverseArray([5,4,3,2,1]))
print(reverseArray([10,20,30,40]))
[1, 2, 3, 4, 5] [40, 30, 20, 10]
Approach (built in method)¶
In [23]:
def reverseArray(array):
return array[::-1]
print(reverseArray([5,4,3,2,1]))
print(reverseArray([10,20,30,40]))
[1, 2, 3, 4, 5] [40, 30, 20, 10]
Print fiboocii¶
Print Fibonacci Series up to Nth term
Examples:
Example 1:
Input: N = 5
Output: 0 1 1 2 3 5
Explanation: 0 1 1 2 3 5 is the fibonacci series up to 5th term.(0 based indexing)
Example 2:
Input: 6
Output: 0 1 1 2 3 5 8
Explanation: 0 1 1 2 3 5 8 is the fibonacci series upto 6th term.(o based indexing)
Fib(n) = Fib (n-1) + Fib(n-2) And Fib(0) = 0 And Fib(1) = 1
In [27]:
def printFib(n):
def helper(idx =0,first =0 , second = 1):
if idx > n:
return
print(first ,end =" ")
helper(idx+1, second , first + second)
helper()
printFib(5)
printFib(6)
0 1 1 2 3 5 0 1 1 2 3 5 8
Check if the given String is Palindrome or not¶
Input: Str = “ABCDCBA”
Output: True
Input: Str = “TAKE U FORWARD”
Output: False
Approach 1 (Iterative)¶
- use left and right ptr
In [ ]:
def is_palindrome(s):
L = len(s)
left ,right = 0 , L-1
while left < right:
if s[left] != s[right]:
return False
left +=1
right -=1
return True
print(is_palindrome("ABCDCBA"))
print(is_palindrome("TAKE U FORWARD"))
True False
Complexity¶
- L = len(s)
- O(N)
- O(1)
Approach 2 (Recursive)¶
- recursive check left and right without loop
In [3]:
def is_palindrome(s):
L = len(s)
def check(left_ptr,right_ptr):
if left_ptr >= right_ptr:
return True
if s[left_ptr] != s[right_ptr]:
return False
return check(left_ptr +1, right_ptr-1)
return check(0,L-1)
print(is_palindrome("ABCDCBA"))
print(is_palindrome("TAKE U FORWARD"))
True False
Complexity¶
- L = len(s)
- O(N)
- O(N)