[Tutor] Python Challange - ord chr incrementing alphabet

Adam adam.jtm30 at gmail.com
Tue Jun 20 15:35:19 CEST 2006


On 20/06/06, Paul D. Kraus <paul.kraus at gmail.com> wrote:
>
> I just started doing the python challenges and was doing the one where the
> hint is 3 letters each shifted two places to the right.
> No big deal just ord / chr and some tests to handle looping past z.
>
> I got my answer. Then reading the solutions i see that they suggest that
> the best way would be to use string.maketrans()
> I just can't figure this out. What does this do and how would it help with
> this problem?
>
> Paul
>

Here's what I came up with for that challenge:
def pyc_map():#1
    import string
    t = string.maketrans("abcdefghijklmnopqrstuvwxyz",
"cdefghijklmnopqrstuvwxyzab")
    s = "g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc
dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle.
sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj."
    print s.translate(t)
    print 'map'.translate(t)

string.maketrans() translates the letters with the same index so a=c b=d
etc. s.translate needs a translation table which is what string.maketrans()
returns so you just use s.translate with t the translation table and it
gives you the proper translation.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20060620/5c1e011c/attachment.htm 


More information about the Tutor mailing list