Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.93 KB | None | 0 0
  1. def isValidWord(word, hand, wordList):
  2.     """
  3.    Returns True if word is in the wordList and is entirely
  4.    composed of letters in the hand. Otherwise, returns False.
  5.  
  6.    Does not mutate hand or wordList.
  7.  
  8.    word: string
  9.    hand: dictionary (string -> int)
  10.    wordList: list of lowercase strings
  11.    """
  12.     letters = {}
  13.     for i in word:
  14.         letters[i.lower()] = letters.get(i, 0)
  15.  
  16.     if word.upper() in wordList or word.lower() in wordList or word in wordList:
  17.         for i in letters:
  18.             if i in hand:
  19.                 if hand[i.lower()] >= letters[i.lower()]:
  20.                     continue
  21.                 else:
  22.                     return False
  23.             else:
  24.                 return False
  25.         return True
  26.     else:
  27.         return False
  28.  
  29.  
  30. word = 'rapture'
  31. wordList = open('words.txt').read().split()
  32. hand =  {'a': 1, 'h': 1, 'r': 1, 'e': 1, 'm': 2}
  33. print(isValidWord(word, hand, wordList))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement