diff --git a/config.json b/config.json index fcf995781b..38e1866913 100644 --- a/config.json +++ b/config.json @@ -955,6 +955,18 @@ "stacks" ] }, + { + "uuid": "f229746e-5ea9-4774-b3e0-9b9c2ebf9558", + "slug": "food-chain", + "core": false, + "unlocked_by": null, + "difficulty": 4, + "topics": [ + "algorithms", + "loops", + "conditionals" + ] + }, { "uuid": "e7351e8e-d3ff-4621-b818-cd55cf05bffd", "slug": "accumulate", diff --git a/exercises/food-chain/README.md b/exercises/food-chain/README.md new file mode 100644 index 0000000000..f45555ddeb --- /dev/null +++ b/exercises/food-chain/README.md @@ -0,0 +1,81 @@ +# Food chain + +Generate the lyrics of the song 'I Know an Old Lady Who Swallowed a Fly'. + +While you could copy/paste the lyrics, +or read them from a file, this problem is much more +interesting if you approach it algorithmically. + +This is a [cumulative song](http://en.wikipedia.org/wiki/Cumulative_song) of unknown origin. + +This is one of many common variants. + +```text +I know an old lady who swallowed a fly. +I don't know why she swallowed the fly. Perhaps she'll die. + +I know an old lady who swallowed a spider. +It wriggled and jiggled and tickled inside her. +She swallowed the spider to catch the fly. +I don't know why she swallowed the fly. Perhaps she'll die. + +I know an old lady who swallowed a bird. +How absurd to swallow a bird! +She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her. +She swallowed the spider to catch the fly. +I don't know why she swallowed the fly. Perhaps she'll die. + +I know an old lady who swallowed a cat. +Imagine that, to swallow a cat! +She swallowed the cat to catch the bird. +She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her. +She swallowed the spider to catch the fly. +I don't know why she swallowed the fly. Perhaps she'll die. + +I know an old lady who swallowed a dog. +What a hog, to swallow a dog! +She swallowed the dog to catch the cat. +She swallowed the cat to catch the bird. +She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her. +She swallowed the spider to catch the fly. +I don't know why she swallowed the fly. Perhaps she'll die. + +I know an old lady who swallowed a goat. +Just opened her throat and swallowed a goat! +She swallowed the goat to catch the dog. +She swallowed the dog to catch the cat. +She swallowed the cat to catch the bird. +She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her. +She swallowed the spider to catch the fly. +I don't know why she swallowed the fly. Perhaps she'll die. + +I know an old lady who swallowed a cow. +I don't know how she swallowed a cow! +She swallowed the cow to catch the goat. +She swallowed the goat to catch the dog. +She swallowed the dog to catch the cat. +She swallowed the cat to catch the bird. +She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her. +She swallowed the spider to catch the fly. +I don't know why she swallowed the fly. Perhaps she'll die. + +I know an old lady who swallowed a horse. +She's dead, of course! +``` + +### Submitting Exercises + +Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. + +For example, if you're submitting `bob.py` for the Bob exercise, the submit command would be something like `exercism submit /python/bob/bob.py`. + + +For more detailed information about running tests, code style and linting, +please see the [help page](http://exercism.io/languages/python). + +## Source + +Inspired by the Extreme Startup game [https://github.com/rchatley/extreme_startup](https://github.com/rchatley/extreme_startup) + +## Submitting Incomplete Solutions +It's possible to submit an incomplete solution so you can see how others have completed the exercise. diff --git a/exercises/food-chain/example.py b/exercises/food-chain/example.py new file mode 100644 index 0000000000..5deb43a94f --- /dev/null +++ b/exercises/food-chain/example.py @@ -0,0 +1,45 @@ +def chain(): + + animals = ["fly", "spider", "bird", "cat", "dog", "goat", "cow", "horse"] + + phrases = [" wriggled and jiggled and tickled inside her.", + "How absurd to swallow a bird!", + "Imagine that, to swallow a cat!", + "What a hog, to swallow a dog!", + "Just opened her throat and swallowed a goat!", + "I don't know how she swallowed a cow!", + "She's dead, of course!"] + + old_lady = "I know an old lady who swallowed a " + swallowed = "She swallowed the to catch the " + die = "I don't know why she swallowed the fly. Perhaps she'll die." + + song = "" + verse = "" + chain = "" + + for number, animal in enumerate(animals): + verse = old_lady + animal + ".\n" + + if number == 7: + verse += phrases[6] + else: + if number == 0: + chain = swallowed + animal + '.\n' + elif number == 1: + verse += "It" + phrases[0] + "\n" + chain = chain.replace("", animal) + verse += chain + chain = swallowed+animal+" that"+phrases[0]+"\n"+chain + else: + verse += phrases[number-1] + "\n" + chain = chain.replace("", animal) + verse += chain + chain = swallowed + animal + ".\n" + chain + + verse += die + "\n" + + verse += "\n" + song += verse + + return song diff --git a/exercises/food-chain/food_chain.py b/exercises/food-chain/food_chain.py new file mode 100644 index 0000000000..fe6bd97378 --- /dev/null +++ b/exercises/food-chain/food_chain.py @@ -0,0 +1,2 @@ +def chain(): + pass diff --git a/exercises/food-chain/food_chain_test.py b/exercises/food-chain/food_chain_test.py new file mode 100644 index 0000000000..100e37ae33 --- /dev/null +++ b/exercises/food-chain/food_chain_test.py @@ -0,0 +1,109 @@ +import unittest + +from food_chain import chain + +# Tests adapted from `problem-specifications//canonical-data.json` @ v1.0.0 + +song = """I know an old lady who swallowed a fly. +I don't know why she swallowed the fly. Perhaps she'll die. + +I know an old lady who swallowed a spider. +It wriggled and jiggled and tickled inside her. +She swallowed the spider to catch the fly. +I don't know why she swallowed the fly. Perhaps she'll die. + +I know an old lady who swallowed a bird. +How absurd to swallow a bird! +She swallowed the bird to catch the spider that wriggled +and jiggled and tickled inside her. +She swallowed the spider to catch the fly. +I don't know why she swallowed the fly. Perhaps she'll die. + +I know an old lady who swallowed a cat. +Imagine that, to swallow a cat! +She swallowed the cat to catch the bird. +She swallowed the bird to catch the spider that wriggled +and jiggled and tickled inside her. +She swallowed the spider to catch the fly. +I don't know why she swallowed the fly. Perhaps she'll die. + +I know an old lady who swallowed a dog. +What a hog, to swallow a dog! +She swallowed the dog to catch the cat. +She swallowed the cat to catch the bird. +She swallowed the bird to catch the spider that wriggled +and jiggled and tickled inside her. +She swallowed the spider to catch the fly. +I don't know why she swallowed the fly. Perhaps she'll die. + +I know an old lady who swallowed a goat. +Just opened her throat and swallowed a goat! +She swallowed the goat to catch the dog. +She swallowed the dog to catch the cat. +She swallowed the cat to catch the bird. +She swallowed the bird to catch the spider that wriggled +and jiggled and tickled inside her. +She swallowed the spider to catch the fly. +I don't know why she swallowed the fly. Perhaps she'll die. + +I know an old lady who swallowed a cow. +I don't know how she swallowed a cow! +She swallowed the cow to catch the goat. +She swallowed the goat to catch the dog. +She swallowed the dog to catch the cat. +She swallowed the cat to catch the bird. +She swallowed the bird to catch the spider that wriggled +and jiggled and tickled inside her. +She swallowed the spider to catch the fly. +I don't know why she swallowed the fly. Perhaps she'll die. + +I know an old lady who swallowed a horse. +She's dead, of course!""" + + +def verses(letter): + return letter.replace('die.', 'die.slice').split('slice') + + +original = [verse.replace("\n", "").replace(" ", "").lower() + for verse in verses(song)] + +generated = [verse.replace("\n", "").replace(" ", "").lower() + for verse in verses(chain())] + + +class FoodChainTest(unittest.TestCase): + + def test_fly(self): + self.assertEqual(original[0], generated[0]) + + def test_spider(self): + self.assertEqual(original[1], generated[1]) + + def test_bird(self): + self.assertEqual(original[2], generated[2]) + + def test_cat(self): + self.assertEqual(original[3], generated[3]) + + def test_dog(self): + self.assertEqual(original[4], generated[4]) + + def test_goat(self): + self.assertEqual(original[5], generated[5]) + + def test_cow(self): + self.assertEqual(original[6], generated[6]) + + def test_horse(self): + self.assertEqual(original[7], generated[7]) + + def test_multiple_verses(self): + self.assertEqual(original[0:3], generated[0:3]) + + def test_full_song(self): + self.assertEqual(original, generated) + + +if __name__ == '__main__': + unittest.main()