158
23
We all know that if you google the word "google" it will break the internet.
Your task is to create a function that accepts one string and returns its length, in the fewest possible Unicode characters.
However, if the given string is google
(lowercase), it will cause an error.
For example, g('bing')
will return 4
but g('google')
will cause an error.
Please provide an example of usage, and the error if possible.
The Catalogue
The Stack Snippet at the bottom of this post generates the catalogue from the answers a) as a list of shortest solution per language and b) as an overall leaderboard.
To make sure that your answer shows up, please start your answer with a headline, using the following Markdown template:
## Language Name, N bytes
where N
is the size of your submission. If you improve your score, you can keep old scores in the headline, by striking them through. For instance:
## Ruby, <s>104</s> <s>101</s> 96 bytes
If there you want to include multiple numbers in your header (e.g. because your score is the sum of two files or you want to list interpreter flag penalties separately), make sure that the actual score is the last number in the header:
## Perl, 43 + 2 (-p flag) = 45 bytes
You can also make the language name a link which will then show up in the snippet:
## [><>](https://esolangs.org/wiki/Fish), 121 bytes
/* Configuration */
var QUESTION_ID = 197625; // Obtain this from the url
// It will be like https://XYZ.stackexchange.com/questions/QUESTION_ID/... on any question page
var ANSWER_FILTER = "!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe";
var COMMENT_FILTER = "!)Q2B_A2kjfAiU78X(md6BoYk";
var OVERRIDE_USER = 8478; // This should be the user ID of the challenge author.
/* App */
var answers = [], answers_hash, answer_ids, answer_page = 1, more_answers = true, comment_page;
function answersUrl(index) {
return "https://api.stackexchange.com/2.2/questions/" + QUESTION_ID + "/answers?page=" + index + "&pagesize=100&order=desc&sort=creation&site=codegolf&filter=" + ANSWER_FILTER;
}
function commentUrl(index, answers) {
return "https://api.stackexchange.com/2.2/answers/" + answers.join(';') + "/comments?page=" + index + "&pagesize=100&order=desc&sort=creation&site=codegolf&filter=" + COMMENT_FILTER;
}
function getAnswers() {
jQuery.ajax({
url: answersUrl(answer_page++),
method: "get",
dataType: "jsonp",
crossDomain: true,
success: function (data) {
answers.push.apply(answers, data.items);
answers_hash = [];
answer_ids = [];
data.items.forEach(function(a) {
a.comments = [];
var id = +a.share_link.match(/\d+/);
answer_ids.push(id);
answers_hash[id] = a;
});
if (!data.has_more) more_answers = false;
comment_page = 1;
getComments();
}
});
}
function getComments() {
jQuery.ajax({
url: commentUrl(comment_page++, answer_ids),
method: "get",
dataType: "jsonp",
crossDomain: true,
success: function (data) {
data.items.forEach(function(c) {
if (c.owner.user_id === OVERRIDE_USER)
answers_hash[c.post_id].comments.push(c);
});
if (data.has_more) getComments();
else if (more_answers) getAnswers();
else process();
}
});
}
getAnswers();
var SCORE_REG = /<h\d>\s*([^\n,<]*(?:<(?:[^\n>]*>[^\n<]*<\/[^\n>]*>)[^\n,<]*)*),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/;
var OVERRIDE_REG = /^Override\s*header:\s*/i;
function getAuthorName(a) {
return a.owner.display_name;
}
function process() {
var valid = [];
answers.forEach(function(a) {
var body = a.body;
a.comments.forEach(function(c) {
if(OVERRIDE_REG.test(c.body))
body = '<h1>' + c.body.replace(OVERRIDE_REG, '') + '</h1>';
});
var match = body.match(SCORE_REG);
if (match)
valid.push({
user: getAuthorName(a),
size: +match[2],
language: match[1],
link: a.share_link,
});
else console.log(body);
});
valid.sort(function (a, b) {
var aB = a.size,
bB = b.size;
return aB - bB
});
var languages = {};
var place = 1;
var lastSize = null;
var lastPlace = 1;
valid.forEach(function (a) {
if (a.size != lastSize)
lastPlace = place;
lastSize = a.size;
++place;
var answer = jQuery("#answer-template").html();
answer = answer.replace("{{PLACE}}", lastPlace + ".")
.replace("{{NAME}}", a.user)
.replace("{{LANGUAGE}}", a.language)
.replace("{{SIZE}}", a.size)
.replace("{{LINK}}", a.link);
answer = jQuery(answer);
jQuery("#answers").append(answer);
var lang = a.language;
lang = jQuery('<a>'+lang+'</a>').text();
languages[lang] = languages[lang] || {lang: a.language, lang_raw: lang, user: a.user, size: a.size, link: a.link};
});
var langs = [];
for (var lang in languages)
if (languages.hasOwnProperty(lang))
langs.push(languages[lang]);
langs.sort(function (a, b) {
if (a.lang_raw.toLowerCase() > b.lang_raw.toLowerCase()) return 1;
if (a.lang_raw.toLowerCase() < b.lang_raw.toLowerCase()) return -1;
return 0;
});
for (var i = 0; i < langs.length; ++i)
{
var language = jQuery("#language-template").html();
var lang = langs[i];
language = language.replace("{{LANGUAGE}}", lang.lang)
.replace("{{NAME}}", lang.user)
.replace("{{SIZE}}", lang.size)
.replace("{{LINK}}", lang.link);
language = jQuery(language);
jQuery("#languages").append(language);
}
}
body {
text-align: left !important;
display: block !important;
}
#answer-list {
padding: 10px;
width: 290px;
float: left;
}
#language-list {
padding: 10px;
width: 500px;
float: left;
}
table thead {
font-weight: bold;
}
table td {
padding: 5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.sstatic.net/Sites/codegolf/all.css?v=ffb5d0584c5f">
<div id="language-list">
<h2>Shortest Solution by Language</h2>
<table class="language-list">
<thead>
<tr><td>Language</td><td>User</td><td>Score</td></tr>
</thead>
<tbody id="languages">
</tbody>
</table>
</div>
<div id="answer-list">
<h2>Leaderboard</h2>
<table class="answer-list">
<thead>
<tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr>
</thead>
<tbody id="answers">
</tbody>
</table>
</div>
<table style="display: none">
<tbody id="answer-template">
<tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr>
</tbody>
</table>
<table style="display: none">
<tbody id="language-template">
<tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr>
</tbody>
</table>
126
I googled google, and Google found Google on Google. Myth Busted.
– Geobits – 2015-09-28T16:32:27.15388@Geobits That is simply a test to see if I will google Google, which I will not. :D – rybo111 – 2015-09-28T16:33:22.727
Does it have to be a function? Some languages like rs only support complete programs. – kirbyfan64sos – 2015-09-29T01:09:29.393
@kirbyfan64sos An equivalent would be fine in that case – rybo111 – 2015-09-29T07:03:04.470
This conversation has been moved to chat.
– Dennis – 2015-09-29T15:40:08.307Is no output when
google
is entered valid, or does it have to give some kind of error? – ASCIIThenANSI – 2015-09-30T17:46:12.850@ASCIIThenANSI It needs to cause an error. If no output is technically an error would depend on your code. – rybo111 – 2015-09-30T17:50:46.470
7Does the function need to be case sensitive? Should it throw given 'gOOgle'? – AXMIM – 2015-09-30T22:07:02.363
2
When I type google into google (the search bar on chrome), a message came up asking if I wanted to go to https://google/. (Now that it is a tld, this makes sense i.e. https://com.google works). I clicked it and got a dns lookup error. Internet:broken!
– Craig – 2015-10-01T04:18:44.1331@AXMIM lower-case:
google
– rybo111 – 2015-10-01T07:28:59.077Oracle: 53 bytes: select length(:x)/decode(:x,'google',0,1) from dual; – Michael Broughton – 2015-10-01T19:26:00.100
2@ThomasKwa courtesy of the meta-effect and the Hot Network Questions side bar (kudos to devs, it has been a fantastic way of increasing participation) – BAR – 2015-10-02T20:18:54.223
1Why is there a close vote? "Too broad"? Other than the case sensitivity, which seems like a red herring (the question says "google" in lower-case), it looks fine. – Nick T – 2015-10-02T23:46:35.650
Unusually many querie on Google for "Google" in last few days.. it's tangetially related to everybody Googling for "Malamanteau". – Ville – 2015-10-05T21:16:42.543
I can't answer but here's my beginners prolog:
g(S,L):-S\=google,string_length(S,L). g(S,L):-throw(S).
- 56 chars, including the \n. – whitfin – 2015-10-06T08:17:38.590Does it matter what error is thrown? – Carcigenicate – 2016-11-29T16:02:08.887
What if your language ignores any errors? – None – 2017-01-24T21:24:09.037
@JackBates Improvise! :) – rybo111 – 2017-01-24T21:28:50.567
@rybo111 Any suggestions? I'm not sure what you mean by improvise. – None – 2017-01-24T21:30:05.927
2@JackBates Some of the better answers on this site are the ones that find loopholes. For example, "cause an error" doesn't strictly mean "displays an error". Your function not returning a count is technically an error in of itself. – rybo111 – 2017-01-24T21:36:11.933
Thanks for the clarification! Also I can't tell if that's an insult or not? – None – 2017-01-24T21:42:35.470
Google – Gryphon – 2017-07-10T00:40:03.353
1I'm voting to close this as unclear because it is not clear what an error means in the context of this question. – Post Rock Garf Hunter – 2018-03-04T20:12:41.983
(related meta.)
– user202729 – 2018-03-09T05:02:44.910@user202729 I'm open to edit suggestions. That's more constructive than voting to close what has proved to be a popular question IMO. – rybo111 – 2018-03-09T06:41:33.693
Probably "print more bytes to STDERR than Hello World program, content printed by compiler doesn't count"? – user202729 – 2018-03-09T06:44:09.507
@user202729 Since the function is supposed to "break", how about "if the given string is
google
it will not return its length." It doesn't matter how the unexpected behaviour is handled. – rybo111 – 2018-03-09T06:54:38.933Sounds good. So "do whatever except returning its length". – user202729 – 2018-03-09T06:56:42.223
@user202729 My concerns are that such an edit might invalidate an existing answer, or sway the difficulty of the challenge. It's unfortunate that this wasn't raised when first posted. I'll give it some thought. – rybo111 – 2018-03-09T07:04:17.347
4I'm voting to reopen this. I have seen no questions about what constitutes an error for this challenge and it already has 154 answers so I don't think it's fair to change the spec. This may not be an example of a good question but it's clear enough. If an answer really comes down to whether or not a certain output is an error it probably just won't get as many upvotes, anyway. – Poke – 2018-08-01T19:52:54.327