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

Having some difficulties with the 'update_hand' functionality of the word game on problem set 3. With this function are we supposed to be 'deleting' the elements of the dictionary 'hand' being used in 'word'? or are we just supposed to change the key-value pair from 'A':1 to 'A':0 (just an example)? if we are supposed to do the latter, does anyone have any idea how to go about that? i would like to try to do this without opening the solutions guide if possible... thanks!

OpenStudy (anonymous):

from the docstring: """ ... returns the new hand, without those letters in it ...""" they provided a test suite for this PS. it has a function to test update_hand. when a coworker supplies you a test suite for code that you are writing, that is as good as a spec. you might want to look at that to help your understanding of the docstring. dictionaries have lots of methods. if using windows and idle, F1 will bring up the help file - it is an excellent resource, easier to use than the online version which is here: http://docs.python.org/2.7/library/stdtypes.html#mapping-types-dict

OpenStudy (anonymous):

in 'update_hand' f(x). . u r nt gonna to delete the elements of the dictionary "hand" whose elements have been used to form words in "word" but u have to return a copy of the hand, containing only the letters remaining. For example: >>> hand = {'a':1, 'q':1, 'l':2, 'm':1, 'u':1, 'i':1} >>> displayHand(hand) # Implemented for you a q l l m u i >>> hand = updateHand(hand, 'quail') # You implement this function! >>> hand {'l': 1, 'm': 1} >>> displayHand(hand) l m

OpenStudy (anonymous):

@bwCA- Thank you for the 'help' reference. I'll definitely do some digging in there. @moli1993- Thank you as well, this is helpful. after I return the 'updated' hand though, if I don't actually 'delete' the elements being used, how do I inform the 'deal_hand' how many additional letters to deal? OR am I completely mis-understanding this problem? Am I incorrect in my understanding of the game? Doesn't the player get dealt 7 letters initially, play a word, and then depending on how many letters they play get dealt 'x' additional letters to get their hand back to 7? or is it-- make as many words as you can with the 7 letters given, once you've played all words with those seven letters, then your 'hand' is done and you can deal an entirely new hand of 7? sorry I've been focusing so much on this one problem that I didn't really read through to the 'play hand' section yet.

OpenStudy (anonymous):

you get one hand. you play it untill there are no letters left or you cannot make a word. the hand does not get 'refreshed' after you make a word.

OpenStudy (anonymous):

makes so much more sense! Thank you

OpenStudy (anonymous):

Ok guys- apparently I’m just slow. I'm still stuck on my update_hand function. I build a little function on the side for testing purposes and have attached it here. Right now the code is set up to print each chunk of iterations so that I can see what is happening to my 'hand' dictionary more easily. I thought I had it working, but when I changed my word from 'bad' to 'bab' it broke. It doesn't decrement the 'count' variable on the second time finding the letter from word matching the key of the dictionary. Also, I've got to be doing this in one of the most inefficient ways right? can anyone offer some pointers for my little snippet here to 1- help me figure out why it isn't decrementing like I think it should be, and 2- what can I do to completely overhaul this code and make it work better? Thanks again for your help- I'm very new to this and really appreciate any advice you can provide. hand = {'a':1,'b':2,'c':1,'d':1,'e':1} word = 'bab' def testit2(hand,word): for i, (count) in hand.items(): for j in word: if j == i: print 'correct letter ', ' J(',j,')=I(',i,')' hand[i] = count-1 else: print 'incorrect letter ', ' J(',j,')!=I(',i,')' print hand print return hand print testit2(hand,word)

OpenStudy (anonymous):

never mind- cleaned up my code and it now works. Code below: but i'd still love to get some feedback on any other/better potential ways i could have accomplished this: hand = {'a':1,'h':1,'t':2,'b':2,'r':1} word = 'that' def testit(hand,word): for i,(count) in hand.items(): for j in word: if j == i: hand[i] -= 1 return hand print testit(hand,word)

OpenStudy (anonymous):

please use a code pasting site like dpaste.com pastebin.com update_hand is not supposed to mutate the the hand object that is passed to it, yours does. also from the docstring: """...returns the new hand, without those letters in it....""" http://dpaste.com/1049425/ http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#use-in-where-possible-1

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!
Latest Questions
YoungBlood: STOP THE YELLOW NAME TAG SLANDER!
6 hours ago 11 Replies 2 Medals
Bubblezz: Art for @euphoriiic
9 hours ago 23 Replies 3 Medals
ilovemybf: i need more drawing ideas
10 hours ago 15 Replies 1 Medal
toga: what is a Mayuri
13 hours ago 3 Replies 1 Medal
Midnight97: Here is a beat I made let me know what y'all think
14 hours ago 24 Replies 2 Medals
toga: who thinks that there should be more titles
15 hours ago 5 Replies 0 Medals
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!