diff --git a/python/collatz-conjecture/.exercism/metadata.json b/python/collatz-conjecture/.exercism/metadata.json new file mode 100644 index 0000000..0b207a6 --- /dev/null +++ b/python/collatz-conjecture/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"python","exercise":"collatz-conjecture","id":"ce1c77cfccd84f15be130ca55382058e","url":"https://exercism.io/my/solutions/ce1c77cfccd84f15be130ca55382058e","handle":"Xevion","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/python/collatz-conjecture/README.md b/python/collatz-conjecture/README.md new file mode 100644 index 0000000..22d2562 --- /dev/null +++ b/python/collatz-conjecture/README.md @@ -0,0 +1,81 @@ +# Collatz Conjecture + +The Collatz Conjecture or 3x+1 problem can be summarized as follows: + +Take any positive integer n. If n is even, divide n by 2 to get n / 2. If n is +odd, multiply n by 3 and add 1 to get 3n + 1. Repeat the process indefinitely. +The conjecture states that no matter which number you start with, you will +always reach 1 eventually. + +Given a number n, return the number of steps required to reach 1. + +## Examples + +Starting with n = 12, the steps would be as follows: + +0. 12 +1. 6 +2. 3 +3. 10 +4. 5 +5. 16 +6. 8 +7. 4 +8. 2 +9. 1 + +Resulting in 9 steps. So for input n = 12, the return value would be 9. + +## Notes + +The Collatz Conjecture is only concerned with strictly positive integers, so your solution should raise a `ValueError` with a meaningful message if given 0 or a negative integer. + + +## 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 collatz_conjecture_test.py` +- Python 3.4+: `pytest collatz_conjecture_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 collatz_conjecture_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/collatz-conjecture` 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 + +An unsolved problem in mathematics named after mathematician Lothar Collatz [https://en.wikipedia.org/wiki/3x_%2B_1_problem](https://en.wikipedia.org/wiki/3x_%2B_1_problem) + +## 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/collatz-conjecture/collatz_conjecture.py b/python/collatz-conjecture/collatz_conjecture.py new file mode 100644 index 0000000..01da59b --- /dev/null +++ b/python/collatz-conjecture/collatz_conjecture.py @@ -0,0 +1 @@ +steps = lambda x, s=0 : exec(f'raise ValueError(\'Value \\\'{x}\\\' is not a valid Collatz Sequence starting point.\')') if x < 1 else s if x == 1 else steps(x // 2, s+1) if x % 2 == 0 else steps((3 * x) + 1, s+1) \ No newline at end of file diff --git a/python/collatz-conjecture/collatz_conjecture_test.py b/python/collatz-conjecture/collatz_conjecture_test.py new file mode 100644 index 0000000..690bf7e --- /dev/null +++ b/python/collatz-conjecture/collatz_conjecture_test.py @@ -0,0 +1,42 @@ +import unittest + +from collatz_conjecture import steps + + +# Tests adapted from `problem-specifications//canonical-data.json` @ v1.2.1 + + +class CollatzConjectureTest(unittest.TestCase): + def test_zero_steps_for_one(self): + self.assertEqual(steps(1), 0) + + def test_divide_if_even(self): + self.assertEqual(steps(16), 4) + + def test_even_and_odd_steps(self): + self.assertEqual(steps(12), 9) + + def test_large_number_of_even_and_odd_steps(self): + self.assertEqual(steps(1000000), 152) + + def test_zero_is_an_error(self): + with self.assertRaisesWithMessage(ValueError): + steps(0) + + def test_negative_value_is_an_error(self): + with self.assertRaisesWithMessage(ValueError): + steps(-15) + + # Utility functions + def setUp(self): + try: + self.assertRaisesRegex + except AttributeError: + self.assertRaisesRegex = self.assertRaisesRegexp + + def assertRaisesWithMessage(self, exception): + return self.assertRaisesRegex(exception, r".+") + + +if __name__ == "__main__": + unittest.main()