【问题标题】:scrabble combination generator generating combinations that cannot be made拼字游戏组合生成器生成无法生成的组合
【发布时间】:2021-03-28 00:04:33
【问题描述】:

所以我制作了一个拼字游戏程序,在给定字符输入的情况下,它会打印所有可能的单词,但是它会打印无法生成的单词,例如运行输入时的 nun。有人可以帮我吗?我不知道如何解决这个问题。这是代码。

from itertools import product
from english_words import english_words_set

a = input()
x = True
li = list(a)
for comb in product(li, repeat=len(li)):
    b = ''.join(comb)
    if b in english_words_set:
        i = 0
        while i < len(li):
            if not li[i] in b:
                x = False
                i += 1
            if li[i] in b:
                x = True
                i += 1
        if x == True:
            print(b)```

【问题讨论】:

    标签: python string combinations


    【解决方案1】:

    您为什么不尝试使用 itertools 进行排列?

    from itertools import permutations
    
    a = input()
    
    for r in range(1, len(a)+1):
        for item in permutations(a,r):
            s = "".join(item)
            print(s)
    

    输入

    run
    

    输出

    r
    u
    n
    ru
    rn
    ur
    un
    nr
    nu
    run
    rnu
    urn
    unr
    nru
    nur
    

    【讨论】:

      猜你喜欢
      • 2012-05-02
      • 1970-01-01
      • 2010-12-23
      • 1970-01-01
      • 2021-01-02
      • 2018-07-07
      • 2012-04-26
      相关资源
      最近更新 更多