diff --git a/python/grains/.exercism/config.json b/python/grains/.exercism/config.json new file mode 100644 index 0000000..55ec49f --- /dev/null +++ b/python/grains/.exercism/config.json @@ -0,0 +1,34 @@ +{ + "blurb": "Calculate the number of grains of wheat on a chessboard given that the number on each square doubles.", + "authors": [], + "contributors": [ + "behrtam", + "BethanyG", + "cmccandless", + "denislooby", + "Dog", + "kytrinyx", + "lowks", + "MojitoMonday", + "N-Parsons", + "pheanex", + "sdublish", + "sjakobi", + "smalley", + "tqa236", + "yawpitch" + ], + "files": { + "solution": [ + "grains.py" + ], + "test": [ + "grains_test.py" + ], + "example": [ + ".meta/example.py" + ] + }, + "source": "JavaRanch Cattle Drive, exercise 6", + "source_url": "http://www.javaranch.com/grains.jsp" +} diff --git a/python/grains/.exercism/metadata.json b/python/grains/.exercism/metadata.json new file mode 100644 index 0000000..e45d640 --- /dev/null +++ b/python/grains/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"python","exercise":"grains","id":"f9029e1bd44842f7b92e33ad000b6e46","url":"https://exercism.org/tracks/python/exercises/grains","handle":"Nebucatnetzer","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/python/grains/HELP.md b/python/grains/HELP.md new file mode 100644 index 0000000..fe2aa56 --- /dev/null +++ b/python/grains/HELP.md @@ -0,0 +1,55 @@ +# Help + +## Running the tests + +To run the included *tests*, run the test file using the `pytest` module, replacing `{exercise_name}`: + +```bash +$ python3 -m pytest {exercise_name}_test.py +``` + +Many IDE's and code editors have built-in support for using Pytest to run tests; check them out [here](https://github.com/exercism/python/blob/main/docs/TOOLS.md#editors-and-ides). + +For more information about running tests using `pytest`, checkout our [Python testing guide](https://github.com/exercism/python/blob/main/docs/TESTS.md#pytest). + +### 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 `python3 -m pytest -h`. + +## Submitting your solution + +You can submit your solution using the `exercism submit grains.py` command. +This command will upload your solution to the Exercism website and print the solution page's URL. + +It's possible to submit an incomplete solution which allows you to: + +- See how others have completed the exercise +- Request help from a mentor + +## Need to get help? + +If you'd like help solving the exercise, check the following pages: + +- The [Python track's documentation](https://exercism.org/docs/tracks/python) +- [Exercism's support channel on gitter](https://gitter.im/exercism/support) +- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs) + +Should those resources not suffice, you could submit your (incomplete) solution to request mentoring. + +Below are some resources for getting help if you run into trouble: + +- [The PSF](https://www.python.org) hosts Python downloads, documentation, and community resources. +- [Python Community on Discord](https://pythondiscord.com/) is a very helpful and active community. +- [#python on Libera.chat](https://www.python.org/community/irc/) this is where the cored developers for the language hang out and get work done. +- [Exercism on Gitter](https://gitter.im/exercism/home) join the Python room for Python-related questions or problems. +- [/r/learnpython/](https://www.reddit.com/r/learnpython/) is a subreddit designed for Python learners. +- [Python Community Forums](https://discuss.python.org/) +- [Pythontutor](http://pythontutor.com/) for stepping through small code snippets visually. + + +Additionally, [StackOverflow](http://stackoverflow.com/questions/tagged/python) is a good spot to search for your problem/question to see if it has been answered already. + If not - you can always [ask](https://stackoverflow.com/help/how-to-ask) or [answer](https://stackoverflow.com/help/how-to-answer) someone else's question. \ No newline at end of file diff --git a/python/grains/README.md b/python/grains/README.md new file mode 100644 index 0000000..bded1b4 --- /dev/null +++ b/python/grains/README.md @@ -0,0 +1,69 @@ +# Grains + +Welcome to Grains on Exercism's Python Track. +If you need help running the tests or submitting your code, check out `HELP.md`. + +## Instructions + +Calculate the number of grains of wheat on a chessboard given that the number +on each square doubles. + +There once was a wise servant who saved the life of a prince. The king +promised to pay whatever the servant could dream up. Knowing that the +king loved chess, the servant told the king he would like to have grains +of wheat. One grain on the first square of a chess board, with the number +of grains doubling on each successive square. + +There are 64 squares on a chessboard (where square 1 has one grain, square 2 has two grains, and so on). + +Write code that shows: +- how many grains were on a given square, and +- the total number of grains on the chessboard + +## For bonus points + +Did you get the tests passing and the code clean? If you want to, these +are some additional things you could try: + +- Optimize for speed. +- Optimize for readability. + +Then please share your thoughts in a comment on the submission. Did this +experiment make the code better? Worse? Did you learn anything from it? + +## Exception messages + +Sometimes it is necessary to [raise an exception](https://docs.python.org/3/tutorial/errors.html#raising-exceptions). When you do this, you should always 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. For situations where you know that the error source will be a certain type, you can choose to raise one of the [built in error types](https://docs.python.org/3/library/exceptions.html#base-classes), but should still include a meaningful message. + +This particular exercise requires that you use the [raise statement](https://docs.python.org/3/reference/simple_stmts.html#the-raise-statement) to "throw" a `ValueError` when the square input is out of range. The tests will only pass if you both `raise` the `exception` and include a message with it. + +To raise a `ValueError` with a message, write the message as an argument to the `exception` type: + +```python +# when the square value is not in the acceptable range +raise ValueError("square must be between 1 and 64") +``` + +## Source + +### Contributed to by + +- @behrtam +- @BethanyG +- @cmccandless +- @denislooby +- @Dog +- @kytrinyx +- @lowks +- @MojitoMonday +- @N-Parsons +- @pheanex +- @sdublish +- @sjakobi +- @smalley +- @tqa236 +- @yawpitch + +### Based on + +JavaRanch Cattle Drive, exercise 6 - http://www.javaranch.com/grains.jsp \ No newline at end of file diff --git a/python/grains/grains.py b/python/grains/grains.py new file mode 100644 index 0000000..83b1672 --- /dev/null +++ b/python/grains/grains.py @@ -0,0 +1,8 @@ +def square(number): + if number <= 0 or number > 64: + raise ValueError("square must be between 1 and 64") + return 2 ** (number - 1) + + +def total(): + return 2 ** 64 - 1 diff --git a/python/grains/grains_test.py b/python/grains/grains_test.py new file mode 100644 index 0000000..f7e277c --- /dev/null +++ b/python/grains/grains_test.py @@ -0,0 +1,52 @@ +import unittest + +from grains import ( + square, + total, +) + +# Tests adapted from `problem-specifications//canonical-data.json` + + +class GrainsTest(unittest.TestCase): + def test_grains_on_square_1(self): + self.assertEqual(square(1), 1) + + def test_grains_on_square_2(self): + self.assertEqual(square(2), 2) + + def test_grains_on_square_3(self): + self.assertEqual(square(3), 4) + + def test_grains_on_square_4(self): + self.assertEqual(square(4), 8) + + def test_grains_on_square_16(self): + self.assertEqual(square(16), 32768) + + def test_grains_on_square_32(self): + self.assertEqual(square(32), 2147483648) + + def test_grains_on_square_64(self): + self.assertEqual(square(64), 9223372036854775808) + + def test_square_0_raises_an_exception(self): + with self.assertRaises(ValueError) as err: + square(0) + self.assertEqual(type(err.exception), ValueError) + self.assertEqual(err.exception.args[0], "square must be between 1 and 64") + + def test_negative_square_raises_an_exception(self): + with self.assertRaises(ValueError) as err: + square(-1) + self.assertEqual(type(err.exception), ValueError) + self.assertEqual(err.exception.args[0], "square must be between 1 and 64") + + def test_square_greater_than_64_raises_an_exception(self): + with self.assertRaises(ValueError) as err: + square(65) + self.assertEqual(type(err.exception), ValueError) + self.assertEqual(err.exception.args[0], "square must be between 1 and 64") + + def test_returns_the_total_number_of_grains_on_the_board(self): + self.assertEqual(total(), 18446744073709551615)