Last active
August 29, 2015 14:04
-
-
Save hugokernel/c64b0317c34449bb9f32 to your computer and use it in GitHub Desktop.
Mes versions du FizzBuzz en Python (http://sametmax.com/fizzbuzz-en-python/)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
from __future__ import print_function | |
''' | |
Écrire un programme qui affiche les nombres de 1 à 199. | |
Mais pour les multiples de 3, afficher “Fizz” au lieu du nombre et pour les multiples de 5 afficher “Buzz”. | |
Pour les nombres multiples de 3 et 5, afficher “FizzBuzz”. | |
''' | |
# Simple version | |
for i in range(1, 200): | |
if not i % 3: | |
print('Fizz', end='') | |
if not i % 5: | |
print('Buzz', end='') | |
print(end='\n') | |
elif not i % 5: | |
print('Buzz') | |
else: | |
print(i) | |
# Lambda style | |
if_m = lambda x, text: text if not i % x else '' | |
if_n = lambda x: str(i) if len([ a for a in x if i % a ]) == len(x) else '' | |
for i in range(1, 200): | |
print(if_m(3, 'Fizz') + if_m(5, 'Buzz') + if_n([ 3, 5 ])) | |
# Exception style : | |
for i in range(1, 200): | |
try: | |
for x, txt in sorted({ 15: ('Fi', 'Bu'), 5: ('Bu',), 3: ('Fi',) }.iteritems(), key=lambda k: k, reverse=True): | |
if not i % x: | |
raise Exception(txt) | |
except Exception as e: | |
print(''.join([ txt + 'zz' for txt in e[0]])) | |
else: | |
print(i) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment