这不是家庭作业。
我看到这篇文章赞扬了 Linq 库以及它在做组合数学方面的出色表现,我心想:Python 可以以更易读的方式来做这件事。
在使用 Python 半小时后,我失败了。请完成我离开的地方。另外,请以最Pythonic和最有效的方式进行。
from itertools import permutations
from operator import mul
from functools import reduce
glob_lst = []
def divisible(n): return (sum(j*10^i for i,j in enumerate(reversed(glob_lst))) % n == 0)
oneToNine = list(range(1, 10))
twoToNine = oneToNine[1:]
for perm in permutations(oneToNine, 9):
for n in twoToNine:
glob_lst = perm[1:n]
#print(glob_lst)
if not divisible(n):
continue
else:
# Is invoked if the loop succeeds
# So, we found the number
print(perm)
谢谢!