r/ProgrammerHumor • • Jan 16 '14

[deleted by user]

[removed]

1.3k Upvotes

446 comments sorted by

View all comments

Show parent comment

36

u/curtmack Jan 16 '14 edited Jan 16 '14

Actually, this is not too dissimilar from one of the most optimal FizzBuzz algorithms:

Create the following lookup list:
  [ "", "", "Fizz", "", "Buzz", "Fizz", "", "", "Fizz", "Buzz", "", "Fizz", "", "", "FizzBuzz" ]
For all numbers n from 1 to 100:
    Take the string in the lookup list at the index (n-1 mod 15), call it s
    If s is the empty string, print the number n
    Otherwise, print s
End for

Convert to the required language as needed. For bonus interviewer points, dynamically generate the lookup list (not hard).

Edit: Syntax error on line 2, near 'FizzBuzz'

45

u/jonnywoh Jan 17 '14

+/u/CompileBot python

a = [ "", "", "Fizz", "", "Buzz", "Fizz", "", "", "Fizz", "Buzz", "", "Fizz", "", "", "FizzBuzz" ]
for i in range(1, 101):
    s = a[(i-1) % 15]
    if len(s) == 0:
        print i
    else:
        print s

61

u/CompileBot Green security clearance Jan 17 '14

Output:

1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz
Fizz
22
23
Fizz
Buzz
26
Fizz
28
29
FizzBuzz
31
32
Fizz
34
Buzz
Fizz
37
38
Fizz
Buzz
41
Fizz
43
44
FizzBuzz
46
47
Fizz
49
Buzz
Fizz
...

^source | ^info | ^git | ^report

33

u/aroymart Jan 17 '14

This is an awesome bot

21

u/Shamus03 Jan 17 '14

+/u/CompileBot python 3

print('I am an awesome bot.')

38

u/CompileBot Green security clearance Jan 17 '14

Output:

I am an awesome bot.

^source | ^info | ^git | ^report

6

u/fallingwalls Jan 17 '14

+/u/CompileBot C#

while (true)
{
Console.WriteLine("I am an awesome bot.");
}

11

u/steamruler Jan 17 '14

But will he respond? Find out the next time, at /r/ProgrammerHumor.

6

u/[deleted] Jan 17 '14

+/u/CompileBot python

from subprocess import call
call(["ls", "-l"])

3

u/CompileBot Green security clearance Jan 17 '14

Output:

total 4
-rw-r--r-- 1 root 1001 181 2014-01-21 09:00 prog

^source | ^info | ^git | ^report

18

u/[deleted] Jan 17 '14

Uh oh.

3

u/rtkwe Jan 17 '14

+/u/CompileBot python

from subprocess import call
call(["ls", "-al","/"])

2

u/CompileBot Green security clearance Jan 17 '14

Output:

total 48
drwxr-xr-x 12 root root 4096 2013-10-18 10:08 .
drwxr-xr-x 12 root root 4096 2013-10-18 10:08 ..
drwxr-xr-x  2 root root 4096 2013-10-18 10:06 bin
drwxrwxr-x  3 root root 4096 2013-10-18 10:05 build
drwxr-xr-x  3 root root 4096 2013-10-18 10:05 dev
drwxr-xr-x 28 root root 4096 2013-10-18 10:08 etc
drwxr-xr-x  3 root root 4096 2014-01-21 11:10 home
drwxr-xr-x  5 root root 4096 2013-10-18 10:08 lib
drwxrwxr-x  2 root root 4096 2013-10-18 10:05 spoj
drwxrwxr-x  5 root root 4096 2014-01-21 11:10 tmp
drwxr-xr-x  7 root root 4096 2013-10-18 10:08 usr
drwxr-xr-x 13 root root 4096 2013-10-18 10:05 var

^source | ^info | ^git | ^report

4

u/rtkwe Jan 17 '14 edited Jan 17 '14

+/u/CompileBot python --include-errors

from subprocess import call
call(["ls", "-alR","/"])
→ More replies
→ More replies (23)
→ More replies (17)

4

u/thirdegree Violet security clearance Jan 17 '14

+/u/CompileBot haskell

test :: a -> a
test n = n

main = print $ test "Can it do haskell?"

4

u/CompileBot Green security clearance Jan 17 '14

Output:

"Can it do haskell?"

^source | ^info | ^git | ^report

→ More replies (2)
→ More replies (3)

5

u/Tmmrn Jan 17 '14

Why empty strings, isn't None easier to recognize as falsey?

+/u/CompileBot python

a = [ None, None, "Fizz", None, "Buzz", "Fizz", None, None, "Fizz", "Buzz", None, "Fizz", None, None, "FizzBuzz" ]
for i in range(1, 101):
    s = a[(i-1) % 15]
    print (s if s else i)

4

u/kqr Jan 17 '14

Because heterogeneous lists are not beautiful. And the empty string is falseish anyway, so your code would work with empty strings as well.

(By the way, s if s else i is just s or i.)

→ More replies (2)
→ More replies (3)

10

u/shanoxilt Jan 16 '14

We could use you on /r/28thworldproblems, a realm where ants talk in pseudo-code.

2

u/mgdmw Jan 17 '14

My mind is blown. Why does that subreddit exist?

→ More replies (1)

2

u/complexitivity Jan 17 '14

Ants? Ents?

2

u/shanoxilt Jan 17 '14

Ants: the insect

1

u/[deleted] Jan 19 '14

Someone in my company send out a FizzBuzz challenge last year. In proceeding to waste a good part of an afternoon, we found several good answers. We actually found that this method was one of the slower methods, even though by calculation it should be super fast. We concluded that this form of lookup table was going to memory every time which resulted in significant lag.

The winner implemented the lookup list as a switch statement. While very similar, it ran significantly faster. My guess is the switch statement was stored in a L cache.

It was all in JS so that will have some to do with the results.

2

u/curtmack Jan 19 '14

That makes sense, but the advantage of the lookup list is that you can dynamically generate it.