Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions solutions/python/bottle-song/6/bottle_song.py
Original file line number Diff line number Diff line change
@@ -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
Loading