jueves, 10 de abril de 2014

FizzBuzz

The typical question in Hacker School, 

Write a program that prints out the numbers 1 to 100 (inclusive). If the number is divisible by 3, print Fizz instead of the number. If it’s divisible by 5, print Buzz. If it’s divisible by both 3 and 5, print FizzBuzz. You can use any language.

and my solution using a text buffer to avoid unnecessary operations and Python as language :
for num in range(1, 101):
    toprint = ''
    if num % 3 == 0: toprint += "Fizz"
    if num % 5 == 0: toprint += "Buzz"

    if toprint: print num, toprint

No hay comentarios:

Publicar un comentario