Skip to content

Commit f74a078

Browse files
authored
Merge pull request #305 from ikostan/exercism-sync/010c461f6bfb5eb2
[Sync Iteration] python/bottle-song/7
2 parents b1899ba + 911ee4b commit f74a078

File tree

7 files changed

+443
-0
lines changed

7 files changed

+443
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
"""
2+
Bottle Song
3+
===========
4+
5+
Recite verses from the children's song "Ten Green Bottles".
6+
7+
This module exposes the precomputed lyrics and a helper to
8+
extract slices of the song by verse.
9+
"""
10+
11+
Ten_Green_Bottles: list[str] = [
12+
"Ten green bottles hanging on the wall,",
13+
"Ten green bottles hanging on the wall,",
14+
"And if one green bottle should accidentally fall,",
15+
"There'll be nine green bottles hanging on the wall.",
16+
"",
17+
"Nine green bottles hanging on the wall,",
18+
"Nine green bottles hanging on the wall,",
19+
"And if one green bottle should accidentally fall,",
20+
"There'll be eight green bottles hanging on the wall.",
21+
"",
22+
"Eight green bottles hanging on the wall,",
23+
"Eight green bottles hanging on the wall,",
24+
"And if one green bottle should accidentally fall,",
25+
"There'll be seven green bottles hanging on the wall.",
26+
"",
27+
"Seven green bottles hanging on the wall,",
28+
"Seven green bottles hanging on the wall,",
29+
"And if one green bottle should accidentally fall,",
30+
"There'll be six green bottles hanging on the wall.",
31+
"",
32+
"Six green bottles hanging on the wall,",
33+
"Six green bottles hanging on the wall,",
34+
"And if one green bottle should accidentally fall,",
35+
"There'll be five green bottles hanging on the wall.",
36+
"",
37+
"Five green bottles hanging on the wall,",
38+
"Five green bottles hanging on the wall,",
39+
"And if one green bottle should accidentally fall,",
40+
"There'll be four green bottles hanging on the wall.",
41+
"",
42+
"Four green bottles hanging on the wall,",
43+
"Four green bottles hanging on the wall,",
44+
"And if one green bottle should accidentally fall,",
45+
"There'll be three green bottles hanging on the wall.",
46+
"",
47+
"Three green bottles hanging on the wall,",
48+
"Three green bottles hanging on the wall,",
49+
"And if one green bottle should accidentally fall,",
50+
"There'll be two green bottles hanging on the wall.",
51+
"",
52+
"Two green bottles hanging on the wall,",
53+
"Two green bottles hanging on the wall,",
54+
"And if one green bottle should accidentally fall,",
55+
"There'll be one green bottle hanging on the wall.",
56+
"",
57+
"One green bottle hanging on the wall,",
58+
"One green bottle hanging on the wall,",
59+
"And if one green bottle should accidentally fall,",
60+
"There'll be no green bottles hanging on the wall.",
61+
]
62+
63+
64+
def recite(start: int, take: int = 1) -> list[str]:
65+
"""
66+
Return a slice of the song lyrics corresponding to ``take`` consecutive
67+
verses starting from the verse that begins with ``start`` green bottles.
68+
69+
The lyrics are stored as a flat list where each verse occupies five lines
70+
(four lyric lines plus a trailing blank line). This function computes the
71+
appropriate slice boundaries to return the requested verses in order.
72+
73+
:param start: The number of green bottles to start from (e.g., 10 for
74+
"Ten green bottles"). Must be between 1 and 10.
75+
:param take: Number of consecutive verses to include starting at ``start``.
76+
Defaults to 1.
77+
:returns: A list of lyric lines forming the requested verses.
78+
"""
79+
start: int = -(start * 5)
80+
end: int = len(Ten_Green_Bottles) + start + (take * 5)
81+
return Ten_Green_Bottles[start + 1 : end]
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
"""
2+
Bottle Song
3+
===========
4+
5+
Recite verses from the children's song "Ten Green Bottles".
6+
7+
This module exposes the precomputed lyrics and a helper to
8+
extract slices of the song by verse.
9+
"""
10+
11+
NUMBERS: tuple = (
12+
"One",
13+
"Two",
14+
"Three",
15+
"Four",
16+
"Five",
17+
"Six",
18+
"Seven",
19+
"Eight",
20+
"Nine",
21+
"Ten",
22+
)
23+
24+
25+
def recite(start: int, take: int = 1) -> list[str]:
26+
"""
27+
Return a slice of the song lyrics corresponding to ``take`` consecutive
28+
verses starting from the verse that begins with ``start`` green bottles.
29+
30+
The lyrics are stored as a flat list where each verse occupies five lines
31+
(four lyric lines plus a trailing blank line). This function computes the
32+
appropriate slice boundaries to return the requested verses in order.
33+
34+
:param start: The number of green bottles to start from (e.g., 10 for
35+
"Ten green bottles"). Must be between 1 and 10.
36+
:param take: Number of consecutive verses to include starting at ``start``.
37+
Defaults to 1.
38+
:returns: A list of lyric lines forming the requested verses.
39+
"""
40+
result: list[str] = []
41+
for i in range(start, start - take, -1):
42+
if i == 1:
43+
verse = [
44+
f"{NUMBERS[i - 1]} green bottle hanging on the wall,",
45+
f"{NUMBERS[i - 1]} green bottle hanging on the wall,",
46+
"And if one green bottle should accidentally fall,",
47+
"There'll be no green bottles hanging on the wall.",
48+
]
49+
elif i != start - take + 1:
50+
verse = [
51+
f"{NUMBERS[i - 1]} green bottles hanging on the wall,",
52+
f"{NUMBERS[i - 1]} green bottles hanging on the wall,",
53+
"And if one green bottle should accidentally fall,",
54+
f"There'll be {NUMBERS[i - 2].lower()} green {'bottles' if NUMBERS[i - 2] != 'One' else 'bottle'} hanging on"
55+
f" the wall.",
56+
"",
57+
]
58+
else:
59+
verse = [
60+
f"{NUMBERS[i - 1]} green bottles hanging on the wall,",
61+
f"{NUMBERS[i - 1]} green bottles hanging on the wall,",
62+
"And if one green bottle should accidentally fall,",
63+
f"There'll be {NUMBERS[i - 2].lower()} green {'bottles' if NUMBERS[i - 2] != 'One' else 'bottle'} hanging on"
64+
f" the wall.",
65+
]
66+
result += verse
67+
return result
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
"""
2+
Bottle Song.
3+
4+
Recite verses from the children's song "Ten Green Bottles".
5+
6+
This module exposes the precomputed lyrics and a helper to
7+
extract slices of the song by verse.
8+
"""
9+
10+
NUMBERS: tuple = (
11+
"One",
12+
"Two",
13+
"Three",
14+
"Four",
15+
"Five",
16+
"Six",
17+
"Seven",
18+
"Eight",
19+
"Nine",
20+
"Ten",
21+
)
22+
23+
24+
def recite(start: int, take: int = 1) -> list[str]:
25+
"""
26+
Return a slice of the song lyrics corresponding to ``take`` consecutive
27+
verses starting from the verse that begins with ``start`` green bottles.
28+
29+
The lyrics are stored as a flat list where each verse occupies five lines
30+
(four lyric lines plus a trailing blank line). This function computes the
31+
appropriate slice boundaries to return the requested verses in order.
32+
33+
:param start: The number of green bottles to start from (e.g., 10 for
34+
"Ten green bottles"). Must be between 1 and 10.
35+
:param take: Number of consecutive verses to include starting at ``start``.
36+
Defaults to 1.
37+
:returns: A list of lyric lines forming the requested verses.
38+
"""
39+
result: list[str] = []
40+
for i in range(start, start - take, -1):
41+
if i == 1:
42+
verse = [
43+
f"{NUMBERS[i - 1]} green bottle hanging on the wall,",
44+
f"{NUMBERS[i - 1]} green bottle hanging on the wall,",
45+
"And if one green bottle should accidentally fall,",
46+
"There'll be no green bottles hanging on the wall.",
47+
]
48+
elif i != start - take + 1:
49+
verse = [
50+
f"{NUMBERS[i - 1]} green bottles hanging on the wall,",
51+
f"{NUMBERS[i - 1]} green bottles hanging on the wall,",
52+
"And if one green bottle should accidentally fall,",
53+
f"There'll be {NUMBERS[i - 2].lower()} green "
54+
f"{'bottles' if NUMBERS[i - 2] != 'One' else 'bottle'} hanging on"
55+
f" the wall.",
56+
"",
57+
]
58+
else:
59+
verse = [
60+
f"{NUMBERS[i - 1]} green bottles hanging on the wall,",
61+
f"{NUMBERS[i - 1]} green bottles hanging on the wall,",
62+
"And if one green bottle should accidentally fall,",
63+
f"There'll be {NUMBERS[i - 2].lower()} green "
64+
f"{'bottles' if NUMBERS[i - 2] != 'One' else 'bottle'} hanging on"
65+
f" the wall.",
66+
]
67+
result += verse
68+
return result
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
"""
2+
Bottle Song.
3+
4+
Recite verses from the children's song "Ten Green Bottles".
5+
6+
This module exposes the precomputed lyrics and a helper to
7+
extract slices of the song by verse.
8+
"""
9+
10+
NUMBERS: tuple = (
11+
"One",
12+
"Two",
13+
"Three",
14+
"Four",
15+
"Five",
16+
"Six",
17+
"Seven",
18+
"Eight",
19+
"Nine",
20+
"Ten",
21+
)
22+
23+
24+
def recite(start: int, take: int = 1) -> list[str]:
25+
"""
26+
Return a slice of the song lyrics corresponding to ``take`` consecutive
27+
verses starting from the verse that begins with ``start`` green bottles.
28+
29+
The lyrics are generated as a flat list where each verse occupies five lines
30+
(four lyric lines plus a trailing blank line). This function generates the
31+
requested verses dynamically based on the song's pattern.
32+
33+
:param start: The number of green bottles to start from (e.g., 10 for
34+
"Ten green bottles"). Must be between 1 and 10.
35+
:param take: Number of consecutive verses to include starting at ``start``.
36+
Defaults to 1.
37+
:returns: A list of lyric lines forming the requested verses.
38+
"""
39+
result: list[str] = []
40+
for i in range(start, start - take, -1):
41+
if i == 1:
42+
verse = [
43+
f"{NUMBERS[i - 1]} green bottle hanging on the wall,",
44+
f"{NUMBERS[i - 1]} green bottle hanging on the wall,",
45+
"And if one green bottle should accidentally fall,",
46+
"There'll be no green bottles hanging on the wall.",
47+
]
48+
elif i != start - take + 1:
49+
verse = [
50+
f"{NUMBERS[i - 1]} green bottles hanging on the wall,",
51+
f"{NUMBERS[i - 1]} green bottles hanging on the wall,",
52+
"And if one green bottle should accidentally fall,",
53+
f"There'll be {NUMBERS[i - 2].lower()} green "
54+
f"{'bottles' if NUMBERS[i - 2] != 'One' else 'bottle'} hanging on"
55+
f" the wall.",
56+
"",
57+
]
58+
else:
59+
verse = [
60+
f"{NUMBERS[i - 1]} green bottles hanging on the wall,",
61+
f"{NUMBERS[i - 1]} green bottles hanging on the wall,",
62+
"And if one green bottle should accidentally fall,",
63+
f"There'll be {NUMBERS[i - 2].lower()} green "
64+
f"{'bottles' if NUMBERS[i - 2] != 'One' else 'bottle'} hanging on"
65+
f" the wall.",
66+
]
67+
result += verse
68+
return result
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"""
2+
Bottle Song.
3+
4+
Recite verses from the children's song "Ten Green Bottles".
5+
6+
This module exposes the precomputed lyrics and a helper to
7+
extract slices of the song by verse.
8+
"""
9+
10+
NUMBERS: tuple = (
11+
"One",
12+
"Two",
13+
"Three",
14+
"Four",
15+
"Five",
16+
"Six",
17+
"Seven",
18+
"Eight",
19+
"Nine",
20+
"Ten",
21+
)
22+
23+
24+
def recite(start: int, take: int = 1) -> list[str]:
25+
"""
26+
Return a slice of the song lyrics corresponding to ``take`` consecutive
27+
verses starting from the verse that begins with ``start`` green bottles.
28+
29+
The lyrics are generated as a flat list where each verse occupies five lines
30+
(four lyric lines plus a trailing blank line). This function generates the
31+
requested verses dynamically based on the song's pattern.
32+
33+
:param start: The number of green bottles to start from (e.g., 10 for
34+
"Ten green bottles"). Must be between 1 and 10.
35+
:param take: Number of consecutive verses to include starting at ``start``.
36+
Defaults to 1.
37+
:returns: A list of lyric lines forming the requested verses.
38+
"""
39+
result: list[str] = []
40+
for i in range(start, start - take, -1):
41+
result += [
42+
f"{NUMBERS[i - 1]} green "
43+
f"{'bottles' if NUMBERS[i - 1] != 'One' else 'bottle'} hanging on the wall,",
44+
f"{NUMBERS[i - 1]} green "
45+
f"{'bottles' if NUMBERS[i - 1] != 'One' else 'bottle'} hanging on the wall,",
46+
"And if one green bottle should accidentally fall,",
47+
f"There'll be {NUMBERS[i - 2].lower() if (i - 2) > -1 else 'no'} green "
48+
f"{'bottles' if NUMBERS[i - 2] != 'One' else 'bottle'} hanging on"
49+
f" the wall.",
50+
]
51+
if i != start - take + 1:
52+
result.append("")
53+
return result
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"""
2+
Bottle Song.
3+
4+
Recite verses from the children's song "Ten Green Bottles".
5+
6+
This module exposes the precomputed lyrics and a helper to
7+
extract slices of the song by verse.
8+
"""
9+
10+
NUMBERS: tuple = (
11+
"One",
12+
"Two",
13+
"Three",
14+
"Four",
15+
"Five",
16+
"Six",
17+
"Seven",
18+
"Eight",
19+
"Nine",
20+
"Ten",
21+
)
22+
23+
24+
def recite(start: int, take: int = 1) -> list[str]:
25+
"""
26+
Return a slice of the song lyrics.
27+
28+
The lyrics are generated as a flat list where each verse occupies five lines
29+
(four lyric lines plus a trailing blank line). This function generates the
30+
requested verses dynamically based on the song's pattern.
31+
32+
:param start: The number of green bottles to start from (e.g., 10 for
33+
"Ten green bottles"). Must be between 1 and 10.
34+
:param take: Number of consecutive verses to include starting at ``start``.
35+
Defaults to 1.
36+
:returns: A list of lyric lines forming the requested verses.
37+
"""
38+
result: list[str] = []
39+
for i in range(start, start - take, -1):
40+
result += [
41+
f"{NUMBERS[i - 1]} green "
42+
f"{'bottles' if NUMBERS[i - 1] != 'One' else 'bottle'} hanging on the wall,",
43+
f"{NUMBERS[i - 1]} green "
44+
f"{'bottles' if NUMBERS[i - 1] != 'One' else 'bottle'} hanging on the wall,",
45+
"And if one green bottle should accidentally fall,",
46+
f"There'll be {NUMBERS[i - 2].lower() if (i - 2) > -1 else 'no'} green "
47+
f"{'bottles' if NUMBERS[i - 2] != 'One' else 'bottle'} hanging on"
48+
f" the wall.",
49+
]
50+
if i != start - take + 1:
51+
result.append("")
52+
return result

0 commit comments

Comments
 (0)