From ad7b513b385c954d9e16530fe576e37bd67f9167 Mon Sep 17 00:00:00 2001 From: luanJPB Date: Fri, 6 Oct 2017 17:58:47 -0300 Subject: [PATCH 1/8] Implement exercise food-chain From db9e968ce73598a96917990c2bc1b52294abb2f6 Mon Sep 17 00:00:00 2001 From: luanJPB Date: Sat, 7 Oct 2017 14:52:11 -0300 Subject: [PATCH 2/8] Implement exercise food-chain --- exercises/food-chain/README.md | 81 +++++++++++++++++++++++++ exercises/food-chain/example.py | 45 ++++++++++++++ exercises/food-chain/food_chain.py | 2 + exercises/food-chain/food_chain_test.py | 64 +++++++++++++++++++ 4 files changed, 192 insertions(+) create mode 100644 exercises/food-chain/README.md create mode 100644 exercises/food-chain/example.py create mode 100644 exercises/food-chain/food_chain.py create mode 100644 exercises/food-chain/food_chain_test.py 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..06641e1c82 --- /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..ef8458a995 --- /dev/null +++ b/exercises/food-chain/food_chain_test.py @@ -0,0 +1,64 @@ +import unittest + +from food_chain import chain + +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!""" + +song = song.replace("\n", "").replace(" ", "").lower() + +class FoodChainTest(unittest.TestCase): + + def test_equality(self): + self.assertEqual(chain().replace("\n", "").replace(" ", "").lower(), song) + +if __name__ == '__main__': + unittest.main() From 79c4870ce9309e985801af7a1b048908bd8bb5e5 Mon Sep 17 00:00:00 2001 From: luanJPB Date: Sun, 15 Oct 2017 10:36:02 -0200 Subject: [PATCH 3/8] Implement exercise food-chain --- exercises/food-chain/example.py | 2 +- exercises/food-chain/food_chain_test.py | 20 ++++++++++++++------ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/exercises/food-chain/example.py b/exercises/food-chain/example.py index 06641e1c82..c13126a5a7 100644 --- a/exercises/food-chain/example.py +++ b/exercises/food-chain/example.py @@ -30,7 +30,7 @@ def chain(): verse += "It" + phrases[0] + "\n" chain = chain.replace("", animal) verse += chain - chain = swallowed + animal + " that" + phrases[0] + "\n" + chain + chain=swallowed+animal+" that"+phrases[0]+"\n"+chain else: verse += phrases[number-1] + "\n" chain = chain.replace("", animal) diff --git a/exercises/food-chain/food_chain_test.py b/exercises/food-chain/food_chain_test.py index ef8458a995..724425623c 100644 --- a/exercises/food-chain/food_chain_test.py +++ b/exercises/food-chain/food_chain_test.py @@ -12,14 +12,16 @@ 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 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 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. @@ -27,7 +29,8 @@ 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 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. @@ -36,7 +39,8 @@ 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 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. @@ -46,7 +50,8 @@ 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 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. @@ -57,8 +62,11 @@ class FoodChainTest(unittest.TestCase): + def test_equality(self): - self.assertEqual(chain().replace("\n", "").replace(" ", "").lower(), song) + to_test = chain().replace("\n", "").replace(" ", "") + self.assertEqual(to_test.lower(),song) + if __name__ == '__main__': unittest.main() From 2fd5cdc0f63b9b1949f639f7af630ecae9d6bd6d Mon Sep 17 00:00:00 2001 From: luanJPB Date: Sun, 15 Oct 2017 10:55:21 -0200 Subject: [PATCH 4/8] Implement exercise food-chain --- exercises/food-chain/food_chain_test.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/exercises/food-chain/food_chain_test.py b/exercises/food-chain/food_chain_test.py index 724425623c..dfef61fd63 100644 --- a/exercises/food-chain/food_chain_test.py +++ b/exercises/food-chain/food_chain_test.py @@ -30,7 +30,7 @@ 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. +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. @@ -60,12 +60,12 @@ song = song.replace("\n", "").replace(" ", "").lower() -class FoodChainTest(unittest.TestCase): +class FoodChainTest(unittest.TestCase): def test_equality(self): to_test = chain().replace("\n", "").replace(" ", "") - self.assertEqual(to_test.lower(),song) + self.assertEqual(to_test.lower(), song) if __name__ == '__main__': From 6bfc606559efc793293affa6455b0644ea099ec8 Mon Sep 17 00:00:00 2001 From: luanJPB Date: Sun, 15 Oct 2017 11:41:35 -0200 Subject: [PATCH 5/8] Implement exercise food-chain --- exercises/food-chain/example.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/food-chain/example.py b/exercises/food-chain/example.py index c13126a5a7..5deb43a94f 100644 --- a/exercises/food-chain/example.py +++ b/exercises/food-chain/example.py @@ -30,7 +30,7 @@ def chain(): verse += "It" + phrases[0] + "\n" chain = chain.replace("", animal) verse += chain - chain=swallowed+animal+" that"+phrases[0]+"\n"+chain + chain = swallowed+animal+" that"+phrases[0]+"\n"+chain else: verse += phrases[number-1] + "\n" chain = chain.replace("", animal) From a886f1c44dd62178d0cc863a1d0c6a0dc3c4efd0 Mon Sep 17 00:00:00 2001 From: luanJPB Date: Sun, 15 Oct 2017 12:37:17 -0200 Subject: [PATCH 6/8] Implement exercise food-chain --- config.json | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/config.json b/config.json index ce1ff7d36a..5a18f80d43 100644 --- a/config.json +++ b/config.json @@ -947,6 +947,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", From 611a3166aff057c044b441c95d47ab852884e7cb Mon Sep 17 00:00:00 2001 From: luanJPB Date: Sun, 15 Oct 2017 15:09:28 -0200 Subject: [PATCH 7/8] Implement exercise food-chain --- exercises/food-chain/food_chain_test.py | 45 ++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/exercises/food-chain/food_chain_test.py b/exercises/food-chain/food_chain_test.py index dfef61fd63..9a4f703531 100644 --- a/exercises/food-chain/food_chain_test.py +++ b/exercises/food-chain/food_chain_test.py @@ -2,6 +2,8 @@ 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. @@ -58,14 +60,49 @@ I know an old lady who swallowed a horse. She's dead, of course!""" -song = song.replace("\n", "").replace(" ", "").lower() + +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_equality(self): - to_test = chain().replace("\n", "").replace(" ", "") - self.assertEqual(to_test.lower(), song) + 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__': From c45b0ea0bebebfb92680487557d69441c488ed71 Mon Sep 17 00:00:00 2001 From: luanJPB Date: Sun, 15 Oct 2017 15:16:27 -0200 Subject: [PATCH 8/8] Implement exercise food-chain --- exercises/food-chain/food_chain_test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/exercises/food-chain/food_chain_test.py b/exercises/food-chain/food_chain_test.py index 9a4f703531..100e37ae33 100644 --- a/exercises/food-chain/food_chain_test.py +++ b/exercises/food-chain/food_chain_test.py @@ -66,10 +66,10 @@ def verses(letter): original = [verse.replace("\n", "").replace(" ", "").lower() -for verse in verses(song)] + for verse in verses(song)] generated = [verse.replace("\n", "").replace(" ", "").lower() -for verse in verses(chain())] + for verse in verses(chain())] class FoodChainTest(unittest.TestCase):