PP Week 6
PP Week 6
WEEK - 5
1. Write a Python program to find the sum of elements in a list recursively
Aim: To calculate the sum of a list's elements usinga recursive function.
E7
escription:
D
This program uses recursion to traverse a list and compute the sum of its elements. It starts by
adding the first element to the sum of the remaining list, which is computed recursively. Such
techniques are foundational in functional programming and demonstrate the concept of breaking
down a problem into smaller, solvable subproblems.
Program:
def lis(l):
#print("list counter")
05
1A
if len(l)==0:
return 0
else:
33
return l[0]+lis(l[1:])
l=[1,2,3,4,5]
print(lis(l))
23
Output:
15
Inference:
Recursion simplifies summation logic and is valuable in scenarios where iterative approaches
are less intuitive. However, recursive depth limitations must be considered for very large
datasets.
2. W
rite a Python program to determine the number of times a given letter occurs in
a string using recursion
escription:
D
This program recursively processes a string, checking each character against the target letter
and counting matches. It demonstrates text processing through recursion, a critical skill in
parsing and analyzing textual data, especially in domains like natural language processing.
Program:
E7
def count(s,l):
if len(s)==0:
return 0
elif s[0]==l:
return 1+count(s[1:],l)
else: 05
1A
return count(s[1:],l)
str="Hello world"
count(str,'o')
Output:
33
2
Inference:
The recursive approach provides a streamlined way to count characters in a string, which can
be extended to more complex text analyses. It highlights recursion’s power in iterative tasks
23
escription:
D
The program checks a number's primality by testing divisors from 2 up to the square root of the
number. Using recursion, it systematically reduces the problem scope by checking one divisor at
a time. This approach reinforces the use of recursion in mathematical problem-solving and
highlights its potential in handling logical checks.
Program:
E7
import math as m
def prime(n,i=2):
# print(i)
return True
elif n%i==0:
05
1A
return False
else:
return prime(n,i+1)
33
n=int(input('enter a number:'))
x=prime(n)
Output:
enter a number:79
79 is prime
Inference:
Recursion elegantly handles primality testing for small numbers. However, it may become
computationally expensive for large inputs due to repetitive checks and deeper call stacks.
4. Write a Python program to find the product of two numbers using recursion
escription:
D
This program uses recursion to compute the product of two numbers by repeatedly adding one
number to itself as many times as the other number specifies. It showcases how simple
operations can be implemented recursively, which is a key concept in functional programming.
Program:
def product(a,b):
E7
if b==1:
return a
else:
return a+product(a,b-1)
print(product(2,3))
05
1A
Output:
6
33
Inference:
Recursive multiplication is a fundamental demonstration of recursion’s power in simplifying
arithmetic operations, though iterative methods are more efficient for larger inputs.
23
5. Write a Python program to find the power of a number using recursion
escription:
D
The program raises a base to a given exponent by recursively multiplying the base with the
result of the function called with a reduced exponent. It demonstrates the divide-and-conquer
strategy, where large problems are broken into smaller, more manageable computations.
Program:
E7
def power(n,p):
if p==0:
return 1
else:
return n*power(n,p-1)
a=pow(3,2)
05
1A
print(a)
Output:
9
33
Inference:
The recursive approach is intuitive for exponentiation and demonstrates the elegance of
breaking a problem into smaller pieces. Optimization techniques like exponentiation by squaring
can further improve efficiency for larger inputs.
23