158
\$\begingroup\$

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.

\$\endgroup\$
  • 123
    \$\begingroup\$ I googled google, and Google found Google on Google. Myth Busted. \$\endgroup\$ – Geobits Sep 28 '15 at 16:32
  • 87
    \$\begingroup\$ @Geobits That is simply a test to see if I will google Google, which I will not. :D \$\endgroup\$ – rybo111 Sep 28 '15 at 16:33
  • 7
    \$\begingroup\$ Does the function need to be case sensitive? Should it throw given 'gOOgle'? \$\endgroup\$ – AXMIM Sep 30 '15 at 22:07
  • 2
    \$\begingroup\$ 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! \$\endgroup\$ – Craig Oct 1 '15 at 4:18
  • 4
    \$\begingroup\$ 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. \$\endgroup\$ – Poke Aug 1 '18 at 19:52

146 Answers 146

1
\$\begingroup\$

05AB1E, 10 bytes

“Š¹“Qi.0ëg

Try it online!


“Š¹“       # Push "google".
    Qi   # If input == "google"...
      .0   # Divide by 0.
     ë   # Else...
      g    # Return length of input.
\$\endgroup\$
  • \$\begingroup\$ ....0 is divide by 0? Why? \$\endgroup\$ – ETHproductions Oct 17 '17 at 19:06
1
\$\begingroup\$

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!

\$\endgroup\$
1
\$\begingroup\$

Common Lisp, 50 bytes

(lambda(n)(assert(not(equal"google"n)))(length n))

Try it online!

\$\endgroup\$
1
\$\begingroup\$

C, 35 bytes

-Df(s)=strlen(s)/strcmp(s,"google")

Compiler flag :) Returns the length of the string provided. If it's google it throws a Floating point exception.

\$\endgroup\$
  • 2
    \$\begingroup\$ How does this return the length of the string? It seems like this would return a negative result in some cases. \$\endgroup\$ – Esolanging Fruit Mar 8 '18 at 21:38
1
\$\begingroup\$

SmileBASIC, 41 38 bytes

DEF G(S)RETURN LEN(S)/(S!="google")END

If S is not google, the length is divided by 1. Otherwise it's divided by 0, causing an error.

G("SAND")
4

G("google")
Divide by 0 in 0:1
\$\endgroup\$
1
\$\begingroup\$

Stax, 9 bytes

ôK▓ÿσ▄Ω?╠

Run and debug online!

Well, defeated some of the 10-byters ...

Explanation

Uses the unpacked version to explain.

`!:X9`|^hy%
`!:X9`         "google"
      |^       array xor (can't use `-` here, which means **set** difference)
        h      head of array, error if it is empty
         y%    length of input
\$\endgroup\$
1
\$\begingroup\$

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

\$\endgroup\$
0
\$\begingroup\$

Groovy, 32 chars

e={(it=='google'?0:it).length()}

A similar approach like a bunch of others here. The closure tries to return the length of either

  • the passed string, if it's not equal to google
  • the length of the integer 0, if the passed string is equal to google, which results in a MissingMethodException as there's no Integer.length() method

Tested with

e('bing')
e('google')
e('duckduckgo')
\$\endgroup\$
  • \$\begingroup\$ No need to assign to a variable, anonymous functions are allowed. .size() is shorter than .length() and is still not implemented for integers. {(it=='google'?0:it).size()} \$\endgroup\$ – manatwork Mar 25 '16 at 9:24
0
\$\begingroup\$

Ruby, 41 characters

def g(s)puts s!="google"?s.length: a end
\$\endgroup\$
0
\$\begingroup\$

Rust, 170 bytes

I didn't see anything in rust, so I figured I'd make one. Hopefully someone can improve this a bit! (or perhaps a byte... see what I did there ;) )

use std::io;fn main(){let s=io::stdin();let mut b=String::new();s.read_line(&mut b).unwrap();let l=b.len();b.truncate(l-2);assert!(b!="google");println!("{}", b.len());}

Throws the error:

thread '<main>' panicked at 'assertion failed: buffer != "google"', main.rs:7

when you try "google."

Ungolfed Code:

use std::io;
fn main() {
    let stdin = io::stdin();
    let mut buffer = String::new();
    stdin.read_line(&mut buffer).unwrap();
    let leng = buffer.len();
    buffer.truncate(leng-2);
    assert!(buffer!="google");
    println!("{}", buffer.len());
}
\$\endgroup\$
0
\$\begingroup\$

Clojure (43 bytes)

I know Clojure has been answered before, and shorter too, but I love the way this one generates a StackOverflow error when "google" is passed in:

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

Example:

user=> (f "google")

StackOverflowError user/f (NO_SOURCE_FILE:213)

\$\endgroup\$
0
\$\begingroup\$

Powershell 34 Bytes

param($a)$a.length*($a-ne'google')

Multiply length by 0,1 boolean value of not-equal google.

Gives 0 if google (not case-sensitive), length otherwise.

\$\endgroup\$
  • \$\begingroup\$ Your answer should be case-sensitive, and should throw an actual error for google. \$\endgroup\$ – lirtosiast Oct 8 '15 at 10:33
0
\$\begingroup\$

𝔼𝕊𝕄𝕚𝕟, 15 chars / 21 bytes (noncompetitive)

ï≔`google`?$:ïꝈ

Try it here (Firefox only).

