9
When you search for something on Google, it conveniently gives a message near the top of the page saying something like About 53,000,000 results (0.22 seconds)
. (The numbers change depending on what was searched of course.)
In this challenge you will write a program that draws a logarithmic ASCII graph of the number of results given by Google when all the non-empty prefixes of a given search-phrase are searched.
A search-phrase is defined as one or more strings of lowercase alphanumeric characters, separated by one space from each other. In Regex a search-phrase is (?:[a-z0-9]+ )*[a-z0-9]+
.
So im ok
, r
, and 1a 2
are all search-phrases, but I'm OK
, R
, 1a 2
, and , are not.
(The character restrictions are in place because Google rarely takes case or special symbols into account. Escaping non-alphanumeric characters in URLs is also a hassle.)
Spec
Your program must take in a search-phrase and a positive floating point number H from either stdin or the command line. (You can assume they are valid and it's fine if you need quotes or something around the search-phrase.)
As a working example let's assume the search phrase is a car
and H = 0.75.
Step 1:
Gather the non-empty prefixes of your search-phrase, and put them in double quotes. The quotes ensure that that the exact phrase will be searched for, avoiding any 'did you mean...' redirections.
Exclude all prefixes that end in a space such as a[space]
.
Prefixes
"a"
"a c"
"a ca"
"a car"
Step 2:
Search each of these terms exactly as they appear using https://www.google.com, and note the number of results returned.
Search Term Message Results
"a" About 6,950,000,000 results (0.27 seconds) 6950000000
"a c" About 861,000,000 results (0.27 seconds) 861000000
"a ca" About 2,990,000 results (0.30 seconds) 2990000
"a car" About 53,900,000 results (0.39 seconds) 53900000
If the search term did not match any documents, put a 0 in the Results
column.
Step 3:
Compute y = floor(H * log10(r + 1))
for each row, where r is the Results
value. H is still 0.75 here.
Search Term Results y
"a" 6950000000 7
"a c" 861000000 6
"a ca" 2990000 4
"a car" 53900000 5
Step 4:
Arrange y
number of vertical bars (|
) above the last character of each unquoted search term, using spaces to fill empty areas, in a sort of bar graph.
|
| |
| | |
| |||
| |||
| |||
| |||
a car
This graph is the final result of your program and the only thing it needs to output. It should go to stdout.
Scoring
This is code-golf, so the shortest program in bytes wins.
Notes
- You may use URL shorteners or other search tools/APIs as long as the results would be the same as searching https://www.google.com.
- I know that double quotes are not a surefire way to exclude "did you mean..." redirections. Adding
&nfpr=1
to the URL doesn't always work either. Don't worry about these inaccuracies. Just look for theAbout X results...
message no matter what pops up, or setResults
to 0 if there is none. - There is an empty column above any space in the search-phrase in the graph.
- The graph should not be wider or taller than it needs to be (e.g. with whitespace).
- It's ok if your program has side effects like opening a web browser so the cryptic Google html/js pages can be read as they are rendered.
I know it's not typical to get zero results, but when you do there is no "About x results...". I assume that should be detected and shown as 0 bars?
– Geobits – 2014-09-19T02:17:44.720@Geobits Yes, assume 0 results. – Calvin's Hobbies – 2014-09-19T03:00:23.713
1Just so you know - don't hammer google - it will respond with a captcha if you hit it too hard/too often, which might break your program – SeanC – 2014-09-19T18:42:44.950