Python中的平原。
如何在Python中实现Plaindromes。
# 回答1
回文是一个短语、一个单词或一个前后读相同的序列。一个这样的例子就是pip!护士跑步就是这样一个例子。
选择 | 换行 | 行号
- >>> def isPalindrome(string):
- left,right=0,len(string)-1
- while right>=left:
- if not string[left]==string[right]:
- return False
- left+=1;right-=1
- return True
- <span style="font-weight: 400">>>> isPalindrome('redrum murder')</span>
- True
- >>> isPalindrome('CC.')
- False
- Well, there are other ways to do this too. Lets try using an iterator.
- >>> def isPalindrome(string):
- left,right=iter(string),iter(string[::-1])
- i=0
- while i<len(string)/2:
- if next(left)!=next(right):
- return False
- i+=1
- return True
- >>> isPalindrome('redrum murder')
- True
- >>> isPalindrome('CC.')
- False
- >>> isPalindrome('CCC.')
- False
- >>> isPalindrome('CCC')
- True
标签: python