全部博文(84)
分类: LINUX
2008-04-01 22:57:03
http://codeftw.blogspot.com/2007/04/reverse-string-in-python.html
While reading "" (which is funny, btw), some commenters are getting crazy and started to write in reverse. You know, in esrever. It's easy to write it, just use your left arrow key, but reading it is a pain.
So
after reading one of them and met another few, I thought, "Let's
reverse it using a program. Python will be great!". Besides, any decent
programmer is expected to know this according to the guys.
I'm new to Python, so forgive my lameness.
This is my first try:
C:\>python
Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> import string
>>> string.reverse("?etisoppo eht siht sI. 'tac' fo draeh ev'I ?'cat' s'tahW")
Traceback (most recent call last):
File "", line 1, in
AttributeError: 'module' object has no attribute 'reverse'
>>> string.rev("?etisoppo eht siht sI. 'tac' fo draeh ev'I ?'cat' s'tahW")
Traceback (most recent call last):
File "", line 1, in
AttributeError: 'module' object has no attribute 'rev'
>>> import string
>>> def reverse(s):
... res = ""
... for x in s:
... res = x + res
...
>>> def reverse(s)
File "", line 1
def reverse(s)
^
SyntaxError: invalid syntax
>>> def reverse(s):
... res = ""
... for x in s:
... res = x + res
... return res
...
>>> print reverse("?etisoppo eht siht sI. 'tac' fo draeh ev'I ?'cat' s'tahW")
What's 'tac'? I've heard of 'cat' .Is this the opposite?
>>> print reverse("(: .em rof gnipyt-esrever eht od ot 'cat' gnisu ot gnitroser
m'I .ylkciuq sa stsop hcus epyt t'nac I taht si FTW rehto ehT")
The other WTF is that I can't type such posts as quickly. I'm resorting to using
'tac' to do the reverse-typing for me. :(