From 72c0d9bdda81c9ce27e17df975f3cde9c76bffa0 Mon Sep 17 00:00:00 2001 From: "exercism-solutions-syncer[bot]" <211797793+exercism-solutions-syncer[bot]@users.noreply.github.com> Date: Fri, 28 Nov 2025 02:42:41 +0000 Subject: [PATCH] [Sync Iteration] python/bottle-song/4 --- solutions/python/bottle-song/4/bottle_song.py | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 solutions/python/bottle-song/4/bottle_song.py diff --git a/solutions/python/bottle-song/4/bottle_song.py b/solutions/python/bottle-song/4/bottle_song.py new file mode 100644 index 0000000..b4bd2ba --- /dev/null +++ b/solutions/python/bottle-song/4/bottle_song.py @@ -0,0 +1,68 @@ +""" +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 corresponding to ``take`` consecutive + verses starting from the verse that begins with ``start`` green bottles. + + 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): + if i == 1: + verse = [ + f"{NUMBERS[i - 1]} green bottle hanging on the wall,", + f"{NUMBERS[i - 1]} green bottle hanging on the wall,", + "And if one green bottle should accidentally fall,", + "There'll be no green bottles hanging on the wall.", + ] + elif i != start - take + 1: + verse = [ + f"{NUMBERS[i - 1]} green bottles hanging on the wall,", + f"{NUMBERS[i - 1]} green bottles hanging on the wall,", + "And if one green bottle should accidentally fall,", + f"There'll be {NUMBERS[i - 2].lower()} green " + f"{'bottles' if NUMBERS[i - 2] != 'One' else 'bottle'} hanging on" + f" the wall.", + "", + ] + else: + verse = [ + f"{NUMBERS[i - 1]} green bottles hanging on the wall,", + f"{NUMBERS[i - 1]} green bottles hanging on the wall,", + "And if one green bottle should accidentally fall,", + f"There'll be {NUMBERS[i - 2].lower()} green " + f"{'bottles' if NUMBERS[i - 2] != 'One' else 'bottle'} hanging on" + f" the wall.", + ] + result += verse + return result