python - 2 like characters in same position -
i trying return number of positions in 2 strings have same character.
tried code:
def matches(stringone, stringtwo): if stringone or stringtwo == " ": return none else: n=0 count=0 if stringone[n] == stringtwo[n]: count=count+1 else: pass n=n+1 return count
example:
matches = matches("abcd", "xbade") print matches 2
thanks! :)
and no, it's not homework.
no zip functions please. i'm not trying use python built in functions.
you can use generator expression zip
iterate through strings letter-by-letter comparison
def matches(stringone, stringtwo): return sum(1 i,j in zip(stringone, stringtwo) if == j) >>> matches("abcd", "xbade") 2 >>> matches('dictionary', 'dibpionabc') 6
if don't want use python's zip
function
def matches(stringone, stringtwo): shorter = min(len(stringone), len(stringtwo)) same = 0 in range(shorter): same += stringone[i] == stringtwo[i] return same
although method uses min
, len
, , range
. tying hands behind if refuse use built-in functions of language.