From bae4589faffb51543d1b96050fc2568051a44a84 Mon Sep 17 00:00:00 2001 From: Meatball Date: Sun, 4 Dec 2022 11:22:27 +0100 Subject: [PATCH] Added bottle_song --- config.json | 20 +++ .../bottle-song/.docs/instructions.md | 57 ++++++++ .../practice/bottle-song/.meta/config.json | 19 +++ .../practice/bottle-song/.meta/example.py | 34 +++++ .../practice/bottle-song/.meta/template.j2 | 23 +++ .../practice/bottle-song/.meta/tests.toml | 31 ++++ exercises/practice/bottle-song/bottle_song.py | 2 + .../practice/bottle-song/bottle_song_test.py | 136 ++++++++++++++++++ 8 files changed, 322 insertions(+) create mode 100644 exercises/practice/bottle-song/.docs/instructions.md create mode 100644 exercises/practice/bottle-song/.meta/config.json create mode 100644 exercises/practice/bottle-song/.meta/example.py create mode 100644 exercises/practice/bottle-song/.meta/template.j2 create mode 100644 exercises/practice/bottle-song/.meta/tests.toml create mode 100644 exercises/practice/bottle-song/bottle_song.py create mode 100644 exercises/practice/bottle-song/bottle_song_test.py diff --git a/config.json b/config.json index df8633bb..912e9b6e 100644 --- a/config.json +++ b/config.json @@ -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", diff --git a/exercises/practice/bottle-song/.docs/instructions.md b/exercises/practice/bottle-song/.docs/instructions.md new file mode 100644 index 00000000..febdfc86 --- /dev/null +++ b/exercises/practice/bottle-song/.docs/instructions.md @@ -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. +``` diff --git a/exercises/practice/bottle-song/.meta/config.json b/exercises/practice/bottle-song/.meta/config.json new file mode 100644 index 00000000..7845207f --- /dev/null +++ b/exercises/practice/bottle-song/.meta/config.json @@ -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" +} diff --git a/exercises/practice/bottle-song/.meta/example.py b/exercises/practice/bottle-song/.meta/example.py new file mode 100644 index 00000000..18e1167d --- /dev/null +++ b/exercises/practice/bottle-song/.meta/example.py @@ -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." diff --git a/exercises/practice/bottle-song/.meta/template.j2 b/exercises/practice/bottle-song/.meta/template.j2 new file mode 100644 index 00000000..37e45402 --- /dev/null +++ b/exercises/practice/bottle-song/.meta/template.j2 @@ -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() }} diff --git a/exercises/practice/bottle-song/.meta/tests.toml b/exercises/practice/bottle-song/.meta/tests.toml new file mode 100644 index 00000000..1f6e40a3 --- /dev/null +++ b/exercises/practice/bottle-song/.meta/tests.toml @@ -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" diff --git a/exercises/practice/bottle-song/bottle_song.py b/exercises/practice/bottle-song/bottle_song.py new file mode 100644 index 00000000..9a5e67fc --- /dev/null +++ b/exercises/practice/bottle-song/bottle_song.py @@ -0,0 +1,2 @@ +def recite(start, take=1): + pass diff --git a/exercises/practice/bottle-song/bottle_song_test.py b/exercises/practice/bottle-song/bottle_song_test.py new file mode 100644 index 00000000..89877dc4 --- /dev/null +++ b/exercises/practice/bottle-song/bottle_song_test.py @@ -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()