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\$
  • 122
    \$\begingroup\$ I googled google, and Google found Google on Google. Myth Busted. \$\endgroup\$ – Geobits Sep 28 '15 at 16:32
  • 86
    \$\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

4
\$\begingroup\$

Japt, 14 bytes

U¥`goog¤`?Þ:Ul

Try it here!

Explanation

U¥`goog¤`?Þ:Ul  
U                     // U is the input
 ¥                    // ¥ is the Unicode shortcut for ==
  goog¤               // "google" compressed
 `      `             // backticks are used to uncompress goog¤
          Þ           // An undefined variable
            Ul        // l is a built-in that returns the length of U
                      // Implicit: output result of last expression
\$\endgroup\$
  • \$\begingroup\$ Can you explain? \$\endgroup\$ – Adám Jan 10 '17 at 23:10
  • 1
    \$\begingroup\$ @Adám Added an explanation :) \$\endgroup\$ – Oliver Jan 10 '17 at 23:40
3
\$\begingroup\$

C# .NET, 65 bytes

int g(string s){return(s!="google")?s.Length:(1/int.Parse("0"));}

Test:

Console.WriteLine(g("str"));
Console.WriteLine(g("google"));
\$\endgroup\$
  • 1
    \$\begingroup\$ with C# 6.0 you can save a few characters by doing an expression-bodied method: int g(string s)=>s!="google"?s.Length:(1/int.Parse("0")); \$\endgroup\$ – DLeh Sep 28 '15 at 19:18
  • 3
    \$\begingroup\$ also you could cause an exception by recursing, eventually you'll get a stack overflow int g(string s)=>s!="google"?s.Length:g(s); \$\endgroup\$ – DLeh Sep 28 '15 at 19:19
  • 1
    \$\begingroup\$ You can make it crash faster by just calling int.Parse(""). Although the infinite recursion strategy would be even shorter. \$\endgroup\$ – sara Sep 29 '15 at 9:23
  • \$\begingroup\$ @kai Something like "".Length is shorter than int.Parse(""). \$\endgroup\$ – Bob Sep 29 '15 at 12:41
  • 1
    \$\begingroup\$ @Bob "".Length just returns 0, it doesn't throw. \$\endgroup\$ – DLeh Oct 1 '15 at 13:03
3
\$\begingroup\$

PHP 5.4+, 40 49 bytes

This one is quite unexpectedly long.

function g($x){echo[strlen($x),[]][$x==google];}

To run it, just pass a value:

g($argv[1]);

Test-cases:

$php google.php googlea
7
$php google.php google
Notice: Array to string conversion in [...][...] on line 1
Array

I'm not sure if this can ve considered an error for this challenge or not.


One could use the over-used boring method:

function g($x){echo strlen($x)/$x==google;}

Which gives me a size of only 43 bytes.


As an alternative, one could use an exit error code, using the exit construct, for also 49 bytes:

function g($x){exit($x==google?strlen($x).'':1);}

All values between 1 and 254 are valid error codes that PHP uses when exiting.

\$\endgroup\$
  • \$\begingroup\$ You could use function g($x){echo$x==google?a():strlen($x);} which is 46 bytes (3 bytes shorter) and throws an actual error and not just a warning. Also your second example function g($x){echo strlen($x)/$x==google;} should be: function g($x){echo strlen($x)/($x!=google);}. / has a higher precedence than == and you have to switch it to !=as well to make it work. \$\endgroup\$ – insertusernamehere Nov 16 '15 at 22:53
3
\$\begingroup\$

Common Lisp, 50 48 bytes

(defun g(s)(if(equal s"google")(g s)(length s)))

Ungolfed:

(defun g(s)                ; Define a function g with one argument s
    (if (equal s "google") ; If s == "google",
        (g s)              ; Then recurse!
        (length s)         ; Else, return the length of s in characters
    )
)

Defines a function g that accepts one string as input. If it is not exactly 'google', then it returns the length. Otherwise, it will call itself on the same string, creating an infinite recursive loop and eventually causing a stack overflow error.

