SE Computer Engineering Practical 15 solution

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

SE Computer Engineering Practical 15 solution

Question:

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

Code:


# A function to do counting sort of arr[] according to
# the digit represented by exp.
def countingSort(arr, exp1):
   n = len(arr)

   # The output array elements that will have sorted arr
   output = [0] * n

   # initialize count array as 0
   count = [0] * 10

   # Store count of occurrences in count[]
   for i in range(0, n):
       index = (arr[i] / exp1)
       count[int(index % 10)] += 1

   # Change count[i] so that count[i] now contains actual
   # position of this digit in output array
   for i in range(1, 10):
       count[i] += count[i - 1]

   # Build the output array
   i = n - 1
   while i >= 0:
       index = (arr[i] / exp1)
       output[count[int(index % 10)] - 1] = arr[i]
       count[int(index % 10)] -= 1
       i -= 1

   # Copying the output array to arr[],
   # so that arr now contains sorted numbers
   i = 0
   for i in range(0, len(arr)):
       arr[i] = output[i]


# Method to do Radix Sort
def radixSort(arr):
   # Find the maximum number to know number of digits
   max1 = max(arr)

   # Do counting sort for every digit. Note that instead
   # of passing digit number, exp is passed. exp is 10^i
   # where i is current digit number
   exp = 1
   while max1 // exp > 0:
       countingSort(arr, exp)
       exp *= 10


# Driver code to test above
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))))

print('The Percentages of Students:\n', perc)

radixSort(perc)

print('After Performing radix sort:\n', 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")

Output:

Enter the number of Students : 7
Enter the percentage of Student 1 : 77
Enter the percentage of Student 2 : 54
Enter the percentage of Student 3 : 76
Enter the percentage of Student 4 : 87
Enter the percentage of Student 5 : 34
Enter the percentage of Student 6 : 63
Enter the percentage of Student 7 : 45
The Percentages of Students:
 [77.0, 54.0, 76.0, 87.0, 34.0, 63.0, 45.0]
After Performing radix sort:
 [34.0, 45.0, 54.0, 63.0, 76.0, 77.0, 87.0]
Top Five Percentages are : 
87.0
77.0
76.0
63.0
54.0

Process finished with exit code 0
Tech Amplifier Final Logo