diff options
| author | Carlos Maiolino <[email protected]> | 2025-07-10 22:55:07 +0200 |
|---|---|---|
| committer | Carlos Maiolino <[email protected]> | 2025-07-10 22:56:55 +0200 |
| commit | d98f46ce647846b0aa30b2e16a30fd4e152a1bf5 (patch) | |
| tree | 267474fcc77cf20b428f6f4c7f768ca09f4cfe0e /mit/scrabble_oop/hand.py | |
| parent | 869e68986aa8f69af6e7842260a68d1e5c6f796f (diff) | |
Add new code
Signed-off-by: Carlos Maiolino <[email protected]>
Diffstat (limited to 'mit/scrabble_oop/hand.py')
| -rw-r--r-- | mit/scrabble_oop/hand.py | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/mit/scrabble_oop/hand.py b/mit/scrabble_oop/hand.py new file mode 100644 index 0000000..63ec124 --- /dev/null +++ b/mit/scrabble_oop/hand.py @@ -0,0 +1,107 @@ +import random + +class Hand(object): + def __init__(self, n): + ''' + Initialize a Hand. + + n: integer, the size of the hand. + ''' + assert type(n) == int + self.HAND_SIZE = n + self.VOWELS = 'aeiou' + self.CONSONANTS = 'bcdfghjklmnpqrstvwxyz' + + # Deal a new hand + self.dealNewHand() + + def dealNewHand(self): + ''' + Deals a new hand, and sets the hand attribute to the new hand. + ''' + # Set self.hand to a new, empty dictionary + self.hand = {} + + # Build the hand + numVowels = self.HAND_SIZE // 3 + + for i in range(numVowels): + x = self.VOWELS[random.randrange(0,len(self.VOWELS))] + self.hand[x] = self.hand.get(x, 0) + 1 + + for i in range(numVowels, self.HAND_SIZE): + x = self.CONSONANTS[random.randrange(0,len(self.CONSONANTS))] + self.hand[x] = self.hand.get(x, 0) + 1 + + def setDummyHand(self, handString): + ''' + Allows you to set a dummy hand. Useful for testing your implementation. + + handString: A string of letters you wish to be in the hand. Length of this + string must be equal to self.HAND_SIZE. + + This method converts sets the hand attribute to a dictionary + containing the letters of handString. + ''' + assert len(handString) == self.HAND_SIZE, "Length of handString ({0}) must equal length of HAND_SIZE ({1})".format(len(handString), self.HAND_SIZE) + self.hand = {} + for char in handString: + self.hand[char] = self.hand.get(char, 0) + 1 + + + def calculateLen(self): + ''' + Calculate the length of the hand. + ''' + ans = 0 + for k in self.hand: + ans += self.hand[k] + return ans + + def __str__(self): + ''' + Display a string representation of the hand. + ''' + output = '' + hand_keys = sorted(self.hand.keys()) + for letter in hand_keys: + for j in range(self.hand[letter]): + output += letter + return output + + def update(self, word): + """ + Does not assume that self.hand has all the letters in word. + + Updates the hand: if self.hand does have all the letters to make + the word, modifies self.hand by using up the letters in the given word. + + Returns True if the word was able to be made with the letter in + the hand; False otherwise. + + word: string + returns: Boolean (if the word was or was not made) + """ + + valid = self.hand.copy() + + for i in word: + if i not in valid.keys(): + return False + elif valid[i] == 0: + return False + else: + valid[i] -= 1 + + self.hand = valid.copy() + return True + # Your code here + + +myHand = Hand(11) +myHand.setDummyHand('fooeeocjhbs') +print(myHand) +ret = myHand.update('coffee') +print(ret) +print(myHand) +print() |