Usage (inside an interactive CLISP shell):

>(load "google.lsp")
Finished loading.
>(g "foo")
3
>(g "Lisp is cool!")
13
>(g "google")
*** - Program stack overflow. RESET

This is pretty long compared to the others, but using recursion seemed fitting for Lisp.

\$\endgroup\$
  • 3
    \$\begingroup\$ This won't cause a stack overflow in some Common Lisp implementations (CCL and SBCL won't overflow). \$\endgroup\$ – nanny Sep 28 '15 at 20:57
3
\$\begingroup\$

Scala, 48

def g(s:String)=if(s=="google")0/0 else s.length

To call:

 g("bing")      //> res1: Int = 4
 g("google")    //> java.lang.ArithmeticException: / by zero
\$\endgroup\$
  • \$\begingroup\$ you can use an anonymous function, and shave off 4 characters. also, it will be more neat, IMHO, to use ??? rather than 0/0. \$\endgroup\$ – Jacob Mar 27 '16 at 14:55
3
\$\begingroup\$

C - 99 75 67 63 bytes

g(char*s){int i=0,j=*s;while(s[i])j^=s[++i];return j^9?i:g(s);}

Ungolfed:

int g(char *s){
    int i = 0,
        j = s[0]; // first char of s
    while(s[i] != '\0'){
        j ^= s[++i]; // j = j XOR [next char]
    }
    if(j != 9){ // g^o^o^g^l^e = 9
        return i;
    } else {
        g(s); // infinite recursion
    }
}

Using it:

g("bing")       // 4
g("duckduckgo") // 10
g("google")     // segmentation fault

Will return the length if g XOR o XOR o XOR g XOR l XOR e isn't 9, else it will infinite recurse causing a stack overflow.

How it works

I used a (very) crappy hashing algorithm (a XOR b XOR c ...) to get a single number from the first six chars of the string. Collision likelyhood is very high, but if we assume the string has to be the name of an actual search engine, then the collision likelyhood is fairly low.

So, first we have i, which iterates through the string until s[i] is NUL (0), and while counting the length of the string, the selected char is XOR'd onto j, which is initialized to the first char in s. For "google", the result of this operation is 9.

The main thing this takes advantage of is the way pointers work in C. A pointer is actually a large number that denotes an address in memory, &var will return a pointer to var, while *var will dereference a pointer (grab the value at that address). Thus, *s is equivalent to s[0]. I think this is where most beginners get lost, confused and frustrated when it comes to C, like when you try to send a struct * to a function that takes struct * by sending &struct_ptr, which would be a memory address to a memory address, effectively making the compiler give an error such as function expects 'struct *' but got 'struct **'.

\$\endgroup\$
  • \$\begingroup\$ Also, this function uses no library functions. I assume using strlen and strcmp is cheating and/or not worth the #include <string.h>. \$\endgroup\$ – Braden Best Sep 29 '15 at 21:39
  • \$\begingroup\$ I think you need to give correct output for every string other than google. \$\endgroup\$ – lirtosiast Sep 29 '15 at 21:50
  • \$\begingroup\$ @ThomasKwa Ah well, I tried. \$\endgroup\$ – Braden Best Sep 29 '15 at 22:02
3
\$\begingroup\$

bash + coreutils, 57 49 38 36 33 bytes

