Python 3.x 如何有效地从线索生成所有可能的二进制字符串?
我可以得到一个列表形式的线索(例如[1,3,1])和字符串的长度(例如8),并根据线索生成所有可能的字符串。即:Python 3.x 如何有效地从线索生成所有可能的二进制字符串?,python-3.x,Python 3.x,我可以得到一个列表形式的线索(例如[1,3,1])和字符串的长度(例如8),并根据线索生成所有可能的字符串。即: 01011101 10111010 10111001 10011101 有三组1,由线索给出的长度的一个或多个0隔开(按顺序) 线索指定由至少一个0分隔的1组的长度。这些组的顺序必须遵循线索列表中的顺序 我的方法是使用递归,每次调用都试图在字符串中插入一组特定的1(按照线索列表的顺序)。它使用for循环将其放置在字符串的所有可能索引中,并使用clue=clue[1://code>
- 01011101
- 10111010
- 10111001
- 10011101
clue=clue[1://code>和size=size-clue[0]
递归地为每个位置调用自己
如何在Python中有效地实现这一点?我只需要使用带有替换的组合来生成所有可能的组合,并以这种方式构建您的答案
from itertools import combinations_with_replacement
from collections import Counter
def generate_answers(clue, length):
segs = len(clue) + 1 # segment indices that a zero can be placed
excess_zeros = length - sum(clue) - (segs - 2) # number of zeros that can be moved around
for comb in combinations_with_replacement(range(segs), excess_zeros):
count = Counter(comb) # number of zeros to be added to each segment
for i in range(1, len(clue)):
count[i] += 1 # add the zeros required to separate the ones
output = ''
for i in range(segs): # build string
output += '0' * count[i]
if i < len(clue):
output += '1' * clue[i]
print(output)
clue = [1, 3, 1]
length = 8
generate_answers(clue, length)
这是在没有外部库的情况下(递归)执行此操作的另一种方法:
def generate_answers(clue, size):
if len(clue) == 0:
return [[0 for _ in range(size)]]
groups = gen_groups(clue)
min_len = sum(clue) + len(clue) - 1
free_spaces = size - min_len
result = recursive_combinations(len(groups) + 1, free_spaces)
solution = []
for res in result:
in_progress = []
for i in range(len(groups)):
in_progress += [0 for _ in range(res[i])]
in_progress += groups[i]
in_progress += [0 for _ in range(res[-1])]
solution.append(in_progress)
return solution
def gen_groups(clue):
result = []
for elem in clue:
in_progress = []
for i in range(elem):
in_progress.append(1)
in_progress.append(0)
result.append(in_progress)
if len(result) > 0:
result[-1].pop()
return result
def recursive_combinations(fields, zeroes):
if fields <= 0 or zeroes< 0:
return []
if fields == 1:
return [[zeroes]]
solution = []
for i in range(zeroes+ 1):
result = recursive_combinations(fields - 1, zeroes- i)
solution += [[i] + res for res in result]
return solution
def生成_答案(线索、大小):
如果len(线索)==0:
返回[[0表示范围内(大小)]]
组=gen_组(线索)
最小长度=总和(线索)+长度(线索)-1
自由空间=大小-最小长度
结果=递归组合(len(组)+1,自由空间)
解决方案=[]
对于结果中的res:
进行中=[]
对于范围内的i(len(组)):
进行中+=[0表示范围内(res[i])]
进行中+=组[i]
正在进行中+=[0表示范围内(res[-1])]
解决方案。追加(正在进行)
返回溶液
def gen_组(线索):
结果=[]
对于线索中的元素:
进行中=[]
对于范围内的i(元素):
正在进行。追加(1)
正在进行。追加(0)
结果。追加(正在进行中)
如果len(结果)>0:
结果[-1].pop()
返回结果
def递归_组合(字段、零):
如果是字段,请澄清“列表形式的线索”是什么意思?[1,3,1]
是指一组1、一组3、一组1吗?这些整齐吗?你有什么方法来解决这个问题吗?是的,所有这些组按特定顺序至少用一个0隔开。我将把我的方法包括在问题中。
def generate_answers(clue, size):
if len(clue) == 0:
return [[0 for _ in range(size)]]
groups = gen_groups(clue)
min_len = sum(clue) + len(clue) - 1
free_spaces = size - min_len
result = recursive_combinations(len(groups) + 1, free_spaces)
solution = []
for res in result:
in_progress = []
for i in range(len(groups)):
in_progress += [0 for _ in range(res[i])]
in_progress += groups[i]
in_progress += [0 for _ in range(res[-1])]
solution.append(in_progress)
return solution
def gen_groups(clue):
result = []
for elem in clue:
in_progress = []
for i in range(elem):
in_progress.append(1)
in_progress.append(0)
result.append(in_progress)
if len(result) > 0:
result[-1].pop()
return result
def recursive_combinations(fields, zeroes):
if fields <= 0 or zeroes< 0:
return []
if fields == 1:
return [[zeroes]]
solution = []
for i in range(zeroes+ 1):
result = recursive_combinations(fields - 1, zeroes- i)
solution += [[i] + res for res in result]
return solution