The problem
Write a operate that calculates the least frequent a number of of its arguments; every argument is assumed to be a non-negative integer. Within the case that there aren’t 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):
lcm=1
for x in args:
if x!=0:
lcm=lcm*x//gcd(lcm,x)
else:
lcm=0
return lcm
Choice 2:
def lcm(*args):
gcd = lambda m,n: m if not n else gcd(n,mpercentn)
return cut 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
Take a look at instances to validate our resolution
@take a look at.describe('Instance Exams')
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)