[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

Need Programming Help!

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 11
Thread images: 5

File: JayProgram1.png (72KB, 1366x768px) Image search: [Google]
JayProgram1.png
72KB, 1366x768px
Count the number of occurences of Jay.(NOT INCLUDING JAYSON OR JAY LENO.)
>>
I keep getting the total which is 5
>>
File: jay.png (23KB, 641x294px) Image search: [Google]
jay.png
23KB, 641x294px
>>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?
>>
File: notepad++_2016-11-13_22-40-22.png (20KB, 994x556px) Image search: [Google]
notepad++_2016-11-13_22-40-22.png
20KB, 994x556px
>>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.
>>
File: jay failo.png (8KB, 604x563px) Image search: [Google]
jay failo.png
8KB, 604x563px
>>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.
>>
File: firefox_2016-11-14_12-04-48.png (8KB, 384x294px) Image search: [Google]
firefox_2016-11-14_12-04-48.png
8KB, 384x294px
>>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".
Thread posts: 11
Thread images: 5


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.