【问题标题】:How to generate all possible binary strings from a clue effectively?如何有效地从线索中生成所有可能的二进制字符串?
【发布时间】:2021-04-22 19:39:36
【问题描述】:

我可以得到一个列表形式的线索(例如 [1,3,1])和字符串的长度(例如 8),并根据线索生成所有可能的字符串。那就是:

  • 01011101
  • 10111010
  • 10111001
  • 10011101

三组 1 由线索给出的一个或多个 0 分隔(按此顺序)。

线索指定由至少一个 0 分隔的 1 组的长度。这些组的顺序必须遵循线索列表中的顺序。

我的方法是使用递归,每次调用都尝试在字符串中插入一组特定的 1(按照线索列表的顺序)。它使用 for 循环将其放置在字符串的所有可能索引中,并使用 clue = clue[1:]size = size - clue[0] 为每个位置递归调用自身。

如何在 Python 中有效地做到这一点?

【问题讨论】:

  • 您能否澄清一下“以列表的形式提供的线索”是什么意思? [1,3,1]是指一组1个1,一组3个1,一组1个1吗?这些有顺序吗?你有什么办法解决这个问题吗?
  • 是的,所有这些组都按特定顺序由至少一个 0 分隔。我将在问题中包含我的方法。

标签: python-3.x


【解决方案1】:

我只会使用combinations_with_replacement 来生成所有可能的组合并以此方式构建您的答案。

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)

输出:

'01011101'
'10011101'
'10111001'
'10111010'

【讨论】:

    【解决方案2】:

    这是另一种在没有外部库的情况下(递归)的方法:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-04
      • 2022-10-05
      相关资源
      最近更新 更多