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.

• @Geobits That is simply a test to see if I will google Google, which I will not. :D – rybo111 Sep 28 '15 at 16:33
• Does the function need to be case sensitive? Should it throw given 'gOOgle'? – AXMIM Sep 30 '15 at 22:07
• When I type google into google (the search bar on chrome), a message came up asking if I wanted to go to google. (Now that it is a tld, this makes sense i.e. com.google works). I clicked it and got a dns lookup error. Internet:broken! – Craig Oct 1 '15 at 4:18
• I'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 Aug 1 '18 at 19:52

# Julia, 26 24 chars

Two characters shorter now, thanks to Dennis.

The challenge says ‘[...]in as few amount of Unicode characters as possible’, so I’m going for ≠ and ℕ in my solution.

Anonymous function. Just assign it to a variable:

f->f≠"google"?endof(f):ℕ


In the case of "google" you get the error message:

julia> (f->f≠"google"?endof(f):ℕ)("google")
ERROR: UndefVarError: ℕ not defined


As ordinary function (26 chars):

x(f)=f≠"google"?endof(f):ℕ


ℕ is an unassigned variable, so it produces an error message. If that doesn’t count or is against the rules, then just using !f (one char longer) instead would produce an error anyway. Boolean not is not defined for strings.

# Fuzzy Octo Guacamole, 19 bytes (non-competing)

"google".^={e}_!r_;


"google' pushes the string "google" to the stack.

. swaps the active stack.

^ gets input.

= is the rocketship operator, pushes 0 for equality, -1 for less than, and 1 for greater than as x <=> y with x as the top of the active stack, and y as the top of the inactive one.

{ and } denote an if statement, like bf's goto/if thing. Skips the code inside braces if the top of the stack is truthy.

e errors. That's it.

_ pops the stack, this time is just to remove the equality check result and show the input.

! sets the universal register to the top of the stack, or the length of the top of the stack for strings/lists.

r clears the register and pushes it back to the stack.

_ pops again, and

; prints.

So this checks for equality, then does a clumsy cast to int, and than prints the result.

• the quotes... aren't maTCHED the song of unmatched quotes will exti​nguish the voices of mor​tal man from the sp​here I can see it can you see ̲͚̖͔̙î̩́t̲͎̩̱͔́̋̀ it is beautiful t​he final snuffing of the lie​s of Man ALL IS LOŚ͖̩͇̗̪̏̈́T ALL I​S LOST the pon̷y he comes he c̶̮omes he comes the ich​or permeates all MY FACE MY FACE ᵒh god no NO NOO̼O​O NΘ stop the an​*̶͑̾̾​̅ͫ͏̙̤g͇̫͛͆̾ͫ̑͆l͖͉̗̩̳̟̍ͫͥͨe̠̅s ͎a̧͈͖r̽̾̈́͒͑e n​ot rè̑ͧ̌aͨl̘̝̙̃ͤ͂̾̆ ZA̡͊͠͝LGΌ ISͮ̂҉̯͈͕̹̘̱ TO͇̹̺ͅƝ̴ȳ̳ TH̘Ë͖́̉ ͠P̯͍̭O̚​N̐Y̡ H̸̡̪̯ͨ͊̽̅̾̎Ȩ̬̩̾͛ͪ̈́̀́͘ ̶̧̨̱̹̭̯ͧ̾ͬC̷̙̲̝͖ͭ̏ͥͮ͟Oͮ͏̮̪̝͍M̲̖͊̒ͪͩͬ̚̚͜Ȇ̴̟̟͙̞ͩ͌͝S̨̥̫͎̭ͯ̿̔̀ͅ – cat Mar 26 '16 at 12:10
• -1 for unmatched quotes ಠ_ಠ – cat Mar 26 '16 at 12:15
• @tac they represent the beginning and end of the string literal. That is intentional. – Rɪᴋᴇʀ Mar 26 '16 at 15:31
• I know that, I just don't know why anyone would willingly create a language that uses unmatched things. (I know many golfing languages do it and it makes my eyes bleed) – cat Mar 26 '16 at 15:32
• @tac I can make it work with matched ones also if you want. – Rɪᴋᴇʀ Mar 26 '16 at 15:39

# C++14, 42 chars

For giggles, as I did not see a very good c++ solution. My TI-89 solution is superior at 34 bytes

[](auto s){return s.size()/(s!="google");}


# TI 89 BASIC, 34 bytes

I did this in TI 89 for fun. There has got to be a more optimized method, but this is the best I could come up with for the moment.

f(x)


Basically abuses the fact that a string divided by itself becomes 1.

0 is necessary to remove the string from the calculation otherwise you would return ("fun" + 3) which i don't think is compliant to the rules.

• Alright, wasn't sure if I could post multiple answers to a solution @lirtosiast , thanks! – STDQ Mar 26 '16 at 20:20

## Hoon, 32 bytes

|*
a/*
?<
(lent a)


Uses a wet gate to avoid having to specify a/tape instead of a/*, assert that a doesn't equal "google" or panic, return the length of the tape.

> %.  "abc"
|*
a/*
?<
(lent a)
3
|*
a/*
?<
(lent a)
ford: build failed ~[/g/~dirdet-lasmes-digwyc-ribrux--rispyx-bitrus-bidmut-winsud/use/dojo/~dirdet-lasmes-digwyc-ribrux--rispyx-bitrus-bidmut-winsud/inn/hand /g/~dirdet-lasmes-digwyc-ribrux--rispyx-bitrus-bidmut-winsud/use/hood/~dirdet-lasmes-digwyc-ribrux--rispyx-bitrus-bidmut-winsud/out/dojo/drum/phat/~dirdet-lasmes-digwyc-ribrux--rispyx-bitrus-bidmut-winsud/dojo /d //term/1]

• Um... is that supposed to link to urbit? – Destructible Lemon Nov 20 '16 at 9:55
• @DestructibleWatermelon Yup. Urbit provides the compiler/interpreter for the Hoon language. – RenderSettings Nov 21 '16 at 4:00

# Tellurium, 13 bytes

This language is newer than the question, so non-competing I guess.

i?google|d]L^


This program gets input using i, and checks if the input is "google". If it is, it tries dividing "google" by zero (d) well, that doesn't work (duh) so it throws an error.

If the input isn't google, it outputs the length of the selected cell's value (which is the input) using L^.

## AWK, 44 bytes

func f(x){return x=="google"?f(x):length(x)}


Example usages:

awk 'func f(x){return x=="google"?f(x):length(x)}{print f($0)}' <<< "non-google string" awk 'func f(x){return x=="google"?f(x):length(x)}{print f($0)}' <<< "google"


Print: 17 and Segmentation fault (core dumped) respectively. The second one may cause your computer to run out of memory before it segfaults if you don't have oodles of RAM.

I find Segmentation fault errors to be a bit more broken than divide by zero since they don't even say where the problem is.

Something that looks more interesting would be:

func f(x){if(x=="google"){printf x;return f(x)}return length(x)}


This would print googlegooglegooglegoogle.... wrapping around the screen until it finally produces the seg. fault, but it's not as 'golfy'

NB. Yeah, I'm a bit late to the party, but nobody had an AWK answer yet. :)

• +1 for "Segmentation fault (core dumped)" -- my favourite error message – cat Mar 25 '16 at 15:46
• It's my favorite message, too... if I'm not the one that has to debug it. :p – Robert Benson Mar 25 '16 at 16:04
• On my 64-bit Ubuntu 16.04 with GAWK 4.1.3, the second one doesn't segfault but just eats all my memory until one of three things happens. A) the oom-killer kills it and says Killed, B) my window manager crashes or C) my computer overheats and switches off. Thus, I am adding a note to your answer that this answer may be harmful on machines which have a big stack. – cat Jun 28 '16 at 13:34
• Aha! That's funny. It is not that your machine has so little memory space that AWK gives up and segfaults immediately, it's that you have so much memory that AWK doesn't cause the kernel to kill it but instead tries to address so much memory that its virtual machine eventually segfaults. I only have a paltry 6 GB of RAM :P – cat Jun 28 '16 at 16:32
• It's not wrong at all, I think recursing forever and possibly forcing a shutdown is a perfectly acceptable error – cat Jun 28 '16 at 16:52

# PowerShell, 36 35 Bytes

param($a)$a.length/($a-cne'google')  This blatantly abuses PowerShell's dynamic casting and uses xnor's trick for dividing by zero. In PowerShell, this is a terminating error and halts execution tossing a most excellently-verbose error (the error, at 242 characters, is over 6.5x the size of the function itself) Attempted to divide by zero. At line:1 char:11 + param($a);$a.length/($a-cne'google')
+           ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [], RuntimeException
+ FullyQualifiedErrorId : RuntimeException


Saved a byte thanks to ConnorLSW

• you can use param($a)$a.length/($a-cne'google') to skip using the the !, saving one byte. – colsw Nov 29 '16 at 10:57 • @ConnorLSW Indeed. Thanks. – AdmBorkBork Nov 29 '16 at 15:54 # PHP, 48 bytes eval((google==$s=$argv[1])."echo strlen(\$s);");


php -r '<code>' google -->

Parse error: syntax error, unexpected 'echo' (T_ECHO) [...] in eval()'d code on line 1

54 bytes for a function:

function g($s){eval((google==$s).'echo strlen($s);');}  # JavaScript, 25 bytes p=>p=='google'?_:p.length  • “However, if the given string is google (lowercase), it will cause an error.” – Will this? – manatwork Nov 29 '16 at 15:15 • Now it matches the challenge's requirement. Sadly the generic expectation is the solutions to be either full programs or functions. In both cases the input and output has to be handle either explicitly by the code or implicitly by the interpreter. So you can not assume your code will find data in some global variable. – manatwork Nov 29 '16 at 15:38 • You can make this valid in one of two ways: 1. Make it into a full program with input and output: g=prompt();if(g!='google')alert(g.length);else throw 0 2. Make it into a function: function(g){if(g!='google')return g.length;else throw 0} – ETHproductions Nov 29 '16 at 16:44 • Also, you don't have to explicitly throw an error; calling some undefined variable, such as _, will do the trick. – ETHproductions Nov 29 '16 at 16:44 ## PHP, 56 bytes function google($i){echo strlen($i)/(__FUNCTION__!=$i);}


Yes, it's a bit long, but I feel this is really in the spirit of the question. You really mustn't google google when google is the actual function's name.

The constant __FUNCTION__ holds the name of the function, which in this case is google. The rest of the functiondisplays the length of the input $i divided by 1 if $i is not google, or by 0 if it is. The latter throws an error.

Try it at Repl.it!

# Jelly, 10 bytes

ßL⁼“æ8Ụ»$?  Try it online! (with "google" as input) Try it online! (with "bing" as input) ## How it works ßL⁼“æ8Ụ»$? - Main link. Argument: s (string)

? - if...
⁼     $- the input is equal to... “æ8Ụ» - "google" - then ß - call the main link (segfault) - else L - return the length of the input  # Aceto, 19 bytes rdM"google"J=$L€lp


Took me a while, but I finally got it. Aceto uses a hilbert curve for it's ip, which is really annoying.

r grabs input
d duplicates it
M pops the top value and stores it in quick memory
"google" pushes google on the stack, but with a lot of spaces in between.
- splits it on whitespace
J concatenates it
= pushes a bool on the stack: whether the top two values are equal
$asserts that the top value is truthy, if not it raises an error. Don't ask me why i don't have to negate it L loads the quick memory € explodes the string, putting each char as a separate val on the stack l takes the height of the stack p prints that num  Try it online! # Pyth - 15 bytes ?qz"google"'0lz  My favorite part of this is how I error. I use the ' function which takes a string, but pass 0, which is not a string. # Brachylog, 12 bytes "google"∧∈|l  Try it online! Since Brachylog simply fails a predicate that tries to divide by 0, the error here comes from trying to unify the output with a list containing an entirely un-instantiated variable instead. If the input can be unified with google, it errors, and otherwise this predicate outputs the length of the input (add one more byte w to the end to make it a full program that prints the length). ## Wd, 10 9 bytes This is a full program but is technically also a function. *↔XÑ║╜▄]ÿ  ## Explanation Decompressed: -Fl'InakS/  After string decompression: google"nakS/  google"n # Does the input *not* equal to "google"? ak # Find the length of the input S/ # Divide the length of the input by the above condition  # Clojure, 47 44 bytes Old answer #(/(count %)(get{false 1 true 0}(= %"google")))  Try it online! New Answer #(/(count %)({false 1 true 0}(= %"google")))  Removed the get function call, because you can call a hashmap with a key to get the value. Try it online! • Welcome to the site, and nice first answer! Be sure to check out our Tips for golfing in Clojure page for ways you can golf your program – caird coinheringaahing Feb 20 at 20:16 • @cairdcoinheringaahing thank you^^ – Satoshi Feb 20 at 20:19 # Vyxal, 7 bytes ‛»Ǒ≠/÷L  Try it Online! ‛»Ǒ≠/÷L ‛»Ǒ # "google" ≠ # ↑ != input / # split input into ↑ even pieces. If input is "google", this will be 0, consequently causing an error ÷L # Get the length of the item  • You might want to clarify that it's length(^) / (^ != ^^) instead of (length(^) / ^) != ^^. – Wezl Feb 22 at 16:02 # Red, 34 bytes func[g][length? any[g ="google"g]]  Try it online! (the code so dense that it crashes the program). ## CoffeeScript, 36 bytes f=(x)->throw 0if'google'==x;x.length  # Emacs Lisp, 43 bytes (lambda(s)(if(equal s"google")*(length s)))  Throws the error (void-variable *) for any string that equals google. • @FryAmTheEggman I'm sorry, I just wrote the explanation wrong, the code is correct. – nanny Sep 28 '15 at 18:55 ## STATA, 44 bytes pr de a if"google"==0' f di length(0') end  prints "unrecognized command: f" when input is "google" # Perl5, 48 bytes sub google{map{eval,-7+length}qq~&{"::$_[0]"}~}


Try it:

perl -e 'sub google{map{eval,-7+length}qq~&{"::$_[0]"}~} print google @ARGV' google  ## 47 bytes The following is one char shorter, but more fiddly on the command line: sub google{map{eval,-6+length}qq~&{"'$_[0]"}~}


# F#, 33 Characters

function"google"->0/0|s->s.Length


When "google" is provided as input, it produces a DevideByZeroException.

Usage:

let g = function"google"->0/0|s->s.Length

g "bing"   // 4


# Java 1.8, 33 bytes

(s)->s.length()/(s=="google"?0:1)


## Explanation

The lambda takes a String named s, finds the length, and if it isn't "google", divides it by one, otherwise dividing it by zero and causing an Exception.

## Usage

Note that java.util.function.Function has to be imported.

Function<String, Integer> f = (s)->s.length()/(s=="google"?0:1); //Assign function to variable
//Note that java type inferencing automatically handles the String type

System.out.println(f.apply("elgoog")); //Prints 6

• As far as I know, required imports need to be counted in the score, so your full code would be the import and the function. – Alex A. Sep 29 '15 at 15:53
• @AlexA. The thing with Java is that lambdas are weird- the question only asked for a function, and the lambda is a function. However, Java needs to squeeze the function into a functional interface with the same method signature (accepts string, returns int), and the Java.util.function.Function interface fits the bill. However, the full function is provided. (Also see codegolf.stackexchange.com/a/58981/41505) – Daniel M. Sep 29 '15 at 16:31
• You are going to have to change == to .equals unless I'm missing something with java 8 – jlars62 Oct 2 '15 at 16:55
• It worked for me using the provided implementation. It may be a subtle difference between String objects and literals, but it works for me – Daniel M. Oct 2 '15 at 17:08

# Lua, 47 bytes

print(assert(arg[1]~="google")and arg[1]:len())


Throws "assertion failed" if it's "google"

# golflua, 25 characters

\g(s)?s=="google"e""$~#s$


Sample run:

bash-4.3$golflua -e '\g(s)?s=="google"e""$~#s$w(g("google"))' golflua: (command line):1: stack traceback: [C]: in function 'e' (command line):1: in function 'g' (command line):1: in main chunk [C]: in ? bash-4.3$ golflua -e '\g(s)?s=="google"e""$~#s$ w(g("yahoo"))'
5


# PHP, 41 bytes

Because the division by zero is overused in the already existing answers, I tried to come up with something else (sacrificing 3 bytes):

<?=strlen($x=$argv[1])+log($x!="google");  It's not a function (the task explicitly asks to write a function) but it can be invoked from the command line in a functional manner: $ echo '<?=strlen($x=$argv[1])+log($x!="google");' | php -- bing 4$ echo '<?=strlen($x=$argv[1])+log($x!="google");' | php -- google -INF  It doesn't produce an error when the argument is google but it doesn't display the length of the string google either. However it displays -INF (i.e. minus infinity) and this value can be considered an error for a function that returns a length (which, by definition, is a count, i.e. a non-negative number). ### A 38 bytes PHP solution using division by zero: <?=strlen($x=$argv[1])/($x!="google");


It can be invoked in the same way as above. When the argument is google it displays a PHP warning.

• You get a +1 from me for the division-by-zero, clever! But a -1 for not making it a function as the topic states :) – Martijn Sep 29 '15 at 12:53
• -2 bytes: The quotation marks are unnecessary. – Titus Nov 29 '16 at 15:48

# Clojure - 41 40 bytes

(defn g[s](if(= s"google")(g)(count s)))


Attempts to call itself with zero arguments for the input "google".

> (g "bing")
4
clojure.lang.ArityException: Wrong number of args (0) passed to: sandbox10419$g  # Swift, 71 Bytes Short but lame: print({assert($0 != "google");return $0.characters.count}(readLine()!))  Longer (75) but not lame: print({{0/$0}($0.hash&-0x20f4f91ecf8d43)^$0.characters.count}(readLine()!))

The second one doesn't use any String literal. Works by subtracting the hash value of the input string by the "google" hashValue. Then it divides 0 by this value, resulting in a runtime error when it's 0 (0/0 = undef.) but in all other cases the result is 0 (0/x = 0). This result gets XOR'd by the character count in the String.