Last active
February 28, 2022 18:40
-
-
Save jonfazzaro/9793972a769ed518c9c2d6a60c71e32d to your computer and use it in GitHub Desktop.
This file contains 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
def number(bottles): | |
return str(bottles) if 0 < bottles else "No" | |
def bottlesOfBeer(bottles): | |
s = "" if bottles == 1 else "s" | |
return f"{number(bottles)} bottle{s} of beer" | |
def passOne(bottles): | |
if bottles == 0: | |
return "There are no more to pass around" | |
return "Take one down, pass it around" | |
def verse(): | |
return """{bottles} on the wall, {bottles}. {passOne}, {next} on the wall.\n""" | |
def singVerse(bottles): | |
return verse().format(bottles=bottlesOfBeer(bottles), | |
passOne=passOne(bottles), | |
next=bottlesOfBeer(max(0, bottles - 1))) | |
def sing(bottles): | |
return "".join(map(singVerse, range(bottles, -1, -1))) | |
# Testing | |
def red(message): | |
print("\033[91m {}\033[00m".format(message)) | |
def green(message): | |
print("\033[92m {}\033[00m".format(message)) | |
def test(name, expected, actual): | |
if actual == expected: | |
green(name) | |
else: | |
red("FAIL: " + name) | |
print("\nExpected:\n") | |
print(expected) | |
print("\nActual:\n") | |
print(actual) | |
print("\n\n") | |
verse2 = """2 bottles of beer on the wall, 2 bottles of beer. Take one down, pass it around, 1 bottle of beer on the wall.\n""" | |
verse1 = """1 bottle of beer on the wall, 1 bottle of beer. Take one down, pass it around, No bottles of beer on the wall.\n""" | |
verse0 = """No bottles of beer on the wall, No bottles of beer. There are no more to pass around, No bottles of beer on the wall.\n""" | |
test("Given 0, sings the last round.", verse0, sing(0)) | |
test("Given 1, sings verse one and the last round.", verse1 + verse0, sing(1)) | |
test("Given 2, sings three verses.", verse2 + verse1 + verse0, sing(2)) | |
print(sing(99)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment