do you fail at FizzBuzz? simple prog test

G

globalrev

http://reddit.com/r/programming/info/18td4/comments

claims people take a lot of time to write a simple program like this:


"Write a program that prints the numbers from 1 to 100. But for
multiples of three print "Fizz" instead of the number and for the
multiples of five print "Buzz". For numbers which are multiples of
both three and five print "FizzBuzz".

for i in range(1,101):
if i%3 == 0 and i%5 != 0:
print "Fizz"
elif i%5 == 0 and i%3 != 0:
print "Buzz"
elif i%5 == 0 and i%3 == 0:
print "FizzBuzz"
else:
print i


is there a better way than my solution? is mine ok?
 
J

John Machin

globalrev said:
http://reddit.com/r/programming/info/18td4/comments

claims people take a lot of time to write a simple program like this:


"Write a program that prints the numbers from 1 to 100. But for
multiples of three print "Fizz" instead of the number and for the
multiples of five print "Buzz". For numbers which are multiples of
both three and five print "FizzBuzz".

for i in range(1,101):
if i%3 == 0 and i%5 != 0:
print "Fizz"
elif i%5 == 0 and i%3 != 0:
print "Buzz"
elif i%5 == 0 and i%3 == 0:
print "FizzBuzz"
else:
print i


is there a better way than my solution? is mine ok?

Try doing it using %3 and %5 only once each.
 
K

Kam-Hung Soh

http://reddit.com/r/programming/info/18td4/comments

claims people take a lot of time to write a simple program like this:


"Write a program that prints the numbers from 1 to 100. But for
multiples of three print "Fizz" instead of the number and for the
multiples of five print "Buzz". For numbers which are multiples of
both three and five print "FizzBuzz".

for i in range(1,101):
if i%3 == 0 and i%5 != 0:
print "Fizz"
elif i%5 == 0 and i%3 != 0:
print "Buzz"
elif i%5 == 0 and i%3 == 0:
print "FizzBuzz"
else:
print i


is there a better way than my solution? is mine ok?

Looks OK to me.

A different version, and I test for multiples of 3 and 5 first:

map(lambda x: (not x%3 and not x%5 and "FizzBuzz") or (not x%3 and "Fizz")
or (not x%5 and "Buzz") or x, xrange(1,101))
 
M

Mensanator

http://reddit.com/r/programming/info/18td4/comments

claims people take a lot of time to write a simple program like this:

"Write a program that prints the numbers from 1 to 100. But for
multiples of three print "Fizz" instead of the number and for the
multiples of five print "Buzz". For numbers which are multiples of
both three and five print "FizzBuzz".

for i in range(1,101):
� � if i%3 == 0 and i%5 != 0:
� � � � print "Fizz"
� � elif i%5 == 0 and i%3 != 0:
� � � � print "Buzz"
� � elif i%5 == 0 and i%3 == 0:
� � � � print "FizzBuzz"
� � else:
� � � � print i

is there a better way than my solution? is mine ok?

Define better.
f = ['','','Fizz']*100
b = ['','','','','Buzz']*100
for i in xrange(1,100):
fb = f[i-1]+b[i-1]
if fb=='':
print i
else:
print fb
 
I

Ivan Illarionov

http://reddit.com/r/programming/info/18td4/comments

claims people take a lot of time to write a simple program like this:


"Write a program that prints the numbers from 1 to 100. But for
multiples of three print "Fizz" instead of the number and for the
multiples of five print "Buzz". For numbers which are multiples of both
three and five print "FizzBuzz".

for i in range(1,101):
if i%3 == 0 and i%5 != 0:
print "Fizz"
elif i%5 == 0 and i%3 != 0:
print "Buzz"
elif i%5 == 0 and i%3 == 0:
print "FizzBuzz"
else:
print i


is there a better way than my solution? is mine ok?

['%s%s' % (not i%3 and 'Fizz' or '', not i%5 and 'Buzz' or '')
or str(i) for i in xrange(1, 101)]

