The problem
This operate ought to take a look at if the issue
is an element of base
.
Return true
if it’s a issue or false
if it’s not.
About components
Elements are numbers you possibly can multiply collectively to get one other quantity.
2 and three are components of 6 as a result of: 2 * 3 = 6
- You will discover an element by dividing numbers. If the rest is 0 then the quantity is an element.
- You should utilize the mod operator (
%
) in most languages to examine for a the rest
Examples:
2 shouldn’t be an element of seven as a result of: 7 % 2 = 1
Word: base
is a non-negative quantity, issue
is a constructive quantity.
The answer in Python
Possibility 1:
def check_for_factor(base, issue):
return base/issue == int(base/issue)
Possibility 2:
def check_for_factor(base, issue):
return base % issue == 0
Possibility 3:
check_for_factor=lambda a,b:not apercentb
Take a look at circumstances to validate our answer
import take a look at
from answer import check_for_factor
@take a look at.describe("Fastened Checks")
def fixed_tests():
@take a look at.it("Ought to return True")
def should_return_true():
take a look at.assert_equals(check_for_factor(10, 2), True)
take a look at.assert_equals(check_for_factor(63, 7), True)
take a look at.assert_equals(check_for_factor(2450, 5), True)
take a look at.assert_equals(check_for_factor(24612, 3), True)
@take a look at.it("Ought to return False")
def should_return_false():
take a look at.assert_equals(check_for_factor(9, 2), False)
take a look at.assert_equals(check_for_factor(653, 7), False)
take a look at.assert_equals(check_for_factor(2453, 5), False)
take a look at.assert_equals(check_for_factor(24617, 3), False)