从python中的给定模式(通配符)生成所有二进制字符串

Sam*_*san 3 python

给定一个模式,我们需要通过填充模式中缺少的位置来生成所有可能的二进制数0 and 1.

例如

Pattern = "x1x";
Run Code Online (Sandbox Code Playgroud)

输出:

010 
110 
011 
111
Run Code Online (Sandbox Code Playgroud)

alf*_*sin 6

这是相对简单的递归,不需要使用库:

def process(patt):
    if "x" not in patt:
        print(patt)
    else:
        process(patt.replace("x", "0", 1))
        process(patt.replace("x", "1", 1))


process("x1x")
Run Code Online (Sandbox Code Playgroud)

OUTPUT

010
011
110
111
Run Code Online (Sandbox Code Playgroud)