[ "$1" = google ];expr ${#1} / $?

Calling it with google produces expr: division by zero.

The quotes around $1 are required in order to handle the empty string correctly.

The use of ${#1} is shamelessly stolen from Tarod's answer.

\$\endgroup\$
  • \$\begingroup\$ Good! But I think you're mixing bash & linux commands. If you want to, check my answer where I'm using bash to count characters. Happy coding! :) \$\endgroup\$ – Tarod Sep 29 '15 at 21:57
  • \$\begingroup\$ @Tarod You're right, I should have said bash + coreutils. Combining your use of Bash to count the length of the input with my use of expr allows a shorter answer than either though - see my edit. – Ben just now edit \$\endgroup\$ – Ben Sep 29 '15 at 22:22
  • \$\begingroup\$ Amazing! :D I'm going to upvote you! Good job, Ben! :) \$\endgroup\$ – Tarod Sep 30 '15 at 6:08
  • \$\begingroup\$ BTW, I think you should set a new title for your answer. Just 'bash'. \$\endgroup\$ – Tarod Sep 30 '15 at 11:55
  • \$\begingroup\$ I don't think so - expr seems to be part of coreutils. \$\endgroup\$ – Ben Sep 30 '15 at 13:16
3
\$\begingroup\$

O, 20 characters

{."google"={@}{.e}?}

Sample run:

bash-4.3$ java xyz.jadonfowler.o.O <( echo '{."google"={@}{.e}?} :f "yahoo" f o' )
5

bash-4.3$ java xyz.jadonfowler.o.O <( echo '{."google"={@}{.e}?} :f "google" f o' )
java.lang.ArrayIndexOutOfBoundsException: Can't pop from empty stack!
        at xyz.jadonfowler.o.Stack.pop(O.java:980)
        at xyz.jadonfowler.o.O.parse(O.java:407)
        at xyz.jadonfowler.o.CodeBlock.run(O.java:1031)
        at xyz.jadonfowler.o.O.parse(O.java:735)
        at xyz.jadonfowler.o.CodeBlock.run(O.java:1031)
        at xyz.jadonfowler.o.O.parse(O.java:99)
        at xyz.jadonfowler.o.O.runFile(O.java:37)
        at xyz.jadonfowler.o.O.main(O.java:20)
java.lang.ArrayIndexOutOfBoundsException: Can't pop from empty stack!
        at xyz.jadonfowler.o.Stack.pop(O.java:980)
        at xyz.jadonfowler.o.O.parse(O.java:425)
        at xyz.jadonfowler.o.O.runFile(O.java:37)
        at xyz.jadonfowler.o.O.main(O.java:20)
\$\endgroup\$
3
\$\begingroup\$

Scala, 36 34 30 29 characters

s=>(Set(s)-"google").max.size

Calling this function on "google" produces java.lang.UnsupportedOperationException: empty.max

\$\endgroup\$
  • \$\begingroup\$ Nice idea! However, I'm afraid it does not compile, one must declare s's type: (s:String)=>... When I copy-paste it to scala's REPL I get <console>:1: error: ';' expected but '=>' found. \$\endgroup\$ – Jacob Mar 27 '16 at 14:58
  • \$\begingroup\$ @Jacob it compiles if the type of s can be inferred from the context, e.g.: val s: String => Int = s=>(Set(s)-"google").max.size. \$\endgroup\$ – Ben Mar 27 '16 at 21:57
  • \$\begingroup\$ If you want us to use it like that, you must add val s:String=>Int= to your answer. The answer should be a valid Scala expression as-is. \$\endgroup\$ – Jacob Mar 28 '16 at 2:06
  • \$\begingroup\$ @Jacob it is a valid Scala expression as-is: an anonymous function is a perfectly valid self-contained answer. Valid Scala expressions aren't only those that can be pasted into the Scala REPL without context. The question asks for a function that takes a string and returns an integer: in contexts in which such a function is expected, the answer will compile. However, feel free to take this to Meta. \$\endgroup\$ – Ben Mar 28 '16 at 8:48
3
\$\begingroup\$

Matlab, 50 51 bytes

Thanks to @rayryeng for removing 1 byte.

This uses an anonymous function, and exploits the fact that the error function with an empty input issues no error.

It's noteworthy that the error given for input 'google' is meta: it's an error using the error function.

@(s)eval('error(find(strcmp(s,''google''))),numel(s)')

Examples:

>> @(s)eval('error(find(strcmp(s,''google''))),numel(s)')
ans = 
    @(s)eval('error(find(strcmp(s,''google''))),numel(s)')
>> ans('abcd')
ans =
     4

>> @(s)eval('error(find(strcmp(s,''google''))),numel(s)')
ans = 
    @(s)eval('error(find(strcmp(s,''google''))),numel(s)')
>> ans('google')
Error using error
The message must be specified as either a string or a message structure.
Error in @(s)eval('error(find(strcmp(s,''google''))),numel(s)')
\$\endgroup\$
  • \$\begingroup\$ You could potentially save a byte if you used strcmp! \$\endgroup\$ – rayryeng Oct 1 '15 at 15:22
  • \$\begingroup\$ Slight change to your error statement... still an isequal there! \$\endgroup\$ – rayryeng Oct 1 '15 at 16:38
  • \$\begingroup\$ @rayryeng Oops... I should have pasted the new thing from scratch! Corrected, thanks \$\endgroup\$ – Luis Mendo Oct 1 '15 at 16:41
3
\$\begingroup\$

Perl 6, 19 bytes

{.comb/!/^google$/}

.comb returns list of characters in string (which in numeric context is a number of characters, I use this instead of .chars because it's shorter). Because there is nothing before a period, Perl 6 assumes you wanted to call a method on $_. This is divided by a negated match on regex ^google$ with implicit $_ variable. If it matches, the value becomes 0, and Perl 6 errors because of a division by zero.

Example:

> {.comb/!/^google$/}("qwerty")
6
> {.comb/!/^google$/}("google")
Attempt to divide 6 by zero using div
  in any interactive at src/Perl6/Compiler.nqp line 62
\$\endgroup\$
3
\$\begingroup\$

Java, 84 68 37 bytes

b->b.equals("google")?1/0:b.length();

This is a java.util.function.Function<String, Integer> that divides by 0 if b is the string google.

\$\endgroup\$
2
\$\begingroup\$

CoffeeScript 43 bytes

(g)->return g.length if g!="google";throw-1

Makes an anonymous function that throws -1 if the string is "google", and returns it's length otherwise. Not as obfuscated as I wanted, though.

Link to the online testing environment.

Another variation, 1 byte longer:

(g)->if g!="google"then g.length else throw-1
\$\endgroup\$
  • \$\begingroup\$ Nah, too late. There is already a better answer in CoffeeScript... \$\endgroup\$ – Bojidar Marinov Sep 28 '15 at 18:15
2
\$\begingroup\$

Lua, 42 Bytes

This challenge has made me realize how hard it is to cause an error in Lua.

x=io.read()print(x~="google"and #x or t())

Takes an input and if that input isn't google than print the length of the input, if not call a function t which doesn't exist, throwing an error.

\$\endgroup\$
  • \$\begingroup\$ Can shave off one byte by removing the space before the '#x'. \$\endgroup\$ – ouflak Nov 22 '18 at 16:27
  • \$\begingroup\$ Can shave off two more bytes on top of that by saying 'or~x' instead of 'or t()'. Throws a conversion error. \$\endgroup\$ – ouflak Nov 22 '18 at 16:38
2
\$\begingroup\$

Prolog – 32 bytes

"google"*_:-x.
X*Y:-length(X,Y).

Defines a predicate (*)/2 that throws ERROR: */2: Undefined procedure: x/0 for the input "google":

?- "bing" * Length.
Length = 4.

?- "google" * Length.
ERROR: */2: Undefined procedure: x/0
   Exception: (8) x ? no debug
\$\endgroup\$
  • \$\begingroup\$ Ha nice! I tried a Prolog one and only got 56 chars (I'm a beginner). What does the * do? \$\endgroup\$ – whitfin Oct 6 '15 at 8:21
  • 1
    \$\begingroup\$ @zackehh *, +, ... are valid names for predicates. They have to be written in infix notation and are quite handy for golfing because you can omit the parentheses and the comma for a predicate with two argument. a(A,B):-A is B.A*B:-A is B.: 3 bytes saved for every invocation. \$\endgroup\$ – kay Oct 6 '15 at 11:22
  • 1
    \$\begingroup\$ learn something new every day, thanks! ;) \$\endgroup\$ – whitfin Oct 6 '15 at 15:11
2
\$\begingroup\$

rs, 31 bytes

^(google)|(.*)$/((^^\2))^^(1\1)

In rs, ^^ is the length and repetition operator. Used in the unary form, it will get the length of the following text (^^\2). Used in binary form, it will repeat the LHS RHS times ((^^\2))^^(1\1).

Take the input bing. The input is not google, so the second group will match, not the first. Once the substitutions take place, the result is ((^^bing))^^(1). ^^ gets the length to result in (4)^^(1), and the result gets repeated 1 time to result in 4.

However, if the input is google, the first group will match. That will result in ((^^))^^(1google) after substitutions. ^^ will just get the length of the empty string (0), so the result after ^^ is applied will be (0)^^(1google). Now the repetition operator can do its work...or not. See it will try to repeat 0 1google times. As 1google isn't a number, the output will be:

Traceback (most recent call last):
  File "c callback", line 6, in <module>
  File "<string>", line 5, in <module>
  File "<string>", line 167, in main
  File "<string>", line 125, in run
  File "<string>", line 72, in expand
ValueError: invalid literal for int() with base 10: '1google'

Live demo. (Put the input text in the box at the bottom left.)

\$\endgroup\$
2
\$\begingroup\$

PHP error via a warning(), 62, 52 46 bytes

I feel dirty for these, but it isn't about pretty:

function g($s){echo$s!=google?strlen($s):g();}

This will give "Warning: Missing argument 1 for g()" when you insert 'google', thought that was funny.

function g($s){echo$s!=google?strlen($s):die(err);}

function g($s){if($s!=google){return strlen($s);}die(error);}

PHP error as string, 46 bytes

function g($s){echo$s!=google?strlen($s):err;}

String dont have to be quoted (but should be!)


You can run these via the commandline using g($argv[1]);, or via an older PHP version's register_globals method as an GET g($_GET['string']) or whichever :)

\$\endgroup\$
  • \$\begingroup\$ If it is to play a bit 'dirty', you could use err instead, since it is a common abbreviature \$\endgroup\$ – Ismael Miguel Sep 29 '15 at 14:14
  • \$\begingroup\$ You mean instead of the string 'error'? I wan't too sure about wether to count those bytes, so I just left it there, it's about the logic anyways. \$\endgroup\$ – Martijn Sep 29 '15 at 14:16
  • \$\begingroup\$ It is. But hey, everybody knows what err means, and you save 2 bytes! Some used undefined variables to show errors. Why can't you use an abbreviation!? \$\endgroup\$ – Ismael Miguel Sep 29 '15 at 14:24
  • \$\begingroup\$ I guess your right, changed it. Still thinking of another method. I could simply call a undefined function, would safe me even more, but I tough of that as cheating \$\endgroup\$ – Martijn Sep 29 '15 at 14:57
  • 1
    \$\begingroup\$ That's quite a funny alternative! I wouldn't have though of that! \$\endgroup\$ – Ismael Miguel Sep 30 '15 at 9:10
2
\$\begingroup\$

C# 4.0, 51 bytes

int g(string s){return s.Length/(s!="google"?1:0);}

Sample:

string userInput = Console.ReadLine();
Console.WriteLine(g(userInput));

Output:

// "pogi" 4
// "google" DivideByZeroException was unhandled
\$\endgroup\$
2
\$\begingroup\$

C++14, 47 bytes

[](auto s){return s=="google"?throw:s.size();};

This is a generic lambda expression in C++14. It simply checks if the argument (string) is "google" and calls terminate if it is - else it returns the length of the string.

It can be called as such:

[](auto s){return s=="google"?throw:s.size();}("some_string"s);

Zereges has a very similar answer here written in C++11. Since this was C++14 it was O.K. by him for me to post this.

\$\endgroup\$
2
\$\begingroup\$

Befunge-93, 59 characters

<+1\_v#+1:~
vp00$<
<*_v#:\
v_v>$"google"*****-!#
< >00g.@>1

Befunge has no way to actually throw an error, so I just push an "infinite" number of 1s onto the stack. An error will happen somewhere eventually...

Explanation

Anyway, this works by reading in the input and simultaneously keeping track of its length.

<+1\_v#+1:~

This length is stored at location 0,0.

vp00$<

The remaining numbers are multiplied together.

<*_v#:\

This is then compared to the product of "google".

v_v>$"google"*****-!#

If they are the same (i.e., if their difference is zero), then the "error" is thrown. Otherwise, the value stored at 0,0 is retrieved and outputted.

< >00g.@>1
\$\endgroup\$
2
\$\begingroup\$

PHP 5.4, 81 bytes

<?function g($s){eval('if(isset('.($s==google?"$s":'$s').'))print strlen($s);');}
// following added for easy testing, not included in bytes count
g($argv[1]);

Everyone's favourite error when searching for Google! Didn't work out quite as obfuscated as I'd hoped...

Usage:

php 58891.php 'test'
4
php 58891.php 'google'
# error
\$\endgroup\$
  • 1
    \$\begingroup\$ Just thought I'd point this out: the challenge ID is 58891, not 85591. Otherwise, good job! \$\endgroup\$ – clap Oct 6 '15 at 1:59
  • 1
    \$\begingroup\$ @ConfusedMr_C hah, good spot! I'd like to say I did that deliberately, but that would definitely be a lie! \$\endgroup\$ – Dom Hastings Oct 6 '15 at 4:43
2
\$\begingroup\$

O, 17 bytes

{e\"google"=L@N?}

To use this function:

"wow" {e\"google"=L@N?} ~

Explanation:

e\               Get the length of the string and swap it on the stack
  "google"=      Compare the string
               ? If
            L@    True (it is google): Rotate top three elements on the stack, throwing an underflow error
              N   False (it is not google): do nothing
                   Length is printed out automatically
\$\endgroup\$
2
\$\begingroup\$

PARI/GP, 21 19 bytes

x->#x/(x!="google")

Example:

? g = x->#x/(x!="google")
%1 = (x)->#x/(x!="google")
? g("bing")
%2 = 4
? g("google")
  ***   at top-level: g("google")
  ***                 ^-----------
  ***   in function g: #x/(x!="google")
  ***                    ^--------------
  *** _/_: impossible inverse in dvmdii: 0.
  ***   Break loop: type 'break' to go back to GP prompt
break> 
\$\endgroup\$
2
\$\begingroup\$

Ruby, 46 32 29 bytes

->s{b=Hash.new(0);b['google']=nil;b[s]+s.size}

New shortened approach:

->g{(g.to_a-['google'])[0].size}

Reduced 3 bytes with manatwork's suggestion

->g{([g]-['google'])[0].size}
\$\endgroup\$
  • \$\begingroup\$ Couple of tips to shorten it: new(0)new 0; nilp. Possibly more tips to shorten it: Tips for golfing in Ruby. \$\endgroup\$ – manatwork Nov 26 '15 at 15:44
  • \$\begingroup\$ Found different approach. Thanks anyway :) @manatwork \$\endgroup\$ – Vasu Adari Nov 26 '15 at 15:59
  • \$\begingroup\$ Interesting. Which Ruby version? The 2.1.5 I use has no String#to_a method. But with the shorter [g]-['google'] works. \$\endgroup\$ – manatwork Nov 26 '15 at 16:02
  • \$\begingroup\$ I missed it. Thank you very much :). My ruby version is 2.0.0p576. \$\endgroup\$ – Vasu Adari Nov 26 '15 at 16:08
2
\$\begingroup\$

Japt, 16 14 bytes (non-competing)

U¥`goog¤`?Δ:Ul

Throws reference error on input of "google". Try it online!

\$\endgroup\$
  • \$\begingroup\$ Nice! Two tips: 1) "google" can be compressed to goog¤, wrapped in backticks. 2) == can be compressed to ¥. \$\endgroup\$ – ETHproductions Nov 26 '15 at 15:36
  • \$\begingroup\$ Totally ignored compression. I have only scratched the surface of your language @ETHproductions, and thought this may be a great starting point for it. The \u<hex> shorthands are easily missable, better put it inside the docs. \$\endgroup\$ – noisyass2 Nov 27 '15 at 2:24
  • \$\begingroup\$ I've added a section on Unicode shortcuts in the online interpreter. @ThomasKwa You are correct. I've edited the post. \$\endgroup\$ – ETHproductions Dec 4 '15 at 17:14
