Count the number of occurences of Jay.(NOT INCLUDING JAYSON OR JAY LENO.)
I keep getting the total which is 5
>>220159
Those are oddly specific requirements for what I'm assuming is an intro assignment.
When it comes right down to it, regular expressions are the best way to do this kind of thing, but they might be a bit much for a beginner. If you're feeling up to it, here's some reading material:
www.regular-expressions.info
...and here's a nice interactive regex editor to fiddle with:
www.regexr.com
The contents of jay.txt are the lines you put in the comments in your image.
>>220159
>When it comes right down to it, regular expressions are the best way to do this kind of thing
Sure they are.
#!/usr/bin/env python
jay="Jay,\n\nI will go with Jayson to see Jay Leno. Jay,\n you are such a nice guy.\n\nSee you later Jay\n\nApril"
print (jay.count('Jay,')+jay.count('Jay\n'))
#Extra credit: print (jay.count('Jay,')+jay.count('Jay.')+jay.count('Jay\n'))
>>220259
What if it encounters "Jay;" Jay with a space in front, Jay at the end of the string without a period?
>>220285 here
Like >>220237 said, the answer to this problem and many problems like it is regex. However if you're struggling to find a solution for this, regex is way beyond your reach. Your prof is going to see that you just got the regex online.
This is a more "naive" approach, but still very valid. Check it out, I commented everything and it should be obvious what is going on.
You basically look for instances of "Jay" and then look what the next character is, if it's a letter, then it's just a word that starts with Jay. If it's not a letter, it doesn't matter what it is, you add to the count.
http://pastebin.com/JHS29Xnh
>>220159
If you can't use regex, just count the number of 'Jay's and subtract it by your exceptions list and keep it above 0.
jay = jay.lower()
exceptions = ["jay leno", "jayson"]
count = jay.count("jay")
for x in exceptions:
count -= jay.count(x)
print(count if (count > 0) else 0)
>>220294
In fact, it keeps itself above 0.
>>220289
That fails on "Jay Leno", which is literally 33% of the problem definition.
>>220285
>What if it encounters "Jay;"
Ask the problem statement what the correct behaviour should be
>Jay with a space in front
Works fine. The code's right in the thread, all you had to do was paste it
>Jay at the end of the string without a period?
Regex has to special-case this too, because EOF is an anchor not a token. In both cases, using a filter to add a /n to the end of the input stream is the cleanest way to go.
>>220388
>>220400
Apparently I read the problem wrong. I misunderstood that you wanted to find Jay as a stand alone word, not part of a bigger word. And didn't correctly acommodate for "Jay Leno". So whatever I guess.
>>220294
This solution is the best approach if you want to avoid regex. Just count all the "Jays" and substract all the "Jayson" and "Jay Leno"s you find.
If you do want to use regex, I propose this one:
Jay(?=\W|$)(?! Leno)
Which basically means: "Jay" followed by something that is NOT a word letter (ie not alphanum) or the end of the string. Also NOT followed by " Leno".