Connecting Tech Pros Worldwide Forums | Help | Site Map

do you fail at FizzBuzz? simple prog test

globalrev
Guest
 
Posts: n/a
#1: Jun 27 '08
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?

John Machin
Guest
 
Posts: n/a
#2: Jun 27 '08

re: do you fail at FizzBuzz? simple prog test


globalrev wrote:
Quote:
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.
Kam-Hung Soh
Guest
 
Posts: n/a
#3: Jun 27 '08

re: do you fail at FizzBuzz? simple prog test


On Sun, 11 May 2008 11:12:37 +1000, globalrev <skanemupp@yahoo.sewrote:
Quote:
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))

--
Kam-Hung Soh <a href="http://kamhungsoh.com/blog">Software Salariman</a>
Mensanator
Guest
 
Posts: n/a
#4: Jun 27 '08

re: do you fail at FizzBuzz? simple prog test


On May 10, 8:12�pm, globalrev <skanem...@yahoo.sewrote:
Quote:
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.
Quote:
Quote:
Quote:
>>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
Grant Edwards
Guest
 
Posts: n/a
#5: Jun 27 '08

re: do you fail at FizzBuzz? simple prog test


On 2008-05-11, John Machin <sjmachin@lexicon.netwrote:
Quote:
Quote:
>"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.
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

There are dozens of other ways to do it.
--
Grant Edwards grante Yow! ... or were you
at driving the PONTIAC that
visi.com HONKED at me in MIAMI last
Tuesday?
Ivan Illarionov
Guest
 
Posts: n/a
#6: Jun 27 '08

re: do you fail at FizzBuzz? simple prog test


On Sat, 10 May 2008 18:12:37 -0700, globalrev wrote:
Quote:
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
Ivan Illarionov
Guest
 
Posts: n/a
#7: Jun 27 '08

re: do you fail at FizzBuzz? simple prog test


On Sun, 11 May 2008 04:26:10 +0000, Ivan Illarionov wrote:
Quote:
On Sat, 10 May 2008 18:12:37 -0700, globalrev wrote:
>
Quote:
>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
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)))

-- Ivan
John Machin
Guest
 
Posts: n/a
#8: Jun 27 '08

re: do you fail at FizzBuzz? simple prog test


On May 11, 1:24 pm, Mensanator <mensana...@aol.comwrote:
Quote:
On May 10, 8:12�pm, globalrev <skanem...@yahoo.sewrote:
>
>
>>
Quote:
claims people take a lot of time to write a simple program like this:
>
Quote:
"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".
>
Quote:
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
>
Quote:
is there a better way than my solution? is mine ok?
>
Define better.
>
Quote:
Quote:
>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.
Mensanator
Guest
 
Posts: n/a
#9: Jun 27 '08

re: do you fail at FizzBuzz? simple prog test


On May 11, 12:04Â*am, John Machin <sjmac...@lexicon.netwrote:
Quote:
On May 11, 1:24 pm, Mensanator <mensana...@aol.comwrote:
>
>
>
>
>
Quote:
On May 10, 8:12�pm, globalrev <skanem...@yahoo.sewrote:
>>
Quote:
Quote:
claims people take a lot of time to write a simple program like this:
>
Quote:
Quote:
"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".
>
Quote:
Quote:
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
>
Quote:
Quote:
is there a better way than my solution? is mine ok?
>
Quote:
Define better.
>
Quote:
Quote:
>>f = ['','','Fizz']*100
>>b = ['','','','','Buzz']*100
>>for i in xrange(1,100):
>
Quote:
Â* Â* Â* Â* 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.
Gabriel Genellina
Guest
 
Posts: n/a
#10: Jun 27 '08

re: do you fail at FizzBuzz? simple prog test


En Sat, 10 May 2008 22:12:37 -0300, globalrev <skanemupp@yahoo.seescribió:
Quote:
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)

--
Gabriel Genellina

bockman@virgilio.it
Guest
 
Posts: n/a
#11: Jun 27 '08

re: do you fail at FizzBuzz? simple prog test


On 12 Mag, 09:00, "Gabriel Genellina" <gagsl-...@yahoo.com.arwrote:
Quote:
En Sat, 10 May 2008 22:12:37 -0300, globalrev <skanem...@yahoo.seescribió:
>
>
>
>
>>
Quote:
claims people take a lot of time to write a simple program like this:
>
Quote:
"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".
>
Quote:
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
>
Quote:
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'sOK.
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)
>
--
Gabriel Genellina- Nascondi testo tra virgolette -
>
- Mostra testo tra virgolette -
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
-----
FB
Arnaud Delobelle
Guest
 