2
\$\begingroup\$

05AB1E, 10 bytes (non-competing)

Non-competing, since the language postdates the challenge.

Code:

g“Š¹“¹Q</Ä

Explanation:

g           # Take the length of the implicit input
 “Š¹“       # Compressed version of "google"
     ¹Q     # Take the first input again and check if equal to "google"
       <    # Decrement on the bool
        /   # Divide the length by the value
         Ä  # Take the abstract value of the result
            # This is then implicitly printed

Try it online!. When the input is google, nothing is printed due to a division by zero exception.

\$\endgroup\$
  • 1
    \$\begingroup\$ *absolute value \$\endgroup\$ – Oliver Ni Feb 6 '17 at 2:08
2
\$\begingroup\$

Gogh, 20 bytes

÷GD$"google"={¤+}¦:$

Usage:

./gogh o '÷GD$"google"={¤+}¦:$' "google"

Explanation

             “ Implicit input                                              ”
÷            “ Duplicate the input                                         ”
GD           “ Push a range of [1, len(input)]                             ”
$            “ Rotate input to the TOS                                     ”
"google"=    “ Determine equality                                          ”
{¤+}¦:       “ Error if equals "google" (empties stack and tries addition) ”
$            “ Leave the length on the stack                               ”
             “ Implicit output                                             ”
