首页 > 解决方案 > 与python的组合

问题描述

我正在尝试生成 ID 的组合

输入:cid = SPARK

输出:所有组合列表如下,每个元素的位置应该是恒定的。我是 python 的初学者,非常感谢这里的任何帮助。

'S****'
'S***K'
'S**R*'
'S**RK'
'S*A**'
'S*A*K'
'S*AR*'
'S*ARK'
'SP***'
'SP**K'
'SP*R*'
'SP*RK'
'SPA**'
'SPA*K'
'SPAR*'
'SPARK'

我在下面尝试过,我需要一个动态代码:

cid = 'SPARK'

# print(cid.replace(cid[1],'*'))
# cu_len = lenth of cid [SPARK] here which is 5
# com_stars = how many stars i.e '*' or '**'
def cubiod_combo_gen(cu_len, com_stars, j_ite, i_ite):
    cubiodList = []
    crange = cu_len
    i = i_ite #2 #3
    j = j_ite #1
    # com_stars = ['*','**','***','****']

    while( i <= crange):
    # print(j,i)
        if len(com_stars) == 1:
            x = len(com_stars)
            n_cid = cid.replace(cid[j:i],com_stars)
            i += x
            j += x
            cubiodList.append(n_cid)
        elif len(com_stars) == 2:
            x = len(com_stars)
            n_cid = cid.replace(cid[j:i],com_stars)
            i += x
            j += x
            cubiodList.append(n_cid)
        elif len(com_stars) == 3:
            x = len(com_stars)
            n_cid = cid.replace(cid[j:i],com_stars)
            i += x
            j += x
            cubiodList.append(n_cid)
    return cubiodList
    #print(i)
    #print(n_cid)
    # for item in cubiodList:
    #     print(item)

print(cubiod_combo_gen(5,'*',1,2))

print(cubiod_combo_gen(5,'**',1,3))

标签: pythonpython-3.x

解决方案


对于给定字符串中的每个字符,您可以将其表示为二进制字符串,使用 1 表示保持不变的字符,使用 0 表示用星号替换的字符。

def cubiod_combo_gen(string, count_star):
    str_list = [char0 for char0 in string] # a list with the characters of the string
    itercount = 2 ** (len(str_list)) # 2 to the power of the length of the input string
    results = []

    for config in range(itercount):

        # return a string of i in binary representation
        binary_repr = bin(config)[2:]
        while len(binary_repr) < len(str_list):
            binary_repr = '0' + binary_repr # add padding

        # construct a list with asterisks
        i = -1
        result_list = str_list.copy() # soft copy, this made me spend like 10 minutes debugging lol
        for char in binary_repr:
            i += 1
            if char == '0':
                result_list[i] = '*'
            if char == '1':
                result_list[i] = str_list[i]

        # now we have a possible string value
        if result_list.count('*') == count_star:
            # convert back to string and add to list of accepted strings
            result = ''
            for i in result_list:
                result = result + i
            results.append(result)

    return results


# this function returns the value, so you have to use `print(cubiod_combo_gen(args))`
# comment this stuff out if you don't want an interactive user prompt
string = input('Enter a string : ')
count_star = input('Enter number of stars : ')
print(cubiod_combo_gen(string, int(count_star)))

它在大约 4 秒内迭代 16 个字符,在大约 17 秒内迭代 18 个字符。你也打错了“长方体”,但我留下了原来的拼写

Enter a string : DPSCT
Enter number of stars : 2
['**SCT', '*P*CT', '*PS*T', '*PSC*', 'D**CT', 'D*S*T', 'D*SC*', 'DP**T', 'DP*C*', 'DPS**']

作为这种二进制计数的副作用,列表按星号排序,其中最早的星号优先,下一个最早的星号打破关系。

如果您想要累积计数,例如“ABCDEFG”中的 1、4、5 和 6 个星号,您可以使用类似

star_counts = (1, 4, 5, 6)
string = 'ABCDEFG'
for i in star_counts:
    print(cubiod_combo_gen(string, star_counts))

如果您想要答案中的漂亮格式,请尝试在代码末尾添加此块:

def formatted_cuboid(string, count_star):
    values = cubiod_combo_gen(string, count_star)
    for i in values:
        print(values[i])

老实说,我不知道你的j_itei_ite是什么,但似乎它们没有用,所以这应该有效。如果您仍想传递这些参数,请将第一行更改为def cubiod_combo_gen(string, count_star, *args, **kwargs):


推荐阅读