我一直在尝试使用Python解决以下问题,到目前为止没有成功:
假设你有一个字符串'0','1'和'?'.'?' 符号可以是'0'或'1'.您的目标是打印此类给定字符串的所有可能输出.例如,字符串'0?1?'的输出 应该是'0010','0011','0110'和'0111'
我尝试过以下方法:
def comb(S):
if not '?' in S:
yield S
else:
yield comb(S.replace('?','0',1))
yield comb(S.replace('?','1',1))
S = '0?1??011'
S_generator = comb(S)
for s in S_generator:
print s
Run Code Online (Sandbox Code Playgroud)
结果很奇怪,而不是我想要得到的:
<generator object comb at 0x106b2ceb0>
<generator object comb at 0x106b2cf00>
Run Code Online (Sandbox Code Playgroud)
知道为什么它不工作,以及我应该如何更改代码才能工作?