\$\endgroup\$
  • \$\begingroup\$ why noncompetitive? \$\endgroup\$ – cat Nov 21 '15 at 22:17
  • 1
    \$\begingroup\$ Language was made after the challenge. \$\endgroup\$ – Mama Fun Roll Nov 21 '15 at 22:19
0
\$\begingroup\$

ಠ_ಠ, 110 bytes (noncompetitive)

ಠ_ಠ
ಠgoogleಠ
ಠ=ಠ
ಠ"ಠ
ಠ$ಠ
ಠ$ಠ
ಠ11ಠ
ಠ13ಠ
ಠ(ಠ
ಠ?ಠ
ಠ_ಠ
ಠ#ಠ
ಠ^ಠ

Try it here.

\$\endgroup\$
0
\$\begingroup\$

Game Maker Language, 60 bytes

a=argument0;if a="google"a=1/0show_message(string_length(a))
\$\endgroup\$
  • 2
    \$\begingroup\$ "Your task is to create a function that accepts 1 string..." This does not meet specs, but all you would have to do is replace the first and last a with argument0. \$\endgroup\$ – GamrCorps Nov 21 '15 at 23:59
  • 1
    \$\begingroup\$ You're right, fixed. \$\endgroup\$ – Timtech Nov 23 '15 at 11:40
0
\$\begingroup\$

MS T-SQL, 63 bytes

CREATE PROC g @p CHAR(9) AS SELECT IIF(@p='google',1/0,LEN(@p))

Ungolfed code;

CREATE PROC g
    @p CHAR(9)
AS
    SELECT IIF(@p='google',1/0,LEN(@p))

Called by either EXEC g 'string' or simply g 'string'

Command: EXEC g 'gogle'

Result: 5

Command: EXEC g 'google'

Result: Msg 8134, Level 16, State 1, Procedure g, Line 1 Divide by zero error encountered.

\$\endgroup\$
0
\$\begingroup\$

Factor, 55 53 49 bytes

: g ( s -- l ) dup length swap "google" = not / ;

Throws the string "google". throw happens to be the same length as 0 0 /. Or I could just divide a string by a number... Or I could just divide the length of the string by if it's not google...

Example use:

cat @ mint-kitty : ~/projects/factor/factor(master??!) $ ./factor -run=listener
Factor 0.98 x86.64 (1742, heads/master-0bb3228063, Tue Mar 15 14:18:40 2016)
[Clang (GCC 4.2.1 Compatible Ubuntu Clang 3.6.2 (tags/RELEASE_362/final))] on linux
IN: scratchpad : dont-google ( str -- int ) dup "google" = [ throw ] [ length ] if ; 
IN: scratchpad "bing" dont-google

--- Data stack:
4
IN: scratchpad "google" dont-google
google

Type :help for debugging help.

--- Data stack:
4
IN: scratchpad 

The error in the UI:

enter image description here

\$\endgroup\$
0
\$\begingroup\$

Scratch, 16 bytes

Script
(scoring used)
Stopping the project is the closest thing to an error that I can get.

\$\endgroup\$
  • \$\begingroup\$ I think this should be 6 bytes + "google" = 12 bytes... \$\endgroup\$ – m654 May 24 '16 at 14:56
  • \$\begingroup\$ Google=6 bytes, 6 blocks, 2 operators, 2 variables. 6+6+2+2=8+8=16 \$\endgroup\$ – weatherman115 May 24 '16 at 15:01
  • \$\begingroup\$ Shouldn't you use a divide by 0 error (only one I know of)? \$\endgroup\$ – Julian Lachniet Jan 11 '17 at 0:42
  • \$\begingroup\$ Does not error in Scratch v456. \$\endgroup\$ – CalculatorFeline May 29 '17 at 23:21
0
\$\begingroup\$

C++, 77 Bytes

#include <string>
int G(std::string S){1/int(S!="google");return S.length();}
\$\endgroup\$
0
\$\begingroup\$

Python 2, 46 bytes

a=raw_input()
print len(a)if a!='google'else b

Errors out on input of "google" because b is an undefined identifier, but succeeds on other strings since control never reaches b in those cases.

\$\endgroup\$
0
\$\begingroup\$

Syms, 18 bytes

{[#~{google}=!1/]}

Try it online!

\$\endgroup\$
0
\$\begingroup\$

shortC, 21 bytes

Df(s)Ss)/Ms,"google")
  • Df(s) defines a function f that takes one argument s.
  • Ss) calculates the length of s.
  • Ms,"google) compares s to "google".
  • Ss)/Ms,"google") divides the length of s by 0 if the strings are equal. This throws floating point exception if they're equal.

Try it online!

\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$
0
\$\begingroup\$

Excel VBA, 52 Bytes

Subroutine that take input a as unassigned/variant of expected type variant/String and outputs to the VBE Immediates window. If a = "google" throws Run-time error '3': Return without GoSub else outputs the length of the input a.

Sub g(a)
If a="google"Then Return
Debug.?Len(a)
End Sub
\$\endgroup\$
0
\$\begingroup\$

Bash

[ $1 == google ]; return $((${#1}/$?))
\$\endgroup\$
0
\$\begingroup\$

GameMaker Language, 46 bytes

x=argument0
return string_length(x)/x!=google

Uses the same logic as this Python answer, abusing GMLs type coercion making the function divide by zero if the string equals google

I also haven't and can't test this because I currently don't have GameMaker installed, just made this from memory

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.