Re: how to insert random error in a programming

Discussion in 'Python' started by Chris Angelico, Oct 15, 2012.

  1. On Tue, Oct 16, 2012 at 6:28 AM, Mark Lawrence <> wrote:
    > I like clearly written code like this
    >
    > "
    > d = {}
    > for c in (65, 97):
    > for i in range(26):
    > d[chr(i+c)] = chr((i+13) % 26 + c)
    >
    > print "".join([d.get(c, c) for c in s])


    Surely there's a shorter way to rot13 a piece of text? CODE GOLF!

    At very least, a single cryptic expression in place of your nice clear
    loops MUST be an improvement.

    d = dict((chr(i+c),chr((i+13)%26+c))for i in range(26)for c in(65,97))

    And with superfluous spaces removed like that, it takes 0.02
    jiggawatts less power in DeLorean Python.

    ChrisA
     
    Chris Angelico, Oct 15, 2012
    #1
    1. Advertising

  2. Chris Angelico

    alex23 Guest

    On Oct 16, 5:52 am, Chris Angelico <> wrote:
    > Surely there's a shorter way to rot13 a piece of text? CODE GOLF!


    In Python2: "a piece of string".encode('rot13') :)

    > At very least, a single cryptic expression in place of your nice clear
    > loops MUST be an improvement.
    >
    > d = dict((chr(i+c),chr((i+13)%26+c))for i in range(26)for c in(65,97))


    Do I get points for explicitness?

    import string as s
    print "a piece of text".translate(
    s.maketrans(
    s.letters,
    s.letters[:26][13:]+s.letters[:26][:13]+s.letters[26:]
    [13:]+s.letters[26:][:13]
    )
    )
     
    alex23, Oct 16, 2012
    #2
    1. Advertising

  3. On Mon, 15 Oct 2012 18:21:55 -0700, alex23 wrote:

    > On Oct 16, 5:52 am, Chris Angelico <> wrote:
    >> Surely there's a shorter way to rot13 a piece of text? CODE GOLF!

    >
    > In Python2: "a piece of string".encode('rot13') :)


    And in Python 3, unfortunately there has been a right-royal mess made of
    the codecs system:

    http://bugs.python.org/issue7475

    So I expect that in Python 3.4 this will work:

    "a piece of string".transform('rot13')

    or this:

    import codecs
    codecs.encode('a piece of string', 'rot13')


    but who knows really?



    --
    Steven
     
    Steven D'Aprano, Oct 16, 2012
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. globalrev
    Replies:
    4
    Views:
    772
    Gabriel Genellina
    Apr 20, 2008
  2. VK
    Replies:
    15
    Views:
    1,176
    Dr J R Stockton
    May 2, 2010
  3. Debashish Saha

    how to insert random error in a programming

    Debashish Saha, Oct 15, 2012, in forum: Python
    Replies:
    4
    Views:
    230
    Roy Smith
    Oct 16, 2012
  4. Chris Angelico
    Replies:
    0
    Views:
    153
    Chris Angelico
    Oct 15, 2012
  5. Jean-Michel Pichavant

    Re: how to insert random error in a programming

    Jean-Michel Pichavant, Oct 15, 2012, in forum: Python
    Replies:
    0
    Views:
    157
    Jean-Michel Pichavant
    Oct 15, 2012
Loading...

Share This Page