On Tue, Mar 12, 2013 at 9:00 AM, RJ Ewing <[email protected]> wrote:
> Thank you all for the help. I really appreciated the suggestions. Some of
> the things you pointed out, I originally used, but started changing thing
> when it wasn't working. I got it to work, but if you could let me know if
> there is anything I should do to make this code more pythonesque that would
> be great.
Two suggestions:
1. Since rot_text itself doesn't use any of the state of the object,
it makes sense to treat it as a plain function.
2. Explicit array mutation here can be avoided here; it turns out
that writing it without mutation is a little easier to read, and is
less bug prone.
Here's what your code looks like with these two suggestions:
##################################
## rot1: char -> char
## Rotates a single character.
def rot1(char):
if char.isupper() or char.islower():
test = 'M' if char.isupper() else 'm'
if char <= test:
return chr(ord(char) + 13)
else:
return chr(ord(char) - 13)
## rot_string: string -> string
## Rotates all the characters.
def rot_string(s):
return "".join([rot1(ch) for ch in s])
##################################
I've refactored the heart of the rotation logic into rot1(). Note
that it doesn't care about arrays or not: its own concern is the
rotation of a single character.
Good luck!
_______________________________________________
Tutor maillist - [email protected]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor