Mainly Tech projects on Python and Electronic Design Automation.

Sunday, March 04, 2007

Fizz-Buzz.py

I am having to set some extra work at home for my ten year old son. I found information on a game Fizz-Buzz and introduced it to my son, only to find that he had already played a variant at school.

Oh well.

By that time I had already created a python program to check his answers (written in simplistic style as I intend one day to get all of my kids to learn Python).

'''
Fizz-Buzz.py

Fizz-Buzz Game.
Instructions for two players, 3-5 game. (Easiest).

1. Players take it in turns counting from one to ninety-nine.
(Don't take too much or too little time between numbers
- try for a steady pace)
2. Player must say Fizz-Buzz instead of their number if the number is a
multiple of three and of five.
3. Otherwise, player must say Fizz instead of their number if the number
is a multiple of three.
4. Otherwise, player must say Buzz instead of their number if the number
is a multiple of Five.

Digit version
Use basic instructions 1-4 as above, then add the following rules:

5. Otherwise, player must say Fizz for each digit in the number that is
a three,
And player must also say Buzz for each digit in the number that is a
five.
(for example, say Fizz-one instead of 31, Fizz-two instead of thirty
two)
6. Remember, the rules must be applied in order so thirty three is
actually Fizz and not Fizz-Fizz as it is first a multiple of three
'''

fizz = 3
buzz = 5
digits_version = True

for tens in (0,1,2,3,4,5,6,7,8,9):
for units in (0,1,2,3,4,5,6,7,8,9):
number = tens*10 + units
if number == 0:
pass
else:
print "For", number, "Say",
if (number % fizz) == 0 and (number % buzz) == 0:
print "FizzBuzz"
elif (number % fizz) == 0:
print "Fizz"
elif (number % buzz) == 0:
print "Buzz"
elif digits_version:
saytens = tens
sayunits = units
digits_as_fizzbuzz = False
if tens == fizz:
saytens = "Fizz"
digits_as_fizzbuzz = True
if units == fizz:
sayunits = "Fizz"
digits_as_fizzbuzz = True
if tens == buzz:
saytens = "Buzz"
digits_as_fizzbuzz = True
if units == buzz:
sayunits = "Buzz"
digits_as_fizzbuzz = True
if digits_as_fizzbuzz:
print saytens,sayunits
else:
print number
else:
print number

4 comments:

  1. I think you just need to checks.. the FizzBuzz case would be covered automatically if you just put two if statements in sequence :)

    ReplyDelete
  2. Thanks Riffraff,
    I had seen that technique later, on a thread from Reddit I think. It just didn't occur to me at the time, so its their in 'long-hand'

    - Paddy.

    ReplyDelete
  3. You don't need to have that whole "if" chain for the digit version. That can be done with a simple string replacement. Here's my version:

    def fizzbuzz(num, digit_version=True, fizz=3, buzz=5):
        say = ''
        if (num % fizz) == 0:
            say += 'Fizz'
        if (num % buzz) == 0:
            say += 'Buzz'
        say = say or str(num)
        if digit_version:
            say = say.replace(str(fizz), 'Fizz').replace(str(buzz), 'Buzz')
        return say

    for num in xrange(1, 100):
        print fizzbuzz(num)

    ReplyDelete
  4. for i in range(1,101):
    if not i%15:
    print "FizzBuzz"
    continue
    if not i%5:
    print "Buzz"
    continue
    if not i%3:
    print "Fizz"
    continue
    print i

    ReplyDelete

Followers

Subscribe Now: google

Add to Google Reader or Homepage

Go deh too!

whos.amung.us