Posts: n/a
#12: Jun 27 '08

re: do you fail at FizzBuzz? simple prog test


On May 11, 4:36*am, Grant Edwards <gra...@visi.comwrote:
Quote:
On 2008-05-11, John Machin <sjmac...@lexicon.netwrote:
>
>
>
Quote:
Quote:
"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".
>
Quote:
Quote:
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
>
Quote:
Quote:
is there a better way than my solution? is mine ok?
>
Quote:
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:
Quote:
Quote:
Quote:
>>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
Quote:
Quote:
Quote:
>>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']
Quote:
Quote:
Quote:
>>>
--
Arnaud

Arnaud Delobelle
Guest
 
Posts: n/a
#13: Jun 27 '08

re: do you fail at FizzBuzz? simple prog test


On May 12, 9:30*am, Arnaud Delobelle <arno...@googlemail.comwrote:
[...]
Quote:
# FizzBuzzer in action:
>
Quote:
Quote:
>fizzbuzz = FizzBuzzer((3, 'Fizz'), (5, 'Buzz'))
>for val in fizzbuzz[1:21]:
>
... * * print val
...
1 21 1
^^^^^^^^
Ignore this, it's debugging output
Quote:
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'))
Quote:
Quote:
>list(abc[25:35])
>
25 35 1
^^^^^^^^^
Same
Quote:
['Cee', 'Ah', 'Bee', 'Ah', 29, 'AhBeeCee', 31, 'Ah', 'Bee', 'Ah']
--
Arnaud

Chris
Guest
 
Posts: n/a
#14: Jun 27 '08

re: do you fail at FizzBuzz? simple prog test


On May 11, 3:12*am, globalrev <skanem...@yahoo.sewrote:
Quote:
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
Duncan Booth
Guest
 
Posts: n/a
#15: Jun 27 '08

re: do you fail at FizzBuzz? simple prog test


Ivan Illarionov <ivan.illarionov@gmail.comwrote:
Quote:
Quote:
Quote:
>>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
John Machin
Guest
 
Posts: n/a
#16: Jun 27 '08

re: do you fail at FizzBuzz? simple prog test


Duncan Booth wrote:
Quote:
Ivan Illarionov <ivan.illarionov@gmail.comwrote:
>
Quote:
Quote:
>>>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) ?
Arnaud Delobelle
Guest
 
Posts: n/a
#17: Jun 27 '08

re: do you fail at FizzBuzz? simple prog test


On May 12, 1:30*pm, John Machin <sjmac...@lexicon.netwrote:
Quote:
Duncan Booth wrote:
[...]
Quote:
Quote:
I think the variant I came up with is a bit clearer:
>
Quote:
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

--
Arnaud

Max Erickson
Guest
 
Posts: n/a
#18: Jun 27 '08

re: do you fail at FizzBuzz? simple prog test


Arnaud Delobelle <arnodel@googlemail.comwrote:
Quote:
On May 12, 1:30*pm, John Machin <sjmac...@lexicon.netwrote:
Quote:
>Duncan Booth wrote:
[...]
Quote:
Quote:
I think the variant I came up with is a bit clearer:
>>
Quote:
for i in range(1,101):
* *print '%s%s' % ('' if i%3 else 'Fizz', '' if i%5 else
'Buzz') or
i
Quote:
>>
>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
>
--
Arnaud
>
--
http://mail.python.org/mailman/listinfo/python-list
>
>
With no loop:

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


max

Paul Hankin
Guest
 
Posts: n/a
#19: Jun 27 '08

re: do you fail at FizzBuzz? simple prog test


On May 12, 1:59 pm, Arnaud Delobelle <arno...@googlemail.comwrote:
Quote:
On May 12, 1:30 pm, John Machin <sjmac...@lexicon.netwrote:
>
Quote:
Duncan Booth wrote:
[...]
Quote:
Quote:
I think the variant I came up with is a bit clearer:
>
Quote:
Quote:
for i in range(1,101):
print '%s%s' % ('' if i%3 else 'Fizz', '' if i%5 else 'Buzz') or i
>
Quote:
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

--
Paul Hankin
Arnaud Delobelle
Guest
 