-- Ivan
 
J

John Machin

claims people take a lot of time to write a simple program like this:
"Write a program that prints the numbers from 1 to 100. But for
multiples of three print "Fizz" instead of the number and for the
multiples of five print "Buzz". For numbers which are multiples of
both three and five print "FizzBuzz".
for i in range(1,101):
� � if i%3 == 0 and i%5 != 0:
� � � � print "Fizz"
� � elif i%5 == 0 and i%3 != 0:
� � � � print "Buzz"
� � elif i%5 == 0 and i%3 == 0:
� � � � print "FizzBuzz"
� � else:
� � � � print i
is there a better way than my solution? is mine ok?

Define better.
f = ['','','Fizz']*100
b = ['','','','','Buzz']*100
for i in xrange(1,100):

fb = f[i-1]+b[i-1]
if fb=='':
print i
else:
print fb

You seem to have an unfortunate fixation on 100. Consider changing the
above instances to 34, 20, and 101.
 
M

Mensanator

Define better.
f = ['','','Fizz']*100
b = ['','','','','Buzz']*100
for i in xrange(1,100):
        fb = f[i-1]+b[i-1]
        if fb=='':
                print i
        else:
                print fb

You seem to have an unfortunate fixation on 100. Consider changing the
above instances to 34, 20, and 101.

Ok, I agree with 101, but I wouldn't necessarily
say the others were unfortunate. You might be
surprised at how often such fixations discover
bugs, something that I have a gift for.
 
G

Gabriel Genellina

http://reddit.com/r/programming/info/18td4/comments

claims people take a lot of time to write a simple program like this:


"Write a program that prints the numbers from 1 to 100. But for
multiples of three print "Fizz" instead of the number and for the
multiples of five print "Buzz". For numbers which are multiples of
both three and five print "FizzBuzz".

for i in range(1,101):
if i%3 == 0 and i%5 != 0:
print "Fizz"
elif i%5 == 0 and i%3 != 0:
print "Buzz"
elif i%5 == 0 and i%3 == 0:
print "FizzBuzz"
else:
print i


is there a better way than my solution? is mine ok?

Is it correct? Did you get at it in less than 15 minutes? If so, then it's OK.
The original test was not "write the most convoluted algorithm you can think of", nor "write the best program to solve this". It was a *practical* test: if you can't get anything remotely working for such a simple problem in 15 minutes, we're not interested in your services.

(We used this question last year - some people gave a sensible answer in less than 5 minutes, but others did not even know how to start)
 
B

bockman

En Sat, 10 May 2008 22:12:37 -0300, globalrev <[email protected]> escribió:










Is it correct? Did you get at it in less than 15 minutes? If so, then it's OK.
The original test was not "write the most convoluted algorithm you can think of", nor "write the best program to solve this". It was a *practical* test: if you can't get anything remotely working for such a simple problem in 15 minutes, we're not interested in your services.

(We used this question last year - some people gave a sensible answer in less than 5 minutes, but others did not even know how to start)

As a test, I would leave out the last sentence, and see how many
people (and how fast) figure out than a number can be multiple of
three _and_ five and that the requirement is somehow incomplete ...

Ciao
 
A

Arnaud Delobelle

Try doing it using %3 and %5 only once each.

for i in xrange(101):
    print (("","Fizz")[i%3==0] + ("","Buzz")[i%5==0]) or str(i)

His is better though, since it's more obvious what's intended.

Here's one that's less opaque

for i in xrange(101):
    s = ""
    if i%3 == 0: s += "Fizz"
    if i%5 == 0: s += "Buzz"
    if s:
        print s
    else:
        print i

Let's not forget to generalise the problem and code it OOP-style :)

class FizzBuzzer(object):
def __init__(self, *fizzles):
self.fizzles = fizzles
def translate(self, n):
return ''.join(val for (p, val) in self.fizzles if not n%p) or
n
def range(self, start, stop=None, step=None):
if stop is None:
start, stop = 0, start
if step is None:
step = 1
for n in xrange(start, stop, step):
yield self.translate(n)
def __getitem__(self, obj):
if isinstance(obj, slice):
return self.range(obj.start, obj.stop, obj.step)
else:
return self.translate(obj)

