22
\$\begingroup\$

July 1st is Canada day (yay Canada)! Or is it? It seems that the Wikipedia page for this day has a lot of Canada related content, but is there another day which is more Canadian?

Your task is to write a program or function which takes a date (month and day) as input and returns or outputs the number of mentions of "Canada" on the Wikipedia page for the inputed date. Some rules:

  • Dates may be input in any reasonable format of your choice
  • Your submission must pull data from the url en.wikipedia.org/wiki/Month_Day.
  • Only "Canada" must be searched for and counted included substrings, and only in title case. "Canadian" does not count, however "Canada's" does count. As long as the exact, case-senstitive text "Canada" exists within a string, it is a match
  • Contents of the page are considered anything within the corresponding .html file (i.e. what shows up if you download as page as a .html and open it in Notepad)
  • Result may be output to STDOUT, returned, or displayed in any other reasonable manner

Test Cases:

July 1 => 34
May 14 => 1
Oct 31 => 2
July 4 => 2

This is code golf, so shortest submission wins

(As an unrewarded bonus, I'm interested to see what the day with the highest count is)

\$\endgroup\$
7
  • \$\begingroup\$ Can the Wikipedia API be used? \$\endgroup\$ Jul 5, 2016 at 17:14
  • \$\begingroup\$ I don't know much about it, so I'm hesitant to say yes in case there is a trivial function to it. Use your best judgement and if it makes it too easy please abstain \$\endgroup\$
    – wnnmaw
    Jul 5, 2016 at 17:16
  • 9
    \$\begingroup\$ So references to Canadaville, Canadair, Canadarm, Canadaga, Canadarago, Canaday, Canadaspis, et al. count? \$\endgroup\$
    – msh210
    Jul 5, 2016 at 17:21
  • \$\begingroup\$ @msh210, Yep, that they do \$\endgroup\$
    – wnnmaw
    Jul 5, 2016 at 17:23
  • 2
    \$\begingroup\$ July 1 is the day with the highest count! Wrote a quick program for it, though it isn't golfed. \$\endgroup\$
    – Andrew
    Jul 6, 2016 at 12:10

12 Answers 12

24
\$\begingroup\$

Bash, 43 42 40 bytes

curl -L enwp.org/$@|grep -o Canada|wc -l

Uses curl, grep, and wc to count occurrences of "Canada" in specified webpage. Like the other answers, input is given in the format July_1. This is my first time posting on the Code Golf SE and I'm not quite familiar with all of the rules. Any feedback would be most welcome.

Didn't realize that output to STDERR is traditionally ignored. Thanks for the 3 bytes, Dennis!

\$\endgroup\$
4
  • \$\begingroup\$ But wouldn't curl -sL still be shorter than wget -qO-? \$\endgroup\$ Jul 7, 2016 at 2:07
  • 1
    \$\begingroup\$ Output to STDERR is ignored by default, so you can use curl without -s (or wget without -q). \$\endgroup\$
    – Dennis
    Jul 7, 2016 at 2:41
  • \$\begingroup\$ @Dennis Thanks! I didn't know that STDERR is ignored. Much appreciated. \$\endgroup\$
    – SG_
    Jul 7, 2016 at 4:33
  • \$\begingroup\$ @kundor That's a good point. For some reason, combining the two flags never occurred to me. Still, since output to STDERR is ignored by default, it'd be shorter to omit the -s entirely. \$\endgroup\$
    – SG_
    Jul 7, 2016 at 4:35
15
\$\begingroup\$

Perl 5, 39 bytes

38 bytes, plus 1 for -pe instead of -e

$_=()=`curl -L enwp.org/$_`=~/Canada/g

Takes input like July_1.

Thanks to busukxuan for saving me seven bytes.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ I'm not familiar with curl, but is it possible to save the six bytes of "http://"? \$\endgroup\$
    – busukxuan
    Jul 5, 2016 at 18:36
  • 1
    \$\begingroup\$ @busukxuan, yep, many thanks. \$\endgroup\$
    – msh210
    Jul 5, 2016 at 19:16
7
\$\begingroup\$

Python 3.5, 117 111 98 90 bytes

(-8 bytes (98 -> 90) thanks to alexwlchan)

from urllib.request import*
lambda i:urlopen('http://enwp.org/'+i).read().count(b"Canada")

Simply uses Python's built-in "urllib" library to fetch HTML data and then counts the occurrences of the word "Canada" in that data. Will try and golf more over time where and when I can. Call it by renaming the lambda function to anything and then calling that name like a normal function wrapped in print(). For instance, if the function were named H, then you would call it like print(H(Month_Day)).

\$\endgroup\$
3
  • 4
    \$\begingroup\$ I think you can save eight characters by replacing .decode().count("Canada") with .count(b"Canada"). \$\endgroup\$
    – alexwlchan
    Jul 5, 2016 at 18:23
  • \$\begingroup\$ @alexwlchan Yes, you are right. Thanks! :) \$\endgroup\$
    – R. Kap
    Jul 5, 2016 at 18:33
  • \$\begingroup\$ Surely this would be shorter in Python 2, since the urllib.urlopen function isn't in a subpackage (from urllib import* versus from urllib.request import*), and the b"Canada" could be replaced with "Canada" since Python 2's strings are bytes by default. I count 81 bytes in Python 2, and it works according to my testing. \$\endgroup\$
    – user45941
    Jul 5, 2016 at 21:19
