我想写一个函数,它将一个字母数组作为参数,并选择一些字母.
假设您提供了8个字母的数组,并希望从中选择3个字母.然后你应该得到:
8! / ((8 - 3)! * 3!) = 56
Run Code Online (Sandbox Code Playgroud)
数组(或单词)返回,每个包含3个字母.
在如何使用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)