First commit
This commit is contained in:
commit
4411d2b266
6 changed files with 79 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
__pycache__/
|
15
README.md
Normal file
15
README.md
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
# Divisibility Rules
|
||||||
|
|
||||||
|
Inspired by [VSauce's video on Divisibility Rules](https://www.youtube.com/watch?v=f6tHqOmIj1E)
|
||||||
|
|
||||||
|
[](https://www.youtube.com/watch?v=f6tHqOmIj1E)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
Requires Python 3 or later.
|
||||||
|
|
||||||
|
## Running Tests
|
||||||
|
|
||||||
|
```bash
|
||||||
|
python -m unittest tests/*_test.py
|
||||||
|
```
|
17
main.py
Normal file
17
main.py
Normal file
|
@ -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])
|
||||||
|
|
18
tests/five_test.py
Normal file
18
tests/five_test.py
Normal file
|
@ -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))
|
14
tests/three_test.py
Normal file
14
tests/three_test.py
Normal file
|
@ -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))
|
14
tests/two_test.py
Normal file
14
tests/two_test.py
Normal file
|
@ -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))
|
Loading…
Add table
Reference in a new issue