5

I have two words, one is a user input string, the other is a randomly selected word from text file. I would like to return the count values that are equal in both stings and share the same string index. e.g word1 = 'bleed', word2 = slice: similar = 1.

word1 = 'frog'
word2 = 'friend'
correct = 0

if len(word1) > len(word2):
    for i in range(len(word2)):
        if word1[i] == word2[i]:
            correct =+ 1
        else:
            correct == 0

else:
   for i in range(len(word1)):
       if word1[i] == word2[i]:
           correct =+ 1
       else:
           correct == 0

I'm new to programming and unfortunately My attempt maxes out at correct = 1. For the words i have used in the example frog and friend i would expect to see correct = 2, my code produces correct = 1. How do i add to correct, beyond 1? thank you

Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
user3636636
  • 2,409
  • 2
  • 16
  • 31
  • `=+` isn't `+=`, `==` doesn't actually do anything, and if you change it to `=`, it's going to do the wrong thing. – user2357112 Jun 15 '14 at 04:07
  • You're using the operator `+=` incorrectly. Your code always make correct receive `+1` instead of incrementing. – Mephy Jun 15 '14 at 04:07
  • 3
    Instead of range-len iteration, just use `zip`. It automatically stops at the end of the shortest input sequence, which is exactly what you want. – user2357112 Jun 15 '14 at 04:07
  • Definitely look into the zip() built-in function. – Jack Leow Jun 15 '14 at 04:09

5 Answers5

9

This is much easier with zip() and the filter() built in function:

Python 2.x:

In [23]: word1 = 'frog'
In [24]: word2 = 'friend'
In [25]: print len(filter(lambda (x, y): x == y, zip(word1, word2)))
2

Python 3.x (see comments for pointers re: changes):

>>> word1 = 'frog'
>>> word2 = 'friend'
>>> len(list(filter(lambda xy: xy[0] == xy[1], zip(word1, word2))))
2

Update: since you're new to programming, I'll attempt to explain this solution a bit:

Python has support for sequences (commonly thought of as an ordered list of items), such as [1,2,3] or ['a', 'b', 'c', 123, 456]. However, Python treats strings as an ordered list of characters as well, so 'hello world' is also a list. As a result, you may apply Python's list-related operators/built-in functions on strings as well. Therefore your problem reduces to treat word1 and word2 as lists and find indexes on these lists where their items match.

So let's use Python's special functions for this.zip is a nifty function that takes more than one sequence, and makes a new list with each item at index X made up of the values at index X in each of the input sequences. For instance, zip([1,2], [3,4]) becomes [(1,3), (2,4)] and zip('foo', 'bar') becomes [('f', 'b'), ('o', 'a'), ('o', 'r')]. This last result is very useful for your problem -- now you just need to run a function through each tuple (i.e. a value of the form (x,y)) through a function to check if the values are equal. You could do that in a loop as in other languages, but you can also make Python do the looping for you and provide just a function that returns the result of applying equality to each tuple on a list by using a function such as filter() or map(). In your case, you want filter() which runs through the input list and includes every value in the original list that causes the value of a passed in function (the lambda in the solution) to be True. The length of the "filtered" list is essentially the number of matches.

Hope this helps. Note that if you are new to Python, you will want to look up list/sequences, tuples, zip, filter, and lambda in a good book or the official Python reference to fully understand the solution.

Oh, and welcome to programming :-)

scorpiodawg
  • 5,612
  • 3
  • 42
  • 62
  • thank you for your time, I really appreciate your extensive answer! I tried to put this to use, and got the error 'invalid syntax' with the opening parenthesis after lambda highlighted. Any idea why this would be? – user3636636 Jun 15 '14 at 04:40
  • 1
    Hmm, looks like a difference between Python 2.7 which I ran this on, and Python 3 which does give the error you indicate. Not sure what syntax has changed -- let me dig in. Do you happen to have Python 2 installed on your machine? For me on Ubuntu, that's just 'python'. – scorpiodawg Jun 15 '14 at 05:56
  • 1
    Ok, here's the problem: tuple auto-unpacking changed in Python 3: http://stackoverflow.com/questions/15712210/python-3-2-lambda-syntax-error. Still seeing an error with the print statement, and filter now returns an iterable but not a straight up list: http://stackoverflow.com/questions/12319025/filters-in-python. I'll update the answer with the correct Python 3 equivalent. – scorpiodawg Jun 15 '14 at 06:00
3

Using zip and for comprehension:

word1 = 'frog'
word2 = 'friend'

sum([1 for (l,r) in zip(word1, word2) if l == r])
Jack Leow
  • 21,945
  • 4
  • 50
  • 55
2

example for 'reduce' .

>>> word1 = 'frog'
>>> word2 = 'friend'
>>> reduce(lambda z,(x,y): (z+1) if x == y else z, zip(word1,word2), 0)
2

reduce take 3 arguments,

  • function called with accumulator and each item of the list.
  • list.
  • initial value of accumulator.

Accumulator looks like temporary variable. But the value of the accumulator is taken over.

In my code:

function is The value of the accumulator is taken over.
list is zip(word1, word2) so [('f', 'f'), ('r', 'r'), ('o', 'i'), ('g', 'e')]
initial value is 0

Let's see how reduce works.

First time, function called with arguments z := 0, (x,y) := ('f', 'f') and return 1, because x == y is True and z + 1 is 1.

Next time, function called with arguments z := 1, (x,y) := ('r', 'r'). z is 1, that is point, please remember accumulator is taken over. so z is last result. In this time, x == y is True, too. function return 2 (z + 1).

Next time, called with 2, ('o', 'i') returns 2. ( 'o' is not 'i' ).

And last, called with 2, ('g', 'e') returns 2.

That's all, result was 2.

fliedonion
  • 912
  • 8
  • 13
1

As user2347112 stated in comments, this task can be easily done with zip() built-in funciton:

word1 = 'frog'
word2 = 'friend'
correct = 0

for i,j in zip(word1,word2):
    if i == j:
        correct+=1

print(correct)
Ruben Bermudez
  • 2,293
  • 14
  • 22
1

To start with, =+ needs to be += to properly increment correct, and get rid of your else clauses as they do nothing.

As mentioned in the comments you should look into zip

In [1]: word1 = 'frog'

In [2]: word2 = 'friend'

In [3]: sum(1 for a, b in zip(word1, word2) if a == b)
Out[3]: 2
MrAlias
  • 1,316
  • 15
  • 26