Created
August 13, 2013 18:17
-
-
Save kaz-tk/6224028 to your computer and use it in GitHub Desktop.
たまにはFizzBuzzでもやってみた。
FizzBuzzの出現回数カウントとか色々盛り込んでみた。
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 -*- | |
import sys | |
import time | |
def _fizzbuzz_decrim(x): | |
""" | |
>>> _fizzbuzz_decrim(1) | |
'1' | |
>>> _fizzbuzz_decrim(0) | |
'FizzBuzz' | |
>>> _fizzbuzz_decrim(15) | |
'FizzBuzz' | |
>>> _fizzbuzz_decrim(3) | |
'Fizz' | |
>>> _fizzbuzz_decrim(5) | |
'Buzz' | |
""" | |
res = '' | |
if x % 3 ==0: | |
res = res + 'Fizz' | |
if x % 5 ==0: | |
res = res + 'Buzz' | |
if res == '': | |
return str(x) | |
return res | |
pass | |
def main(args): | |
n = 0 | |
if not args.isdigit(): | |
raise TypeError('ONLY Decimal Arguments are allowed') | |
n = int(args) | |
for target in map(_fizzbuzz_decrim,range(0,n)): | |
print target | |
pass | |
def fizzbuzz_counter(args): | |
if not args.isdigit(): | |
raise TypeError('ONLY Decimal Arguments are allowed') | |
n = int(args) | |
def _num_generator(num): | |
i=0 | |
while True: | |
i = i + 1 | |
yield i | |
if i >= num: | |
break | |
pass | |
f = 0 | |
b = 0 | |
fb =0 | |
for i in _num_generator(n): | |
target = _fizzbuzz_decrim(i) | |
if target.find('Fizz') !=-1: | |
f = f + 1 | |
if target.find('Buzz') != -1: | |
b = b + 1 | |
if target.find('FizzBuzz')!= -1: | |
fb =fb +1 | |
print 'Fizz:%5d,Buzz:%5d,FizzBuzz:%5d \r' % (f,b,fb), | |
sys.stdout.flush() | |
# time.sleep(0.05) | |
print 'Fizz:%5d,Buzz:%5d,FizzBuzz:%5d \r' % (f,b,fb) | |
pass | |
def _test(): | |
import doctest | |
doctest.testmod() | |
if __name__ == '__main__': | |
_test() | |
args = sys.argv | |
if len(args) <2 : | |
print """ | |
USAGE: | |
numeric argument is needed. | |
python FizzBuzz.py NUM [options] | |
""" | |
else: | |
# main(args[1]) | |
fizzbuzz_counter(args[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
判定ロジックだけ外だしの方がいいかな?