Skip to content

Implement exercise food-chain #755

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Oct 15, 2017
12 changes: 12 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
81 changes: 81 additions & 0 deletions exercises/food-chain/README.md
Original file line number Diff line number Diff line change
@@ -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/<exerciseName>` directory.

For example, if you're submitting `bob.py` for the Bob exercise, the submit command would be something like `exercism submit <path_to_exercism_dir>/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.
45 changes: 45 additions & 0 deletions exercises/food-chain/example.py
Original file line number Diff line number Diff line change
@@ -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 <animal> 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>", animal)
verse += chain
chain = swallowed+animal+" that"+phrases[0]+"\n"+chain
else:
verse += phrases[number-1] + "\n"
chain = chain.replace("<animal>", animal)
verse += chain
chain = swallowed + animal + ".\n" + chain

verse += die + "\n"

verse += "\n"
song += verse

return song
2 changes: 2 additions & 0 deletions exercises/food-chain/food_chain.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def chain():
pass
109 changes: 109 additions & 0 deletions exercises/food-chain/food_chain_test.py
Original file line number Diff line number Diff line change
@@ -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()