Posts: n/a
#20: Jun 27 '08

re: do you fail at FizzBuzz? simple prog test


Paul Hankin <paul.hankin@gmail.comwrites:
Quote:
On May 12, 1:59 pm, Arnaud Delobelle <arno...@googlemail.comwrote:
Quote:
>On May 12, 1:30 pm, John Machin <sjmac...@lexicon.netwrote:
>>
Quote:
Duncan Booth wrote:
>[...]
Quote:
I think the variant I came up with is a bit clearer:
>>
Quote:
for i in range(1,101):
print '%s%s' % ('' if i%3 else 'Fizz', '' if i%5 else 'Buzz') or i
>>
Quote:
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

--
Arnaud
bruno.desthuilliers@gmail.com
Guest
 
Posts: n/a
#21: Jun 27 '08

re: do you fail at FizzBuzz? simple prog test


On 12 mai, 09:00, "Gabriel Genellina" <gagsl-...@yahoo.com.arwrote:
Quote:
En Sat, 10 May 2008 22:12:37 -0300, globalrev <skanem...@yahoo.seescribió:
>
>
>>
Quote:
claims people take a lot of time to write a simple program like this:
>
Quote:
"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".
>
Quote:
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
>
Quote:
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'sOK.
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.

Grant Edwards
Guest
 
Posts: n/a
#22: Jun 27 '08

re: do you fail at FizzBuzz? simple prog test


On 2008-05-12, Paul Hankin <paul.hankin@gmail.comwrote:
Quote:
for i in xrange(1, 101):
print 'Fizz'*(i%3<1)+'Buzz'*(i%5<1) or i
Doh! It never occured to me that 'string' * 0 == ''.

--
Grant Edwards grante Yow! An air of FRENCH FRIES
at permeates my nostrils!!
visi.com
Terry Reedy
Guest
 
Posts: n/a
#23: Jun 27 '08

re: do you fail at FizzBuzz? simple prog test



"Gabriel Genellina" <gagsl-py2@yahoo.com.arwrote in message
news:op.ua04evz4x6zn5v@a98gizw.cpe.telecentro.net. ar...
En Sat, 10 May 2008 22:12:37 -0300, globalrev <skanemupp@yahoo.se>
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



Kam-Hung Soh
Guest
 
Posts: n/a
#24: Jun 27 '08

re: do you fail at FizzBuzz? simple prog test


On Tue, 13 May 2008 03:42:30 +1000, bruno.desthuilliers@gmail.com
<bruno.desthuilliers@gmail.comwrote:
Quote:
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.

--
Kam-Hung Soh <a href="http://kamhungsoh.com/blog">Software Salariman</a>

John Machin
Guest
 
Posts: n/a
#25: Jun 27 '08

re: do you fail at FizzBuzz? simple prog test


Terry Reedy wrote:
Quote:
"Gabriel Genellina" <gagsl-py2@yahoo.com.arwrote in message
news:op.ua04evz4x6zn5v@a98gizw.cpe.telecentro.net. ar...
En Sat, 10 May 2008 22:12:37 -0300, globalrev <skanemupp@yahoo.se>
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
"#3.0" is rather cryptic. AFAIK that will work with *any* version of
Python up to 3.0. The "() after "print" are required in 3.0 and optional
in earlier versions.

alex23
Guest
 
Posts: n/a
#26: Jun 27 '08

re: do you fail at FizzBuzz? simple prog test


<bruno.desthuilli...@gmail.comwrote:
Quote:
I just can't believe someone applying for a programmer position cannot
provide a sensible anwser in 5 or less minutes.
Try taking a look at the level of discourse in the Google App Engine
group.

It's pretty clear that some - let's say "developers" rather than
"programmers" - developers are simply interested in acquiring usage
patterns for the particular language they're required to develop in.
Being able to extend existing patterns or create new solutions is
completely outside of their scope of interest or ability. My
'favourite' is the "how do I do x?", "can you show me a working
example?", "can you add your example to my code?" sequence, you see a
lot of that.

I'd honestly expect a lot of the current GAE group posters to fail the
FizzBuzz test, especially given that an amazing number of them seem to
be constantly griping about not being able to run production code on
what is currently only a preview framework.

- alex23
Bruno Desthuilliers
Guest
 
Posts: n/a
#27: Jun 27 '08

re: do you fail at FizzBuzz? simple prog test


