Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #48848 > unrolled thread
| Started by | nickgan.sps@windowslive.com |
|---|---|
| First post | 2013-06-21 01:57 -0700 |
| Last post | 2013-06-22 12:58 -0400 |
| Articles | 7 — 5 participants |
Back to article view | Back to comp.lang.python
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
| From | nickgan.sps@windowslive.com |
|---|---|
| Date | 2013-06-21 01:57 -0700 |
| Subject | Simple I/O problem can't get solved |
| Message-ID | <4785de5f-a84d-4417-9709-68bf2f4076e8@googlegroups.com> |
Greetings to everybody,
I have recently started to solve my first Python problems but I came across to this:
Write a version of a palindrome recogniser that accepts a file name from the user, reads each line, and prints the line to the screen if it is a palindrome.(by http://www.ling.gu.se/~lager/python_exercises.html)
I think I coded it correctly but still I get no output. I do not have any errors I just do not get any output.
This is the code:
***
import is_palindrome
filename = raw_input('Enter a file: ') # A text file
f = open(filename)
while True:
line = f.readline()
if len(line) == 0:
break
elif is_palindrome.is_palindrome(line):
print line,
f.close()
***
I have coded the is_palindrome module before and it works very well.
Here is this module's code:
***
def reverse(text):
return text[::-1]
def replace_all(text, dic):
for i, j in dic.iteritems():
text = text.replace(i, j)
return text
def is_palindrome(text):
return text == reverse(text)
points = {'.':'', ',':'', '!':'', ';':'', '?':'', '/':'', '\\':''}
something = raw_input('Enter text: ').lower().replace(' ', '')
replace = replace_all(something, points)
if is_palindrome(replace):
print 'Yes, it is a palindrome:', reverse(replace)
else:
print 'No, it is not a palindrome:', reverse(replace)
***
What should I do to output only the palindrome lines from the text file?
[toc] | [next] | [standalone]
| From | Jussi Piitulainen <jpiitula@ling.helsinki.fi> |
|---|---|
| Date | 2013-06-21 12:11 +0300 |
| Message-ID | <qotwqpn3k1h.fsf@ruuvi.it.helsinki.fi> |
| In reply to | #48848 |
nickgan.sps@windowslive.com writes:
> I think I coded it correctly but still I get no output. I do not
> have any errors I just do not get any output.
>
> This is the code:
>
> ***
> import is_palindrome
>
> filename = raw_input('Enter a file: ') # A text file
> f = open(filename)
> while True:
> line = f.readline()
> if len(line) == 0:
> break
> elif is_palindrome.is_palindrome(line):
> print line,
> f.close()
There's a newline at the end of each input line. Strip that. (It might
be another sequence that means end-of-line in your system. It can
still be stripped as trailing whitespace. Strings have a method for
stripping trailing whitespace.)
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2013-06-21 11:15 +0200 |
| Message-ID | <mailman.3656.1371806106.3114.python-list@python.org> |
| In reply to | #48848 |
nickgan.sps@windowslive.com wrote:
> I have recently started to solve my first Python problems but I came
> across to this:
> Write a version of a palindrome recogniser that accepts a file name from
> the user, reads each line, and prints the line to the screen if it is a
> palindrome.(by http://www.ling.gu.se/~lager/python_exercises.html)
>
> I think I coded it correctly but still I get no output. I do not have any
> errors I just do not get any output.
>
> This is the code:
> import is_palindrome
>
> filename = raw_input('Enter a file: ') # A text file
> f = open(filename)
> while True:
> line = f.readline()
The line includes the trailing newline character which breaks the symmetry:
>>> "abba" == "abba"[::-1]
True
>>> "abba\n" == "abba\n"[::-1]
False
Use
word = line.strip()
to remove all leading and trailing whitespace.
> if len(line) == 0:
> break
> elif is_palindrome.is_palindrome(line):
> print line,
> f.close()
Note that python allows you to iterate over the lines of a file with
for line in f:
...
Combining these modifications:
for line in f:
word = line.strip()
if is_palindrome.is_palindrome(word):
print word
[toc] | [prev] | [next] | [standalone]
| From | nickgan.sps@windowslive.com |
|---|---|
| Date | 2013-06-21 02:20 -0700 |
| Message-ID | <5aef90cd-fcf2-4b91-aeaa-edbc04135b2b@googlegroups.com> |
| In reply to | #48851 |
Τη Παρασκευή, 21 Ιουνίου 2013 12:15:59 μ.μ. UTC+3, ο χρήστης Peter Otten έγραψε:
> nickgan.sps@windowslive.com wrote:
>
>
>
> > I have recently started to solve my first Python problems but I came
>
> > across to this:
>
> > Write a version of a palindrome recogniser that accepts a file name from
>
> > the user, reads each line, and prints the line to the screen if it is a
>
> > palindrome.(by http://www.ling.gu.se/~lager/python_exercises.html)
>
> >
>
> > I think I coded it correctly but still I get no output. I do not have any
>
> > errors I just do not get any output.
>
> >
>
> > This is the code:
>
>
>
> > import is_palindrome
>
> >
>
> > filename = raw_input('Enter a file: ') # A text file
>
> > f = open(filename)
>
> > while True:
>
> > line = f.readline()
>
>
>
> The line includes the trailing newline character which breaks the symmetry:
>
>
>
> >>> "abba" == "abba"[::-1]
>
> True
>
> >>> "abba\n" == "abba\n"[::-1]
>
> False
>
>
>
> Use
>
>
>
> word = line.strip()
>
>
>
> to remove all leading and trailing whitespace.
>
>
>
> > if len(line) == 0:
>
> > break
>
> > elif is_palindrome.is_palindrome(line):
>
> > print line,
>
> > f.close()
>
>
>
> Note that python allows you to iterate over the lines of a file with
>
>
>
> for line in f:
>
> ...
>
>
>
> Combining these modifications:
>
>
>
> for line in f:
>
> word = line.strip()
>
> if is_palindrome.is_palindrome(word):
>
> print word
Thank you a lot it worked!
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-06-22 19:07 +1000 |
| Message-ID | <mailman.3690.1371892081.3114.python-list@python.org> |
| In reply to | #48848 |
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'. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2013-06-22 12:11 +0200 |
| Message-ID | <mailman.3691.1371895849.3114.python-list@python.org> |
| In reply to | #48848 |
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
[toc] | [prev] | [next] | [standalone]
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
|---|---|
| Date | 2013-06-22 12:58 -0400 |
| Message-ID | <mailman.3700.1371920311.3114.python-list@python.org> |
| In reply to | #48848 |
On Sat, 22 Jun 2013 19:07:59 +1000, Chris Angelico <rosuav@gmail.com>
declaimed the following:
>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'.
>
There's also the matter of whether one is looking for strict
palindromes -- where spacing is included (but not letter case)
Able was I ere I saw Elba
or a looser definition where only the letters are involved in the
determination (again ignore case and punctuation, but also ignore the
spaces between words)
Madam I'm Adam
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web