0% found this document useful (0 votes)
9 views5 pages

PP Week 6

This document outlines a Python programming lab focused on recursion, detailing five exercises. Each exercise includes an aim, description, program code, output, and inference regarding the use of recursion for tasks such as summing elements in a list, counting letter occurrences, checking primality, calculating products, and finding powers. The document emphasizes the advantages and limitations of recursion in various computational scenarios.

Uploaded by

23331a05e7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

PP Week 6

This document outlines a Python programming lab focused on recursion, detailing five exercises. Each exercise includes an aim, description, program code, output, and inference regarding the use of recursion for tasks such as summing elements in a list, counting letter occurrences, checking primality, calculating products, and finding powers. The document emphasizes the advantages and limitations of recursion in various computational scenarios.

Uploaded by

23331a05e7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

‭PYTHON PROGRAMMING LAB‬

‭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 using‬‭a 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‬

I‭nference‬‭:‬
‭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‬

‭Aim‬‭: To count the occurrences of a specific letter‬‭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‬

I‭nference‬‭:‬
‭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

‭over sequential data.‬


‭3.‬ ‭Write a Python program to find if a number is prime or not using recursion‬

‭Aim‬‭: To determine whether a given number is prime‬‭using a recursive function.‬

‭ 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)‬

‭if i== int(m.sqrt(n)):‬

‭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)‬

‭if x == True:print("{} is prime".format(n))‬


‭23

‭else: print("{} is not prime".format(n))‬

‭Output‬‭:‬

enter a number:79‬

79 is prime‬

I‭nference‬‭:‬
‭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‬

‭Aim‬‭: To calculate the product of two numbers using‬‭recursive addition.‬

‭ 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

I‭nference‬‭:‬
‭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‬

‭Aim‬‭: To calculate the power of a number using a recursive‬‭function.‬

‭ 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

I‭nference‬‭:‬
‭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

You might also like