mirror of
https://github.com/Xevion/exercism.git
synced 2025-12-12 16:11:37 -06:00
food chain exercise
This commit is contained in:
113
python/food-chain/README.md
Normal file
113
python/food-chain/README.md
Normal file
@@ -0,0 +1,113 @@
|
||||
# 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!
|
||||
```
|
||||
|
||||
## Exception messages
|
||||
|
||||
Sometimes it is necessary to raise an exception. When you do this, you should include a meaningful error message to
|
||||
indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. Not
|
||||
every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include
|
||||
a message.
|
||||
|
||||
To raise a message with an exception, just write it as an argument to the exception type. For example, instead of
|
||||
`raise Exception`, you should write:
|
||||
|
||||
```python
|
||||
raise Exception("Meaningful message indicating the source of the error")
|
||||
```
|
||||
|
||||
## Running the tests
|
||||
|
||||
To run the tests, run the appropriate command below ([why they are different](https://github.com/pytest-dev/pytest/issues/1629#issue-161422224)):
|
||||
|
||||
- Python 2.7: `py.test food_chain_test.py`
|
||||
- Python 3.4+: `pytest food_chain_test.py`
|
||||
|
||||
Alternatively, you can tell Python to run the pytest module (allowing the same command to be used regardless of Python version):
|
||||
`python -m pytest food_chain_test.py`
|
||||
|
||||
### Common `pytest` options
|
||||
|
||||
- `-v` : enable verbose output
|
||||
- `-x` : stop running tests on first failure
|
||||
- `--ff` : run failures from previous test before running other test cases
|
||||
|
||||
For other options, see `python -m pytest -h`
|
||||
|
||||
## Submitting Exercises
|
||||
|
||||
Note that, when trying to submit an exercise, make sure the solution is in the `$EXERCISM_WORKSPACE/python/food-chain` directory.
|
||||
|
||||
You can find your Exercism workspace by running `exercism debug` and looking for the line that starts with `Workspace`.
|
||||
|
||||
For more detailed information about running tests, code style and linting,
|
||||
please see [Running the Tests](http://exercism.io/tracks/python/tests).
|
||||
|
||||
## Source
|
||||
|
||||
Wikipedia [http://en.wikipedia.org/wiki/There_Was_an_Old_Lady_Who_Swallowed_a_Fly](http://en.wikipedia.org/wiki/There_Was_an_Old_Lady_Who_Swallowed_a_Fly)
|
||||
|
||||
## Submitting Incomplete Solutions
|
||||
|
||||
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
|
||||
Reference in New Issue
Block a user