5
\$\begingroup\$

Mathematica, 60 bytes

Import["http://enwp.org/"<>#,"Source"]~StringCount~"Canada"&

Anonymous function. Similarly to the Perl 5 solution, takes input like July_1.

\$\endgroup\$
1
  • \$\begingroup\$ Just to close the loop, this use of the API is totally fine \$\endgroup\$
    – wnnmaw
    Jul 5, 2016 at 18:54
5
\$\begingroup\$

PowerShell, 52 bytes

((iwr enwp.org/$($args[0]))-csplit"Canada").length-1
  • Input as July_1.
  • iwr is short for Invoke-WebRequest.
  • $($args[0]) is first command line argument. Start script as OhCanada.ps1 July_1.
  • -csplit is case sensitive split.
\$\endgroup\$
5
\$\begingroup\$

C#, 85 bytes

return Regex.Matches(new WebClient().DownloadString("http://enwp.org/"+d),"Canada").Count;

Takes input d like July_1.

And July_1 is truly Canada Day, having the most references. With February_1 and April_23 sharing 2nd place with 18 "Canada"s each.

Find "Canada" day (in parallel), 207 bytes:

return Enumerable.Range(0,366).Select(i=>new DateTime(8,1,1).AddDays(i).ToString("MMMM_d")).AsParallel().OrderBy(d=>Regex.Matches(new WebClient().DownloadString("http://enwp.org/"+d),"Canada").Count).Last();

(Year 8 is the leap year with the shortest representation). Potentially inefficient, in that the OrderBy probably generates >366 web calls, but just going for shorter and appears to complete in not much more time.

\$\endgroup\$
4
\$\begingroup\$

Pyth, 31 bytes

/jk'+"http://enwp.org/"z"Canada

Does not work on the online implementation, the server disables Internet access. I wanted to use http://wki.pe/July_1 but sadly it's a client-side redirect so it fetches the wrong page. The input format is July_1.

The code is basically just:

"".join(open("http://enwp.org/"+input())).count("Canada")
\$\endgroup\$
4
\$\begingroup\$

R, 99 96 bytes

x=function(d){p=readLines(paste0("http://enwp.org/",d));sum(nchar(p)-nchar(gsub("Canada","",p)))/6}

d=scan(,"");p=readLines(paste0("http://enwp.org/",d));sum(nchar(p)-nchar(gsub("Canada","",p)))/6

This takes input d in the form "July_1" and returns the count of Canadas. It counts the words by counting the number of characters on the page, then removes the word Canada from the page and counts the characters again. The number of times Canada shows up is the difference in these counts divided by the number of letters in Canada, 6.

edit: I appreciate the tip below about replacing my function with scan.

\$\endgroup\$
2
  • \$\begingroup\$ I think you can drop the x=function(d){ and replace with d=scan(,'') making it program instead of function and saving some bytes. \$\endgroup\$
    – pajonk
    Jul 6, 2016 at 17:58
  • \$\begingroup\$ Thanks! That saved three bytes. I haven't used scan before. \$\endgroup\$
    – Austin
    Jul 6, 2016 at 20:01
4
\$\begingroup\$

ES6, 89 bytes

d=>fetch('http://enwp.org/'+d).then(r=>r.text().then(t=>alert(t.split`Canada`.length-1)))

Sadly Unwrapping all the promises penalises the size :/

\$\endgroup\$
5
  • \$\begingroup\$ Nice answer, welcome to the site! \$\endgroup\$
    – DJMcMayhem
    Jul 6, 2016 at 0:08
  • 1
    \$\begingroup\$ Couple comments. You can apply the same "input is in the format July_1" trick as the rest of the questions to save a few bytes. You also have an error using split().length(), which will give you a response greater than the goal. \$\endgroup\$ Jul 6, 2016 at 9:00
  • \$\begingroup\$ Agree with @IvanSanchez on the input format and needing a -1 after the .length, but you can save some bytes by omitting the https: part of the URL, and use split'Canada' (but with backticks!) instead of split('Canada') to save a couple more! \$\endgroup\$ Jul 6, 2016 at 11:39
  • \$\begingroup\$ Wow had no idea about backticks! I have made the changes mentioned. \$\endgroup\$ Jul 7, 2016 at 1:40
  • \$\begingroup\$ Firefox allows you to drop the // after http. \$\endgroup\$ Jul 7, 2016 at 8:50
2
\$\begingroup\$

Clojure, 71 bytes

#(count(re-seq #"Canada"(slurp(str"https://en.wikipedia.org/wiki/"%))))

Yeah, it would be nice to use http://enwp.org but I guess slurp does not handle redirects(?). Anonymous function which take day in the format "July_1".

\$\endgroup\$
2
\$\begingroup\$

Ruby + curl, 44 bytes

p`curl -L enwp.org/#$_`.scan(/Canada/).size

ruby -n + 43 bytes. Takes input like July_1.

\$\endgroup\$
2
\$\begingroup\$

PHP, 65 bytes

echo substr_count(file_get_contents('http://enwp.org'),'Canada');
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.