By nine
This commit is contained in:
parent
aeb5ce3afd
commit
3873020e15
2 changed files with 27 additions and 5 deletions
16
main.py
16
main.py
|
@ -5,16 +5,22 @@ 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
|
||||
return _sum_digits(num) % 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 divide_by_nine(num): # Sum each digit in number, then check divisibility of that number
|
||||
return _sum_digits(num) % 9 == 0
|
||||
|
||||
def _get_last_digit(num):
|
||||
return int(f"{num}"[-1])
|
||||
|
||||
def _sum_digits(num):
|
||||
num_string = f"{num}"
|
||||
num_sum = 0
|
||||
for digit_char in num_string:
|
||||
num_sum += int(digit_char)
|
||||
return num_sum
|
||||
|
||||
|
|
16
tests/nine_test.py
Normal file
16
tests/nine_test.py
Normal file
|
@ -0,0 +1,16 @@
|
|||
from main import divide_by_nine
|
||||
import unittest
|
||||
|
||||
class TestDivideByNine(unittest.TestCase):
|
||||
def test_by_1(self):
|
||||
self.assertFalse(divide_by_nine(1))
|
||||
def test_by_3(self):
|
||||
self.assertFalse(divide_by_nine(3))
|
||||
def test_by_9(self):
|
||||
self.assertTrue(divide_by_nine(9))
|
||||
def test_by_10(self):
|
||||
self.assertFalse(divide_by_nine(10))
|
||||
def test_by_18(self):
|
||||
self.assertTrue(divide_by_nine(18))
|
||||
def test_by_362880(self):
|
||||
self.assertTrue(divide_by_nine(362880))
|
Loading…
Add table
Reference in a new issue