Kam-Hung Soh a écrit :
Quote:
On Tue, 13 May 2008 03:42:30 +1000, bruno.desthuilliers@gmail.com
<bruno.desthuilliers@gmail.comwrote:
>
Quote:
>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.
I've done some recruitment - for a programmer position - in my previous
shop , and none of the guys that we interwieved were *that* bad.
Matthew Woodcraft
Guest
 
Posts: n/a
#28: Jun 27 '08

re: do you fail at FizzBuzz? simple prog test


Gabriel Genellina <gagsl-py2@yahoo.com.arwrote:
Quote:
I would like to write a similar problem without this non-programming
distracting issues (that is, a problem simple enough to be answered in a
few minutes, that requires only programming skills to be solved, and
leaving out any domain-specific knowledge).
Another reason not to like the FizzBuzz example is that it's quite
closely balanced between two reasonable approaches (whether to just
special-case multiples of 15 and say 'FizzBuzz', or whether to somehow
add the Fizz and the Buzz together in that case). The interviewee might
reasonably guess that this distinction is what's being tested, and get
unnecessarily stressed about it.

-M-

John Machin
Guest
 
Posts: n/a
#29: Jun 27 '08

re: do you fail at FizzBuzz? simple prog test


Matthew Woodcraft wrote:
Quote:
Gabriel Genellina <gagsl-py2@yahoo.com.arwrote:
Quote:
>I would like to write a similar problem without this non-programming
>distracting issues (that is, a problem simple enough to be answered in a
>few minutes, that requires only programming skills to be solved, and
>leaving out any domain-specific knowledge).
>
Another reason not to like the FizzBuzz example is that it's quite
closely balanced between two reasonable approaches (whether to just
special-case multiples of 15 and say 'FizzBuzz', or whether to somehow
add the Fizz and the Buzz together in that case). The interviewee might
reasonably guess that this distinction is what's being tested, and get
unnecessarily stressed about it.
>
For such a trivial problem, fifteen minutes is more than enough to
present alternative answers. Perhaps the intention is to weed out those
who do become unnecessarily stressed in such circumstances. Employers
like to play tricks to see how interviewees respond e.g. hand over two
pages of a listing of code in language X, announce that there are 10
syntax errors, and ask the interviewee to find and circle all the syntax
errors. Correct answer: 11 circles.
Mensanator
Guest
 
Posts: n/a
#30: Jun 27 '08

re: do you fail at FizzBuzz? simple prog test


On May 13, 3:57*pm, John Machin <sjmac...@lexicon.netwrote:
Quote:
Matthew Woodcraft wrote:
Quote:
Gabriel Genellina <gagsl-...@yahoo.com.arwrote:
Quote:
I would like to write a similar problem without this non-programming *
distracting issues (that is, a problem simple enough to be answered in a *
few minutes, that requires only programming skills to be solved, and *
leaving out any domain-specific knowledge).
>
Quote:
Another reason not to like the FizzBuzz example is that it's quite
closely balanced between two reasonable approaches (whether to just
special-case multiples of 15 and say 'FizzBuzz', or whether to somehow
add the Fizz and the Buzz together in that case). The interviewee might
reasonably guess that this distinction is what's being tested, and get
unnecessarily stressed about it.
>
For such a trivial problem, fifteen minutes is more than enough to
present alternative answers. Perhaps the intention is to weed out those
who do become unnecessarily stressed in such circumstances. Employers
like to play tricks to see how interviewees respond e.g. hand over two
pages of a listing of code in language X, announce that there are 10
syntax errors, and ask the interviewee to find and circle all the syntax
errors. Correct answer: 11 circles.
They get away with that?

The one time I was asked to compose a (hardware) test was
because some interviewee threated to sue claiming
discrimination. So I was asked to make an absolutely
trick-free test.

Such as what's the voltage at point A?

+5v
|
220 ohm
|
+-- A
|
330 ohm
|
ground

Would you be surprised at how many applicants couldn't
figure that out because they forgot to bring their
calculator?

Sure, I had a page from a schematic, but only to ask
how one of the shown T-flip-flops worked.

One female interviewee took one glance at the schematic
and said, "Oh, it's a parity generator."

She got hired.

I'm surprised anyone has to resort to tricks.
Gabriel Genellina
Guest
 
Posts: n/a
#31: Jun 27 '08

re: do you fail at FizzBuzz? simple prog test


