Sunday, February 25, 2018

Python program : String manipulations

The possible grades are A, AB, B, BC, C, CD, D with corresponding grade points 10, 9, 8, 7, 6, 5 and 4. The grade point average of a student is the sum of his/her grade points divided by the number of courses. For instance, if a student has taken two courses with grades A and C, the grade point average is 8 = (10+6)÷2. If a student has not completed any courses, the grade point average is defined to be 0.
You may assume that the data is internally consistent. For every grade, there is a corresponding course code and roll number in the input data.
Each section of the input starts with a line containing a single keyword. The first section begins with a line containing Courses. The second section begins with a line containing Students. The third section begins with a line containing Grades. The end of the input is marked by a line containing EndOfInput.
Write a Python program to read the data as described above and print out a line listing the grade point average for each student in the following format:
Roll Number~Full Name~Grade Point Average
Your output should be sorted by Roll Number. The grade point average should be rounded off to 2 digits after the decimal point. Use the built-in function round().
Here is a sample input and its corresponding output.
Sample Input
Courses
TRAN~Transfiguration~1~2011-2012~Minerva McGonagall
CHAR~Charms~1~2011-2012~Filius Flitwick
Students
SLY2301~Hannah Abbott
SLY2302~Euan Abercrombie
SLY2303~Stewart Ackerley
SLY2304~Bertram Aubrey
SLY2305~Avery
SLY2306~Malcolm Baddock
SLY2307~Marcus Belby
SLY2308~Katie Bell
SLY2309~Sirius Orion Black
Grades
TRAN~1~2011-2012~SLY2301~AB
TRAN~1~2011-2012~SLY2302~B
TRAN~1~2011-2012~SLY2303~B
TRAN~1~2011-2012~SLY2305~A
TRAN~1~2011-2012~SLY2306~BC
TRAN~1~2011-2012~SLY2308~A
TRAN~1~2011-2012~SLY2309~AB
CHAR~1~2011-2012~SLY2301~A
CHAR~1~2011-2012~SLY2302~BC
CHAR~1~2011-2012~SLY2303~B
CHAR~1~2011-2012~SLY2305~BC
CHAR~1~2011-2012~SLY2306~C
CHAR~1~2011-2012~SLY2307~B
CHAR~1~2011-2012~SLY2308~AB
EndOfInput
Sample Output
SLY2301~Hannah Abbott~9.5
SLY2302~Euan Abercrombie~7.5
SLY2303~Stewart Ackerley~8.0
SLY2304~Bertram Aubrey~0
SLY2305~Avery~8.5
SLY2306~Malcolm Baddock~6.5
SLY2307~Marcus Belby~8.0
SLY2308~Katie Bell~9.5
SLY2309~Sirius Orion Black~9.0
 
def gpa(l):
  n=len(l)
  m=0
  M=0
  for i in range(0,n):
    if l[i]=='A':
      m=10
    elif l[i]=='AB':
      m=9
    elif l[i]=='B':
      m=8
    elif l[i]=='BC':
      m=7
    elif l[i]=='C':
      m=6
    elif l[i]=='CD':
      m=5
    elif l[i]=='D':
      m=4
    elif l[i]=='E':
      return(0)
    M += m
    #M_round=round(M ,2)
  gpa = (M/n)
  return(round(gpa,2))


n=None
c=''
s=''
g=''
gl=''
while(n!='EndOfInput'):      
  n=input()
  c += n +'\n'
  if (n=='Students'):
    while(n!='Grades'):
      n=input()
      s += n +'~'+'\n'
  if (n=='Grades'):
    while(n!='EndOfInput'):
      n=input()
      g += n + '\n'
      gl += n
      

#print(c)
#print(s)
#print(g)
G=g[ :-12]
#print(gl)
#print(G)
S=s[ :-8]
gs=G.split('\n')
#print(G,end='')
#print(gs)
#print(S,end='')
stu=S.split('\n')
#print(stu)
stud=stu[ : -1]
#print(stud)
stut=[]
for a in range(0,len(stud)):
  stut += [stud[a].split('~')]
#print(stut)

sorted_stut= sorted(stut)
#print(sorted_stut)


for p in range(0,len(stut)):
  if gl.find(stut[p][0])== -1:
    gs += ['t~t~t~' + str(stut[p][0])+'~E']

#print(gs)

gsl=[]
for i in range(0,len(gs)):
  gsl += [gs[i].split('~')]
  
#print(gsl)
gslm=[]
for j in range(0,len(gsl)):
  gslm += [{gsl[j][3]:gsl[j][4]}]
#print(gslm)

r={}
for d in gslm:
  for k,v in d.items():
    r.setdefault(k,[]).append(v)

#print(r)
R={}
R=sorted(r.items())
#print(R)
grades=[]
for z in range(0,len(R)):
  grades += [R[z][1]]

#print(grades)


glist=[]
for x in range(0,len(grades)):
  glist += [gpa(grades[x])]
#print(glist)

outputlist=[]
for x in range(0,len(R)):
  outputlist += [[sorted_stut[x][0]] +['~']+[sorted_stut[x][1]]+['~']+ [str(glist[x])]+['\n']]
#print(outputlist)
for x in range(0,len(outputlist)):
    print(outputlist[x][0]+outputlist[x][1]+outputlist[x][2]+ outputlist[x][3]+outputlist[x][4])
 
Thanks
Happy Programming ! 
 
 

4 comments:

Derivatives stock list at NSE

Complete FNO stock list at NSE. ABB India Ltd ACC Ltd APL Apollo Tubes Ltd AU Small Finance Bank Ltd Aarti Industries Ltd Abbott India Ltd A...