Re: Incrementing letters

2005-05-31 Thread Dan Sommers
On 27 May 2005 10:52:36 -0400,
Dan Sommers [EMAIL PROTECTED] wrote:

 And use string.ascii_letters[ 1 : ] + string.ascii_letters[ 0 ] for the
 second parameter to string.maketrans.

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

Regards,
Dan

-- 
Dan Sommers
http://www.tombstonezero.net/dan/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Incrementing letters

2005-05-27 Thread Heiko Wundram
Am Freitag, 27. Mai 2005 13:31 schrieb Michael:
 if(C == 'z') C='a';
 else C++;

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

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


pgpC3uVPL36vD.pgp
Description: PGP signature
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Incrementing letters

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


HTH,
Wolfram
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Incrementing letters

2005-05-27 Thread Duncan Booth
Michael wrote:

 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??

 import string
 upone = string.maketrans(
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
'bcdefghijklmnopqrstuvwxyzaBCDEFGHIJKLMNOPQRSTUVWXYZA')
 string.translate(I've got a string s, upone)
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.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Incrementing letters

2005-05-27 Thread Wolfram Kraus
Duncan Booth wrote:
 Michael wrote:
 
 
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??
 
 
import string
upone = string.maketrans(
 
 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
 'bcdefghijklmnopqrstuvwxyzaBCDEFGHIJKLMNOPQRSTUVWXYZA')
 
string.translate(I've got a string s, upone)
 
 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.

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

Wolfram
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Incrementing letters

2005-05-27 Thread Simon Brunning
On 5/27/05, Michael [EMAIL PROTECTED] wrote:
 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??

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

http://www.brunningonline.net/simon/blog/archives/001787.html

-- 
Cheers,
Simon B,
[EMAIL PROTECTED],
http://www.brunningonline.net/simon/blog/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Incrementing letters

2005-05-27 Thread Dan Sommers
On Fri, 27 May 2005 16:10:32 +0200,
Wolfram Kraus [EMAIL PROTECTED] wrote:

 Duncan Booth wrote:

 import string
 upone = string.maketrans(
 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
 'bcdefghijklmnopqrstuvwxyzaBCDEFGHIJKLMNOPQRSTUVWXYZA')
 
 string.translate(I've got a string s, upone)

 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.

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

And use string.ascii_letters[ 1 : ] + string.ascii_letters[ 0 ] for the
second parameter to string.maketrans.

Regards,
Dan

-- 
Dan Sommers
http://www.tombstonezero.net/dan/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Incrementing letters

2005-05-27 Thread Duncan Booth
Dan Sommers wrote:

 Wolfram Kraus [EMAIL PROTECTED] wrote:
 
 Duncan Booth wrote:
 
 import string
 upone = string.maketrans(
 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
 'bcdefghijklmnopqrstuvwxyzaBCDEFGHIJKLMNOPQRSTUVWXYZA')
 
 string.translate(I've got a string s, upone)
 
 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.
 
 Wow, that's quite nice. You really learn something new every day :-) A
 minor improvement: Use string.ascii_letters as the first parameter for
 string.maketrans

Yes, my first attempt at responding did that, but I changed it because that 
makes the assumption that string.ascii_letters is in a specific order (did 
you know that lowercase came first and uppercase second without checking?)

 
 And use string.ascii_letters[ 1 : ] + string.ascii_letters[ 0 ] for the
 second parameter to string.maketrans.
 
Bzzt. Wrong answer. Look closely at the middle of the string.

I did have:

 upone = string.maketrans(string.ascii_letters,
'z'+string.ascii_lowercase[:-1] +
'Z' + string.ascii_uppercase[:-1])

but as I said, that makes too many assumptions for my liking about the 
contents of those variables.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Incrementing letters

2005-05-27 Thread Rocco Moretti
Dan Sommers wrote:
 On Fri, 27 May 2005 16:10:32 +0200,
 Wolfram Kraus [EMAIL PROTECTED] wrote:
 
 
Duncan Booth wrote:
 
 
import string
upone = string.maketrans(

'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
'bcdefghijklmnopqrstuvwxyzaBCDEFGHIJKLMNOPQRSTUVWXYZA')


string.translate(I've got a string s, upone)
  
 
Wow, that's quite nice. You really learn something new every day :-) A
minor improvement: Use string.ascii_letters as the first parameter for
string.maketrans
 
 
 And use string.ascii_letters[ 1 : ] + string.ascii_letters[ 0 ] for the
 second parameter to string.maketrans.
 

Not quite:

  string.ascii_letters[ 1 : ] + string.ascii_letters[ 0 ]
'bcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZa'

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?
-- 
http://mail.python.org/mailman/listinfo/python-list