views:

214

answers:

6

Possible Duplicate:
What is your solution to the FizzBuzz problem?

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".

Here is what I have right now:

#!/usr/bin/python
for i in range(101):
    if i % 3 == 0:
        i = 'fizz'
        print i
    elif i % 5 == 0:
        i = 'buzz'
        print i
    elif i % 3 == 0 and i % 5 == 0:
        i = 'fizz buzz'
        print i
    else:
        print i
+2  A: 
#!/usr/bin/python 
for i in range(101): 
 if i % 15 == 0:
  print 'FizzBuzz'
 elif i % 3 == 0: 
  print 'Fizz'
 elif i % 5 == 0: 
  print 'Buzz' 
 else:
  print i

Please, send the credits to my univerzity, I need them.

Yossarian
Your code is missing a ':' after the 0 in the first if clause.
clay
A: 

Well it's working now, i swapped two lines (the 'if i % 3 == 0 & i % 5 == 0' line and one 'elif' line.)

Working script is:

#!/usr/bin/python

for i in range(1,101):
 if i % 3 and i % 5 == 0:
  i = 'fizz'
  print i
 elif i % 5 == 0:
  i = 'buzz'
  print i
 elif i % 3 == 0:
  i = 'fizz buzz'
  print i
 else:
  print i
Dananjaya
Note that `range(101)` gives you 0-100, not 1-100.
Justin Ardini
A previous answer (that has since disappeared) used `range(1,101)`. That would work.
31eee384
range(1,101) fixed.
Dananjaya
+6  A: 

The following is probably more efficient:

print "1"
print "2"
print "Fizz"
print "4"
print "Buzz"
...
print "98"
print "Fizz"
print "Buzz"
Philipp
We could write a program that generates this for a one-time computational cost. Definitely worth it in an amortized sense if we run it multiple times!
orangeoctopus
Unless efficiency means the size of the program rather than runtime.
clay
Oh come on guys, this is highly nonoptimal. `print "1\n\2\nFizz\n\4..."` really blows this solution away.
Tim Pietzcker
Tim: You've already got several bugs...
clay
Oh dear. Went a bit overboard with the backslashes there :)
Tim Pietzcker
If only there was a way to have the computer generate it. ;) Interestingly, \2 and \4 generate symbols on my computer as if they were escape codes. I've never seen that before? Bug or feature do you think?
clay
+1  A: 
#!/usr/bin/python

for i in range(1,101):
  out = ''
  if i % 3 == 0: out = 'Fizz '
  if i % 5 == 0: out = out + 'Buzz'
  print (out or i)

This avoids a third check for multiples of 15

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

It's silly to assign a new value to i if all you're doing is printing it.

If you want to be clever:

for i in xrange(1, 101):
   print 'FuzzBuzz' if not i % 15 else \
         'Buzz' if not i % 5 else \
         'Fuzz' if not i % 3 else \
         i

After calling timeit.timeit with 100,000 repetitions, on each method from 1-100, and 1-1000, there doesn't seem to be a whole lot of difference in the two methods:

Clever to 100:  9.38496558538
Clever to 1000:  79.0384339106
Straight to 100:  9.45068505041
Straight to 1000:  78.4853689489
Wayne Werner
A: 
for i in range(1, 101):
  s = ''
  if not i % 3:
    s += 'Fizz'
  if not i % 5:
    s += 'Buzz
  print(s or i)
Zonda333