commit 4411d2b2666621b30950759621d63613500971c5 Author: Ava Gaiety Wroten Date: Tue Sep 15 21:09:44 2020 -0500 First commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c18dd8d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__pycache__/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..33b91a4 --- /dev/null +++ b/README.md @@ -0,0 +1,15 @@ +# Divisibility Rules + +Inspired by [VSauce's video on Divisibility Rules](https://www.youtube.com/watch?v=f6tHqOmIj1E) + +[![Preview](http://i3.ytimg.com/vi/f6tHqOmIj1E/maxresdefault.jpg)](https://www.youtube.com/watch?v=f6tHqOmIj1E) + +--- + +Requires Python 3 or later. + +## Running Tests + +```bash +python -m unittest tests/*_test.py +``` diff --git a/main.py b/main.py new file mode 100644 index 0000000..aecd51e --- /dev/null +++ b/main.py @@ -0,0 +1,17 @@ +def divide_by_two(num): # Take last digit, divide by two only on that + return _get_last_digit(num) % 2 == 0 + +def divide_by_three(num): # Sum each digit in number, then check divisibility of that number + num_string = f"{num}" + num_sum = 0 + for digit_char in num_string: + num_sum += int(digit_char) + return num_sum % 3 == 0 + +def divide_by_five(num): # If last digit is a zero or a five return true + last_digit = _get_last_digit(num) + return last_digit == 0 or last_digit == 5 + +def _get_last_digit(num): + return int(f"{num}"[-1]) + diff --git a/tests/five_test.py b/tests/five_test.py new file mode 100644 index 0000000..5430279 --- /dev/null +++ b/tests/five_test.py @@ -0,0 +1,18 @@ +from main import divide_by_five +import unittest + +class TestDivideByFive(unittest.TestCase): + def test_by_1(self): + self.assertFalse(divide_by_five(1)) + def test_by_2(self): + self.assertFalse(divide_by_five(2)) + def test_by_3(self): + self.assertFalse(divide_by_five(3)) + def test_by_4(self): + self.assertFalse(divide_by_five(4)) + def test_by_5(self): + self.assertTrue(divide_by_five(5)) + def test_by_10(self): + self.assertTrue(divide_by_five(10)) + def test_by_362880(self): + self.assertTrue(divide_by_five(362880)) diff --git a/tests/three_test.py b/tests/three_test.py new file mode 100644 index 0000000..3ca43ec --- /dev/null +++ b/tests/three_test.py @@ -0,0 +1,14 @@ +from main import divide_by_three +import unittest + +class TestDivideByThree(unittest.TestCase): + def test_by_1(self): + self.assertFalse(divide_by_three(1)) + def test_by_2(self): + self.assertFalse(divide_by_three(2)) + def test_by_3(self): + self.assertTrue(divide_by_three(3)) + def test_by_4(self): + self.assertFalse(divide_by_three(4)) + def test_by_362880(self): + self.assertTrue(divide_by_three(362880)) diff --git a/tests/two_test.py b/tests/two_test.py new file mode 100644 index 0000000..caf669b --- /dev/null +++ b/tests/two_test.py @@ -0,0 +1,14 @@ +from main import divide_by_two +import unittest + +class TestDivideByTwo(unittest.TestCase): + def test_by_1(self): + self.assertFalse(divide_by_two(1)) + def test_by_2(self): + self.assertTrue(divide_by_two(2)) + def test_by_3(self): + self.assertFalse(divide_by_two(3)) + def test_by_4(self): + self.assertTrue(divide_by_two(4)) + def test_by_362880(self): + self.assertTrue(divide_by_two(362880))