英文:
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_letters
和new_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 = ['*', '*', 'd', '*', '*', 'm']
. 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 = ['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: #means the left part of the word is empty
for a in letters:
if a < word[i]:
new_left_letters.append(a)
elif j>i: #means the right part of the word is empty
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 #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('ABCD', 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 = ['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 # 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] != '*':
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 != '*':
# find the available letters for the * slots prior to this 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}")
# 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]))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论