Added bottle_song

This commit is contained in:
Meatball
2022-12-04 11:22:27 +01:00
committed by BethanyG
parent 6182702de1
commit bae4589faf
8 changed files with 322 additions and 0 deletions

View File

@@ -2151,6 +2151,26 @@
"difficulty": 3,
"status": "deprecated"
},
{
"slug": "bottle-song",
"name": "Bottle Song",
"uuid": "70bec74a-0677-40c9-b9c9-cbcc49a2eae4",
"practices": ["generators"],
"prerequisites": [
"basics",
"conditionals",
"dicts",
"lists",
"list-methods",
"loops",
"numbers",
"strings",
"string-methods",
"tuples"
],
"difficulty": 3,
"status": "deprecated"
},
{
"slug": "trinary",
"name": "Trinary",

View File

@@ -0,0 +1,57 @@
# Instructions
Recite the lyrics to that popular children's repetitive song: Ten Green Bottles.
Note that not all verses are identical.
```text
Ten green bottles hanging on the wall,
Ten green bottles hanging on the wall,
And if one green bottle should accidentally fall,
There'll be nine green bottles hanging on the wall.
Nine green bottles hanging on the wall,
Nine green bottles hanging on the wall,
And if one green bottle should accidentally fall,
There'll be eight green bottles hanging on the wall.
Eight green bottles hanging on the wall,
Eight green bottles hanging on the wall,
And if one green bottle should accidentally fall,
There'll be seven green bottles hanging on the wall.
Seven green bottles hanging on the wall,
Seven green bottles hanging on the wall,
And if one green bottle should accidentally fall,
There'll be six green bottles hanging on the wall.
Six green bottles hanging on the wall,
Six green bottles hanging on the wall,
And if one green bottle should accidentally fall,
There'll be five green bottles hanging on the wall.
Five green bottles hanging on the wall,
Five green bottles hanging on the wall,
And if one green bottle should accidentally fall,
There'll be four green bottles hanging on the wall.
Four green bottles hanging on the wall,
Four green bottles hanging on the wall,
And if one green bottle should accidentally fall,
There'll be three green bottles hanging on the wall.
Three green bottles hanging on the wall,
Three green bottles hanging on the wall,
And if one green bottle should accidentally fall,
There'll be two green bottles hanging on the wall.
Two green bottles hanging on the wall,
Two green bottles hanging on the wall,
And if one green bottle should accidentally fall,
There'll be one green bottle hanging on the wall.
One green bottle hanging on the wall,
One green bottle hanging on the wall,
And if one green bottle should accidentally fall,
There'll be no green bottles hanging on the wall.
```

View File

@@ -0,0 +1,19 @@
{
"blurb": "Produce the lyrics to the popular children's repetitive song: Ten Green Bottles.",
"authors": ["meatball133", "BethanyG"],
"contributors": [
],
"files": {
"solution": [
"bottle_song.py"
],
"test": [
"bottle_song_test.py"
],
"example": [
".meta/example.py"
]
},
"source": "Wikipedia",
"source_url": "https://en.wikipedia.org/wiki/Ten_Green_Bottles"
}

View File

@@ -0,0 +1,34 @@
NUMBERS = {10: "ten", 9: "nine", 8: "eight", 7: "seven", 6: "six", 5: "five", 4: "four", 3: "three", 2: "two", 1: "one", 0: "no"}
def recite(start, take=1):
results = []
for idx in range(start, start - take, -1):
results.extend(verse(idx))
if idx > start - take + 1:
results.append('')
return results
def verse(number):
return [
*main_verse(number),
"And if one green bottle should accidentally fall,",
last_verse(number)
]
def main_verse(number):
if number == 1:
return [
f'One green bottle hanging on the wall,',
f'One green bottle hanging on the wall,',
]
else:
return [
f'{NUMBERS[number].capitalize()} green bottles hanging on the wall,',
f'{NUMBERS[number].capitalize()} green bottles hanging on the wall,',]
def last_verse(number):
if number -1 == 1:
return f"There'll be one green bottle hanging on the wall."
else:
return f"There'll be {NUMBERS[number-1]} green bottles hanging on the wall."

View File

@@ -0,0 +1,23 @@
{%- import "generator_macros.j2" as macros with context -%}
{{ macros.header() }}
class {{ exercise | camel_case }}Test(unittest.TestCase):
{% for case in cases -%}
{% for subcase in case["cases"] -%}
{% for subsubcase in subcase["cases"] -%}
def test_{{ subsubcase["description"] | to_snake }}(self):
{% set start = subsubcase["input"]["startBottles"] -%}
{% set take = subsubcase["input"]["takeDown"] -%}
expected = {{ subsubcase["expected"] }}
{%- if take == 1 %}
self.assertEqual({{ subsubcase["property"] }}(start={{ start}}), expected)
{% else %}
self.assertEqual({{ subsubcase["property"] }}(start={{ start}}, take={{ take }}), expected)
{% endif %}
{% endfor %}
{% endfor %}
{% endfor %}
{{ macros.footer() }}

View File

@@ -0,0 +1,31 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.
[d4ccf8fc-01dc-48c0-a201-4fbeb30f2d03]
description = "verse -> single verse -> first generic verse"
[0f0aded3-472a-4c64-b842-18d4f1f5f030]
description = "verse -> single verse -> last generic verse"
[f61f3c97-131f-459e-b40a-7428f3ed99d9]
description = "verse -> single verse -> verse with 2 bottles"
[05eadba9-5dbd-401e-a7e8-d17cc9baa8e0]
description = "verse -> single verse -> verse with 1 bottle"
[a4a28170-83d6-4dc1-bd8b-319b6abb6a80]
description = "lyrics -> multiple verses -> first two verses"
[3185d438-c5ac-4ce6-bcd3-02c9ff1ed8db]
description = "lyrics -> multiple verses -> last three verses"
[28c1584a-0e51-4b65-9ae2-fbc0bf4bbb28]
description = "lyrics -> multiple verses -> all verses"

View File

@@ -0,0 +1,2 @@
def recite(start, take=1):
pass

View File

@@ -0,0 +1,136 @@
import unittest
from bottle_song import (
recite,
)
# Tests adapted from `problem-specifications//canonical-data.json`
class BottleSongTest(unittest.TestCase):
def test_first_generic_verse(self):
expected = [
"Ten green bottles hanging on the wall,",
"Ten green bottles hanging on the wall,",
"And if one green bottle should accidentally fall,",
"There'll be nine green bottles hanging on the wall.",
]
self.assertEqual(recite(start=10), expected)
def test_last_generic_verse(self):
expected = [
"Three green bottles hanging on the wall,",
"Three green bottles hanging on the wall,",
"And if one green bottle should accidentally fall,",
"There'll be two green bottles hanging on the wall.",
]
self.assertEqual(recite(start=3), expected)
def test_verse_with_2_bottles(self):
expected = [
"Two green bottles hanging on the wall,",
"Two green bottles hanging on the wall,",
"And if one green bottle should accidentally fall,",
"There'll be one green bottle hanging on the wall.",
]
self.assertEqual(recite(start=2), expected)
def test_verse_with_1_bottle(self):
expected = [
"One green bottle hanging on the wall,",
"One green bottle hanging on the wall,",
"And if one green bottle should accidentally fall,",
"There'll be no green bottles hanging on the wall.",
]
self.assertEqual(recite(start=1), expected)
def test_first_two_verses(self):
expected = [
"Ten green bottles hanging on the wall,",
"Ten green bottles hanging on the wall,",
"And if one green bottle should accidentally fall,",
"There'll be nine green bottles hanging on the wall.",
"",
"Nine green bottles hanging on the wall,",
"Nine green bottles hanging on the wall,",
"And if one green bottle should accidentally fall,",
"There'll be eight green bottles hanging on the wall.",
]
self.assertEqual(recite(start=10, take=2), expected)
def test_last_three_verses(self):
expected = [
"Three green bottles hanging on the wall,",
"Three green bottles hanging on the wall,",
"And if one green bottle should accidentally fall,",
"There'll be two green bottles hanging on the wall.",
"",
"Two green bottles hanging on the wall,",
"Two green bottles hanging on the wall,",
"And if one green bottle should accidentally fall,",
"There'll be one green bottle hanging on the wall.",
"",
"One green bottle hanging on the wall,",
"One green bottle hanging on the wall,",
"And if one green bottle should accidentally fall,",
"There'll be no green bottles hanging on the wall.",
]
self.assertEqual(recite(start=3, take=3), expected)
def test_all_verses(self):
expected = [
"Ten green bottles hanging on the wall,",
"Ten green bottles hanging on the wall,",
"And if one green bottle should accidentally fall,",
"There'll be nine green bottles hanging on the wall.",
"",
"Nine green bottles hanging on the wall,",
"Nine green bottles hanging on the wall,",
"And if one green bottle should accidentally fall,",
"There'll be eight green bottles hanging on the wall.",
"",
"Eight green bottles hanging on the wall,",
"Eight green bottles hanging on the wall,",
"And if one green bottle should accidentally fall,",
"There'll be seven green bottles hanging on the wall.",
"",
"Seven green bottles hanging on the wall,",
"Seven green bottles hanging on the wall,",
"And if one green bottle should accidentally fall,",
"There'll be six green bottles hanging on the wall.",
"",
"Six green bottles hanging on the wall,",
"Six green bottles hanging on the wall,",
"And if one green bottle should accidentally fall,",
"There'll be five green bottles hanging on the wall.",
"",
"Five green bottles hanging on the wall,",
"Five green bottles hanging on the wall,",
"And if one green bottle should accidentally fall,",
"There'll be four green bottles hanging on the wall.",
"",
"Four green bottles hanging on the wall,",
"Four green bottles hanging on the wall,",
"And if one green bottle should accidentally fall,",
"There'll be three green bottles hanging on the wall.",
"",
"Three green bottles hanging on the wall,",
"Three green bottles hanging on the wall,",
"And if one green bottle should accidentally fall,",
"There'll be two green bottles hanging on the wall.",
"",
"Two green bottles hanging on the wall,",
"Two green bottles hanging on the wall,",
"And if one green bottle should accidentally fall,",
"There'll be one green bottle hanging on the wall.",
"",
"One green bottle hanging on the wall,",
"One green bottle hanging on the wall,",
"And if one green bottle should accidentally fall,",
"There'll be no green bottles hanging on the wall.",
]
self.assertEqual(recite(start=10, take=10), expected)
if __name__ == "__main__":
unittest.main()