class Solution():
def get_lcm(self, x):
#请在此添加代码,实现求出给定的所有正整数的最小公倍数,并将其返回
#********** Begin *********#
def gcd(x,y):
return x if y==0 else gcd(y,x%y)
def lcm(x,y):
return x // gcd(x,y)*y
ans = x[0]
for index in range(len(x)-1):
x[index+1]=lcm(x[index],x[index+1])
ans=max(ans,x[index+1])
return ans
#********** End **********#
pass
第2关:输出指定范围内的素数
class Solution():
def solve(self, l, r):
'''
:type l, r: int
:rtype : list
'''
#请在此添加代码,实现求得[l, r]范围内的所有素数,并将其返回
#********** Begin *********#
from math import sqrt
#通过素数表判断整数是不是素数
def is_prime(x):
if x == 1:
return False
for num in prime_table:
if num * num > x:
return True
if x % num == 0:
return False
#欧拉筛法
def ouler(x):
vis = [0 for i in range(x+1)]
prime_table = []
ln = 0
for num in range(2, x+1):
if vis[num] == 0:
prime_table.append(num)
ln += 1
for j in range(ln):
if num * prime_table[j] > x:
break
vis[num * prime_table[j]] = 1
if num % prime_table[j] == 0:
break
return prime_table
prime_table = ouler(10000)
ans = []
for num in range(l, r+1):
if is_prime(num):
ans.append(num)
return ans
#********** End *********#
第3关:Python对文件的操作
class Solution():
def solve(self, file_1, file_2, file_3):
'''
:type file_1, file_2, file_3: str
:rtype : None
'''
#请在此添加代码,实现将文件file_1和file_2中的数字按从小到大的顺序,写入文件file_3中
#********** Begin *********#
list1=[]
with open(file_1,'r') as f:
list1.extend(list(map(lambda x :int(x), f.readlines())))
with open(file_2,'r') as f:
list1.extend(list(map(lambda x :int(x), f.readlines())))
list1.sort()
with open(file_3,'w') as f:
for line in list1:
f.write(str(line)+'\n')
##********** End *********#