相关疑难解决方法(0)

从n返回k个元素的所有组合的算法

我想写一个函数,它将一个字母数组作为参数,并选择一些字母.

假设您提供了8个字母的数组,并希望从中选择3个字母.然后你应该得到:

8! / ((8 - 3)! * 3!) = 56
Run Code Online (Sandbox Code Playgroud)

数组(或单词)返回,每个包含3个字母.

algorithm combinations

551
推荐指数
23
解决办法
43万
查看次数

使用Python的Base-2(二进制)表示

如何使用Python表达二进制文字的基础上,我正在考虑以明智,直观的方式来实现以基础2形式显示整数的编程101板栗.这是我提出的最好的,但我想用更好的算法替换它,或者至少一个应该具有尖叫 - 快速性能的算法.

def num_bin(N, places=8):
    def bit_at_p(N, p):
        ''' find the bit at place p for number n '''
        two_p = 1 << p   # 2 ^ p, using bitshift, will have exactly one
                         # bit set, at place p
        x = N & two_p    # binary composition, will be one where *both* numbers
                         # have a 1 at that bit.  this can only happen 
                         # at position p.  will yield  two_p if  N has a 1 …
Run Code Online (Sandbox Code Playgroud)

python

6
推荐指数
1
解决办法
2万
查看次数

标签 统计

algorithm ×1

combinations ×1

python ×1