Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 18 Online
OpenStudy (harsimran_hs4):

Problem set 3 (6.00 , 2011) hi guys i am struck up with ps3a in the function is_valid_word. my code passes the 2 out of five test successfully and error with the rest. please can you tell me the problem. here is the code.. http://pastebin.com/B0uyA893

OpenStudy (anonymous):

It looks like you put some code from update_hand into is_valid_word. You should not be modifying hand at all, just checking if the word exists in the hand. Since you are deleting the entry when you update_hand, you only need to check if the key exists in the hand. Something like: if c in hand.keys(): is better than: if c in hand == false: also the in operator already returns a bool so you don't need the == operators you can just use the not operator instead as in: if c not in hand.keys(): hope that helps

OpenStudy (harsimran_hs4):

i made these few corrections and also made it look better here is the code http://pastebin.com/G42uhuSN

OpenStudy (anonymous):

looks a bit better but you are still modifying the hand in is_valid_word. everything else looks like it should work at a glance, but you need to remove the parts where you modify hand. take this out: if c in hand and hand[c] == 1: del hand[c] if c in hand and hand[c] > 1: hand[c] = hand[c] - 1 also you need to break out of the loop when in_hand becomes false so put in a break statement like this: for c in word: in_hand = c in hand.keys() if not in_hand: break also remember to use hand.keys() instead of just hand and thats all you need to check if the word is in the hand, the rest looks fine

OpenStudy (anonymous):

My code is quite diferent, try to use a copy of the hand, to manipulate, not the variable hand, I just see it through coz I gotta run for c in word: in_hand = c in hand ***** if c in hand and hand[c] == 1: del hand[c] if c in hand and hand[c] > 1: hand[c] = hand[c] - 1 In those ***** you re-write the value if is False could be True later, maybe a if 'False' then 'break', take the word of your error and recreate it step by step.

OpenStudy (anonymous):

ohh sorry, I just realized why you are trying to modify hand, but Gianko is right, you don't want to mess with the hand variable itself make a copy if you want to do it that way. But the way I did it was to turn the word into a dict with the given get_word_frequency function and then check that each key in the word dict is in the hand dict and that the value of the word dict at each key is not greater than the value of the hand at each key. but it can be done the way you were thinking, just make sure you make a copy of the hand before you delete elements in it. Sorry for the mistake, I guess I should look at my own code before I post something lol

OpenStudy (harsimran_hs4):

thanks guys .....i had figured out the problem and now the code is fine.....

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!