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.)
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
1a 2 are all search-phrases, but
1a 2, and
(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.)
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.
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
Prefixes "a" "a c" "a ca" "a car"
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
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
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.
- 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=1to the URL doesn't always work either. Don't worry about these inaccuracies. Just look for the
About X results...message no matter what pops up, or set
Resultsto 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.