我需要计算在Python combinatorials(NCR),但无法找到的功能做在math,numpy或stat 图书馆.类似于类型函数的东西:
comb = calculate_combinations(n, r)
Run Code Online (Sandbox Code Playgroud)
我需要可能的组合数量,而不是实际的组合,所以itertools.combinations我不感兴趣.
最后,我想避免使用阶乘,因为我将计算组合的数字可能变得太大而且阶乘将变得非常可怕.
这似乎是一个非常容易回答的问题,但是我被淹没在关于生成所有实际组合的问题中,这不是我想要的.:)
非常感谢
我有一些代码可以计算排列和组合,我正在努力让它更适合大数字.
我已经找到了一个更好的排列算法,避免了大的中间结果,但我仍然认为我可以做更好的组合.
到目前为止,我已经提出了一个特殊情况来反映nCr的对称性,但我仍然希望找到一种更好的算法来避免调用阶乘(r),这是一个不必要的大中间结果.如果没有这个优化,最后一次doctest尝试计算阶乘(99000)需要太长时间.
任何人都可以建议一种更有效的方法来计算组合?
from math import factorial
def product(iterable):
prod = 1
for n in iterable:
prod *= n
return prod
def npr(n, r):
"""
Calculate the number of ordered permutations of r items taken from a
population of size n.
>>> npr(3, 2)
6
>>> npr(100, 20)
1303995018204712451095685346159820800000
"""
assert 0 <= r <= n
return product(range(n - r + 1, n + 1))
def ncr(n, r):
"""
Calculate the number of unordered combinations of r items taken …Run Code Online (Sandbox Code Playgroud) 所以我正在学习python所以我正在经历一些项目的euler问题.而且我不确定这是否是我遇到的python问题,或者只是我被阻止了,但我似乎得到了问题53的错误答案.这里是问题的链接http://projecteuler.net/的index.php?节=问题&ID = 53
这是我的代码:
from math import factorial
def ncr(n,r):
return (factorial(n)/(factorial(r)*factorial(n-r)))
i = 0
for x in range(1,100):
for y in range(0,x):
if(ncr(x,y) > 1000000):
i=i+1
print i
我得到3982这显然是错误的答案.我正在做的那些特定于python的错误吗?
我想用10k+ 数字处理大数字,所以我使用元组,因为不能使用整数类型(数字的精度很重要,因为数字的总和)。只有 2 个唯一数字,9并且0.
第一个条件:与 999 的数字和的模数为 0。
第二个条件:它是大整数,所以第一位数字不能是0。
最后一个条件:最后一位数字是0。
我的解决办法是:
from itertools import product
L = [10000,11000,12000]
for M in L:
for i in product([0, 9], repeat=M):
if i[0] != 0 and i[-1] == 0:
s = sum(i)
if s % 999 == 0:
print (s)
Run Code Online (Sandbox Code Playgroud)
在这些条件下是否可以计算生成值的数量?是否可能更改解决方案/其他解决方案以减少生成预期输出的时间,例如仅生成数字sum of digits更像999?我在考虑Decimal,但这是sum数字的问题。生成的numbers(元组、数组)的顺序并不重要。
它是如此巨大的元组,所以容易测试有问题。我尝试创建一些示例(不知道是否有用):
for i in product([9, 0], repeat=20):
if i[0] != …Run Code Online (Sandbox Code Playgroud) 给出两个数字,X并且Y它们之间存在多少个数字,其中至少有一半的数字相同?例如,1122并且4444将工作,而11234并112233不会工作.
显然,最直接的方法是从X1 开始并逐渐递增Y,然后检查每个数字,但这太慢了,因为X和Y之间的边界100和10^18.我知道它是某种形式的动态编程,我应该使用字符串来表示数字,但我无法进一步了解.
任何帮助都会被接受.谢谢!
我尝试生成给定数组的所有可能的 2 元素交换。
例如:
candidate = [ 5, 9, 1, 8, 3, 7, 10, 6, 4, 2]
result = [[ 9, 5, 1, 8, 3, 7, 10, 6, 4, 2]
[ 1, 9, 5, 8, 3, 7, 10, 6, 4, 2]
[ 8, 9, 1, 5, 3, 7, 10, 6, 4, 2]
[ 3, 9, 1, 8, 5, 7, 10, 6, 4, 2]
[ 7, 9, 1, 8, 3, 5, 10, 6, 4, 2]
[10, 9, 1, 8, 3, 7, …Run Code Online (Sandbox Code Playgroud) 我有两个元组:
t1 = ('A', 'B')
t2 = ('C', 'D', 'E')
Run Code Online (Sandbox Code Playgroud)
我想知道如何在元组之间创建组合,因此结果应该是:
AC, AD, AE, BC, BD, BE
Run Code Online (Sandbox Code Playgroud)
编辑
运用
list(itertools.combinations('abcd',2))
Run Code Online (Sandbox Code Playgroud)
我可以为给定的字符串生成组合列表:
[('a', 'b'), ('a', 'c'), ('a', 'd'), ('b', 'c'), ('b', 'd'), ('c', 'd')]
Run Code Online (Sandbox Code Playgroud)
如果我插入元组而不是字符串,则会发生以下错误:
TypeError: sequence item 0: expected string, tuple found
Run Code Online (Sandbox Code Playgroud)
有什么建议怎么办?
python ×6
algorithm ×2
combinations ×2
tuples ×2
arrays ×1
c++ ×1
filter ×1
math ×1
optimization ×1
performance ×1
permutation ×1
product ×1
python-3.x ×1
statistics ×1
string ×1
swap ×1