\$\endgroup\$
  • \$\begingroup\$ Note: this answer is non-competing. \$\endgroup\$ – Zach Gates Mar 26 '16 at 3:39
  • 1
    \$\begingroup\$ Gogh is epic. Just saying. \$\endgroup\$ – Rɪᴋᴇʀ Mar 26 '16 at 3:53
2
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ 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 ̲͚̖͔̙î̩́t̲͎̩̱͔́̋̀ it is beautiful t​he final snuffing of the lie​s of Man ALL IS LOŚ͖̩͇̗̪̏̈́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 rè̑ͧ̌aͨl̘̝̙̃ͤ͂̾̆ ZA̡͊͠͝LGΌ ISͮ̂҉̯͈͕̹̘̱ TO͇̹̺ͅƝ̴ȳ̳ TH̘Ë͖́̉ ͠P̯͍̭O̚​N̐Y̡ H̸̡̪̯ͨ͊̽̅̾̎Ȩ̬̩̾͛ͪ̈́̀́͘ ̶̧̨̱̹̭̯ͧ̾ͬC̷̙̲̝͖ͭ̏ͥͮ͟Oͮ͏̮̪̝͍M̲̖͊̒ͪͩͬ̚̚͜Ȇ̴̟̟͙̞ͩ͌͝S̨̥̫͎̭ͯ̿̔̀ͅ \$\endgroup\$ – cat Mar 26 '16 at 12:10
  • \$\begingroup\$ -1 for unmatched quotes ಠ_ಠ \$\endgroup\$ – cat Mar 26 '16 at 12:15
  • \$\begingroup\$ @tac they represent the beginning and end of the string literal. That is intentional. \$\endgroup\$ – Rɪᴋᴇʀ Mar 26 '16 at 15:31
  • \$\begingroup\$ 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) \$\endgroup\$ – cat Mar 26 '16 at 15:32
  • \$\begingroup\$ @tac I can make it work with matched ones also if you want. \$\endgroup\$ – Rɪᴋᴇʀ Mar 26 '16 at 15:39
2
\$\begingroup\$

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");}
\$\endgroup\$

protected by mbomb007 Sep 30 '15 at 18:27

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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