我的信息查找程序返回' 0'

发布于 2025-01-26 11:03:21 字数 2791 浏览 2 评论 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沙沙粒小 2025-02-02 11:03:21

您应该通过int 计数器来计算字母。与其取list的长度:

def get_possible_letters(word):
    possible_letters = letters.copy()
    counter = 0
    for i in word:
        if i in possible_letters:
            possible_letters.remove(i)
            counter += 1
    return counter

然后您需要制作current_score,而不是最高分数。仅当您找到一个更好的得分&gt时才更新它。 Current_score

def get_best_word():
    current_score = 0
    for i in wordlist:
        score = get_possible_letters(i)
        if score > current_score:
            current_score = score
    return current_score

这将尝试获取最低评分单词(4

if score < max_score:
    max_score = score

You should be counting the letters by an int counter. Rather than taking the length of a list:

def get_possible_letters(word):
    possible_letters = letters.copy()
    counter = 0
    for i in word:
        if i in possible_letters:
            possible_letters.remove(i)
            counter += 1
    return counter

Then you need to make a current_score, rather than a maximum score. And only update it when you find one better score > current_score.

def get_best_word():
    current_score = 0
    for i in wordlist:
        score = get_possible_letters(i)
        if score > current_score:
            current_score = score
    return current_score

This will try and get the lowest scoring word (4)

if score < max_score:
    max_score = score
风吹雪碎 2025-02-02 11:03:21
  possible_letters = letters[:]

使用切片创建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[:]
  word = word.lower()
  for c in word:
    if c in possible_letters:
      possible_letters.remove(c)
  return possible_letters

class Word:
  def __init__(self, text, coloured_text):
    self.text = text
    self.coloured_text = coloured_text

    
def get_best_word():
  min_score = len(letters)
  for w in wordlist:
    score = len(get_possible_letters(w))
    if score < min_score:
      min_score = score
  return len(letters)-min_score
print(get_best_word())

更正:

  1. 您必须正确复制列表,否则它仍然指向letters并修改它。

  2. 您需要在单词中使用小写字母,因为您字母列表仅包含较低的案例字母。

  3. 理想情况下,您正在寻找具有尽可能多不同字母的单词,但是您的评分代码是错误的。您应该从字母总数中减去您的单词中所需的正确数量的不同字母数量。

另一个解决方案(简短)

def get_best_word1():
  max_distinct_letters = 0
  for w in wordlist:
    w = w.lower()
    distinct_letters= len(set(list(w)))
    if distinct_letters > max_distinct_chars :
      max_distinct_chars = distinct_letters
  return max_distinct_chars 

其他点:尝试使用适当的变量名称。在最后一个代码中,您可以看到变量在某种程度上清楚地传达了这个想法。这实际上对于阅读代码和理解它非常有帮助。

  possible_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:

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[:]
  word = word.lower()
  for c in word:
    if c in possible_letters:
      possible_letters.remove(c)
  return possible_letters

class Word:
  def __init__(self, text, coloured_text):
    self.text = text
    self.coloured_text = coloured_text

    
def get_best_word():
  min_score = len(letters)
  for w in wordlist:
    score = len(get_possible_letters(w))
    if score < min_score:
      min_score = score
  return len(letters)-min_score
print(get_best_word())

Corrections:

  1. You have to copy the list properly otherwise it still points to the letters and it modifies it.

  2. You need to use lowercase letters in words since you letters list contains only lower case letters.

  3. 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)

def get_best_word1():
  max_distinct_letters = 0
  for w in wordlist:
    w = w.lower()
    distinct_letters= len(set(list(w)))
    if distinct_letters > max_distinct_chars :
      max_distinct_chars = distinct_letters
  return max_distinct_chars 

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文