【发布时间】:2013-03-06 20:38:46
【问题描述】:
您好,我刚刚开始学习如何编程并拥有一个我需要用 Python 编写的函数,这就是它背后的想法:
如果word 在wordList 中并且完全由手中的字母组成,则返回True。否则,返回False。不会改变 hand 或 wordList。
有一个函数可以调用来检查用户想出的单词中字母的频率,这是一个转换为字典的函数,我尝试过各种方式使用 iteritems 但无济于事,我被卡住了重复字母的单词,当我在用户手中没有该字母的两个条目时,它们将被返回为真。
抱歉,如果不清楚,我是两周前才开始的。 任何指针都会很棒我已经坚持了很长时间!
def isValidWord(hand,word,wordList):
"""
Returns True if word is in the wordList and is entirely
composed of letters in the hand. Otherwise, returns False.
Does not mutate hand or wordList.
word: string
hand: dictionary (string -> int)
wordList: list of lowercase strings
"""
wordC = getFrequencyDict(word)
handC = dict.copy(hand)
if word not in wordList:
return False
for c in word:
if c not in hand:
return False
for k,v in wordC.iteritems():
if k in hand and v > 1:
handC[k] -= 1
基本上,我的下一步是试图弄清楚如何将 word 与具有修正值的 handC 进行比较,并折扣任何值为 0 的键。 我认为(希望)这会奏效。
【问题讨论】:
-
请发布您目前拥有的代码。
-
如果你觉得你没有清楚地解释你的问题,发布你想出的代码通常很有帮助,因为它通常会清楚你的意图是什么以及您遇到麻烦的地方。它还可以帮助人们看到您至少尝试过一些东西。
标签: python dictionary