[Tutor] trouble with function-- trying to check differences btwn 2 strings

2007-03-05 Thread zannah marsh

I am very new to Python (I've been learning it for about 2 weeks, in a
class). I wrote this function ( as part of a larger program, for my
homework) which is supposed to step through two given strings and check for
differences between the strings. It returns a list of the indices at which
the characters are different. Here it is:

def getDiff(word1,word2):
   #this function takes two words and returns a list containing the index
of each point of difference in the words
   difList = [] #creates empty list to hold index (location) of differences
in words
   index = 0 #sets variable index to 0 to start
   for item in word1: #steps throug string
   if item == item in word2: #checks characters against each other
   print "there is a match at index ", index
   print word1[index]
   print word2[index]
   index += 1 #increments index

   else:
   print "they are not the same at index ", index
   print word1[index]
   print word2[index]
   difList.append(index) #adds index value to list
   index += 1 # increments index
   return difList

It works, but only sometimes-- in some cases, it evaluates letters that are
different as being the same. Seems to be happening after a run of a few
matching letters? It also works for "carpet" "carrot", but not for "carrot"
"carpet". I think it must be something wrong with the structure of the
loop-- or how i am using it-- but I can't see it. Does anyone have any
suggestions? Here is output from some sample runs:


getDiff("desk","foot") # works fine

they are not the same at index  0
d
f
they are not the same at index  1
e
o
they are not the same at index  2
s
o
they are not the same at index  3
k
t
[0, 1, 2, 3]

getDiff("beep","bees") # works fine

there is a match at index  0
b
b
there is a match at index  1
e
e
there is a match at index  2
e
e
they are not the same at index  3
p
s
[3]

getDiff("freeze","fresia")  # this one did not work!

there is a match at index  0
f
f
there is a match at index  1
r
r
there is a match at index  2
e
e
there is a match at index  3
e
s
they are not the same at index  4
z
i
there is a match at index  5
e
a
[4]

getDiff("carrot","carpet") # this one did not work!

there is a match at index  0
c
c
there is a match at index  1
a
a
there is a match at index  2
r
r
there is a match at index  3
r
p
they are not the same at index  4
o
e
there is a match at index  5
t
t
[4]

getDiff("carpet","carrot") #but this does work (inverted order from

above)
there is a match at index  0
c
c
there is a match at index  1
a
a
there is a match at index  2
r
r
they are not the same at index  3
p
r
they are not the same at index  4
e
o
there is a match at index  5
t
t
[3, 4]
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] trouble with function-- trying to check differences btwn 2 strings

2007-03-06 Thread zannah marsh

what I was trying to do with that loop is check each character in the string
against the corresponding character at the same position in the second
string. rikart pointed out that my loop was actually checking if that
character exists anywhere in the second string.
basically, in pseudocode:

for thing in bunch of things
   check to see if thing  from bunch of things is present in second bunch
of things

so "thing" here (called item in my original code) does not represent the
index. it's just a counter for things.
in java (which is the only language i am even kind of familar with) loops
and iterators are different, and everything has to be defined...

rikart pointed out that you need to use a range to get to the indicies of
the items in the string.

for item in range(len(string))...
if word1[item] == word2[item]


and that's the fix. whether i am doing something totally weird, or that
could be done a better way in python, i don't know...


zannah



On 3/6/07, Alan Gauld <[EMAIL PROTECTED]> wrote:



"David Perlman" <[EMAIL PROTECTED]> wrote

> I can't figure out how this would ever work at all.  It seems like
> it's either checking to see whether boolean TRUE is in word2, or
> else
> it's checking to see whether item is equal to boolean TRUE or FALSE,
> and neither of those should ever be true.

It's doing the latter and since anything that's not 'empty' in
Python evaluates to true we wind up checking whether
true == (item in word)

So if the item is in word we get true == true which is true.

HTH,


--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld


___
Tutor maillist  -   Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor