Wednesday, November 29, 2023
HomeSoftware EngineeringThe way to Test for Consider Python

The way to Test for Consider Python


The problem

This perform ought to check if the issue is an element of base.

Return true if it’s a issue or false if it’s not.

About elements

Components are numbers you’ll be able to multiply collectively to get one other quantity.

2 and three are elements of 6 as a result of: 2 * 3 = 6

  • Yow will discover an element by dividing numbers. If the rest is 0 then the quantity is an element.
  • You need to use the mod operator (%) in most languages to test 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 optimistic 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

Check circumstances to validate our answer

import check
from answer import check_for_factor

@check.describe("Mounted Checks")
def fixed_tests():    
    @check.it("Ought to return True")
    def should_return_true():
        check.assert_equals(check_for_factor(10, 2), True)
        check.assert_equals(check_for_factor(63, 7), True)
        check.assert_equals(check_for_factor(2450, 5), True)
        check.assert_equals(check_for_factor(24612, 3), True)
        
    @check.it("Ought to return False")
    def should_return_false():
        check.assert_equals(check_for_factor(9, 2), False)
        check.assert_equals(check_for_factor(653, 7), False)
        check.assert_equals(check_for_factor(2453, 5), False)
        check.assert_equals(check_for_factor(24617, 3), False)
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments