Record Program 1-3

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Record Program 1:

PROGRAM TO TRAVERSE AND SEARCH

PROBLEM DEFINITION:

To write an interactive menu driven program in Python using


functions to traverse and search for the student name from a list of
students containing student roll number, name and average.

Program:

#Program to traverse and search for the student name from a list of students
def Traverse(L,nametosearch):
for i in L:
if i[1]==nametosearch:
print("Name Available")
print("%5s"%i[0],"%10s"%i[1],"%20s"%i[2])
break
else:
print("Name not found in the List")

print("Program to Traverse and Search Student Detail")


n=int(input("Enter the Number of Students"))
i=0
K=[]
while i<n:
sno=int(input("Enter the Student Number"))
sname=input("Enter the Student Name")
avg=int(input("Enter the Average"))
K.append([sno,sname,avg])
i+=1
ans='y'
while ans=='y':
nametosearch=input("Enter the Name to be searched")
print("The Given List is")
print("%5s"%"Student Number","%10s"%"Student Name","%20s"%"Average")
for i in K:
print("%5s"%i[0],"%15s"%i[1],"%20s"%i[2])
Traverse(K,nametosearch)
ans=input("Do You want to Continue the Search")
Output:

Program to Traverse and Search Student Detail


Enter the Number of Students 5
Enter the Student Number 1
Enter the Student Name Arjun
Enter the Average 67
Enter the Student Number 2
Enter the Student Name Ashok
Enter the Average 56
Enter the Student Number 3
Enter the Student Name Balaguru
Enter the Average 89
Enter the Student Number 4
Enter the Student Name Sekar
Enter the Average 98
Enter the Student Number 5
Enter the Student Name Varun
Enter the Average 87
Enter the Name to be searched Sekar
The Given List is
Student Number Student Name Average
1 Arjun 67
2 Ashok 56
3 Balaguru 89
4 Sekar 98
5 Varun 87
Name Available
4 Sekar 98
Do You want to Continue the Search y
Enter the Name to be searched Tejas
The Given List is
Student Number Student Name Average
1 Arjun 67
2 Ashok 56
3 Balaguru 89
4 Sekar 98
5 Varun 87
Name not found in the List
Do You want to Continue the Search n
Record Program 2:

PROGRAM FOR LIST MANIPULATION

PROBLEM DEFINITION:

To write an interactive Python program to do the following using functions:


(i) Accept a list of elements and exchange the first half with the second
half of the list.
(ii) Accept a list of strings and display the number of palindrome words
present in it.

Program:

#Program for List Manipulation


def half_and_half(my_list):
if len(my_list)%2 == 0:
start = 0
else:
start = 1
L = len(my_list)//2
for i in range(L):
temp = my_list[i]
my_list[i] = my_list[i+L+start]
my_list[i+L+start] = temp
rep='y'
print("Program for List Manipulation")
while rep=='y':
print("1. Exchange of List Elements")
print("2. Count the Number of Palindrome")
ch=int(input("Enter the Choice"))
if ch==1:
my_list = list(eval(input("Enter the list of Numbers")))
half_and_half(my_list)
print(my_list)
else:
count=0
listofstr=list(eval(input("Enter the List of Strings")))
print(listofstr)
for i in listofstr:
if i==i[::-1]:
count+=1
print("The Number of Palindrome are",count)
rep=input("Do you want to Continue")
Output:

Program for List Manipulation


1. Exchange of List Elements
2. Count the Number of Palindrome
Enter the Choice 1
Enter the list of Numbers [1,2,3,4,5]
[4, 5, 3, 1, 2]
Do you want to Continue y
1. Exchange of List Elements
2. Count the Number of Palindrome
Enter the Choice 1
Enter the list of Numbers [1,2,3,4,5,6]
[4, 5, 6, 1, 2, 3]
Do you want to Continue y
1. Exchange of List Elements
2. Count the Number of Palindrome
Enter the Choice 2
Enter the List of Strings ["mom","god","wow","is","great"]
['mom', 'god', 'wow', 'is', 'great']
The Number of Palindrome are 2
Do you want to Continue n
Record Program 3:

PROGRAM FOR MATRIX MANIPULATION

PROBLEM DEFINITION:

To Write an interactive Python program using functions to find the sum of


boundary, non-boundary, left diagonal and right diagonal elements of a
nested List A of size M x N.
Program:

#Program for Matrix Manipulation


def bound_nonbound(L,m,n):
sumb=sumnb=0
for i in range(m):
for j in range(n):
if i==0 or j==0 or i==m-1 or j==n-1:
sumb+=L[i][j]
if not(i==0 or j==0 or i==m-1 or j==n-1):
sumnb+=L[i][j]
return sumb,sumnb
def left_right(L,m,n):
suml=sumr=0
for i in range(m):
for j in range(n):
if i==j:
suml+=L[i][j]
if i+j==m-1:
sumr+=L[i][j]
return suml,sumr
K=[]
print("Program for Matrix Manipulation")
m=int(input("Enter the Number of Rows"))
n=int(input("Enter the Number of Columns"))
for i in range(m):
t=[]
for j in range(n):
x=int(input("Enter the Number"))
t.append(x)
K.append(t)
print("The Given Matrix is ")
for i in range(m):
for j in range(n):
print(K[i][j],end=" ")
print()
rep='y'
while rep=='y':
print("1. Finding the sum of Boundary and Non boundary elements")
print("2. Finding the sum of Left and Right Diagonal elements")
ch=int(input("Enter the Choice"))
if ch==1:
b=bound_nonbound(K,m,n)
print("Sum of Boundary elements",b[0])
print("Sum of Non Boundary elements",b[1])
if ch==2:
if m!=n:
print("Sum of diagonal elements not possible")
else:
l=left_right(K,m,n)
print("Sum of left diagonal elements",l[0])
print("Sum of right diagonal elements",l[1])
rep=input("Do you want to Continue")

Output:

Program for Matrix Manipulation


Enter the Number of Rows 3
Enter the Number of Columns 4
Enter the Number 1
Enter the Number 2
Enter the Number 3
Enter the Number 4
Enter the Number 5
Enter the Number 6
Enter the Number 7
Enter the Number 8
Enter the Number 9
Enter the Number 1
Enter the Number 2
Enter the Number 3
The Given Matrix is
1234
5678
9123
1. Finding the sum of Boundary and Non boundary elements
2. Finding the sum of Left and Right Diagonal elements
Enter the Choice 1
Sum of Boundary elements 38
Sum of Non Boundary elements 13
Do you want to Continue y
1. Finding the sum of Boundary and Non boundary elements
2. Finding the sum of Left and Right Diagonal elements
Enter the Choice 2
Sum of diagonal elements not possible
Do you want to Continue n

You might also like