Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- """
- letters = {}
- for i in word:
- letters[i.lower()] = letters.get(i, 0)
- if word.upper() in wordList or word.lower() in wordList or word in wordList:
- for i in letters:
- if i in hand:
- if hand[i.lower()] >= letters[i.lower()]:
- continue
- else:
- return False
- else:
- return False
- return True
- else:
- return False
- word = 'rapture'
- wordList = open('words.txt').read().split()
- hand = {'a': 1, 'h': 1, 'r': 1, 'e': 1, 'm': 2}
- print(isValidWord(word, hand, wordList))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement