diff options
author | Andinus <andinus@nand.sh> | 2021-08-11 15:26:15 +0530 |
---|---|---|
committer | Andinus <andinus@nand.sh> | 2021-08-11 15:26:15 +0530 |
commit | 321825828ac918bad28d0597a8616c6dc9802c3c (patch) | |
tree | 0b8e9cb1012197750eb58e972736319b2a6abac2 /python/two-fer | |
parent | 2979ef790ac5b8f58495e0dd08cafd6a3a2e30a5 (diff) | |
download | exercism-321825828ac918bad28d0597a8616c6dc9802c3c.tar.gz |
Add solved exercises
Diffstat (limited to 'python/two-fer')
-rw-r--r-- | python/two-fer/README.md | 72 | ||||
-rw-r--r-- | python/two-fer/two_fer.py | 2 | ||||
-rw-r--r-- | python/two-fer/two_fer_test.py | 20 |
3 files changed, 94 insertions, 0 deletions
diff --git a/python/two-fer/README.md b/python/two-fer/README.md new file mode 100644 index 0000000..5528964 --- /dev/null +++ b/python/two-fer/README.md @@ -0,0 +1,72 @@ +# Two Fer + +`Two-fer` or `2-fer` is short for two for one. One for you and one for me. + +Given a name, return a string with the message: + +```text +One for X, one for me. +``` + +Where X is the given name. + +However, if the name is missing, return the string: + +```text +One for you, one for me. +``` + +Here are some examples: + +|Name |String to return +|:-------|:------------------ +|Alice |One for Alice, one for me. +|Bob |One for Bob, one for me. +| |One for you, one for me. +|Zaphod |One for Zaphod, one for me. + +## 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 `pytest two_fer_test.py` + +Alternatively, you can tell Python to run the pytest module: +`python -m pytest two_fer_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/two-fer` 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 + +[https://github.com/exercism/problem-specifications/issues/757](https://github.com/exercism/problem-specifications/issues/757) + +## Submitting Incomplete Solutions + +It's possible to submit an incomplete solution so you can see how others have completed the exercise. diff --git a/python/two-fer/two_fer.py b/python/two-fer/two_fer.py new file mode 100644 index 0000000..c49fbce --- /dev/null +++ b/python/two-fer/two_fer.py @@ -0,0 +1,2 @@ +def two_fer(name = "you"): + return "One for {0}, one for me.".format(name) diff --git a/python/two-fer/two_fer_test.py b/python/two-fer/two_fer_test.py new file mode 100644 index 0000000..90a2f93 --- /dev/null +++ b/python/two-fer/two_fer_test.py @@ -0,0 +1,20 @@ +import unittest + +from two_fer import two_fer + +# Tests adapted from `problem-specifications//canonical-data.json` + + +class TwoFerTest(unittest.TestCase): + def test_no_name_given(self): + self.assertEqual(two_fer(), "One for you, one for me.") + + def test_a_name_given(self): + self.assertEqual(two_fer("Alice"), "One for Alice, one for me.") + + def test_another_name_given(self): + self.assertEqual(two_fer("Bob"), "One for Bob, one for me.") + + +if __name__ == "__main__": + unittest.main() |