0

Return True if the string "cat" and "dog" appear the same number of times in the given string.

This is the question I'm attempting. my code is:

def cat_dog(str):
    count1 = 0
    count2 = 0
   
    if 'dog' and 'cat' not in str:
        return True
    for i in range(len(str)-1):
        if str[i:i+3] == 'cat':
            count1 += 1
        if str[i:i+3] == 'dog':
            count2 += 1
    if count1 == count2:
        return True
    else:
        return False

i know this is incorrect as the code does not seem to be looping through the whole string and picking up cat and dog. not sure how to rectify this.

Simon Guy
  • 9
  • 1
  • 3

8 Answers8

1

Just use count method to count the number of occurrences of a string.

>>> 'catdog'.count('cat') == 'catdog'.count('dog')
True
>>> 'catdogdog'.count('cat') == 'catdogdog'.count('dog')
False
>>> 

You need to add a condition before this code otherwise the above code should return true if both cat or dog does not exists on the input string.

if 'dog' in string or 'cat' in string:
    return string.count('cat') == string.count('dog')
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
  • 1
    `count` doesn't count overlapping matches that the OP's code would - eg `banana.count("ana") == 1`. Not that it makes a difference for "cat" and "dog" – John La Rooy Feb 24 '16 at 06:25
1

This could work:

def cat_dog(str):
  counter_cat = 0
  counter_dog = 0
  for i in range(0, len(str)-2):
    if str[i] == 'c' and str[i+1] == 'a' and str[i+2] == 't':
      counter_cat += 1
    elif str[i] == 'd' and str[i+1] == 'o' and str[i+2] == 'g':
      counter_dog += 1
  return counter_cat == counter_dog
0
def cat_dog(str):
    count_of_cat = str.count("cat") 
    count_of_dog = str.count("dog")
    if count_of_cat == count_of_dog:
        return True
    else:
        return False
0

this is a one line solution:

return str.count('cat') == str.count('dog')

this block of code is not needed is this case

if 'dog' and 'cat' not in str:
    return True

simply beacause you intialize count1 and count2 to 0 then if no dog or cat in word they are equal, they don´t increment

also in this block of code

if count1 == count2:
    return True
else:
    return False

you can reduce to this

return count1 == count2

it will see the boolean values for that expression return true if count1 == count2 else return false

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 28 '21 at 03:12
-1
if 'dog' and 'cat' not in str:

This doesn't do what you think it does. Python interprets it like this:

if ('dog') and ('cat' not in str):

The first part is always true, because 'dog' is a non-empty string, so it comes down to 'cat' not in str. So you are really only checking to see if the string doesn't contain 'cat'.

You want something like this:

if 'dog' not in str and 'cat' not in str:

Or, equivalently:

if not ('dog' in str or 'cat' in str):

Or, if you have more tests to make, this is more compact for more than a couple:

if not any(x in str for x in ('cat', 'dog', 'mouse', 'beaver')):

This will affect whether the function even gets to the loop, so it is probably throwing you off.

Also, don't name your variable str. str is a built-in type, and you might need to use it, but can't because you've reassigned it.

kindall
  • 178,883
  • 35
  • 278
  • 309
  • yes. codingbat uses str as a variable name so you're kind of forced to use it. thanks for the suggestions. – Simon Guy Feb 25 '16 at 09:03
-1

The part where you return from the function should not be inside the for loop. This is why the loop is exiting prematurely for you

def cat_dog(str):
    count1 = 0
    count2 = 0

    if 'dog' not in str and 'cat' not in str: # <= kindall pointed this out
        return True

    for i in range(len(str)-1):
        if str[i:i+3] == 'cat':
            count1 += 1
        if str[i:i+3] == 'dog':
            count2 += 1

    if count1 == count2:  # <= These shouldn't be part of the for loop
        return True
    else:
        return False

The last 4 lines should normally be written as

    return count1 == count2
John La Rooy
  • 295,403
  • 53
  • 369
  • 502
-1

Your code just has 2 minute mistakes. Once rectified, it gives perfect results without any further changes. Check below.

Edit1 in line8:
Instead of "for i in range(len(str)-1):", it needs: "for i in range(len(str)-2):" - As the words cat and dog both are 3 letter words and for capturing every single combination making "cat" and "dog", we need to count from 3rd last letter. hence "-2" instead of "-1"

Edit2 in lines 12,13,14,15:
Indentation is incorrect. It compares the count1 and count2 immediately after the first iteration of value i and hence returns wrong results. Just correct indentation and your code is perfect to go!

Check the corrected code without any additional inputs from my end.

def cat_dog(str):
    count1 = 0
    count2 = 0

    if 'dog' and 'cat' not in str:
        return True
    for i in range(len(str)-2):
        if str[i:i+3] == 'cat':
            count1 += 1
        if str[i:i+3] == 'dog':
            count2 += 1
    if count1 == count2:
        return True
    else:
        return False
Yogi Agravat
  • 113
  • 7
-2
    def cat_dog(str):

       cat = dog = 0
  
       for i in range(len(str)-2):
          if str[i:i+3] == "cat":
             cat += 1
          if str[i:i+3] == "dog":
             dog += 1
       return cat == dog
Ironkey
  • 2,568
  • 1
  • 8
  • 30
  • 1
    Please explain to the OP what was wrong with their code, rather than just posting a working version. – tgdavies Nov 07 '20 at 23:19