[Tutor] bug in exam score conversion program
Steve Willoughby
steve at alchemy.com
Sat Oct 4 22:27:17 CEST 2008
Dragos Ionescu wrote:
> ---- Original Message ----
> From: Steve Willoughby <steve at alchemy.com>
> To: Dragos Ionescu <idragos at ymail.com>
> Cc: bob gailer <bgailer at gmail.com>; David <ldl08 at gmx.net>; tutor at python.org
> Sent: Saturday, October 4, 2008 11:04:30 PM
> Subject: Re: [Tutor] bug in exam score conversion program
>
> Dragos Ionescu wrote:
> > ----- Original Message ----
> > From: bob gailer <bgailer at gmail.com <mailto:bgailer at gmail.com>>
> > To: David <ldl08 at gmx.net <mailto:ldl08 at gmx.net>>
> > Cc: tutor at python.org <mailto:tutor at python.org>
> > Sent: Saturday, October 4, 2008 10:15:10 PM
> > Subject: Re: [Tutor] bug in exam score conversion program
> >
> > Lots of good responses. And now for something completely different:
> >
> > import string
> > x = string.maketrans('567891', 'FDCBAA')
> > score = raw_input('score>')
> > print "Your grade is:", score[0].translate(x)
> > --
> > Bob Gailer
> > Chapel Hill NC
> > 919-636-4239
> >
> > When we take the time to be aware of our feelings and
> > needs we have more satisfying interatctions with others.
> >
> > Nonviolent Communication provides tools for this awareness.
> >
> > As a coach and trainer I can assist you in learning this process.
> >
> > What is YOUR biggest relationship challenge?
> >
> > _______________________________________________
> > Tutor maillist - Tutor at python.org <mailto:Tutor at python.org>
> <mailto:Tutor at python.org <mailto:Tutor at python.org>>
> > http://mail.python.org/mailman/listinfo/tutor
> >
> >
> > Wow! Bob Gailer's solution is so elegant. Can someone plese explain what
> > is the algorithm behind string.maketrans. More exactly, how is this
> > function doing the coding?
>
> Actually, I don't think the point was to be elegant as much
> as to get you thinking about something you might not have
> explored--never hurts to keep learning new features so you
> don't inefficiently apply the same old small set of things
> to new problems.
>
> You wouldn't *really* want to implement a production grade
> system like that, cute though it is. This is setting up a
> translation table mapping the first character in the score
> to a letter grade. So a 9 is changed to an A. The obvious
> problem though is how it handles a score of, say, "1". Or,
> for that matter, "37".
>
>
> I know how string.maketrans works. I was wondering how to implement such
> a function. Would that be very hard? I must admit that I was 'surprised'
> when I printed x...
How to implement... the equivalent of maketrans/translate? Pretty
easy really. maketrans just builds a 256-byte table showing a mapping
from one character set to another (compare perl's y/// or tr///). Once
you have that translation table, all you really need to do is take each
character of a string and make a new string by looking up each source
character and returning what the table says (effectively table[ord(i)]
for each character i in the source string). Which is pretty much
what string.translate() is doing.
or did I misunderstand which function you wanted to implement?
More information about the Tutor
mailing list