diff --git a/solutions/python/bottle-song/6/bottle_song.py b/solutions/python/bottle-song/6/bottle_song.py new file mode 100644 index 0000000..ef96d37 --- /dev/null +++ b/solutions/python/bottle-song/6/bottle_song.py @@ -0,0 +1,52 @@ +""" +Bottle Song. + +Recite verses from the children's song "Ten Green Bottles". + +This module exposes the precomputed lyrics and a helper to +extract slices of the song by verse. +""" + +NUMBERS: tuple = ( + "One", + "Two", + "Three", + "Four", + "Five", + "Six", + "Seven", + "Eight", + "Nine", + "Ten", +) + + +def recite(start: int, take: int = 1) -> list[str]: + """ + Return a slice of the song lyrics. + + The lyrics are generated as a flat list where each verse occupies five lines + (four lyric lines plus a trailing blank line). This function generates the + requested verses dynamically based on the song's pattern. + + :param start: The number of green bottles to start from (e.g., 10 for + "Ten green bottles"). Must be between 1 and 10. + :param take: Number of consecutive verses to include starting at ``start``. + Defaults to 1. + :returns: A list of lyric lines forming the requested verses. + """ + result: list[str] = [] + for i in range(start, start - take, -1): + result += [ + f"{NUMBERS[i - 1]} green " + f"{'bottles' if NUMBERS[i - 1] != 'One' else 'bottle'} hanging on the wall,", + f"{NUMBERS[i - 1]} green " + f"{'bottles' if NUMBERS[i - 1] != 'One' else 'bottle'} hanging on the wall,", + "And if one green bottle should accidentally fall,", + f"There'll be {NUMBERS[i - 2].lower() if (i - 2) > -1 else 'no'} green " + f"{'bottles' if NUMBERS[i - 2] != 'One' else 'bottle'} hanging on" + f" the wall.", + ] + if i != start - take + 1: + result.append("") + return result