0

嗨,我刚刚开始学习如何编程并拥有一个我需要用 Python 编写的函数,这就是它背后的想法:

它返回Trueif wordis in thewordList并且完全由手中的字母组成。否则,返回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 的键打折。我认为(希望)这会奏效。

4

4 回答 4

1

像这样的东西怎么样:

def isValidWord(hand, word, word_list):
    if word not in word_list:
        return False
    for c in word:
        if c not in hand:
            return False
    return True

由于字符串是可迭代的,您可以逐个字符地检查。

祝你好运

于 2013-03-06T20:49:55.820 回答
1

没有您的代码,让我看看我是否理解您想要的内容:您正在尝试查看给定的单词是否可以使用 in 中的字母拼写,hand就好像用户对 in 中的每个字母都有一个拼字游戏瓷砖一样hand,是吗?

就我个人而言,我只是复制hand字典,然后允许对副本进行更改。像这样的东西:

def is_valid_word(hand, word, wordlist):
    hand_cp = dict(hand)
    for letter in word:
        if hand_cp.get(letter):
            # The letter is in our hand, so "use it up".
            hand_cp[letter] = hand_cp[letter] - 1
        else:
            # The letter isn't in our hand, so the word isn't valid.
            return False

    # If we can make the word, now make sure it's a real word:
    # (If wordlist is long, you might want to sort it and do a real search)
    if word not in wordlist: 
        return False

    # We haven't found any reason to return False, so this is a valid word.
    return True
于 2013-03-06T20:56:39.533 回答
0

pythonCounter类是你的朋友。您可以在python 2.7 及更高版本中执行此操作:

from collections import Counter

def is_valid_word(hand, word, word_list):
    letter_leftover = Counter(hand)
    letter_leftover.subtract(Counter(word))
    return word in word_list and all(v >= 0 for v in letter_leftover.values())

然后:

>>> def test():
...     hand = "traipse"
...     word_list = ["all", "the", "words", "in", "English", 
                     "parts", "pines", "partiers"]
...     print is_valid_word(hand, "parts", word_list)
...     print is_valid_word(hand, "pines", word_list)
...     print is_valid_word(hand, "partiers", word_list)
... 
>>> test()
True
False
False
于 2013-03-06T21:43:28.267 回答
0

这是我的

def isValidWord(word, hand, 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
    """
    if word in wordList:
        if set(word).issubset(set(hand)):
            return True
        else:
            return False
    else:
        return False
于 2017-07-07T22:44:24.790 回答