# FizzBuzzer in action:
fizzbuzz = FizzBuzzer((3, 'Fizz'), (5, 'Buzz'))
for val in fizzbuzz[1:21]:
... print val
...
1 21 1
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz
abc = FizzBuzzer((2, 'Ah'), (3, 'Bee'), (5, 'Cee'))
list(abc[25:35])
25 35 1
['Cee', 'Ah', 'Bee', 'Ah', 29, 'AhBeeCee', 31, 'Ah', 'Bee', 'Ah']
 
A

Arnaud Delobelle

fizzbuzz = FizzBuzzer((3, 'Fizz'), (5, 'Buzz'))
for val in fizzbuzz[1:21]:

...     print val
...
1 21 1[/QUOTE]
^^^^^^^^
Ignore this, it's debugging output
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz>>> abc = FizzBuzzer((2, 'Ah'), (3, 'Bee'), (5, 'Cee'))

25 35 1 ^^^^^^^^^
Same

['Cee', 'Ah', 'Bee', 'Ah', 29, 'AhBeeCee', 31, 'Ah', 'Bee', 'Ah']
 
C

Chris

http://reddit.com/r/programming/info/18td4/comments

claims people take a lot of time to write a simple program like this:

"Write a program that prints the numbers from 1 to 100. But for
multiples of three print "Fizz" instead of the number and for the
multiples of five print "Buzz". For numbers which are multiples of
both three and five print "FizzBuzz".

for i in range(1,101):
    if i%3 == 0 and i%5 != 0:
        print "Fizz"
    elif i%5 == 0 and i%3 != 0:
        print "Buzz"
    elif i%5 == 0 and i%3 == 0:
        print "FizzBuzz"
    else:
        print i

is there a better way than my solution? is mine ok?

personally if you're just checking if a modulus result is 0 or not I
would rather do as it looks neat imho.

for i in xrange(1,101):
if not i % 3 and i % 5:
print 'Fizz'
elif i % 3 and not i % 5:
print 'Buzz'
elif not i % 3 and not i % 5:
print 'FizzBuzz'
else:
print i
 
J

John Machin

Duncan said:
Ivan Illarionov said:
is there a better way than my solution? is mine ok?
['%s%s' % (not i%3 and 'Fizz' or '', not i%5 and 'Buzz' or '')
or str(i) for i in xrange(1, 101)]

-- Ivan
or, more correctly, if you actually need to "print":

