From 216bf321211edc6b29d5453a7ee8e4279f16a068 Mon Sep 17 00:00:00 2001 From: Zach Waltman Date: Thu, 3 Aug 2017 01:40:12 -0700 Subject: [PATCH 1/2] collatz-conjecture: add exercise --- config.json | 10 +++ exercises/collatz-conjecture/README.md | 61 +++++++++++++++++++ .../collatz-conjecture/collatz_conjecture.py | 2 + .../collatz_conjecture_test.py | 30 +++++++++ exercises/collatz-conjecture/example.py | 16 +++++ 5 files changed, 119 insertions(+) create mode 100644 exercises/collatz-conjecture/README.md create mode 100644 exercises/collatz-conjecture/collatz_conjecture.py create mode 100644 exercises/collatz-conjecture/collatz_conjecture_test.py create mode 100644 exercises/collatz-conjecture/example.py diff --git a/config.json b/config.json index 2122329c328..7737c8c8110 100644 --- a/config.json +++ b/config.json @@ -2,6 +2,16 @@ "language": "Python", "active": true, "exercises": [ + { + "uuid": "33f689ee-1d9c-4908-a71c-f84bff3510df", + "slug": "collatz-conjecture", + "core": false, + "unlocked_by": null, + "difficulty": 1, + "topics": [ + + ] + }, { "uuid": "f458c48a-4a05-4809-9168-8edd55179349", "slug": "hello-world", diff --git a/exercises/collatz-conjecture/README.md b/exercises/collatz-conjecture/README.md new file mode 100644 index 00000000000..6b2ede8b2b0 --- /dev/null +++ b/exercises/collatz-conjecture/README.md @@ -0,0 +1,61 @@ +# 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. + +(Keep in mind that the Conjecture is only concerned with strictly positive +integers, so your solution should return `None` if given 0 or a negative +integer.) + +## Examples + +Starting with n = 3, the steps would be as follows: + +0. 3 +1. 10 +2. 5 +3. 16 +4. 8 +5. 4 +6. 2 +7. 1 + +Resulting in 7 steps. So for input n = 3, the return value would be 7. + +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. + +### Submitting Exercises + +Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. + +For example, if you're submitting `bob.py` for the Bob exercise, the submit command would be something like `exercism submit /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 + +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/exercises/collatz-conjecture/collatz_conjecture.py b/exercises/collatz-conjecture/collatz_conjecture.py new file mode 100644 index 00000000000..5378d4d972c --- /dev/null +++ b/exercises/collatz-conjecture/collatz_conjecture.py @@ -0,0 +1,2 @@ +def collatz_steps(): + pass diff --git a/exercises/collatz-conjecture/collatz_conjecture_test.py b/exercises/collatz-conjecture/collatz_conjecture_test.py new file mode 100644 index 00000000000..fa82129efb4 --- /dev/null +++ b/exercises/collatz-conjecture/collatz_conjecture_test.py @@ -0,0 +1,30 @@ +import unittest + +from collatz_conjecture import collatz_steps + +# Test cases adapted from `x-common//canonical-data.json` @ version: 1.1.1 + +class CollatzConjectureTests(unittest.TestCase): + + def test_zero_steps_for_one(self): + self.assertEqual(collatz_steps(1), 0) + + def test_divide_if_even(self): + self.assertEqual(collatz_steps(16), 4) + + def test_even_and_odd_steps(self): + self.assertEqual(collatz_steps(12), 9) + + def test_large_number_of_even_and_odd_steps(self): + self.assertEqual(collatz_steps(1000000), 152) + + def test_zero_is_invalid_input(self): + self.assertEqual(collatz_steps(0), None) + + def test_negative_number_is_invalid_input(self): + self.assertEqual(collatz_steps(-1), None) + + self.assertEqual(collatz_steps(-15), None) + +if __name__ == '__main__': + unittest.main() diff --git a/exercises/collatz-conjecture/example.py b/exercises/collatz-conjecture/example.py new file mode 100644 index 00000000000..875a69c635c --- /dev/null +++ b/exercises/collatz-conjecture/example.py @@ -0,0 +1,16 @@ +def collatz_steps(n): + if n <= 0: + return + + step_count = 0 + while n > 1: + if is_odd(n): + n = n * 3 + 1 + else: + n = n / 2 + step_count += 1 + + return step_count + +def is_odd(n): + return n % 2 == 1 From 80d4a21a01b0b1f9b5e9e4f54a25c06f21c2d2d8 Mon Sep 17 00:00:00 2001 From: Zach Waltman Date: Thu, 3 Aug 2017 01:53:40 -0700 Subject: [PATCH 2/2] Move position of collatz-conjecture in config.json --- config.json | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/config.json b/config.json index 7737c8c8110..1a13dbdf2bf 100644 --- a/config.json +++ b/config.json @@ -2,16 +2,6 @@ "language": "Python", "active": true, "exercises": [ - { - "uuid": "33f689ee-1d9c-4908-a71c-f84bff3510df", - "slug": "collatz-conjecture", - "core": false, - "unlocked_by": null, - "difficulty": 1, - "topics": [ - - ] - }, { "uuid": "f458c48a-4a05-4809-9168-8edd55179349", "slug": "hello-world", @@ -811,6 +801,16 @@ "Algorithms" ] }, + { + "uuid": "33f689ee-1d9c-4908-a71c-f84bff3510df", + "slug": "collatz-conjecture", + "core": false, + "unlocked_by": null, + "difficulty": 1, + "topics": [ + + ] + }, { "uuid": "e7351e8e-d3ff-4621-b818-cd55cf05bffd", "slug": "accumulate",