divisibilityrules/main.py
Ava Gaiety Wroten 715ae7bf4c By four
2020-09-15 21:51:13 -05:00

37 lines
1.3 KiB
Python

def divide_by_one(num): # Is whole number
return float(num).is_integer()
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 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
def divide_by_six(num): # Same rules as dividing by three, and ensure original number is even
num_sum = _sum_digits(num)
return num_sum % 3 == 0 and divide_by_two(num)
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, indexFromEnd = 1):
return int(f"{num}"[-1 * indexFromEnd])
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