Reputation: 180391
Hi I am just starting to learn how to program and have a function that I need to write in Python, this is the idea behind it:
It 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.
There is a function to call that checks the frequency of the letters in the word the user comes up with and that is a converted to a dict, I have tried using iteritems various ways but to no avail, I am getting stuck on the words that have repeated letters, they are being returned as true when I don't have two entries for that letter in the users hand.
Sorry if this is unclear I only started two weeks ago. any pointers would be great I have been stuck on this for a long time!
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
basically my next step was trying to figure out how to compare word to handC with amended value and discounting any key with a value of zero. I think(hope) that will work.
Upvotes: 0
Views: 1280
Reputation: 1
here is mine
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
Upvotes: 0
Reputation: 49003
The python Counter
class is your friend. You can do this in python 2.7 and later:
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())
Then:
>>> 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
Upvotes: 0
Reputation: 17168
Without your code, let me see if I understand what you want: you're trying to see if the given word can be spelled using the letters in hand
as if the user had a Scrabble tile for each letter in hand
, yes?
Personally I'd just copy the hand
dictionary and then allow changes to the copy. Something like this:
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
Upvotes: 1
Reputation: 1367
How about something like this:
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
As strings are iterable you can check character by character.
Good luck
Upvotes: 1