Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #48921

Re: Simple I/O problem can't get solved

From Peter Otten <__peter__@web.de>
Subject Re: Simple I/O problem can't get solved
Date 2013-06-22 12:11 +0200
Organization None
References <4785de5f-a84d-4417-9709-68bf2f4076e8@googlegroups.com> <kq15i6$80e$1@ger.gmane.org> <CAPTjJmoCTvXRMfU2aiZMM-6UiyHspbgRfMFSqHqr+Xm5nHUE1A@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.3691.1371895849.3114.python-list@python.org> (permalink)

Show all headers | View raw


Chris Angelico wrote:

> On Fri, Jun 21, 2013 at 7:15 PM, Peter Otten <__peter__@web.de> wrote:
>> Combining these modifications:
>>
>> for line in f:
>>     word = line.strip()
>>     if is_palindrome.is_palindrome(word):
>>         print word
> 
> Minor quibble: I wouldn't use the name 'word' here, unless you're
> expecting the file to consist of one-word lines (such as DICTOT.DIC
> from old PMMail). For detecting phrase/sentence palindromes, I'd keep
> the name 'line'.

Somehow it didn't occur to me that you might test anything but words.
However, if you want to check sentences (as some unused code in the original 
post suggests) I think you should not strip off the newline and leave the 
necessary cleanup to the test function:

>>> def is_palindrome(s):
...     t = "".join(c for c in s.casefold() if c.isalnum())
...     return t == t[::-1]
... 
>>> is_palindrome("A man, a plan, a canal - Panama!\n")
True

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Simple I/O problem can't get solved nickgan.sps@windowslive.com - 2013-06-21 01:57 -0700
  Re: Simple I/O problem can't get solved Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-06-21 12:11 +0300
  Re: Simple I/O problem can't get solved Peter Otten <__peter__@web.de> - 2013-06-21 11:15 +0200
    Re: Simple I/O problem can't get solved nickgan.sps@windowslive.com - 2013-06-21 02:20 -0700
  Re: Simple I/O problem can't get solved Chris Angelico <rosuav@gmail.com> - 2013-06-22 19:07 +1000
  Re: Simple I/O problem can't get solved Peter Otten <__peter__@web.de> - 2013-06-22 12:11 +0200
  Re: Simple I/O problem can't get solved Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-06-22 12:58 -0400

csiph-web