Chinaunix首页 | 论坛 | 博客
  • 博客访问: 312674
  • 博文数量: 84
  • 博客积分: 6010
  • 博客等级: 准将
  • 技术积分: 2100
  • 用 户 组: 普通用户
  • 注册时间: 2006-09-25 14:02
文章分类

全部博文(84)

文章存档

2011年(10)

2010年(22)

2009年(17)

2008年(35)

我的朋友
最近访客

分类: 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'

Oops, no reverse function there.

>>> 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. :(


Wow, I made it. String reverse in few minutes.
But that hack job is surely not scalable. So just as an exercise, I decided to improve the it.
At first, I want to do a half loop of the characters and swap the character in mirror image.
But Python string is immutable, I didn't know how to create an empty list with predetermined length (it's supposed to be done like this: s = [0] * 10).

Then I stumbled upon an article about . It says that using list comprehension is the fastest. Interesting. Let's apply the method there. I came up with these:
  • ''.join([s[len(s) - 1 - i] for i in range(len(s))])
  • ''.join([s[i] for i in range(len(s) - 1, -1, -1)])
And finally, after reading about , I came up with this.
  • ''.join([x for x in reversed(s)])
While writing about this, I found . I found this gem there:
  • s[::-1]
Wow, that's cool and I feel very newbie. But at least my method is supposed to be faster (maybe) and still in one line. :)

To conclude, Python seems to have good language features. Imagine how string reverse is implemented in Java. One line? I don't think so.
阅读(936) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:转载:Linux下的调试工具

给主人留下些什么吧!~~