sys.stdout.write('\n'.join('%s%s' %
(not i%3 and 'Fizz' or '', not i%5 aBuzz' or '')
or str(i)
for i in xrange(1, 101)))

I think the variant I came up with is a bit clearer:

for i in range(1,101):
print '%s%s' % ('' if i%3 else 'Fizz', '' if i%5 else 'Buzz') or i

More than a bit clearer, IMO. How about
print ('' if i%3 else 'Fizz') + ('' if i%5 else 'Buzz') or i
(or perhaps
print (('' if i%3 else 'Fizz') + ('' if i%5 else 'Buzz')) or i
to save looking up the precedence rules) ?
 
A

Arnaud Delobelle

Duncan Booth wrote: [...]
I think the variant I came up with is a bit clearer:
for i in range(1,101):
   print '%s%s' % ('' if i%3 else 'Fizz', '' if i%5 else 'Buzz') or i

More than a bit clearer, IMO. How about
     print ('' if i%3 else 'Fizz') + ('' if i%5 else 'Buzz') or i
(or perhaps
     print (('' if i%3 else 'Fizz') + ('' if i%5 else 'Buzz')) or i
to save looking up the precedence rules) ?

Stuff clarity! How about

for i in xrange(1, 101):
print 'FizzBuzz'[4*(i%3>0):4+4*(i%5<1)] or i
 
M

Max Erickson

Arnaud Delobelle said:
Duncan Booth wrote: [...]
I think the variant I came up with is a bit clearer:
for i in range(1,101):
   print '%s%s' % ('' if i%3 else 'Fizz', '' if i%5 else
'Buzz') or
i

More than a bit clearer, IMO. How about
     print ('' if i%3 else 'Fizz') + ('' if i%5 else 'Buzz') or
i (or perhaps
     print (('' if i%3 else 'Fizz') + ('' if i%5 else 'Buzz'))
or i to save looking up the precedence rules) ?

Stuff clarity! How about

for i in xrange(1, 101):
print 'FizzBuzz'[4*(i%3>0):4+4*(i%5<1)] or i

With no loop:

i=1
exec"print'FizzBuzz'[4*(i%3>0):4+4*(i%5<1)]or i;i+=1;"*100


max
 
P

Paul Hankin

Duncan Booth wrote: [...]
I think the variant I came up with is a bit clearer:
for i in range(1,101):
print '%s%s' % ('' if i%3 else 'Fizz', '' if i%5 else 'Buzz') or i
More than a bit clearer, IMO. How about
print ('' if i%3 else 'Fizz') + ('' if i%5 else 'Buzz') or i
(or perhaps
print (('' if i%3 else 'Fizz') + ('' if i%5 else 'Buzz')) or i
to save looking up the precedence rules) ?

Stuff clarity! How about

for i in xrange(1, 101):
print 'FizzBuzz'[4*(i%3>0):4+4*(i%5<1)] or i

for i in xrange(1, 101):
print 'Fizz'*(i%3<1)+'Buzz'*(i%5<1) or i
 
A

Arnaud Delobelle

Paul Hankin said:
Duncan Booth wrote: [...]
I think the variant I came up with is a bit clearer:
for i in range(1,101):
print '%s%s' % ('' if i%3 else 'Fizz', '' if i%5 else 'Buzz') or i
More than a bit clearer, IMO. How about
print ('' if i%3 else 'Fizz') + ('' if i%5 else 'Buzz') or i
(or perhaps
print (('' if i%3 else 'Fizz') + ('' if i%5 else 'Buzz')) or i
to save looking up the precedence rules) ?

Stuff clarity! How about

for i in xrange(1, 101):
print 'FizzBuzz'[4*(i%3>0):4+4*(i%5<1)] or i

for i in xrange(1, 101):
print 'Fizz'*(i%3<1)+'Buzz'*(i%5<1) or i

I like this, I can imagine plenty of use cases...

....in codegolf
 
B

bruno.desthuilliers

En Sat, 10 May 2008 22:12:37 -0300, globalrev <[email protected]> escribió:








Is it correct? Did you get at it in less than 15 minutes? If so, then it's OK.
The original test was not "write the most convoluted algorithm you can think of", nor "write the best program to solve this". It was a *practical* test: if you can't get anything remotely working for such a simple problem in 15 minutes, we're not interested in your services.

(We used this question last year - some people gave a sensible answer in less than 5 minutes, but others did not even know how to start)

I just can't believe someone applying for a programmer position cannot
provide a sensible anwser in 5 or less minutes.
 
T

Terry Reedy

En Sat, 10 May 2008 22:12:37 -0300, globalrev <[email protected]>
escribió:

| (We used this question last year - some people gave a sensible answer
| in less | than 5 minutes, but others did not even know how to start)

Another approach I do not remember seeing here (under 10 minutes)

fb = ('FizzBuzz', None, None, 'Fizz', None, 'Buzz', 'Fizz',
None, None, 'Fizz', 'Buzz', None, 'Fizz', None, None)
for i in range(1,101): print(fb[i%15] or i) #3.0
 
K

Kam-Hung Soh

I just can't believe someone applying for a programmer position cannot
provide a sensible anwser in 5 or less minutes.

You should join the recruitment and interview panel in your organization
to test your faith.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top