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

Exp1 3

The document describes Python programs that implement various abstract data types (ADTs) and algorithms using recursion. It includes programs to: 1) Implement classes for perimeter and area calculations. 2) Calculate factorials and Fibonacci series recursively. 3) Implement a list ADT using a Python array, demonstrating append, insert, remove, reverse, and other array operations.

Uploaded by

Mr Movie
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)
227 views5 pages

Exp1 3

The document describes Python programs that implement various abstract data types (ADTs) and algorithms using recursion. It includes programs to: 1) Implement classes for perimeter and area calculations. 2) Calculate factorials and Fibonacci series recursively. 3) Implement a list ADT using a Python array, demonstrating append, insert, remove, reverse, and other array operations.

Uploaded by

Mr Movie
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

EX.

NO: 1 Implementation of simple ADTs as Python classes

Aim: Write a Python Program to implement ADTs as Python classes.

Algorithm:

Step 1. Start the program


Step 2. Create classes of ADT
Step 3. Access the classes
Step 4. Print the statement
Step 5. Stop the program

Program:

class perimeter():
height=float(input("Enter the height ="))
width=float(input("Enter the width ="))
perim= (height*2)+(width*2)
print("Perimeter = ",perim)
class area(perimeter):
def fun(self):
area1=ar.height*ar.width
print("Area = ",area1)
ar=area()
ar.fun()

Output :

Enter the height = 10


Enter the width = 5
Perimeter = 30.0
Area = 50.0

Result:
Thus, the above program for ADTs python classes was successfully completed.
EX. NO: 2 a) Implementation of Recursive algorithms in Python
Finding Factorial of a given number

Aim: Write a Python program to implement Factorial using recursive algorithm.

Algorithm :

Step 1: Start
Step 2: Read number n
Step 3: Call factorial(n)
Step 4: Print factorial f
Step 5: Stop

factorial(n)
Step 1: If n==1 then return 1
Step 2: Else
f=n*factorial(n-1)
Step 3: Return

Program:
def factorial(num):
if (num == 0):
return 1
else:
return num * factorial(num - 1)
print('{}! is {}'.format(4, factorial(4)))
print('{}! is {}'.format(2, factorial(2)))

Output:
4! is 24
2! is 2

Result:
Thus, the above program for implementing factorial using recursion was successfully
completed.
EX. NO: 2 b) Implementation of Recursive algorithms in Python
Implementation of Fibonacci Series

Aim: Write a Python program to implement Fibonacci Series using recursive algorithm.

Algorithm :

Step-1 Start the Program


Step-2 Call the Procedure Recursive_Fibonacci(n)
int f0, f1
f0 := 0
f1 := 1
Step-3 If(n <= 1):
Step-4 Return n
Step-5 Return Recursive_Fibonacci(n-1) + Recursive_Fibonacci(n-2)
Step-6 Stop the program

Program:

def fibonacci(n):
if(n <= 1):
return n
else:
return(fibonacci(n-1) + fibonacci(n-2))
n = int(input("Enter number of terms:"))
print("Fibonacci sequence:")
for i in range(n):
print(fibonacci(i))

Output:

0
1
1
2
3
5

Result:
Thus, the above program for implementing Fibonacci series using recursion was
successfully completed.
EX. NO: 3 Implement List ADT using Python arrays

Aim: Write a Python program to implement Fibonacci Series using recursive algorithm.

Algorithm :

Step-1 Start the Program


Step-2 Import the copy library
Step-3 Create an array using range function
Step-4 Append the array
Step-5 Insert an element in a specified position
Step-6 Remove an element
Step-7 Printing the array in reverse order
Step-8 Stop the program

Program:

import array
arr = array.array('i', [1, 2, 3])
print ("The new created array is : ",end=" ")
for i in range (0, 3):
print (arr[i], end=" ")
print("\r")
arr.append(4)
print("The appended array is : ", end="")
for i in range (0, 4):
print (arr[i], end=" ")
arr.insert(2, 5)
print("\r")
print ("The array after insertion is : ", end="")
for i in range (0, 5):
print (arr[i], end=" ")
print("\r")
print ("The popped element is : ",end="")
print (arr.pop(2));
# printing array after popping
print ("The array after popping is : ",end="")
for i in range (0,4):
print (arr[i],end=" ")
print("\r"
# using remove() to remove 1st occurrence of 1
arr.remove(1)

# printing array after removing


print ("The array after removing is : ",end="")
for i in range (0,3):
print (arr[i],end=" ")
arr.reverse()
# printing array after reversing
print("\r")
print ("The array after reversing is : ",end="")
for i in range (0,3):
print (arr[i],end=" ")

Output :

The new created array is : 1 2 3


The appended array is : 1 2 3 4
The array after insertion is : 1 2 5 3 4
The popped element is : 5
The array after popping is : 1 2 3 4
The array after removing is : 2 3 4
The array after reversing is : 4 3 2

Result:
Thus, the above program for implementing Fibonacci series using recursion was successfully
completed.

You might also like