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

169 Answers 169

1 2 3 4 5
6
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\$
1
  • \$\begingroup\$ Your answer should be case-sensitive, and should throw an actual error for google. \$\endgroup\$
    – lirtosiast
    Commented Oct 8, 2015 at 10:33
0
\$\begingroup\$

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

ï≔`google`?$:ïꝈ

Try it here (Firefox only).

\$\endgroup\$
2
  • \$\begingroup\$ why noncompetitive? \$\endgroup\$
    – cat
    Commented Nov 21, 2015 at 22:17
  • 1
    \$\begingroup\$ Language was made after the challenge. \$\endgroup\$ Commented Nov 21, 2015 at 22:19
0
\$\begingroup\$

ಠ_ಠ, 110 bytes (noncompetitive)

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

Try it here.

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

Game Maker Language, 60 bytes

a=argument0;if a="google"a=1/0show_message(string_length(a))
\$\endgroup\$
2
  • 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
    Commented Nov 21, 2015 at 23:59
  • 1
    \$\begingroup\$ You're right, fixed. \$\endgroup\$
    – Timtech
    Commented Nov 23, 2015 at 11:40
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\$
4
  • \$\begingroup\$ I think this should be 6 bytes + "google" = 12 bytes... \$\endgroup\$
    – m654
    Commented May 24, 2016 at 14:56
  • \$\begingroup\$ Google=6 bytes, 6 blocks, 2 operators, 2 variables. 6+6+2+2=8+8=16 \$\endgroup\$ Commented May 24, 2016 at 15:01
  • \$\begingroup\$ Shouldn't you use a divide by 0 error (only one I know of)? \$\endgroup\$ Commented Jan 11, 2017 at 0:42
  • \$\begingroup\$ Does not error in Scratch v456. \$\endgroup\$ Commented May 29, 2017 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\$

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\$

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

Keg, 16 bytes

:`google`=[0/|÷!

Try it online!

Attempt ruined by the fact that google isn't in Keg's dictionary. That's why I use DuckDuckGo, which at least has parts of it's name in the dictionary!

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

Lua, 35 bytes

load'return #(...=="google"or ...)'

Try it online!

Idea is like one in @QuertyKeyboard's answer, but better optimized: load is used to create function and inverting check for "googleness" allows us to save one byte by replacing and with or.

Again, if full programs are allowed, we can cut down to 29 bytes!

Lua, 29 bytes

print(#(...=="google"or ...))

Try it online!

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

VBScript, 47 bytes

sub f(x)
msgbox len(x)/abs(x<>"google")
end sub

VBScript will return -1 for True, so we have to abs() it. And attempts to divide it by the length, like the python answer.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Why abs than -? \$\endgroup\$
    – l4m2
    Commented Apr 10, 2021 at 10:24
0
\$\begingroup\$

Knight, 17 bytes

O/L=gP!?g"google"

Try it online!

Requires a patch to fix an errata in the """golfed""" C interpreter, where it will leave a newline in the input from PROMPT. This matches the behavior of the AST interpreter and the wording of the spec.

This is the simple division by zero method.

# print length(g = prompt()) / (g != "google")
OUTPUT / (LENGTH(= g PROMPT)) (! (? g "google"))

Relies on division by zero throwing an exception. The AST interpreter will always throw an exception in software, but for the golfed interpreter, it requires the x86 behavior of throwing an exception on the hardware level.

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

Acc!!, 123 bytes

-1
Count i while _/256^i {
_+N*256^(i+1)+1
}
_%256+0/(_/256-2926266033336167)
Write _/100+48
Write _/10%10+48
Write _%10+48

Try it online! (Note: The official interpreter occasionally, but not always, adds a trailing newline to the end of user input, and it is impossible to tell from within the language if the newline was manually entered or automatic. Thus, an input of "bing" becomes "bing\n" and evaluates to length 5.)

Example output (with input "google")

Acc!!

Traceback (most recent call last):
  File "/opt/accbb/acc.py", line 108, in <module>
    main()
  File "/opt/accbb/acc.py", line 15, in main
    exec(code, {"inputStream": inputStream()})
  File "<string>", line 8, in <module>
ZeroDivisionError: integer division or modulo by zero

Explanation

# Accumulator has length at _%256, followed by input bytes starting at _/256.
# Read all input
-1 # Trigger loop to start and also correct off-by-one
Count i while _/256^i { # While last byte read nonzero:
    _ + N*256^(i+1) + 1 # Read another byte and increment length
}

# Isolate length byte, and divide by zero if the input was google
_%256 + 0/(_/256-2926266033336167)

# Print length byte
Write _/100+48
Write _/10%10+48
Write _%10+48
\$\endgroup\$
1
  • \$\begingroup\$ This also crashes on googel, and zzbaae \$\endgroup\$ Commented May 22 at 5:52
0
\$\begingroup\$

C (gcc), 49 bytes

#define f(x)""[!strcmp(x,"google")<<31]+strlen(x)

Try it online!

Not a very great answer.

If the string is google, this tries to access the 2147483649th character in the string "\0", which causes a segmentation fault, otherwise the length of the string is returned.

\$\endgroup\$
2
  • \$\begingroup\$ Thanks for your program. \$\endgroup\$
    – None1
    Commented May 23 at 23:22
  • \$\begingroup\$ 42 bytes \$\endgroup\$
    – ceilingcat
    Commented May 25 at 1:27
1 2 3 4 5
6

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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