-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathL12_hand.py
147 lines (122 loc) · 4.58 KB
/
L12_hand.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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 = self.hand.keys()
hand_keys.sort()
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)
"""
# Your code here
# raise NotImplementedError()
word_hand = Hand(len(word))
word_hand.setDummyHand(word)
return_bool = True
for w in word_hand.hand.keys():
return_bool = return_bool and self.hand.has_key(w)
if return_bool:
for w in word_hand.hand.keys():
self.hand[w] = self.hand.get(w, 0) - word_hand.hand[w] # no letter or not enough
return return_bool
myHand = Hand(7)
myHand.setDummyHand('aulqqik')
print myHand.update('quail')
print myHand
myHand = Hand(9)
myHand.setDummyHand('iiklroxgx')
myHand.update('milk')
print myHand
# ====== Course Solution ======
# This is the solution for the update method only.
# 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)
# """
# # Make a copy of the hand, and try to update it
# new_hand = self.hand.copy()
# for letter in word:
# try:
# new_hand[letter] -= 1
# except KeyError:
# # if 'letter' isn't in the hand, we can't make the word from this hand.
# return False
# for letter in new_hand.keys():
# # If any of the letter counts of the new hand are less than zero after the
# # update, then we can't make the word from this hand.
# if new_hand[letter] < 0:
# return False
# # If we've gotten to here, we must be able to make the word from this hand.
# # Set self.hand to the new, updated hand and return True.
# self.hand = new_hand
# return True