我的信息查找程序返回' 0'
我正在尝试制作一个程序,该程序将找到最佳单词,以便在给出单词列表时一次消除键盘字母的最多字母。我的程序总是返回“ 0”。我希望该值约为5。我怀疑我的错误在“ get_best_word”函数中,但它也可能是“ get_possible_letters”功能。感谢任何帮助,我的代码如下。
import os
import random
wordlist = ['crane', 'soare', 'adieu', 'wario', 'crazy', 'aback', 'Abuse','Adult', 'Agent', 'Anger', 'Apple', 'Award', 'Basis', 'Beach', 'Birth', 'Block', 'Blood', 'Board', 'Brain', 'Bread', 'Break', 'Brown', 'Buyer', 'Cause', 'Chain', 'Chair', 'Chest', 'Chief', 'Child', 'China', 'Claim', 'Class', 'Clock', 'Coach', 'Coast', 'Court', 'Cover', 'Cream', 'Crime', 'Cross', 'Crowd', 'Crown', 'Cycle', 'Dance', 'Death', 'Depth', 'Doubt', 'Draft', 'Drama', 'Dream', 'Dress', 'Drink', 'Drive', 'Earth', 'Enemy', 'Entry', 'Error', 'Event', 'Faith', 'Fault', 'Field', 'Fight', 'Floor', 'Focus', 'Force', 'Frame', 'Frank', 'Front', 'Fruit', 'Glass', 'Grant', 'Grass', 'Green', 'Group', 'Guide', 'Heart', 'Henry', 'Horse', 'Hotel', 'House', 'Image', 'Index', 'Input', 'Issue', 'Japan', 'Jones', 'Judge', 'Knife', 'Laura', 'Layer', 'Level', 'Lewis', 'Light', 'Limit', 'Lunch', 'Major', 'March', 'Match', 'Metal', 'Model', 'Money', 'Month', 'Motor', 'Mouth', 'Music', 'Night', 'Noise', 'North', 'Novel', 'Nurse', 'Offer', 'Order', 'Other', 'Owner', 'Panel', 'Paper', 'Party', 'Peace', 'Peter', 'Phase', 'Phone', 'Piece', 'Pilot', 'Pitch', 'Place', 'Plane', 'Plant', 'Plate', 'Point', 'Pound', 'Power', 'Press', 'Price', 'Pride', 'prize', 'Proof', 'Queen', 'Radio', 'Range', 'Ratio', 'Reply', 'Right', 'River', 'Round', 'Route', 'Rugby', 'Scale', 'Scene', 'Scope', 'Score', 'Sense', 'Shape', 'Share', 'Sheep', 'Sheet', 'Shift', 'Shirt', 'Shock', 'Sight', 'Simon', 'Skill', 'Sleep', 'Smile', 'Smith', 'Smoke', 'Sound', 'South', 'Space', 'Speed', 'Spite', 'Sport', 'Squad', 'Staff', 'Stage', 'Start', 'State', 'Steam', 'Steel', 'Stock', 'Stone', 'Store', 'Study', 'Stuff', 'Style', 'Sugar', 'Table', 'Taste', 'Terry', 'Theme', 'Thing', 'Title', 'Total', 'Touch', 'Tower', 'Track', 'Trade', 'Train', 'Trend', 'Trial', 'Trust', 'Truth', 'Uncle', 'Unity', 'Value', 'Video', 'Visit', 'Voice', 'Waste', 'Watch', 'Water', 'While', 'White', 'Whole', 'Woman', 'World', 'Youth']
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']
def get_possible_letters(word):
possible_letters = letters
for i in word:
if i in possible_letters:
possible_letters.remove(i)
return possible_letters
class Word:
def __init__(self, text, coloured_text):
self.text = text
self.coloured_text = coloured_text
def get_best_word():
max_score = len(letters)
for i in wordlist:
score = len(get_possible_letters(i))
if score < max_score:
max_score = score
return score
print(get_best_word())
I am trying to make a program which will find the optimal word to eliminate the most letters from the keyboards alphabet at once when given a list of words. My program always returns "0". I would expect the value to be around 5. I suspect my error is in the "get_best_word" function, but it could be the "get_possible_letters" function as well. I would appreciate any help, my code is as follows.
import os
import random
wordlist = ['crane', 'soare', 'adieu', 'wario', 'crazy', 'aback', 'Abuse','Adult', 'Agent', 'Anger', 'Apple', 'Award', 'Basis', 'Beach', 'Birth', 'Block', 'Blood', 'Board', 'Brain', 'Bread', 'Break', 'Brown', 'Buyer', 'Cause', 'Chain', 'Chair', 'Chest', 'Chief', 'Child', 'China', 'Claim', 'Class', 'Clock', 'Coach', 'Coast', 'Court', 'Cover', 'Cream', 'Crime', 'Cross', 'Crowd', 'Crown', 'Cycle', 'Dance', 'Death', 'Depth', 'Doubt', 'Draft', 'Drama', 'Dream', 'Dress', 'Drink', 'Drive', 'Earth', 'Enemy', 'Entry', 'Error', 'Event', 'Faith', 'Fault', 'Field', 'Fight', 'Floor', 'Focus', 'Force', 'Frame', 'Frank', 'Front', 'Fruit', 'Glass', 'Grant', 'Grass', 'Green', 'Group', 'Guide', 'Heart', 'Henry', 'Horse', 'Hotel', 'House', 'Image', 'Index', 'Input', 'Issue', 'Japan', 'Jones', 'Judge', 'Knife', 'Laura', 'Layer', 'Level', 'Lewis', 'Light', 'Limit', 'Lunch', 'Major', 'March', 'Match', 'Metal', 'Model', 'Money', 'Month', 'Motor', 'Mouth', 'Music', 'Night', 'Noise', 'North', 'Novel', 'Nurse', 'Offer', 'Order', 'Other', 'Owner', 'Panel', 'Paper', 'Party', 'Peace', 'Peter', 'Phase', 'Phone', 'Piece', 'Pilot', 'Pitch', 'Place', 'Plane', 'Plant', 'Plate', 'Point', 'Pound', 'Power', 'Press', 'Price', 'Pride', 'prize', 'Proof', 'Queen', 'Radio', 'Range', 'Ratio', 'Reply', 'Right', 'River', 'Round', 'Route', 'Rugby', 'Scale', 'Scene', 'Scope', 'Score', 'Sense', 'Shape', 'Share', 'Sheep', 'Sheet', 'Shift', 'Shirt', 'Shock', 'Sight', 'Simon', 'Skill', 'Sleep', 'Smile', 'Smith', 'Smoke', 'Sound', 'South', 'Space', 'Speed', 'Spite', 'Sport', 'Squad', 'Staff', 'Stage', 'Start', 'State', 'Steam', 'Steel', 'Stock', 'Stone', 'Store', 'Study', 'Stuff', 'Style', 'Sugar', 'Table', 'Taste', 'Terry', 'Theme', 'Thing', 'Title', 'Total', 'Touch', 'Tower', 'Track', 'Trade', 'Train', 'Trend', 'Trial', 'Trust', 'Truth', 'Uncle', 'Unity', 'Value', 'Video', 'Visit', 'Voice', 'Waste', 'Watch', 'Water', 'While', 'White', 'Whole', 'Woman', 'World', 'Youth']
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']
def get_possible_letters(word):
possible_letters = letters
for i in word:
if i in possible_letters:
possible_letters.remove(i)
return possible_letters
class Word:
def __init__(self, text, coloured_text):
self.text = text
self.coloured_text = coloured_text
def get_best_word():
max_score = len(letters)
for i in wordlist:
score = len(get_possible_letters(i))
if score < max_score:
max_score = score
return score
print(get_best_word())
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该通过
int
计数器
来计算字母。与其取list
的长度:然后您需要制作
current_score
,而不是最高分数。仅当您找到一个更好的得分&gt时才更新它。 Current_score
。这将尝试获取最低评分单词(
4
)You should be counting the letters by an
int
counter
. Rather than taking the length of alist
:Then you need to make a
current_score
, rather than a maximum score. And only update it when you find one betterscore > current_score
.This will try and get the lowest scoring word (
4
)使用切片创建
Letters
的副本。否则,您会更改原始列表 - 这不是您想要的,因为您需要每次检查它。这是正确的代码和可能的解决方案:更正:
您必须正确复制列表,否则它仍然指向
letters
并修改它。您需要在单词中使用小写字母,因为您
字母
列表仅包含较低的案例字母。理想情况下,您正在寻找具有尽可能多不同字母的单词,但是您的评分代码是错误的。您应该从字母总数中减去您的单词中所需的正确数量的不同字母数量。
另一个解决方案(简短)
其他点:尝试使用适当的变量名称。在最后一个代码中,您可以看到变量在某种程度上清楚地传达了这个想法。这实际上对于阅读代码和理解它非常有帮助。
Create a copy of
letters
using slicing. Otherwise you change the original list - which is not what you want since you need to check against it everytime. Here is the correct code and possible solution:Corrections:
You have to copy the list properly otherwise it still points to the
letters
and it modifies it.You need to use lowercase letters in words since you
letters
list contains only lower case letters.Ideally you are looking for words that has as many different letters as possible but your code for scoring was wrong. You should subtract from total number of letters to get the desired correct number of distinct letter in your words.
Another solution (short)
Few other points: try to use proper variable names. In the last code, you can see that the variables clearly conveys the idea to some extent. This is actually quite helpful for reading the code and understanding it.