I am working on a python exercise that asks:
# Return True if the string "cat" and "dog" appear the same number of
# times in the given string.
# cat_dog('catdog') → True
# cat_dog('catcat') → False
# cat_dog('1cat1cadodog') → True
This is my current code:
def cat_dog(str):
c=0
d=0
for i in range(len(str)-2):
if str[i:i+3]=="cat":
c=+1
if str[i:i+3]=="dog":
d=+1
return (c-d)==0.0
I look at it and I think it should work but it failed some of the tests, which suggest to me I am not understanding something in how Python logic works. Any explanation as to why my solution is not working would be great. These are all the test results:
cat_dog('catdog') → True True OK
cat_dog('catcat') → False False OK
cat_dog('1cat1cadodog') → True True OK
cat_dog('catxxdogxxxdog') → False True X
cat_dog('catxdogxdogxcat') → True True OK
cat_dog('catxdogxdogxca') → False True X
cat_dog('dogdogcat') → False True X
cat_dog('dogdogcat') → False True OK
cat_dog('dog') → False False OK
cat_dog('cat') → False False OK
cat_dog('ca') → True True OK
cat_dog('c') → True True OK
cat_dog('') → True True OK
c += 1andd += 1instead ofc=+1andd=+1