Quantcast

Got Homework?

Connect with other students for help. It's a free community.

  • across
    MIT Grad Student
    Online now
  • laura*
    Helped 1,000 students
    Online now
  • Hero
    College Math Guru
    Online now

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

whadlock Group Title

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!

  • one year ago
  • one year ago

  • This Question is Closed
  1. bwCA Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    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

    • one year ago
  2. moli1993 Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    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

    • one year ago
  3. whadlock Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    @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.

    • one year ago
  4. bwCA Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    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.

    • one year ago
  5. whadlock Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    makes so much more sense! Thank you

    • one year ago
  6. whadlock Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    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)

    • one year ago
  7. whadlock Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    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)

    • one year ago
  8. bwCA Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    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

    • one year ago
    • Attachments:

See more questions >>>

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.