[Tutor] Finding palindromes in a list

Dave Angel d at davea.name
Sat Jun 9 04:46:53 CEST 2012


On 06/08/2012 10:17 PM, Mike Nickey wrote:
> On Fri, Jun 8, 2012 at 7:03 PM, Dave Angel <d at davea.name> wrote:
>> On 06/08/2012 09:46 PM, Mike Nickey wrote:
>>> Thanks guys,
>>>
This time you forgot to include the list in your posting.  Easiest way
is to do Reply-all.  But you've done it right several times, so this was
probably just a slip of the fingers.

>>  <SNIP>
>> Where's a sample input?  What's the file supposed to look like? One word
>> per line, or multiple ones?
> 
> I've attached a sample file that I am using as a test. I hope that in
> replying below each area will help restore some context.

And I've pasted the first few lines of that sample into the message,
since many people can't see attachments.


{\rtf1\ansi\ansicpg1252\cocoartf1138\cocoasubrtf320
{\fonttbl\f0\fswiss\fcharset0 Helvetica;\f1\froman\fcharset0 Times-Roman;}
{\colortbl;\red255\green255\blue255;}
\margl1440\margr1440\vieww20780\viewh14200\viewkind0
\deftab720
\pard\pardeftab720

>>>
>>> CODE:
>>> def getFile():
>>>     fname = raw_input('Enter location and filename: ')
>>>     print
>>>
>>> #attempt the open the file for reading
>>>     try:
>>>         fobj = open(fname, 'r')
>>>     except IOError, e:
>>>         print "*** file open error:", e
>>>     else:
>>>     #display contents to the screen
>>>         for eachLine in fobj:
>>>             # create list here
>>>             eachLine.strip().split()
>>
>> Why don't you do something with the results of these operations.  As
>> they stand, they do nothing useful.
>>
> This is simply to populate a list from a file. From what I was taught,
> and there may be a differing opinion here, each def is supposed to do
> one thing. This definition is supposed to populate a list from a text
> file.

I was referring to a specific source line   "eachLine.strip().split()"
You don't save the output of this expression,  Chances are you intended to.

> 
>>>             wordList.append(eachLine)
>>
>> So wordList is a list of the raw lines from the file, complete with
>> newline on each line.  If that's what you want, call it linelist.
> 
> Noted and adopted. I have changed the list to be named lineList.
>>
>>>         fobj.close()
>>>
>>> def compareElements(wordList):
>>>     for item in wordList:
>>>         if item == item[::-1]:          #wordList[item] ==
>>> wordList[item].reverse():
>>>             print item
>>>             palindromes.append(item)
>>>
>>> wordList = []
>>> palindromes = []
>>> getFile()
>>> print "\nPrinting new Word list!!!"
>>> print wordList[:]
>>
>> Did this display what you wanted?  If not, fix that before trying to
>> figure out the compareElements part.  Hint, you probably wanted WORDs in
>> your wordlist, not lines.
>>
> It did however they are not really words but variable strings of
> characters separated by a semicolon.  


Is the semi-colon relevant?  Did you maybe mean to separate the line
based on those semicolons?  And perhaps remove the newline at the end of
each line?  That is approximately what you might have intended with the
line that had strip and split in it.  So maybe you should correct the
line, and save its output.
And it's output might be what you want to combine into the wordList.  Or
something.


> 
>>> print "\nComparing Word List!!!"
>>> compareElements(wordList)
>>> print "\nPrinting palindromes"
>>> print palindromes[:]
>>> print "\nDone!!!"
>>>
>>>
>>
>>
>> --
>>
>> DaveA
> 
> 
> 

none of the lines in your sample data come close to looking like a
palindrome.  Maybe you should build an artificial file that has some
lines that should trigger, and some that should not.

-- 

DaveA


More information about the Tutor mailing list