Wednesday, April 22, 2009

Playing with Dictionary

This post is to develop a ranklist. Using python it is very simple. Its a mere ten to fifteen lines of code.


At the terminal. Give this command.
$python test4.py > test4

The code is given below.

#! /usr/bin/python
f = file('test2')
ranklist = []
while(True):
c = f.readline()
if(c):
d = c.split('')

marks = d[-1].split('')[-1].split()[0]
try:
marks = int(d[-1].split('')[-1].split()[0])
name = d[0].split('')[1].split('<')[0]
percentage=round((float(marks)/9),2)
usn=name.split()[-1][1:-1]
cusn=usn[-3:]
f.readline()
ranklist.append([marks,name,percentage,cusn])
except:
pass
else :
break

ranklist.sort(reverse=True)
print "RankName(USN)MarksPercentage"

for i in range(len(ranklist)):

print ""+str(i+1)+""+str(ranklist[i][0])+""+str(ranklist[i][2])+""

print ""

NOTE: The formatting may not be proper. Indentation is very important in python. I have attached those files. You can download them .
P.S: Thanks to subbu who assisted me.

No comments:

Post a Comment