Comp Sci Return the number of times a word appears in a string

  • Thread starter Thread starter ver_mathstats
  • Start date Start date
  • Tags Tags
    String
AI Thread Summary
The discussion revolves around a Python function designed to count occurrences of the substring "code" in a given string. Key issues identified include potential index range errors and incorrect indentation, which would prevent the function from executing properly. A suggestion was made to use Python's built-in string method `.count()` for simplicity, though the original poster indicated they needed to follow specific instructional guidelines. The importance of learning fundamental programming concepts was emphasized, alongside the potential use of regular expressions for more advanced string manipulation. Overall, the focus remains on correcting the function while adhering to educational requirements.
ver_mathstats
Messages
258
Reaction score
21
Homework Statement
Return the number of times that the string "code" appears anywhere in a given string
Relevant Equations
Using python
Python:
def count_code(str):
       count = 0
       for c in range(len(str)-1):
              if str[c] == 'c' and str[c+1] == 'o' and str[c+2] == 'd' and str[c+3] == 'e':
                          count+=1
              return count
Here is my code so far, I did use some test results with print statements but nothing comes up when I run my code and I am unsure of where I am going wrong.

Any help would be greatly appreciated. Thank you.
<mentor add code tags>
 
Last edited by a moderator:
Physics news on Phys.org
I would think the term str[c+3] would give an index range error when c points at the 2nd and 3rd last chars of the string. That could be something to do with why the results are not what expected. Only let c run up to the fourth-last character.

Also, you have no indentation for the function, the for statement or the if statement. It won't work without indentation. If the latter is an artefact of formatting on this website, enclose your code within delimiters [code] and [/code] when posting on here so the indentation does not disappear.
 
Yes I realized some mistakes and fixed it. Thank you. And yes I am sorry about that I do have it indented in python, I just did not know how exactly to format it on PhysicsForum but thank you, will use that now. Thank you for the help.
 
Use code tags - they look like this with no spaces:
[ c o d e = python ]
... code lines here ...
[ / c o d e ]
 
jim mcnamara said:
Use code tags - they look like this with no spaces:
[ c o d e = python ]
... code lines here ...
[ / c o d e ]
Yes thank you. Greatly appreciated.
 
ver_mathstats said:
Homework Statement:: Return the number of times that the string "code" appears anywhere in a given string
Relevant Equations:: Using python

Python:
def count_code(str):
       count = 0
       for c in range(len(str)-1):
              if str[c] == 'c' and str[c+1] == 'o' and str[c+2] == 'd' and str[c+3] == 'e':
                          count+=1
              return count
Here is my code so far, I did use some test results with print statements but nothing comes up when I run my code and I am unsure of where I am going wrong.

Any help would be greatly appreciated. Thank you.
<mentor add code tags>
I don't see a good reason in this instance for your not presenting the whole script.

If it were up to me, I'd code it as:
Python:
givenstring = 'code some code and then code some more'
print givenstring.count('code')
but I'm not confident that your instructor wouldn't take a dim view of your using the .count shortcut. :wink:
 
  • Like
Likes jim mcnamara
The return count statement is too much indented. The function will return the first time around the for loop, so it can only return 1 if the input starts with 'code' and 0 otherwise..
 
  • Like
Likes ver_mathstats and FactChecker
sysprog said:
but I'm not confident that your instructor wouldn't take a dim view of your using the .count shortcut. :wink:
I agree. There is a lot of general programming that this exercise teaches. In any case, there are functions for using regular expressions that, IMHO, one should eventually learn.
 
  • Like
Likes ver_mathstats, sysprog and jim mcnamara
I second @FactChecker comment about learning REGEX (regular expressions)
 
  • Like
Likes ver_mathstats and sysprog
  • #10
In python a ##\mathtt{\text{regex}}## command (not just ##\mathtt{\text{re}}## ) can be configured to allow recursion and to use an extenal stack. Technically that makes it a context-free grammar instead of properly or strictly regular.

Even so, it's helpful to have the regex iterator in the Boost library if you want to get all instances of a substring in a string -- https://www.boost.org/doc/libs/1_72_0/libs/regex/doc/html/boost_regex/ref/regex_iterator.html
 
  • Like
Likes ver_mathstats
  • #11
sysprog said:
I don't see a good reason in this instance for your not presenting the whole script.

If it were up to me, I'd code it as:
Python:
givenstring = 'code some code and then code some more'
print givenstring.count('code')
but I'm not confident that your instructor wouldn't take a dim view of your using the .count shortcut. :wink:
Sorry, we're asked to do it in a specific way with concepts we had learned the previous week. Thank you for the help though.
 
  • Informative
Likes sysprog
  • #12
ver_mathstats said:
Sorry, we're asked to do it in a specific way with concepts we had learned the previous week. Thank you for the help though.
That seems right to me. Getting a correct answer isn't all that you're seeking. It's important to build the concepts and skills.
 
  • Like
Likes FactChecker and jim mcnamara
Back
Top