Incrementing letters

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael

    Incrementing letters

    Hi,
    I've got a string s, and i want to shift all the letters up by one, eg a->b,
    b->c ........ z->a
    In c++ i can do this quite simply with

    if(C == 'z') C='a';
    else C++;

    but i can't work out how to do this this in python??

    Regards

    Michael


  • Heiko Wundram

    #2
    Re: Incrementing letters

    Am Freitag, 27. Mai 2005 13:31 schrieb Michael:[color=blue]
    > if(C == 'z') C='a';
    > else C++;[/color]

    if C == "z":
    C = "a"
    else:
    C = chr(ord(C)+1)

    --
    --- Heiko.
    see you at: http://www.stud.mh-hannover.de/~hwundram/wordpress/

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.1 (GNU/Linux)

    iD8DBQBClyLcf0b pgh6uVAMRApeFAJ sG0SdfKvVZzqV/HApjdYpZrs7x8gC eOccN
    bZyo0OUdxVy5be+ U6pHsA5M=
    =RZOy
    -----END PGP SIGNATURE-----

    Comment

    • Wolfram Kraus

      #3
      Re: Incrementing letters

      Heiko Wundram wrote:[color=blue]
      > Am Freitag, 27. Mai 2005 13:31 schrieb Michael:
      >[color=green]
      >>if(C == 'z') C='a';
      >>else C++;[/color]
      >
      >
      > if C == "z":
      > C = "a"
      > else:
      > C = chr(ord(C)+1)
      >[/color]
      According to the OP's problem (with the assumption that only characters
      from a-z are given) he might even try a lil LC:[color=blue][color=green][color=darkred]
      >>> s = "shiftthis"
      >>> ''.join([chr(((ord(x)-ord('a')+1)%26) +ord('a')) for x in s])[/color][/color][/color]
      'tijguuijt'


      HTH,
      Wolfram

      Comment

      • Duncan Booth

        #4
        Re: Incrementing letters

        Michael wrote:
        [color=blue]
        > Hi,
        > I've got a string s, and i want to shift all the letters up by one, eg
        > a->b, b->c ........ z->a
        > In c++ i can do this quite simply with
        >
        > if(C == 'z') C='a';
        > else C++;
        >
        > but i can't work out how to do this this in python??[/color]
        [color=blue][color=green][color=darkred]
        >>> import string
        >>> upone = string.maketran s([/color][/color][/color]
        'abcdefghijklmn opqrstuvwxyzABC DEFGHIJKLMNOPQR STUVWXYZ',
        'bcdefghijklmno pqrstuvwxyzaBCD EFGHIJKLMNOPQRS TUVWXYZA')[color=blue][color=green][color=darkred]
        >>> string.translat e("I've got a string s....", upone)[/color][/color][/color]
        "J'wf hpu b tusjoh t...."[color=blue][color=green][color=darkred]
        >>>[/color][/color][/color]

        Note the difference though: the Python code does what you said you wanted,
        whereas your sample code corrupts punctuation.

        Comment

        • Wolfram Kraus

          #5
          Re: Incrementing letters

          Duncan Booth wrote:[color=blue]
          > Michael wrote:
          >
          >[color=green]
          >>Hi,
          >>I've got a string s, and i want to shift all the letters up by one, eg
          >>a->b, b->c ........ z->a
          >>In c++ i can do this quite simply with
          >>
          >>if(C == 'z') C='a';
          >>else C++;
          >>
          >>but i can't work out how to do this this in python??[/color]
          >
          >[color=green][color=darkred]
          >>>>import string
          >>>>upone = string.maketran s([/color][/color]
          >
          > 'abcdefghijklmn opqrstuvwxyzABC DEFGHIJKLMNOPQR STUVWXYZ',
          > 'bcdefghijklmno pqrstuvwxyzaBCD EFGHIJKLMNOPQRS TUVWXYZA')
          >[color=green][color=darkred]
          >>>>string.tran slate("I've got a string s....", upone)[/color][/color]
          >
          > "J'wf hpu b tusjoh t...."
          >
          >
          > Note the difference though: the Python code does what you said you wanted,
          > whereas your sample code corrupts punctuation.[/color]

          Wow, that's quite nice. You really learn something new every day :-)
          A minor improvement: Use string.ascii_le tters as the first parameter for
          string.maketran s

          Wolfram

          Comment

          • Dan Sommers

            #6
            Re: Incrementing letters

            On Fri, 27 May 2005 16:10:32 +0200,
            Wolfram Kraus <kraus@hagen-partner.de> wrote:
            [color=blue]
            > Duncan Booth wrote:[/color]
            [color=blue][color=green][color=darkred]
            >>>>> import string
            >>>>> upone = string.maketran s([/color]
            >> 'abcdefghijklmn opqrstuvwxyzABC DEFGHIJKLMNOPQR STUVWXYZ',
            >> 'bcdefghijklmno pqrstuvwxyzaBCD EFGHIJKLMNOPQRS TUVWXYZA')
            >>[color=darkred]
            >>>>> string.translat e("I've got a string s....", upone)[/color][/color][/color]
            [color=blue][color=green]
            >> "J'wf hpu b tusjoh t...."[/color][/color]
            [color=blue][color=green]
            >> Note the difference though: the Python code does what you said you
            >> wanted, whereas your sample code corrupts punctuation.[/color][/color]
            [color=blue]
            > Wow, that's quite nice. You really learn something new every day :-) A
            > minor improvement: Use string.ascii_le tters as the first parameter for
            > string.maketran s[/color]

            And use string.ascii_le tters[ 1 : ] + string.ascii_le tters[ 0 ] for the
            second parameter to string.maketran s.

            Regards,
            Dan

            --
            Dan Sommers
            <http://www.tombstoneze ro.net/dan/>

            Comment

            • Simon Brunning

              #7
              Re: Incrementing letters

              On 5/27/05, Michael <slick_mick_00@ hotmail.com> wrote:[color=blue]
              > Hi,
              > I've got a string s, and i want to shift all the letters up by one, eg a->b,
              > b->c ........ z->a
              > In c++ i can do this quite simply with
              >
              > if(C == 'z') C='a';
              > else C++;
              >
              > but i can't work out how to do this this in python??[/color]

              Here's one that works on multiple character strings, with carrying.
              Rather silly, really.

              <http://www.brunningonl ine.net/simon/blog/archives/001787.html>

              --
              Cheers,
              Simon B,
              simon@brunningo nline.net,

              Comment

              • Duncan Booth

                #8
                Re: Incrementing letters

                Dan Sommers wrote:
                [color=blue]
                > Wolfram Kraus <kraus@hagen-partner.de> wrote:
                >[color=green]
                >> Duncan Booth wrote:[/color]
                >[color=green][color=darkred]
                >>>>>> import string
                >>>>>> upone = string.maketran s(
                >>> 'abcdefghijklmn opqrstuvwxyzABC DEFGHIJKLMNOPQR STUVWXYZ',
                >>> 'bcdefghijklmno pqrstuvwxyzaBCD EFGHIJKLMNOPQRS TUVWXYZA')
                >>>
                >>>>>> string.translat e("I've got a string s....", upone)[/color][/color]
                >[color=green][color=darkred]
                >>> "J'wf hpu b tusjoh t...."[/color][/color]
                >[color=green][color=darkred]
                >>> Note the difference though: the Python code does what you said you
                >>> wanted, whereas your sample code corrupts punctuation.[/color][/color]
                >[color=green]
                >> Wow, that's quite nice. You really learn something new every day :-) A
                >> minor improvement: Use string.ascii_le tters as the first parameter for
                >> string.maketran s[/color][/color]

                Yes, my first attempt at responding did that, but I changed it because that
                makes the assumption that string.ascii_le tters is in a specific order (did
                you know that lowercase came first and uppercase second without checking?)
                [color=blue]
                >
                > And use string.ascii_le tters[ 1 : ] + string.ascii_le tters[ 0 ] for the
                > second parameter to string.maketran s.
                >[/color]
                Bzzt. Wrong answer. Look closely at the middle of the string.

                I did have:
                [color=blue][color=green][color=darkred]
                >>> upone = string.maketran s(string.ascii_ letters,[/color][/color][/color]
                'z'+string.asci i_lowercase[:-1] +
                'Z' + string.ascii_up percase[:-1])

                but as I said, that makes too many assumptions for my liking about the
                contents of those variables.

                Comment

                • Rocco Moretti

                  #9
                  Re: Incrementing letters

                  Dan Sommers wrote:[color=blue]
                  > On Fri, 27 May 2005 16:10:32 +0200,
                  > Wolfram Kraus <kraus@hagen-partner.de> wrote:
                  >
                  >[color=green]
                  >>Duncan Booth wrote:[/color]
                  >
                  >[color=green][color=darkred]
                  >>>>>>import string
                  >>>>>>upone = string.maketran s(
                  >>>
                  >>>'abcdefghijk lmnopqrstuvwxyz ABCDEFGHIJKLMNO PQRSTUVWXYZ',
                  >>>'bcdefghijkl mnopqrstuvwxyza BCDEFGHIJKLMNOP QRSTUVWXYZA')
                  >>>
                  >>>
                  >>>>>>string.tr anslate("I've got a string s....", upone)[/color][/color]
                  >
                  >[color=green]
                  >>Wow, that's quite nice. You really learn something new every day :-) A
                  >>minor improvement: Use string.ascii_le tters as the first parameter for
                  >>string.maketr ans[/color]
                  >
                  >
                  > And use string.ascii_le tters[ 1 : ] + string.ascii_le tters[ 0 ] for the
                  > second parameter to string.maketran s.
                  >[/color]

                  Not quite:
                  [color=blue][color=green][color=darkred]
                  >>> string.ascii_le tters[ 1 : ] + string.ascii_le tters[ 0 ][/color][/color][/color]
                  'bcdefghijklmno pqrstuvwxyzABCD EFGHIJKLMNOPQRS TUVWXYZa'

                  i.e. you get 'z' -> 'A' and 'Z' -> 'a'

                  Another issue is locale settings and special characters - what should be
                  done with accented letters? Preserve letter, or shift and loose accent?

                  Comment

                  • Dan Sommers

                    #10
                    Re: Incrementing letters

                    On 27 May 2005 10:52:36 -0400,
                    Dan Sommers <me@privacy.net > wrote:
                    [color=blue]
                    > And use string.ascii_le tters[ 1 : ] + string.ascii_le tters[ 0 ] for the
                    > second parameter to string.maketran s.[/color]

                    Oops. Thank you Duncan and Rocco for correcting my mistake.

                    Regards,
                    Dan

                    --
                    Dan Sommers
                    <http://www.tombstoneze ro.net/dan/>

                    Comment

                    Working...