Guest User

Untitled

a guest
Jun 20th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. import random
  2.  
  3. class Hand(object):
  4. def __init__(self, n):
  5. '''
  6. Initialize a Hand.
  7.  
  8. n: integer, the size of the hand.
  9. '''
  10. assert type(n) == int
  11. self.HAND_SIZE = n
  12. self.VOWELS = 'aeiou'
  13. self.CONSONANTS = 'bcdfghjklmnpqrstvwxyz'
  14.  
  15. # Deal a new hand
  16. self.dealNewHand()
  17.  
  18. def dealNewHand(self):
  19. '''
  20. Deals a new hand, and sets the hand attribute to the new hand.
  21. '''
  22. # Set self.hand to a new, empty dictionary
  23. self.hand = {}
  24.  
  25. # Build the hand
  26. numVowels = self.HAND_SIZE // 3
  27.  
  28. for i in range(numVowels):
  29. x = self.VOWELS[random.randrange(0,len(self.VOWELS))]
  30. self.hand[x] = self.hand.get(x, 0) + 1
  31.  
  32. for i in range(numVowels, self.HAND_SIZE):
  33. x = self.CONSONANTS[random.randrange(0,len(self.CONSONANTS))]
  34. self.hand[x] = self.hand.get(x, 0) + 1
  35.  
  36. def setDummyHand(self, handString):
  37. '''
  38. Allows you to set a dummy hand. Useful for testing your implementation.
  39.  
  40. handString: A string of letters you wish to be in the hand. Length of this
  41. string must be equal to self.HAND_SIZE.
  42.  
  43. This method converts sets the hand attribute to a dictionary
  44. containing the letters of handString.
  45. '''
  46. assert len(handString) == self.HAND_SIZE, "Length of handString ({0}) must equal length of HAND_SIZE ({1})".format(len(handString), self.HAND_SIZE)
  47. self.hand = {}
  48. for char in handString:
  49. self.hand[char] = self.hand.get(char, 0) + 1
  50.  
  51.  
  52. def calculateLen(self):
  53. '''
  54. Calculate the length of the hand.
  55. '''
  56. ans = 0
  57. for k in self.hand:
  58. ans += self.hand[k]
  59. return ans
  60.  
  61. def __str__(self):
  62. '''
  63. Display a string representation of the hand.
  64. '''
  65. output = ''
  66. hand_keys = sorted(self.hand.keys())
  67. for letter in hand_keys:
  68. for j in range(self.hand[letter]):
  69. output += letter
  70. return output
  71.  
  72. def update(self, word):
  73. """
  74. Does not assume that self.hand has all the letters in word.
  75.  
  76. Updates the hand: if self.hand does have all the letters to make
  77. the word, modifies self.hand by using up the letters in the given word.
  78.  
  79. Returns True if the word was able to be made with the letter in
  80. the hand; False otherwise.
  81.  
  82. word: string
  83. returns: Boolean (if the word was or was not made)
  84. """
  85. dup_hand = self.hand.copy()
  86. for letter in word:
  87. if(dup_hand.get(letter,0) == 0):
  88. return False
  89. else:
  90. dup_hand[letter] -= 1
  91. self.hand = dup_hand.copy()
  92. return True
  93.  
  94. myHand = Hand(7)
  95. print(myHand)
  96. print(myHand.calculateLen())
  97.  
  98. myHand.setDummyHand('aazzmsp')
  99. print(myHand)
  100. print(myHand.calculateLen())
  101.  
  102. myHand.update('za')
  103. print(myHand)
Add Comment
Please, Sign In to add comment