This commit is contained in:
Ava Gaiety Wroten 2020-09-15 21:51:13 -05:00
parent 4fb6a5fb8f
commit 715ae7bf4c
2 changed files with 27 additions and 2 deletions

11
main.py
View file

@ -7,6 +7,13 @@ def divide_by_two(num): # Take last digit, divide by two only on that
def divide_by_three(num): # Sum each digit in number, then checking that sum is divisible by three
return _sum_digits(num) % 3 == 0
def divide_by_four(num): # If last two digits are divisible by four
if (num < 10): return num % 4 == 0
tens_place = _get_last_digit(num, 2)
ones_place = _get_last_digit(num)
num_to_check = (tens_place * 10) + ones_place
return num_to_check % 4 == 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
@ -18,8 +25,8 @@ def divide_by_six(num): # Same rules as dividing by three, and ensure original n
def divide_by_nine(num): # Same rules as dividing by three, but checking that sum is divisible by nine
return _sum_digits(num) % 9 == 0
def _get_last_digit(num):
return int(f"{num}"[-1])
def _get_last_digit(num, indexFromEnd = 1):
return int(f"{num}"[-1 * indexFromEnd])
def _sum_digits(num):
num_string = f"{num}"

18
tests/four_test.py Normal file
View file

@ -0,0 +1,18 @@
from main import divide_by_four
import unittest
class TestDivideByTwo(unittest.TestCase):
def test_by_1(self):
self.assertFalse(divide_by_four(1))
def test_by_2(self):
self.assertFalse(divide_by_four(2))
def test_by_3(self):
self.assertFalse(divide_by_four(3))
def test_by_4(self):
self.assertTrue(divide_by_four(4))
def test_by_5(self):
self.assertFalse(divide_by_four(5))
def test_by_8(self):
self.assertTrue(divide_by_four(8))
def test_by_362880(self):
self.assertTrue(divide_by_four(362880))