如何在Python中找到按升序排列的单词的缺失字母?

huangapple go评论109阅读模式
英文:

How to find the missing letters of an ascending order arranged word in Python?

问题

以下是翻译的代码部分:

letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
word = ['*', '*', 'd', '*', '*', 'm']

if '*' not in word:
    for i in range(len(word)):
        if word[i] != '*':
            for j in range(len(word)):
                if word[j] == '*':
                    if j < i:       # 表示单词的左侧为空
                        for a in letters:
                            if a < word[i]:
                                new_left_letters.append(a)
                    elif j > i:         # 表示单词的右侧为空
                        for a in letters:
                            if a > word[i]:
                                new_right_letters.append(a)
                        
    final_letters = new_left_letters + new_right_letters 

else:
    final_letters = letters     # 表示整个字母表都为空

print(final_letters)

请注意,你的代码中有一些未定义的变量(例如,new_left_lettersnew_right_letters),你可能需要在代码中初始化它们。

英文:

The problem statement is as follows:

A word (list) is arranged in ascending order. Now some of the letters of the word are masked while some letters are visible. My task is to make a list of all the letters which aren't present in the word such that the ascending order condition of the word is maintained.

To give an example: consider the word = [&#39;*&#39;, &#39;*&#39;, &#39;d&#39;, &#39;*&#39;, &#39;*&#39;, &#39;m&#39;]. Now here the first two blanks can only have letters - a, b or c while the two blanks towards the end can take letters - e, f, g, h, i, j, k, l

The existing letters in the word can't be used to fill the blanks. To solve this I have tried to write the following code, however there's some glitch. Some help would be great!

letters = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;, &#39;e&#39;, &#39;f&#39;, &#39;g&#39;, &#39;h&#39;, &#39;i&#39;, &#39;j&#39;, &#39;k&#39;, &#39;l&#39;, &#39;m&#39;, &#39;n&#39;, &#39;o&#39;, &#39;p&#39;, &#39;q&#39;, &#39;r&#39;, &#39;s&#39;, &#39;t&#39;, &#39;u&#39;, &#39;v&#39;, &#39;w&#39;, &#39;x&#39;, &#39;y&#39;, &#39;z&#39;]
word = [&#39;*&#39;, &#39;*&#39;, &#39;d&#39;, &#39;*&#39;, &#39;*&#39;, &#39;m&#39;]

if &#39;*&#39; not in word:
    for i in range(len(word)):
        if word[i] != &#39;*&#39;:
            for j in range(len(word)):
                if word[j] == &#39;*&#39; :
                    if j&lt; i:       #means the left part of the word is empty
                        for a in letters:
                            if a &lt; word[i]:
                                new_left_letters.append(a)
                    elif j&gt;i:         #means the right part of the word is empty
                        for a in letters:
                            if a &gt; word[i]:
                                new_right_letters.append(a)
                        
    final_letters = new_left_letters + new_right_letters 

else:
    final_letters = letters     #means the entire letter is empty

print(final_letters)

答案1

得分: 1

一个 "gap" 是一个或多个连续的被屏蔽字母,两端被未屏蔽字母或输入数组的一端所界定。

我们可以独立解决这些间隙,然后将它们相乘以获得最终结果。

假设我们正在解决长度为 r 的间隙,其中有 k >= r 个可供选择的字母(如果 k < r,则没有解决方案)。可能解决方案的数量是 choose(r, k)。

要枚举这些解决方案,请使用 itertools

combinations('valid_letters', r)

例如,

combinations('ABCD', 2) = AB AC AD BC BD CD

完成了每个间隙的这个操作后,您需要将间隙1的每个有效解与间隙2的每个有效解以此类推进行组合。

英文:

Let's say a 'gap' is one or more consecutive masked letters, bounded on both ends by either an unmasked letter or one of the ends of the input array.

We can solve our gaps independently, then multiply them to get the final results.

Say we're solving a gap of length r which has k >= r letters to choose from (if k < r then there's no solution). The count of possible solutions is choose(r, k).

To enumerate these, use itertools.

combinations('valid_letters', r)

E.g.,

combinations(&#39;ABCD&#39;, 2) = AB AC AD BC BD CD

Once you've done this for each gap, you need to combine every valid solution for gap 1 with every valid solution for gap 2 and so on.

答案2

得分: 0

以下是翻译好的代码部分:

master_letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
word_letters = ['*', '*', 'd', '*', '*', 'm']

alphabets = []

previous_letter_index = 0 # 将保存上一个解析的字母的索引

def find_next_letter(word_letter, word_letters):
    found_wl = False
    for i in range(len(word_letters)):
        if found_wl and word_letters[i] != '*':
            return word_letters[i]
        elif word_letters[i] == word_letter:
            found_wl = True
    return -1

for i, word_letter in enumerate(word_letters):
    if word_letter != '*':
        # 找到这个字母之前的*槽位可用的字母
        print(f"Found letter: {word_letter}")
        master_letter_index = master_letters.index(word_letter)
        print(f"Master index: {master_letter_index}")
        available_letters = master_letters[previous_letter_index:master_letter_index]
        print(f"Available letters: {available_letters}")

        # 如果单词中还有其他字母,限制可用字母
        next_letter = find_next_letter(word_letter, word_letters)
        if previous_letter_index != 0 and next_letter != -1:
            for _ in range(previous_letter_index, i):
                alphabets.append(available_letters[:available_letters.index(next_letter)])
        else:
            for _ in range(previous_letter_index, i):
                alphabets.append(tuple(available_letters))

        previous_letter_index = master_letter_index

print(sorted(list(set(alphabets)), key=lambda x: x[0]))

希望这有助于你理解代码!如果有其他问题,请告诉我。

英文:

Here is how I attacked this question!

master_letters = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;, &#39;e&#39;, &#39;f&#39;, &#39;g&#39;, &#39;h&#39;, &#39;i&#39;, &#39;j&#39;, &#39;k&#39;, &#39;l&#39;, &#39;m&#39;, &#39;n&#39;, &#39;o&#39;, &#39;p&#39;, &#39;q&#39;, &#39;r&#39;, &#39;s&#39;, &#39;t&#39;, &#39;u&#39;, &#39;v&#39;, &#39;w&#39;, &#39;x&#39;, &#39;y&#39;, &#39;z&#39;]
word_letters = [&#39;*&#39;, &#39;*&#39;, &#39;d&#39;, &#39;*&#39;, &#39;*&#39;, &#39;m&#39;]

alphabets = []

previous_letter_index = 0 # will hold the index of the last resolved letter

def find_next_letter(word_letter, word_letters):
    found_wl = False
    for i in range(len(word_letters)):
        if found_wl and word_letters[i] != &#39;*&#39;:
            return word_letters[i]
        elif word_letters[i] == word_letter:
            found_wl = True
    return -1

for i, word_letter in enumerate(word_letters):
    if word_letter != &#39;*&#39;:
        # find the available letters for the * slots prior to this letter
        print(f&quot;Found letter: {word_letter}&quot;)
        master_letter_index = master_letters.index(word_letter)
        print(f&quot;Master index: {master_letter_index}&quot;)
        available_letters = master_letters[previous_letter_index:master_letter_index]
        print(f&quot;Available letters: {available_letters}&quot;)

        # If there is another letter in the word, restrict the available letters.
        next_letter = find_next_letter(word_letter, word_letters)
        if previous_letter_index != 0 and next_letter != -1:
            for _ in range(previous_letter_index, i):
               alphabets.append(available_letters[:available_letters.index(next_letter)])
        else:
            for _ in range(previous_letter_index, i):
                alphabets.append(tuple(available_letters))

        previous_letter_index = master_letter_index

print(sorted(list(set(alphabets)), key=lambda x: x[0]))

huangapple
  • 本文由 发表于 2023年8月5日 04:40:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76838997.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定