422,757 Members | 1,286 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 422,757 IT Pros & Developers. It's quick & easy.

How to count the occurrence of a particular word with edge cases in Python

P: 1
I have written the code to count the occurrence of a certain word cat in it, the code is passing only some of my test cases, it is not passing all the test, especially the edge cases. Please note, am not allowed to use inbuilt functions, just loops or conditionals for now.
Here is my code:

Expand|Select|Wrap|Line Numbers
  1. mystery_string = "my cat your cat"
  2.     splitword = mystery_string.split()
  3.     sum = 0
  4.     for string in splitword:
  5.         if string == "cat":
  6.             sum += 1
  7.     print (sum)

when i change the variable mystery_string to
Expand|Select|Wrap|Line Numbers
  1. `mystery_string = "catcatcatcatcat!!".
` or
Expand|Select|Wrap|Line Numbers
  1. `mystery_string = "cacacat cacacat"
` or this one
Expand|Select|Wrap|Line Numbers
  1. `mystery_string = "ucatX"`
or even this
Expand|Select|Wrap|Line Numbers
  1. `mystery_string = ".$cacacatcatcat".
` the code just breaks. i have researched stackoverflow for similar problem but no solution.
Sep 8 '17 #1
Share this Question
Share on Google+
2 Replies


Expert 100+
P: 592
Print "string" when using "catcatcatcatcat!!"as you only look at one letter at a time. You have to break into 3 letter substrings, or use the "in" keyword, or "find" for strings. Note that "string" is a Python builtin keyword so don't use it.
Expand|Select|Wrap|Line Numbers
  1. """ don't know if some of this code is builtins as I 
  2. don't know how you define that word.  If so, you 
  3. will have to modity it
  4.  
  5. ************************************************
  6. print statements added so you can see what is 
  7. happening, as opposed to "it doesn't work and I
  8. don't know why"
  9. ************************************************
  10. """
  11. for mystery_string in ["my cat your cat",
  12.                        "catcatcatcatcat!!",
  13.                        "cacacat cactcat",
  14.                        "ucatX",
  15.                        ".$cacacatcatcat"]:
  16.  
  17.     print(mystery_string)
  18.     splitword = mystery_string.split()
  19.     print("     testing the list", splitword)
  20.     for str in splitword:
  21.         print("%stesting the word %s" % (" "*10, str))
  22.         ## len() is a builtin so you will have to count the letters instead
  23.         if len(str) > 2:
  24.             for ctr in range(0, len(str)-2):  ## don't check last 2 letters
  25.                 print(str[ctr:ctr+3])
  26.                 if "cat"==str[ctr:ctr+3]:
  27.                     print("*****cat found at", ctr) 
Sep 8 '17 #2

Rabbit
Expert Mod 10K+
P: 12,204
You could split the string on the substring cat and then use a loop to count the number of elements minus 1
Sep 8 '17 #3

Post your reply

Sign in to post your reply or Sign up for a free account.