这是我接受采访时必须重新创建的Wallstreet拼字游戏。每个谜题均由七个不同的字母组成(第一个字母为关键字母),并遵循以下规则尽可能多地添加单词。
示例
输入:
说明
所以我有75分钟的时间来解决它,虽然我走得很远,但是却无法找出关键的一步。我无法正确显示分数的地方,只能手动对单词列表进行排序。我尝试添加一些计数器,但无法使它们工作。
return continueAction ? defaultGetStateForAction(action, state) : null;
我想将分数添加到字典或追加到列表,但是我没时间了。我主要只是想看看真正的解决方案是什么。
到目前为止,它只能打印
test_list = ["apple","pleas","please"]
puzzles = ["aelwxyz","aelpxyz","aelpsxy","saelpxy","xaelpsy"]
puzzles_list = ["a","e","l","p","s","x","y"]
def check_words(letters,words):
i = 1
score = 0
letters = list(letters)
for word in words:
if all(x in set(letters) for x in word) and letters[0] in word:
#this checks if the word contains any letter from the word and the first letter(aka key letter)
print("test passed")
score +=1
print(word,letters,i)
print(score)
return
#here we have to add a value to a counter to show for that set of letters how many words it can spell.
if all(x in set(word) for x in letters):
#only if the puzzle and the word match exactly aka apple would have to only have a,p,l,e in the test
print(word,letters)
else:
return
else:
print("no matching letters and or not matching key letter.")
return
def spelling_bee_solutions(wordlist,puzzles):
for puzzle in puzzles:
puzzle = list(puzzle)
check_words(puzzle,wordlist)
# check_words(puzzles_list,test_list)
spelling_bee_solutions(test_list,puzzles)
答案 0 :(得分:1)
您可以使用列表推导将每个拼图映射到生成器表达式的总和,该生成器表达式将迭代单词列表,如果拼图中的字符集是单词中字符集的超集,则输出1;并且拼图中的第一个字符是单词:
[sum(set(p) >= set(w) and p[0] in w for w in wordlist) for p in puzzles]
这将返回:
[0, 1, 3, 2, 0]
您可以通过将单词列表首先转换为字符集列表来进一步优化它,从而不必每次迭代都完成集转换:
wordsets = list(map(set, wordlist))
[sum(w.issubset(p) and p[0] in w for w in wordsets) for p in puzzles]