Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.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
Python 我如何找到在保持每个字符串顺序的同时洗牌两个字符串的所有结果?_Python - Fatal编程技术网

Python 我如何找到在保持每个字符串顺序的同时洗牌两个字符串的所有结果?

Python 我如何找到在保持每个字符串顺序的同时洗牌两个字符串的所有结果?,python,Python,我如何找到洗牌两个字符串的所有迭代x和y: x = "ab" y = "cd" shuffle(x, y) 将返回: ['abcd', 'acbd', 'acdb', 'cabd', 'cadb', 'cdab'] 它保持每个字符串的顺序,即ab和cd,但在任何时候都与另一个字符串混洗。下面是一个解决方案模板: def shuffle(x,y): if x=='': #nothing left in x to shuffle return [y] if y==

我如何找到洗牌两个字符串的所有迭代
x
y

x = "ab"
y = "cd"
shuffle(x, y)
将返回:

['abcd', 'acbd', 'acdb', 'cabd', 'cadb', 'cdab']

它保持每个字符串的顺序,即
ab
cd
,但在任何时候都与另一个字符串混洗。

下面是一个解决方案模板:

def shuffle(x,y):
    if x=='': #nothing left in x to shuffle
        return [y]
    if y=='': #nothing left in y to shuffle
        return [x]
    else:
        # Everything in the result begins either with x[0] or y[0]
        # All results that start with x[0] come from shuffling the strings that remain after removing x[0]
        # Similar idea for y[0]

这就像将两个排序数组合并成一个排序数组。

@TemporalWolf:shuffle(x,y)没有什么问题。这个问题确实有输入和输出的例子。@ScottHunter你说得对,我把它理解为附加的第三个字符串中的洗牌。欢迎来到。除了误解,我们不是一个代码编写服务。请回顾并向我们展示您的尝试。@Mayneman您想要两组结果吗?1.它将返回:['abcd','acbd','acdb','cabd','cadb','cdab']2。它保持每个字符串的顺序,即ab和cd,但在任何时候都会与另一个字符串混洗。看起来您希望我们为您编写一些代码。虽然许多用户愿意为陷入困境的程序员编写代码,但他们通常只在海报已经试图自己解决问题时才提供帮助。演示这项工作的一个好方法是包括您迄今为止编写的代码(形成一个示例)、示例输入(如果有)、预期输出和实际获得的输出(输出、回溯等)。你提供的细节越多,你可能得到的答案就越多。检查和。
def printshuffle(x, xi, y, yi, result):
    if xi == len(x) and yi == len(y):
        print(result)    
    elif xi == len(x):
        printshuffle(x, xi, y, yi+1, result+y[yi]) 
    elif yi == len(y):
        printshuffle(x, xi+1, y, yi, result+x[xi])
    else:
        printshuffle(x, xi+1, y, yi, result+x[xi])
        printshuffle(x, xi, y, yi+1, result+y[yi])

def shuffle(x, y):
    result=""
    printshuffle(x, 0, y, 0, result)

# main
x="ab"
y="cd"
shuffle(x, y)