Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/450.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何获得给定数组的所有重复排列_Javascript_Python_Algorithm_Combinations_Permutation - Fatal编程技术网

Javascript 如何获得给定数组的所有重复排列

Javascript 如何获得给定数组的所有重复排列,javascript,python,algorithm,combinations,permutation,Javascript,Python,Algorithm,Combinations,Permutation,我试图实现的是,当我们组合数组的元素时,得到所有可能的数字 我无法为这个创建算法 我试图用0和1创建一个长度为5的数字,如果从末尾开始增加1,就会得到这个值 00000 00001 00010 00100 01000 10000 00011 00110 01100 11000 00111 01110 11100 01111 11110 11111 但我无法在这两种情况下看到01011 我想纠正这种方法来获得所有的数字,如果有人能提供python或javascript语言的代码,那就太

我试图实现的是,当我们组合数组的元素时,得到所有可能的数字

我无法为这个创建算法

我试图用0和1创建一个长度为5的数字,如果从末尾开始增加1,就会得到这个值

00000

00001
00010
00100
01000
10000

00011
00110
01100
11000

00111
01110
11100

01111
11110

11111
但我无法在这两种情况下看到01011

我想纠正这种方法来获得所有的数字,如果有人能提供python或javascript语言的代码,那就太好了

用代码编辑

我尝试了下面的代码,得到了元素0,1,0,1,1

z = list(itertools.product([0,1], repeat=5))
for a in z:
    print(a)
但测试下面的其他数组代码不打印0,2,2,3

z = list(itertools.product([0,1,2,3,4,5,6,7,8,9], repeat=5))
for a in z:
    if sum(a) == 12:
        print(a)

你所做的不是排列。你只是在写从0到31的二进制数,仅此而已。 你可以用这个

for i in range(32):
    print(bin(i))

您可以尝试以下方法:

from itertools import *
a = list(product([0,1],repeat=5)) #===the list with all the combinations
print(a)

请向我们展示您的方法嗨,如果您发布您的代码、输入和所需输出的示例,我们可以更好地帮助您。@georg编辑并添加了code@jabaa我想我对这里的逻辑很迷茫,但我尝试了python itertools、排列、产品、,多个for循环和递归函数,但每次都缺少一些组合。为什么要打印?您的条件表明,这些数字的总和应始终为6。但是0,2,2,3的和是9。我已经这样做了,但是我没有用不同的数组得到预期的结果。让我编辑我的问题。数字的长度应该是5。
# Python function to print permutations of a given list
def permutation(lst):

    # If lst is empty then there are no permutations
    if len(lst) == 0:
        return []

    # If there is only one element in lst then, only
    # one permuatation is possible
    if len(lst) == 1:
        return [lst]

    # Find the permutations for lst if there are
    # more than 1 characters

    l = [] # empty list that will store current permutation

    # Iterate the input(lst) and calculate the permutation
    for i in range(len(lst)):
    m = lst[i]

    # Extract lst[i] or m from the list. remLst is
    # remaining list
    remLst = lst[:i] + lst[i+1:]

    # Generating all permutations where m is first
    # element
    for p in permutation(remLst):
        l.append([m] + p)
    return l


# Driver program to test above function
data = list('**array goes here**')
for p in permutation(data):
    print p
from itertools import *
a = list(product([0,1],repeat=5)) #===the list with all the combinations
print(a)