Thursday, September 21, 2023
HomeSoftware EngineeringThe way to Calculate the Least Frequent A number of in Python

The way to Calculate the Least Frequent A number of in Python

The problem

Write a operate that calculates the least widespread a number of of its arguments; every argument is assumed to be a non-negative integer. Within the case that there are not any arguments (or the supplied array in compiled languages is empty), return 1.

The answer in Python code

Choice 1:

from math import gcd
def lcm(*args):
    for x in args:
        if x!=0:
    return lcm

Choice 2:

def lcm(*args):
    gcd = lambda m,n: m if not n else gcd(n,mpercentn)
    return scale back( lambda x, y: x*y/gcd(x, y), args)

Choice 3:

def lcm(*args):
    args = set(args)  
    if 0 in args:
        return 0
    x = max(args)
    y = x
    args.take away(x)
    whereas any(x % z for z in args):
        x += y
    return x

Check circumstances to validate our answer

@take a look at.describe('Instance Checks')
def example_tests():
    take a look at.assert_equals(lcm(2,5),10)
    take a look at.assert_equals(lcm(2,3,4),12)
    take a look at.assert_equals(lcm(9),9)
    take a look at.assert_equals(lcm(0),0)
    take a look at.assert_equals(lcm(0,1),0)


Please enter your comment!
Please enter your name here

Most Popular

Recent Comments