Don't google "google"

  • 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.

    I googled google, and Google found Google on Google. Myth Busted.

    @Geobits That is simply a test to see if I will google Google, which I will not. :D

    Does it have to be a function? Some languages like rs only support complete programs.

    @kirbyfan64sos An equivalent would be fine in that case

    This conversation has been moved to chat.

    Is no output when `google` is entered valid, or does it have to give some kind of error?

    @ASCIIThenANSI It needs to cause an error. If no output is technically an error would depend on your code.

    Does the function need to be case sensitive? Should it throw given 'gOOgle'?

    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. works). I clicked it and got a dns lookup error. Internet:broken!

    @AXMIM lower-case: `google`

    Oracle: 53 bytes: select length(:x)/decode(:x,'google',0,1) from dual;

    @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)

    Why 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.

    Unusually many querie on Google for "Google" in last few days.. it's tangetially related to everybody Googling for "Malamanteau".

    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.

    Does it matter what error is thrown?

    What if your language ignores any errors?

    @JackBates Improvise! :)

    @rybo111 Any suggestions? I'm not sure what you mean by improvise.

    @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.

    Thanks for the clarification! Also I can't tell if that's an insult or not?

    I'm voting to close this as unclear because it is not clear what an error means in the context of this question.

    @user202729 I'm open to edit suggestions. That's more constructive than voting to close what has proved to be a popular question IMO.

  • Python 2, 29

    lambda x:len(x)/(x!='google')

    Gives a ZeroDivisionError on "google", and the length otherwise. This takes advantage of Python's booleans equaling 0 and 1.

    +1. You need to set `g` to your `lambda`, or call it anonymously with an input, though.

    Just for fun, I tried this technique with JavaScript ES6. It ended up at 25, but returns `Infinity` for "google" instead of throwing an error...

    @ZachGates The consensus on meta is that anonymous functions are allowed unless specifically disallowed. Since the question does seem to imply something like this (but doesn't explicitly disallow it, yet), you should ask the OP about it.

    @Kevin you would need a `return` if you used `def`

    It's anonymous if you don't set it to anything (a `def` is not). A lambda like this has to be called at creation. @Kevin

    Amusingly, this translated to Pyth does better than my prior best Pyth solution. It is `L/lbnb"google`, 13 bytes.

    Nice one, exactly what I was thinking as soon as I saw this question :)

  • Excel, 23 characters

    Paste this into a cell other than A1 and type your search query into A1.


    For example:


    What's the general consensus on using Excel?

    @BetaDecay. Creative, uncommon, seems to work. Won't be applicable to all CG puzzles, but it is here!

    Managers love it!

    So Efficient, CG Users Will Hate You For It. But Wait. B3 Will Change Your Life Forever!

    What's the specific concensus on using Excel?

    This is certainly not the first time I've used it. I figure there's MB of libraries behind any other language used here, so this is no different. The "source code" is the code typed in cells required to meet the criteria. If code is typed in multiple cells, I count another byte per cell as a separator (like semi-colon or new-line.) If a challenge required "output for 1 to 100", copying and pasting the code into 100 cells and using the ROW() command as a counter variable is reasonable. Excel has poor recursion and variable handling so is rarely a good choice for challenges.

    If the formula is typed into file 'google.csv' and you open The file with Excel, …

  • C#, 43 bytes

    An improvement over Salah Alami's answer. Recurses to throw a stack overflow exception on providing "google"

    int g(string s)=>s!="google"?s.Length:g(s);

    Yeah I thought that was a pretty clever way to save some characters to throw an exception. At 4 characters, it might be the smallest way to throw an exception in C#, not sure.

    This is clever! However, recent versions of C# have support for tail recursion so this function will never throw `StackOverflowException`. In fact it will never return (behaves as `while(true){}`).

    @NightElfik interesting! Running in LinqPad I get the message `Query ended because an uncatchable exception was thrown`, and in VS2015 I do get an SOException but I can't catch it when i wrap in a try-catch. Very interesting, thanks for the info! It still crashes the process though, which means it still kind of behaves like an exception, just an uncatchable one. Here's a screenshot of my test code

    @DLeh Tail recursion calls are little bit tricky. You have to run on x64 JIT and without debugger (debugger attached will cause tail recursion to not work for obvious reasons). Here is my program as a proof: and little more reading about tail recursion: ;)

    Haha: g(string)... I'll see myself out...

    @DLeh after battling for a while I think you are right.. the closest I can get is 44 bytes by causing division by zero like so int g(string s)=>s.Length/(s=="google"?0:1);

    or 45 bytes causing a null reference exception int g(string s)=>(s=="google"?null:s).Length;

    @lee yeah but that's longer :)

    @DLeh oh wait no I can match your 43 bytes but not beat it. :) int g(string s)=>s!="google"?s.Length:s[9];

  • Pyth, 14 13 characters


    Defines a named function y.

    This divides the length by 1 if the string is not google and by 0 otherwise. The idea is not novel, but I came up with it independently.

    Try it online.

    How it works

    L                 Define y(b):
    lb Compute len(b).
    nb"google Compute (b != "google").
    / Set _ = len(b) / (b != "google").
    Return _. (implicit)

    Yeah I'm actually unsure about this, I don't think it's happened before with a string. Normally you could close it with `;` but obviously you can't here...

    You don't need the end quote.

    @Maltysen I think I do. The function definition wouldn't be complete without it.

    But it is complete. merely unusable ;)

    "*Defines a named function `y`.*" But there's no `y` in your code!?

    @A.L That's correct. The built-in `L` redefines the function `y`.

    I'm not sure, but I think I hate Pyth.

    @MrLister Yes. Implicit commands. .__.

    I can't find this Pyth language on the Intartubes.

    @Prof.Falken Just click the *Pyth* link at the top of the online interpreter.

    @Dennis. I see. Well, I guess Pyth could get extended to add an operator to solve ...

    I don't get any output when trying the link. Also, you don't seem to need the closing `"`

    @FelixDombek The code is interpreted on the server side, which could fail for a lot of reasons. It's working fine for me. (You're the third person to suggest that I can remove the `"`. I'm not sure why I'm fighting this.)

    @Dennis ah, my bad, I thought I needed to put the test string in the "input" field

    @FelixDombek I've edited the permalink. Input now comes from *Input*.

    len(b)/(b != google) : utterly awesome (possibly not the originator in this answer, but the first I've read)

  • MATLAB, 63 41 40 38 36 bytes

    Thanks to Tom Carpenter for shaving off 1 byte!

    Thanks to Stewie Griffin for shaving off 2 bytes!


    Unlike the other more elegant solutions, performing a division by zero operation in MATLAB will not give an error, but rather Inf. This solution finds the length of the string by nnz. The string that is produced is in such a way that you index from the beginning of the string to the end, which is essentially a copy of the string. However, what is important is that the beginning of where to access the string is produced by checking whether or not the input is equal to 'google'. If it isn't, this produces a beginning index of 1 and we index into the string normally... as MATLAB starts indexing at 1. Should it be equal, the index produced is 0 and MATLAB will throw an indexing error stating that the index needs to be a positive integer. The extra + is to ensure that the output of the equality check is numerical rather than Boolean/logical. Omitting the + will produce a warning, but because this challenge's specifications doesn't allow for warnings, the + is required... thus completing the code.

    Example uses

    >> [email protected](x)nnz(x(+~strcmp('google',x):end)) %// Declare anonymous function

    f =


    >> f('bing')

    ans =


    >> f('google')
    Subscript indices must either be real positive integers or logicals.

    Error in @(x)nnz(x(+~strcmp('google',x):end))

    A more fun version, 83 77 76 74 72 bytes

    Thanks to Tom Carpenter for shaving off 1 byte!

    Thanks to Stewie Griffin for shaving off 2 bytes!

    @(x)eval('if strcmp(''google'',x),web([x ''.com/i'']);else nnz(x),end');

    The above isn't an official submission, but it's something that's a bit more fun to run. Abusing eval within anonymous functions, what the code does is that it checks to see if the input string is equal to 'google'... and if it is, this will open up MATLAB's built-in web browser and shows Google's 404 error page trying to access the subpage located at i when that doesn't exist. If not, we display the length of the string normally.

    Example uses

    >> [email protected](x)eval('if strcmp(''google'',x),web([x ''.com/i'']);else nnz(x),end'); %// Declare anonymous function
    >> f('bing')

    ans =


    >> f('google')

    The last call using 'google' gives us this screen:

    enter image description here

    You could save a byte by using ```strcmp``` instead of ```isequal```.

    @TomCarpenter - Funny. I actually told myself to use `strcmp` but ended up using `isequal` for some reason.... thanks!

    `nnz` is two bytes shorter than `numel`. You had my vote a few years ago :-)

  • JavaScript ES6, 34 27 25 characters


    Throws a ReferenceError on Δ for google.


    You could use a ternary operator to save two bytes.

    Nice one thanks

    Yay, that's exactly what I just got. If you want to be fancy, use a symbol people never use instead of g to be sure it won't exist as a global variable. Δ makes for a good variable name :)

    `Δ = "google is evil";` besides, I believe that these programs are self-contained.

    I would use `Δ` but it is 2 bytes :(

    @Jonathan The question wants you to count "unicode characters", so I guess it's not talking about bytes, but code points. :)

    You could use `#`, it errors in JS afaik

    Δ Google Illuminati confirmed

    f=>f!='google'||f.length (not an error, but false...)

  • TI-BASIC, 15 bytes

    Heck, while we're at it, might as well get a TI-BASIC answer in here.

    Input format is "string":prgmNAME. Credit to Thomas Kwa for finding it first!


    (Guide: add 1 byte for each lowercase letter replacing an upper-case one. So s/GOOGLE/google/g => +6 bytes.)

    ahhhhh test cases!


    20 bytes: `length(Ans)/(Ans≠"google`. You also have the case wrong; if uppercase is allowed it's 14 bytes. By the way, it's valid to pass arguments through Ans.

    `AGOOGLE` should give 7, correct? And you shouldn't be counting the program header in your code size, so subtract 10 bytes.

    *ERROR:* I was thinking substrings. Kindly forgive me.

    @ThomasKwa I didn't see your comment with the code. It just so happens that we both stumbled upon the same solution. However, if you believe you deserve the credit, the credit shall be yours. ^_^ (*EDIT* If you would read the code, it isn't the exact same thing.)

    @lirtosiast `length(` is two bytes, that would make your numbers 21 and 15 bytes.

    @Timtech My mistake; Conor corrected me anyway.

  • Python 3, 30 bytes

    lambda u:[len][u=='google'](u)

    Indexes the 1-element function list, raising an IndexError if the u=='google' predicate is True (= 1). Such functional.

    Much variants. Wow:

    lambda u:[len(u)][u=='google']
    lambda u:len([u][u=='google'])

    If the challenge was inverted (error on everything not "google"), could save a char:

    lambda u:{'google':len}[u](u)

    But you already know the length, so just hardcode it.

    works for python 2.7 too

    For some reason, I love this solution.

  • APL (14)



    • : length

    • ÷: divided by

    • 'google∘≢: argument is not equal to 'google'.

    gives the length of the string, which is divided by 1 if the string does not equal google (which gives the length back unchanged), or by 0 if the string does equal google (giving an error).

    I think you don't need to count the parens, as it can be assigned to a variable without them.

    Kind-of fails on single-char arguments. Fix by replacing `⍴` with `≢`. Also, you can make it cooler-looking by swapping the operands of `∘`. Oh, don't forget to remove the parens. All-in-all: `≢÷≢∘'google'`

  • Haskell, 24 bytes

    g s|s/="google"=length s


    Main> g "google"

    Program error: pattern match failure: g "google"

    Main> g "bing"

License under CC-BY-SA with attribution

Content dated before 7/24/2021 11:53 AM

Tags used