SE Computer Engineering Practical 14 solution

Home » All Blogs » Python » Python assignments » SE Computer Engineering » SE Computer Engineering Practical 14 solution

SE Computer Engineering Practical 14 solution

Question:

Write a python program to store 12’th class percentage of students in array.
Write function for sorting array of floating point numbers in ascending order using bucket sort and display top five scores

Code:

def input_percentage():
   perc = []
   number_of_students = int(input("Enter the number of Students : "))
   for i in range(number_of_students):
       perc.append(float(input("Enter the percentage of Student {0} : ".format(i + 1))))
   return perc


# Function for printing the percentage of the Students

def print_percentage(perc):
   for i in range(len(perc)):
       print(perc[i], sep="\n")

# Function for performing bucket Sort on the Data


def Bucket_sort(input_list):
   # Find maximum value and length of the input list to determine which value in the list goes into which bucket
   largest = max(input_list)
   length = len(input_list)
   size = largest / length

   # Create n empty buckets where n is equal to the length of the input list
   buckets = [[] for _ in range(length)]

   # Put list elements into different buckets based on the index
   for i in range(length):
       j = int(input_list[i] / size)

       if j != length:
           buckets[j].append(input_list[i])
       else:
           buckets[length - 1].append(input_list[i])

   # Sort elements within the buckets using Insertion sort
   for i in range(length):
       insertionSort(buckets[i])

   # Concatenate buckets with sorted elements into a single list
   final_output = []
   for i in range(length):
       final_output = final_output + buckets[i]

   return final_output

   # Performs insertion sort algorithm on the individual bucket lists


def insertionSort(bucket_list):
   for i in range(1, len(bucket_list)):
       var = bucket_list[i]
       j = i - 1
       while j >= 0 and var < bucket_list[j]:
           bucket_list[j + 1] = bucket_list[j]
           j = j - 1
       bucket_list[j + 1] = var


# Function for Displaying Top Five Percentages of Students

def display_top_five(perc):
   print("Top Five Percentages are : ")
   if len(perc) < 5:
       start, stop = len(perc) - 1, -1
   else:
       start, stop = len(perc) - 1, len(perc) - 6

   for i in range(start, stop, -1):
       print(perc[i], sep="\n")


unsorted_percentage = []
sorted_percentage = []
flag = 1

while flag == 1:
   print("\n--------------------MENU--------------------")
   print("1. Accept the Percentage of Students")
   print("2. Display the Percentages of Students")
   print("3. Perform Bucket Sort on the Data")
   print("4. Exit")

   ch = int(input("Enter your choice (from 1 to 4) : "))

   if ch == 1:
       unsorted_percentage = input_percentage()

   elif ch == 2:
       print_percentage(unsorted_percentage)

   elif ch == 3:
       print("Percentages of 10th Class Students after performing Bucket Sort : ")
       sorted_percentage = Bucket_sort(unsorted_percentage)
       print_percentage(sorted_percentage)
       a = input("Do you want to display the Top 5 Percentages of Students (yes/no) : ")
       if a == 'yes':
           display_top_five(sorted_percentage)

   elif ch == 4:
       print("Thanks for using this program!!")
       flag = 0

   else:
       print("Invalid Choice!!")

Output:

--------------------MENU--------------------
1. Accept the Percentage of Students
2. Display the Percentages of Students
3. Perform Bucket Sort on the Data
4. Exit
Enter your choice (from 1 to 4) : 1
Enter the number of Students : 7
Enter the percentage of Student 1 : 66
Enter the percentage of Student 2 : 70
Enter the percentage of Student 3 : 50
Enter the percentage of Student 4 : 81
Enter the percentage of Student 5 : 94
Enter the percentage of Student 6 : 38
Enter the percentage of Student 7 : 69

--------------------MENU--------------------
1. Accept the Percentage of Students
2. Display the Percentages of Students
3. Perform Bucket Sort on the Data
4. Exit
Enter your choice (from 1 to 4) : 2
66.0
70.0
50.0
81.0
94.0
38.0
69.0

--------------------MENU--------------------
1. Accept the Percentage of Students
2. Display the Percentages of Students
3. Perform Bucket Sort on the Data
4. Exit
Enter your choice (from 1 to 4) : 3
Percentages of 10th Class Students after performing Bucket Sort : 
38.0
50.0
66.0
69.0
70.0
81.0
94.0
Do you want to display the Top 5 Percentages of Students (yes/no) : yes
Top Five Percentages are : 
94.0
81.0
70.0
69.0
66.0

--------------------MENU--------------------
1. Accept the Percentage of Students
2. Display the Percentages of Students
3. Perform Bucket Sort on the Data
4. Exit
Enter your choice (from 1 to 4) : 4
Thanks for using this program!!

Process finished with exit code 0
Tech Amplifier Final Logo