r/ProgrammerHumor • • Jan 16 '14

[deleted by user]

[removed]

1.3k Upvotes

446 comments sorted by

View all comments

Show parent comment

0

u/[deleted] Jan 16 '14

[deleted] — view deleted comment

38

u/atrain728 Jan 16 '14

As someone that interviews, I'd like to say I'd give credit for cleverness, but I think I'd mostly see this as being a smartass.

I don't think it'd go well from there.

77

u/[deleted] Jan 16 '14

[removed] — view removed 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.");
}
→ More replies

6

u/[deleted] Jan 17 '14

+/u/CompileBot python

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

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?"
→ More replies
→ 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)
→ More replies (2)
→ More replies (2)

2

u/Ph0X Jan 17 '14

It's from 1 to 100 (a constant), so it'll be really hard to have a finite yet non-O(1) algorithm for fizzbuzz. I'm sure someone will manage though.

EDIT: Well, technically, it's impossible, because O(1) refers to the time complexity with respect to the input, but fizzbuzz doesn't have an input, so that doesn't work.

2

u/FoeHammer99099 Jan 17 '14

The problem does have an input, the range of numbers that the results will be printed for. It's just that everyone only asks for a specific example of the FizzBuzz problem.

→ More replies (4)
→ More replies (1)

11

u/grimeMuted Jan 16 '14

I don't think smartasses like to do tedious things.

16

u/gimpwiz Jan 16 '14

Good. Let them automate the tedious tasks.

15

u/[deleted] Jan 16 '14

You mean like a program of some sort?

15

u/gimpwiz Jan 16 '14

Maybe even executed by some sort of very fast hardware.

10

u/[deleted] Jan 16 '14

Possibly organized into an array of words that translate to machine code but is easily readable?

15

u/gimpwiz Jan 16 '14

Sir, I do not believe such a thing is possible.

→ More replies (1)