En Tue, 13 May 2008 19:31:03 -0300, Mensanator <mensanator@aol.com>
escribió:
Quote:
Such as what's the voltage at point A?
>
+5v
|
220 ohm
|
+-- A
|
330 ohm
|
ground
>
Would you be surprised at how many applicants couldn't
figure that out because they forgot to bring their
calculator?
<OT>
Just a few hours ago I was helping a high school boy (senior) with some
chemistry homework. He actually had to use a calculator to evaluate
120*1/1 (and didn't even noticed he got the "same" number until I told
him!)
I hope he'll pass his test tomorrow...
</OT>

--
Gabriel Genellina

Wolfgang Grafen
Guest
 
Posts: n/a
#32: Jun 27 '08

re: do you fail at FizzBuzz? simple prog test


globalrev schrieb:
Quote:
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?
Your example is comprehensive compared to many other suggestions which
is an advantage IMO. I minimised it further

for i in range(start, stop, step):
if not i%15:
print "FizzBuzz"
elif not i%3:
print "Fizz"
elif not i%5:
print "Buzz"
else:
print i

Best regards

Wolfgang
castironpi
Guest
 
Posts: n/a
#33: Jun 27 '08

re: do you fail at FizzBuzz? simple prog test


On May 20, 6:57*am, Wolfgang Grafen <wolfgang.gra...@ericsson.com>
wrote:
Quote:
globalrev schrieb:
>
>
>
>
>>
Quote:
claims people take a lot of time to write a simple program like this:
>
Quote:
"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".
>
Quote:
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
>
Quote:
is there a better way than my solution? is mine ok?
>
Your example is comprehensive compared to many other suggestions which
is an advantage IMO. I minimised it further
>
for i in range(start, stop, step):
* * *if not i%15:
* * * * *print "FizzBuzz"
* * *elif not i%3:
* * * * *print "Fizz"
* * *elif not i%5:
* * * * *print "Buzz"
* * *else:
* * * * *print i
>
Best regards
>
Wolfgang- Hide quoted text -
>
- Show quoted text -
This one eliminates the modulo operation.

dyn= list( xrange( 1, 101 ) )
for i in xrange( 1, len( dyn ), 3 ):
dyn[ i ]= ''
for i in xrange( 1, len( dyn ), 5 ):
dyn[ i ]= ''
for i in xrange( 1, len( dyn ), 3 ):
dyn[ i ]+= 'Fizz'
for i in xrange( 1, len( dyn ), 5 ):
dyn[ i ]+= 'Buzz'
for d in dyn:
print d

Can we compare?
Chuckk Hubbard
Guest
 
Posts: n/a
#34: Oct 3 '08

re: do you fail at FizzBuzz? simple prog test


range(1,101), no?

On Tue, May 20, 2008 at 4:46 PM, Sells, Fred
<fred.sells@adventistcare.orgwrote:
Quote:
or
for i in range(1,100):
print ('fizz','','')[i%3] + ('buzz','','','','')[i%5] or i
>
Quote:
Quote:
>
"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?
>>
>>
--
http://mail.python.org/mailman/listinfo/python-list
>


--
http://www.badmuthahubbard.com
Tobiah
Guest
 
Posts: n/a
#35: Oct 6 '08

re: do you fail at FizzBuzz? simple prog test


Or to allow the numbers 3 and 5 to be easily changed:

for i in range(1, 101):
print 'fizz' * (not i % 3) + 'buzz' * (not i % 5) or i

Tobiah

Quote:
Quote:
>for i in range(1,100):
> print ('fizz','','')[i%3] + ('buzz','','','','')[i%5] or i
>>
Quote:
>>>"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?
>>>
>--
>http://mail.python.org/mailman/listinfo/python-list
>>
>
>
>
** Posted from http://www.teranews.com **
Tobiah
Guest
 
Posts: n/a
#36: Oct 6 '08

re: do you fail at FizzBuzz? simple prog test


Or to allow the numbers 3 and 5 to be easily changed:

for i in range(1, 101):
print 'fizz' * (not i % 3) + 'buzz' * (not i % 5) or i

Tobiah

Quote:
Quote:
>for i in range(1,100):
> print ('fizz','','')[i%3] + ('buzz','','','','')[i%5] or i
>>
Quote:
>>>"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?
>>>
>--
>http://mail.python.org/mailman/listinfo/python-list
>>
>
>
>
Closed Thread