Practical V - PYTHON
Practical V - PYTHON
Practical V - PYTHON
(AUTONOMOUS)
An Autonomous Institution - Affiliated to Bharathiar University
NAME : __________________________
REGISTER NO : __________________________
CLASS : __________________________
SEMESTER : __________________________
YEAR : __________________________
HINDUSTHAN COLLEGE OF ARTS AND SCIENCE
(AUTONOMOUS)
An Autonomous Institution - Affiliated to Bharathiar University
CERTIFICATE
Date:
Place: Coimbatore
CONTENTS
AIM
ALGORITHM
STEP 1 Start the program
STEP 2 If X=0 then GCD(X, Y)=Y since the Greatest Common Divisor of 0 and Y is
Y.
STEP 3 If Y=0 then GCD(X,Y)=X since the Greatest Common Divisor of 0 and X is
X.
STEP 5 Find GCD( Y, R ) because GCD( X, Y ) = GCD( Y, R ). Use the above steps
again.
1
SOURCE CODE
def gcd_fun (x, y):
if (y == 0):
return x
else:
num = gcd_fun(x, y)
print(num)
2
OUTPUT
RESULT
The above program is executed and hence captured the necessary output.
3
PROGRAM .NO: 2 DATE:
AIM
ALGORITHM
STEP 1: Start the Program.
4
SOURCE CODE
# Creating a recursive function
5
OUTPUT
RESULT
The above program is executed and hence captured the necessary output.
6
PROGRAM .NO: 3 DATE:
AIM
ALGORITHM
Approach for finding minimum element:
Traverse the node from root to left recursively until left is NULL.
The node whose left is NULL is the node with minimum value.
Traverse the node from root to right recursively until right is NULL.
The node whose right is NULL is the node with maximum value.
7
SOURCE CODE
#Find minimum and maximum in binary tree using recursion
class newNode:
self.data = data
def findMax(root):
if (root == None):
return float('-inf')
res = root.data
lres = findMax(root.left)
rres = findMax(root.right)
res = lres
res = rres
return res
def findMin(root):
if (root == None):
return float('inf')
res = root.data
lres = findMin(root.left)
8
rres = findMin(root.right)
res = lres
res = rres
return res
if __name__ == '__main__':
root = newNode(2)
root.left = newNode(7)
root.right = newNode(5)
root.left.right = newNode(6)
root.left.right.left = newNode(1)
root.left.right.right = newNode(11)
root.right.right = newNode(9)
root.right.right.left = newNode(4)
9
OUTPUT
RESULT
The above program is executed and hence captured the necessary output.
10
PROGRAM .NO: 4 DATE:
ALGORITHM
STEP 1: Start the Program
STEP 2: Create Class Matrices as:
def __init__(self,A,B):
self.A=A
self.B=B
def display(self,C):
print('Resultant Matrix is:')
for i in range(0,m):
print('\n')
for j in range(0,n):
print(' {}'.format(C[i][j]),end=" ")
11
SOURCE CODE
# -*- coding: utf-8 -*-
"""
"""
class Matrices:
def __init__(self,A,B):
self.A=A
self.B=B
def display(self,C):
for i in range(0,m):
print('\n')
for j in range(0,n):
def Add(self,C):
for i in range(0,m):
for j in range(0,n):
def Sub(self,C):
for i in range(0,m):
12
for j in range(0,n):
def Mul(self,C):
for i in range(0,m):
for j in range(0,q):
for k in range(0,n):
if __name__=='__main__':
obj = Matrices(A,B)
13
var =1
while var!='0':
if choice==1:
obj.Add(C)
obj.display(C)
else:
elif choice==2:
obj.Sub(C)
obj.display(C)
else:
elif choice==3:
if n==p:
14
C = [[0 for j in range(0, q)] for i in range(0, m)]
obj.Mul(C)
obj.display(C)
else:
elif choice==4:
exit(0)
else:
15
OUTPUT
RESULT
The above program is executed and hence captured the necessary output.
16
PROGRAM .NO: 5 DATE:
ALGORITHM
Step 1: Set i to 1
Step 4: Set i to i + 1
Step 5: Go to Step 2
Step 8: Exit
Step 4 : Divide the list using probing formula and find the new
middle.
17
Step 6 : If data is smaller than middle, search in lower sub-list.
SOURCE CODE
from array import *
for i in range(len(arr)):
if arr[i] == n:
return i
return -1
arr = array('i',[])
for i in range(n):
x = int(input("Element: "))
arr.append(x)
r = linear_search(arr, num)
if(r == -1):
else:
low = 0
18
high = len(arr2) - 1
mid = 0
if arr2[mid] < n:
low = mid + 1
high = mid - 1
else:
return mid
return -1
arr2 = array('i',[])
for i in range(n):
x = int(input("Element: "))
arr2.append(x)
r = binary_searchiter(arr2, number)
if r != -1:
else:
OUTPUT
19
RESULT
The above program is executed and hence captured the necessary output.
20
PROGRAM .NO: 6 DATE:
AIM
ALGORITHM
Step 1: Start the Program
Step 2: Create Function to Calculate sum of each Row.
Step 3: To find the sum of row:
for i in range(0, n) :
for j in range(n) :
# Add the element
sum += arr[i][j]
21
SOURCE CODE
# -*- coding: utf-8 -*-
"""
Created on Mon Aug 2 12:50:34 2021
22
# Function to calculate sum of diagnals
def diagonal_sum(arr, n):
principal = 0
secondary = 0
print("\nFinding Sum of diagnals:\n----------------------------")
for i in range(0, n):
principal += arr[i][i]
secondary += arr[i][n - i -1]
print("Principal Diagnal:", principal)
print("Secondary Diagnal:", secondary)
arr = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
print("\n")
print("The given matrix is: \n----------------------------\n", arr[0],"\n", arr[1], "\n",
arr[2])
row_sum(arr, 3)
column_sum(arr, 3)
diagonal_sum(arr,3)
OUTPUT
23
RESULT
The above program is executed and hence captured the necessary output.
24
INSERTION OPERATION USING SINGLY LINKED LIST
AIM
ALGORITHM
Write Overflow
Go To Step 7
[End Of If]
Step 7: Exit
SOURCE CODE
class Node:
25
# Function to initialize the node object
class LinkedList:
def __init__(self):
self.head = None
new_node = Node(new_data)
new_node.next = self.head
self.head = new_node
26
def insertAfter(self, prev_node, new_data):
if prev_node is None:
return
new_node = Node(new_data)
new_node.next = prev_node.next
prev_node.next = new_node
new_node = Node(new_data)
if self.head is None:
self.head = new_node
return
last = self.head
while (last.next):
last = last.next
last.next = new_node
class Node:
class LinkedList:
self.head = None
new_node = Node(new_data)
new_node.next = self.head
self.head = new_node
if prev_node is None:
29
print ("The given previous node must inLinkedList.")
return
new_node = Node(new_data)
new_node.next = prev_node.next
prev_node.next = new_node
new_node = Node(new_data)
30
# new node as head
if self.head is None:
self.head = new_node
return
last = self.head
while (last.next):
last = last.next
last.next = new_node
def printList(self):
temp = self.head
while (temp):
print (temp.data)
temp = temp.next
if __name__=='__main__':
31
# Start with the empty list
llist = LinkedList()
llist.append(6)
llist.push(7);
llist.push(1);
llist.append(4)
# Insert 8, after 7. So linked list becomes 1 -> 7-> 8-> 6-> 4-> None
llist.insertAfter(llist.head.next, 8)
llist.printList()
32
OUTPUT
33
RESULT
The above program is executed and hence captured the necessary output.
ALGORITHM
Step 1: Start the Program.
fname = sys.argv[0]
lines = 0
words = 0
letters = 0
34
Step 4: End the program.
SOURCE CODE
# -*- coding: utf-8 -*-
"""
Created on Mon Aug 2 12:50:34 2021
import sys
fname = sys.argv[0]
lines = 0
words = 0
letters = 0
pos = 'out'
for letter in line:
if letter != ' ' and pos == 'out':
words += 1
pos = 'in'
elif letter == ' ':
pos = 'out'
35
print("Lines:", lines)
print("Words:", words)
print("Letters:", letters)
OUTPUT
36
RESULT
The above program is executed and hence captured the necessary output.
ALGORITHM
Write UNDERFLOW
Go to Step 5
[END OF IF]
Step 5: EXIT
37
SOURCE CODE
class Node:
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
new_node = Node(new_data)
38
new_node.next = self.head
self.head = new_node
temp = self.head
if (temp.data == key):
self.head = temp.next
temp = None
return
if temp.data == key:
break
prev = temp
temp = temp.next
39
# if key was not present in linked list
if(temp == None):
return
prev.next = temp.next
temp = None
def printList(self):
temp = self.head
while(temp):
temp = temp.next
# Driver program
llist = LinkedList()
llist.push(7)
llist.push(1)
llist.push(3)
llist.push(2)
llist.printList()
llist.deleteNode(1)
40
llist.printList()
OUTPUT
41
RESULT
The above program is executed and hence captured the necessary output.
ALGORITHM
Step 1: Start the Program.
42
Step 2: import XLWRITTER and add the name of the workbook as:
workbook = xlsxwriter.Workbook('chart_line.xlsx')
worksheet = workbook.add_worksheet()
chart1.add_series({
})
chart1.add_series({
})
Step 7: Set X-axis and Y-axis to Display the line Graph in Chart.
43
SOURCE CODE
# -*- coding: utf-8 -*-
"""
Created on Mon Aug 2 12:50:34 2021
import xlsxwriter
workbook = xlsxwriter.Workbook('chart_line.xlsx')
worksheet = workbook.add_worksheet()
bold = workbook.add_format({'bold':1})
headings = ['Year', 'Company', 'Profit']
data = [
[2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2010, 2011, 2012, 2013,
2014, 2015, 2016, 2017, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017],
['Microsoft', 'Microsoft', 'Microsoft', 'Microsoft', 'Microsoft', 'Microsoft',
'Microsoft', 'Microsoft', 'Alphabet', 'Alphabet', 'Alphabet', 'Alphabet',
'Alphabet', 'Alphabet', 'Alphabet', 'Alphabet', 'Amazon', 'Amazon', 'Amazon',
'Amazon', 'Amazon', 'Amazon', 'Amazon', 'Amazon'],
[18.76, 23.15, 16.98, 21.86, 22.07, 12.19, 16.8, 21.2, 8.372, 9.706, 10.179,
12.733, 14.136, 16.348, 19.478, 12.662, 1.152, 0.631, 0.139, 0.274, 0.241,
0.596, 2.371, 3.033]
]
worksheet.write_column('A2', data[0])
worksheet.write_column('B2', data[1])
worksheet.write_column('C2', data[2])
44
chart1 = workbook.add_chart({'type': 'line'})
chart1.add_series({
'name': ' = Sheet1 !$B$1',
'categories': ' = Sheet1 !$A$2:$A$25',
'values': ' = Sheet1 !$B$2:$B$25',
})
chart1.add_series({
'name': ['Sheet1', 0, 2],
'categories': ['Sheet1', 1, 0, 6, 0],
'values': ['Sheet1', 1, 2, 6, 2],
})
workbook.close()
45
OUTPUT
46
RESULT
The above program is executed and hence captured the necessary output.
47