Pratham FDS
Pratham FDS
word_counts = {}
for word in text.split():
word_counts[word] = word_counts.get(word, 0) + 1
print("Word Count:",word_counts)
Output
Enter a string: Pranav
Longest Word: Pranav
Enter a character to find its frequency: 1
Frequency of '1': 0
Is Palindrome: False
Enter a substring to find its first occurrence: praval10
Index of first occurrence of 'pranav': -1
Word Count: {'Pranav': 1}
PR:-1
Title:- Write a Python program to store marks scored in subject “Fundamental of Data
Structure” by N students in the class. Write functions to compute following:
a) The average score of class
b) Highest score and lowest score of class
c) Count of students who were absent for the test
d) Display mark with highest frequency
Code:-
num_students = int(input("Enter the number of students: "))
marklist = []
print("Enter the marks scored by each student (use 'None' for absent students):")
for i in range(num_students):
mark = input(f"Student {i + 1} mark: ")
if mark.lower() == "none":
marklist.append(None)
else:
try:
marklist.append(int(mark))
except ValueError:
print(f"Invalid input for student {i + 1}. Please enter a valid number or 'None'.")
marklist.append(None)
avg = 0
absent = 0
present_marks = []
total = 0
if present_marks:
total = sum(present_marks)
avg = total / len(present_marks)
print("The average score of class:", avg)
print("Highest score and lowest score of class: Max =", max(present_marks), "Min =",
min(present_marks))
else:
print("No students were present for the test.")
print("Count of students who were absent for the test:", absent)
freq = {}
for mark in present_marks:
if mark in freq:
freq[mark] += 1
else:
freq[mark] = 1
if freq:
highest_freq_mark = max(freq, key=freq.get)
highest_freq_count = freq[highest_freq_mark]
print("Marks with the highest frequency are:", highest_freq_mark, "with a frequency
of:", highest_freq_count)
else:
print("No marks available to calculate frequency.")
Output:-
Enter the number of students: 5
Enter the marks scored by each student (use 'None' for absent students):
Student 1 mark: 80
Student 2 mark: 78
Student 3 mark: 98
Student 4 mark: 65
Student 5 mark: 84
The average score of class: 84
Highest score and lowest score of class: Max = 98 Min = 65
Marks with the highest frequency are: 78 with a frequency of: 1
PR:-3
Title:- Write a Python program to compute following computation on matrix:
a)Addition of two matrices
b) Subtraction of two matrices
c) Multiplication of two matrices
d) Transpose of a matrix
Code:-
row = int(input("Enter the row count: "))
col = int(input("Enter the column count: "))
# Input matrices
print("Enter data for matrix 1:")
for i in range(row):
for j in range(col):
mat1[i][j] = int(input(f"Enter value for row {i + 1} and col {j + 1}: "))
if len(mat1[0]) == len(mat2):
multiplication_result = [[0 for _ in range(len(mat2[0]))] for _ in range(len(mat1))]
for i in range(row):
for j in range(len(mat2[0])):
for k in range(col):
multiplication_result[i][j] += mat1[i][k] * mat2[k][j]
print("\nMultiplication:")
display_matrix(multiplication_result, "Multiplication Result")
else:
print("\nMultiplication not possible due to incompatible dimensions.")
Output:-
Matrix 1:
2 5
9 7
Matrix 2:
5 4
6 8
Addition:
Addition Result:
7 9
15 15
Subtraction:
Subtraction Result:
-3 1
3 -1
Transpose of Matrix 1:
Transpose Result:
2 9
5 7
Multiplication:
Multiplication Result:
40 48
87 92
PR:-4
Title:- 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)SelectionSort
b) Bubble sort and display top five scores
Code:-
def selection_sort(arr):
n = len(arr)
for i in range(n):
min_index = i
for j in range(i + 1, n):
if arr[j] < arr[min_index]:
min_index = j
arr[i], arr[min_index] = arr[min_index], arr[i]
def bubble_sort(arr):
n = len(arr)
for i in range(n - 1):
for j in range(n - i - 1):
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
def top_five_scores(arr):
return arr[-5:][::-1]
selection_sorted = percentages[:]
selection_sort(selection_sorted)
print("\nSorted by Selection Sort:", selection_sorted)
print("Top 5 Scores (Selection Sort):", top_five_scores(selection_sorted))
bubble_sorted = percentages[:]
bubble_sort(bubble_sorted)
print("\nSorted by Bubble Sort:", bubble_sorted)
print("Top 5 Scores (Bubble Sort):", top_five_scores(bubble_sorted))
Output:-
Enter the number of students: 5
Enter percentage of student 1: 45
Enter percentage of student 2: 89
Enter percentage of student 3: 98
Enter percentage of student 4: 85
Enter percentage of student 5: 84
def shell_sort(arr):
n = len(arr)
gap = n // 2
while gap > 0:
for i in range(gap, n):
temp = arr[i]
j=i
while j >= gap and arr[j - gap] > temp:
arr[j] = arr[j - gap]
j -= gap
arr[j] = temp
gap //= 2
def top_five_scores(arr):
return arr[-5:][::-1]
insertion_sorted = percentages[:]
insertion_sort(insertion_sorted)
print("\nSorted by Insertion Sort:", insertion_sorted)
print("Top 5 Scores (Insertion Sort):", top_five_scores(insertion_sorted))
shell_sorted = percentages[:]
shell_sort(shell_sorted)
print("\nSorted by Shell Sort:", shell_sorted)
print("Top 5 Scores (Shell Sort):", top_five_scores(shell_sorted))
Output:-
Enter the number of students: 5
Enter percentage of student 1: 89
Enter percentage of student 2: 87
Enter percentage of student 3: 85
Enter percentage of student 4: 77
Enter percentage of student 5: 76
per = []
n = int(input("Enter the number of students: "))
for i in range(n):
m = float(input("Enter the student %i percentage: " % (i + 1)))
per.append(m)
quicksort(per, 0, n - 1)
Output:-
Enter the number of students: 5
Enter the student 1 percentage: 98
Enter the student 2 percentage: 78
Enter the student 3 percentage: 65
Enter the student 4 percentage: 87
Enter the student 5 percentage: 75
Sorted array is:
65.0
75.0
78.0
87.0
98.0
Top 5 scores are:
98.0
87.0
78.0
75.0
65.0