Software Engineering

The right way to Test for Consider Python

The right way to Test for Consider Python
Written by admin


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 isn’t.

About components

Components are numbers you may 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 use the mod operator (%) in most languages to test for a the rest

Examples:

2 isn’t an element of seven as a result of: 7 % 2 = 1

Notice: base is a non-negative quantity, issue is a constructive quantity.

The answer in Python

Choice 1:

def check_for_factor(base, issue):
    return base/issue == int(base/issue)

Choice 2:

def check_for_factor(base, issue):
    return base % issue == 0

Choice 3:

check_for_factor=lambda a,b:not apercentb

Check instances to validate our resolution

import take a look at
from resolution import check_for_factor

@take a look at.describe("Fastened Assessments")
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)

About the author

admin

Leave a Comment