Python Notes for mca i year students osmania university.docx
fds Practicle 1to 6 program.pdf
1. Practicle 1
def accept_set(A,Str):
n = int(input("Enter the total no. of student who play %s : "%Str))
for i in range(n) :
x = input("Enter the name of student %d who play %s : "%((i+1),Str))
A.append(x)
print("Set accepted successfully");
def display_set(A,Str):
n = len(A)
if(n == 0) :
print("nGroup of Students who play %s = { }"%Str)
else :
print("nGroup of Students who play %s = {"%Str,end=' ')
for i in range(n-1) :
print("%s,"%A[i],end=' ')
print("%s }"%A[n-1]);
def search_set(A,X) :
n = len(A)
for i in range(n):
if(A[i] == X) :
return (1)
return (0)
def find_intersection_set(A,B,C):
for i in range(len(A)):
2. flag = search_set(B,A[i]);
if(flag == 1) :
C.append(A[i])
def find_difference_set(A,B,C):
for i in range(len(A)):
flag = search_set(B,A[i]);
if(flag == 0) :
C.append(A[i])
def find_union_set(A,B,C):
for i in range(len(A)):
C.append(A[i])
for i in range(len(B)):
flag = search_set(A,B[i]);
if(flag == 0) :
C.append(B[i])
Group_A = []
Group_B = []
Group_C = []
while True :
accept_set(Group_A,"Cricket")
accept_set(Group_B,"Badminton")
accept_set(Group_C,"Football")
display_set(Group_A,"Cricket")
display_set(Group_B,"Badminton")
3. display_set(Group_C,"Football")
break
def main() :
print("______________MENU___________")
print ("1 : List of students who play both cricket and badminton")
print ("2 : List of students who play either cricket or badminton but not both")
print ("3 : Number of students who play neither cricket nor badminton")
print ("4 : Number of students who play cricket and football but not badminton")
print ("5 : Exit")
ch = int(input("Enter your choice : "))
Group_R = []
if (ch==1):
display_set(Group_A,"Cricket")
display_set(Group_B,"Badminton")
find_intersection_set(Group_A,Group_B,Group_R)
display_set(Group_R," both Cricket and Badminton")
print()
main()
elif (ch==2):
display_set(Group_A,"Cricket")
display_set(Group_B,"Badminton")
R1 = []
find_union_set(Group_A,Group_B,R1)
R2 = []
find_intersection_set(Group_A,Group_B,R2)
find_difference_set(R1,R2,Group_R)
4. display_set(Group_R," either cricket or badminton but not both")
print()
main()
elif (ch==3):
display_set(Group_A,"Cricket")
display_set(Group_B,"Badminton")
display_set(Group_C,"Football")
R1 = []
find_union_set(Group_A,Group_B,R1)
find_difference_set(Group_C,R1,Group_R)
display_set(Group_R," neither cricket nor badminton")
print("Number of students who play neither cricket nor badminton = %s"%len(Group_R))
print()
main()
elif (ch==4):
display_set(Group_A,"Cricket")
display_set(Group_C,"Football")
display_set(Group_B,"Badminton")
R1 = []
find_intersection_set(Group_A,Group_C,R1)
find_difference_set(R1,Group_B,Group_R)
display_set(Group_R,"cricket and football but not badminton")
print("Number of students who play cricket and football but not badminton = %s"%len(Group_R))
print()
main()
elif (ch==5):
exit
else :
print ("Wrong choice entered !! Try again")
5. main()
practicle 2
# The average_score score of class
def average_score(l):
sum = 0
cnt = 0
for i in range(len(l)):
if l[i] != -1:
sum += l[i]
cnt += 1
avg = sum / cnt
print("Total Marks are : ", sum)
print("average_score Marks are : {:.2f}".format(avg))
# Highest score in the class
def highest_score(l):
Max = l[0]
for i in range(len(l)):
if l[i] > Max:
Max = l[i]
return (Max)
6. # Lowest score in the class
def lowest_score(l):
# Assign first element in the array which corresponds to marks of first present student
# This for loop ensures the above condition
for i in range(len(l)):
if l[i] != -1:
Min = l[i]
break
for j in range(i + 1, len(l)):
if l[j] != -1 and l[j] < Min:
Min = l[j]
return (Min)
# Count of students who were absent for the test
def absent_student(l):
cnt = 0
for i in range(len(l)):
if l[i] == -1:
cnt += 1
return (cnt)
7. # Display mark with highest frequency
# refeence link : https://www.youtube.com/watch?v=QrIXGqvvpk4&t=422s
def highest_frequancy(l):
i = 0
Max = 0
print(" Marks ----> frequency count ")
for ele in l:
if l.index(ele) == i:
print(ele, "---->", l.count(ele))
if l.count(ele) > Max:
Max = l.count(ele)
mark = ele
i += 1
return (mark, Max)
# Input the number of students and their corresponding marks in FDS
student_marks = []
noStudents = int(input("Enter total number of students : "))
for i in range(noStudents):
marks = int(input("Enter marks of Student " + str(i + 1) + " : "))
student_marks.append(marks)
def main():
print("tt__________MENU_________")
print("1. The average_score score of class ")
print("2. Highest score and lowest score of class ")
8. print("3. Count of students who were absent for the test ")
print("4. Display mark with highest frequency ")
print("5. Exit ")
choice = int(input("Enter your choice : "))
if choice == 1:
average_score(student_marks)
print()
main()
elif choice == 2:
print("Highest score in the class is : ", highest_score(student_marks))
print("Lowest score in the class is : ", lowest_score(student_marks))
print()
main()
elif choice == 3:
print("Count of students who were absent for the test is : ", absent_student(student_marks))
print()
main()
elif choice == 4:
mark, count = highest_frequancy(student_marks)
print("Highest frequency of marks {0} is {1} ".format(mark, count))
print()
main()
elif choice == 5:
exit
else:
print("Wrong choice")
9. print()
main()
practicle 3
print("basic matrix operation using python")
m1=[]
m=[]
m2=[]
res=[[0,0,0,0,0], [0,0,0,0,0], [0,0,0,0,0], [0,0,0,0,0],[0,0,0,0,0]]
print("enter the number of rows and colums")
row1=int(input("enter no of rows in first matrix:"))
col1=int(input("enter no of column in first matrix:"))
row2=int(input("enter no of rows in second matrix:"))
col2=int(input("enter no of rows in second matrix:"))
def main():
print("1. addition of two matrix")
print("2. subtraction of two matrix ")
print("3. multiplication of two matrix")
print("4. transpose of first matrix")
print("5. transpose of second matrix")
print("6. exit")
choice =int(input("enter your choice:"))
10. if choice==1:
print(" addition of two matrix" )
if ((row1==row2)and (col1==col2)):
matrix_addition(m1,m2,row1,col1)
show_matrix(res,row1,col1)
else:
print("addition is not possible")
print()
main()
elif choice==2:
print(" subtraction of two matrix" )
if ((row1==row2)and (col1==col2)):
matrix_subtraction(m1,m2,row1,col1)
show_matrix(res,row1,col1)
else:
print("subtraction is not possible")
print()
main()
elif choice==3:
print("multiplication of matrix")
if (col1==row2):
matrix_multiplication(m1,m2,row2,col1)
show_matrix(res,row2,col1)
else:
print("multiplication is not possible")
print()
main()
elif choice==4:
11. print("after applying transpose matrix elemnt are:")
matrix_transpose(m1,row1,col1)
show_matrix(res, row1,col1)
print()
main()
elif choice==5:
print("after applying transpose matrix elemnt are:")
matrix_transpose(m1,row1,col1)
show_matrix(res, row2,col2)
print()
main()
elif choice==6:
exit
else:
print("enter valid choice")
def accept_matrix(m,row,col):
for i in range (row):
c=[]
for j in range(col):
no=int(input("enter the value of matrix[" + str(i) + "] [" + str(j) + "]::"))
c.append(no)
print("-----------------")
m.append(c)
print("enter the value of for first matrix")
12. accept_matrix(m1,row1,col1)
print("enter the value for second matrix")
accept_matrix(m2,row2,col2)
def show_matrix(m,row,col):
for i in range(row):
for j in range(col):
print(m[i][j],end=" ")
print()
# show_matrix matrix
print("the first matrix is:")
show_matrix(m1,row1,col1)
print("the second matrix :")
show_matrix(m2,row2,col2)
def matrix_addition(m1,m2,row,col):
for i in range(row):
for j in range(col):
res[i][j]=m1[i][j]+m2[i][j]
def matrix_subtraction(m1,m2,row,col):
for i in range(row):
for j in range(col):
res[i][j]=m1[i][j] - m2[i][j]
def matrix_multiplication(m1,m2,row,col):
for i in range (row):
for j in range(col):
13. for k in range(col):
res[i][j]=res[i][j]+m1[i][k]*m2[k][j]
def matrix_transpose(m,row,col):
for i in range (row):
for j in range(col):
res[i][j]= m[j][i]
main()
practicle 4
#-----------------------------------------------
"""
Write a python program to store first year percentage of students in array.
Write function for sorting array of floating point numbers in ascending order using
a) Selection Sort
b) Bubble sort and display top five scores.
"""
def bubbleSort(arr):
n = len(arr)
for i in range(n-1):
for j in range(0, n-i-1):
if arr[j] > arr[j+1] :
arr[j], arr[j+1] = arr[j+1], arr[j]
14. def selectionSort(arr):
for i in range(len(arr)):
min_idx = i
for j in range(i+1, len(arr)):
if arr[min_idx] > arr[j]:
min_idx = j
arr[i], arr[min_idx] = arr[min_idx], arr[i]
def top_five(arr):
print("Top five score are : ")
cnt = len(arr)
if cnt < 5:
start, stop = cnt - 1, -1 # stop set to -1 as we want to print the 0th element
else:
start, stop = cnt - 1, cnt - 6
for i in range(start, stop, -1):
print("t {0:.2f}".format(arr[i]), end=" ")
arr=[]
Num=int(input("Enter the number of students: "))
for i in range (Num):
per=float(input("Enter the marks of student " + str(i+1) + " : "))
arr.append(per)
15. def main():
print("1. selection sort")
print("2. bubble sort")
print("3. display top five marks")
print("4. exit")
choice=int(input("enter choice for sort:"))
if choice==1:
selectionSort(arr)
print ("Sorted array")
m=[]
for i in range(len(arr)):
m.append(arr)
print(m)
break
print()
main()
elif choice==2:
bubbleSort(arr)
print ("Sorted array is:")
n=[]
for i in range(len(arr)):
n.append(arr)
print(n)
break
main()
elif choice==3:
top_five(arr)
16. elif choice==4:
exit
else:
print("enter valid input")
print(arr)
main()
practicle 5
"""
Write a python program to store first year percentage of students in array.
Write function for sorting array of floating point numbers in ascending order using
a) shell sort
b) Insertion sort and display top five scores.
"""
def shellSort(arr):
n = len(arr)
gap = n // 2
# Do a gapped insertion_sort for this gap size.
# The first gap elements a[0..gap-1] are already in gapped
# order keep adding one more element until the entire array
# is gap sorted
while gap > 0:
for i in range(gap, n):
# add arr[i] to the elements that have been gap sorted
17. # save arr[i] in temp and make a hole at position i
temp = arr[i]
# shift earlier gap-sorted elements up until the correct
# location for arr[i] is found
j = i
while j >= gap and arr[j - gap] > temp:
arr[j] = arr[j - gap]
j -= gap
# put temp (the original arr[i]) in its correct location
arr[j] = temp
gap //= 2
return arr
def insertion_sort(arr):
for i in range(1, len(arr)):
key = arr[i]
# Move elements of a[0..i-1], that are
# greater than key, to one position ahead
# of their current position
j = i - 1
while j >= 0 and key < arr[j]:
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key
return arr
18. def top_five(arr):
print("Top five score are : ")
cnt = len(arr)
if cnt < 5:
start, stop = cnt - 1, -1 # stop set to -1 as we want to print the 0th element
else:
start, stop = cnt - 1, cnt - 6
for i in range(start, stop, -1):
print("t {0:.2f}".format(arr[i]), end=" ")
arr=[]
Num=int(input("Enter the number of students: "))
for i in range (Num):
per=float(input("Enter the marks of student " + str(i+1) + " : "))
arr.append(per)
def main():
print("1. shell sort")
print("2. insertion sort")
print("3. display top five marks")
print("4. exit")
choice=int(input("enter choice for sort:"))
if choice==1:
19. insertion_sort(arr)
print ("Sorted array")
m=[]
for i in range(len(arr)):
m.append(arr)
print(m)
break
print()
main()
elif choice==2:
shellSort(arr)
print ("Sorted array is:")
n=[]
for i in range(len(arr)):
n.append(arr)
print(n)
break
main()
elif choice==3:
top_five(arr)
elif choice==4:
exit
else:
print("enter valid input")
print(arr)
main()
20. practicle 6
#-----------------------------------------------
"""
Write a python program to store first year percentage of students in array.
Write function for sorting array of floating point numbers in ascending order using
a) quick_sort sort display top five scores.
"""
print("tt______________PROGRAM_____________")
print()
def Partition(arr,low,high):
pivot = arr[low]
i=low+1
j=high
flag = False
while(not flag):
while(i<=j and arr[i]<=pivot):
i = i + 1
while(i<=j and arr[j]>=pivot):
j = j - 1
if(j<i):
flag = True
else:
temp = arr[i]
arr[i] = arr[j]
arr[j] = temp
temp = arr[low]
21. arr[low] = arr[j]
arr[j] = temp
return j
def quick_sort(arr,low,high):
if(low<high):
m=Partition(arr,low,high)
quick_sort(arr,low,m-1)
quick_sort(arr,m+1,high)
def top_five(arr):
print("Top five score are : ")
cnt = len(arr)
if cnt < 5:
start, stop = cnt - 1, -1 # stop set to -1 as we want to print the 0th element
else:
start, stop = cnt - 1, cnt - 6
for i in range(start, stop, -1):
print("t {0:.2f}".format(arr[i]), end=" ")
arr=[]
Num=int(input("Enter the number of students: "))
for i in range (Num):
per=float(input("Enter the marks of student " + str(i+1) + " : "))