Created
February 8, 2017 06:46
-
-
Save zonuexe/26284a9469b64a89b32c67e391ca3bce to your computer and use it in GitHub Desktop.
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 -*- | |
def fb(n): | |
s = "" | |
if (n % 3 == 0): | |
s += "Fizz" | |
if (n % 5 == 0): | |
s += "Buzz" | |
if (s == ""): | |
s = str(n) | |
return s | |
def fizzbuzz(n): | |
""" | |
1 から n までのFizzBuzzを含むリストを返す | |
n は 1 以上の int | |
""" | |
assert(type(n) == int) | |
assert(n > 0) | |
result = [] | |
for m in range(1, n + 1): | |
result.append(fb(m)) | |
return result | |
error_values = [1.1 , "1", [], -1] | |
for ev in error_values: | |
try: | |
print ev | |
fizzbuzz(ev) | |
except AssertionError: | |
print "catch!" | |
else: | |
print "failure" | |
print "ok" | |
assert(fb(1) == "1") | |
assert(fb(3) == "Fizz") | |
assert(fb(15) == "FizzBuzz") | |
assert(fb(100) == "Buzz") | |
assert(fizzbuzz(1) == ["1"]); | |
assert(fizzbuzz(2) == ["1", "2"]); | |
assert(fizzbuzz(3) == ["1", "2", "Fizz"]); | |
assert(fizzbuzz(4) == ["1", "2", "Fizz", "4"]); | |
assert(fizzbuzz(5) == ["1", "2", "Fizz", "4", "Buzz"]); | |
assert(fizzbuzz(10) == ["1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz"]); | |
assert(fizzbuzz(15) == ["1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz", "13", "14", "FizzBuzz"]); | |
print "done!" | |
# help(fizzbuzz) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment