Sunday 20 August 2017

INLAB 4

******************************************************************************************
Q.)
A team of experts formed by Govt. of India conducted a survey on colleges in India. Let us assume that the survey was conducted in ‘n’ number of institutes. The experts were asked to rank the institutes based on three different metrics. The metrics are facilities, academics and infrastructure. Maximum score in each category is as follows.
Facilities = 25
Academics = 50
Infrastructure = 25
At the end of the survey the scores of the individual metrics are added up to get the total score and the institutes are ranked based on the total score. The institute that scores the highest score is ranked 1st. Next highest score is given the rank 2 and so on. Write a program to read the scores of the three metrics for each institute, store the scores in a list. Make a list of individual score list for 10 institutes. Print only the Total score in the sorted (Descending) order. Use insertion sort for arranging the data in descending order.
Input:
Read 'n' - the number of institutes
For all institutes, :
1) Read Facilities 
 scores
2) Read Academics scores
3) Read Infrastructure scores

Output:
List of total scores in the descending order

Code:
**(The # ones are the comments __ No need to include those.)** 


n=int(input()) #number of institutes
score_fac=[] #list of scores for Facilities
score_acad=[] #list of scores for Academics
score_infra=[] #list of scores for Infrastructure
score_total=[] #list of total scores of an institute
for i in range(n):
a=int(input()) #score for Facilities
b=int(input()) #score for Academics
c=int(input()) #score for Infrastructure
if(0<a<=25 and 0<b<=50 and 0<c<=25): #checking for the boundary condition
score_fac.append(a)
score_acad.append(b)
score_infra.append(c)
score_total.append(a+b+c)
else:
print('Invalid input')


#Function for Inserion Sort:
def insertionSort(arr):
    for i in range(1, len(arr)):
        key = arr[i]
        j = i-1
        while j >=0 and key < arr[j] :
                arr[j+1] = arr[j]
                j -= 1
        arr[j+1] = key
    return arr
    
            
score_total=insertionSort(score_total) #list of total scores in increasing order
score_total_final=[] #list of total scores in descending order
for i in range(n):
score_total_final.append(score_total[n-i-1])

print(score_total_final)


Procedure:
def insertionSort(arr):
    for i in range(1, len(arr)):
        key = arr[i]
        j = i-1
        while j >=0 and key < arr[j] :
                arr[j+1] = arr[j]
                j -= 1
        arr[j+1] = key
    return arr


Pseudo-Code:
1) Read 'n' - the number of institutes
2) For all institutes, :
        Read Facilities scores
       Read Academics scores
       Read Infrastructure scores
3) Insert the scores in separate lists
4) Make a list of total scores
5) Sort the list of total scores
6) Reverse the above list and print it.

******************************************************************************************
**(The # ones are the comments __ No need to include those.)**
******************************************************************************************

7 comments:

  1. Can we use sort and reverse functions for this?

    ReplyDelete
  2. Yes.But sort() cant be used as it uses bubble sort.

    ReplyDelete
  3. I need clarity on how to use sort command.

    ReplyDelete
  4. what is the use of return arr

    ReplyDelete
  5. Short and sweet:
    n=int(input())
    l=[]
    for i in range(n):
    x=int(input())
    y=int(input())
    z=int(input())
    l.append(x+y+z)
    l.sort(reverse=True)
    print(l)

    ReplyDelete
  6. its better to use

    final=sorted(score_total , reverse=True) # After print ("invalid input ")
    print (final)

    ReplyDelete