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

Finished ps5 today..:)..had lots of bugs at first..but debugging was fun...especially figuring out where the None was coming from..:D...So PLEASE check my code and suggest some improvements and optimizations.. thanks ... http://dpaste.com/hold/569640/

OpenStudy (anonymous):

It works and it looks pretty clean. A couple of small points...the user is not instructed the user how to finish the played hand although I see from your code it is ".". Also, and this may be a bigger point, you may want to look at problem 2. You did it exactly the way I would have done it but, but the way I read the assignment, the original hand is not supposed to be mutated and a new hand is supposed to be returned. If I remember right this is to leave an option to replay the hand later....easy enough fix. But nice job overall.

OpenStudy (anonymous):

I don't quite get what mutating hand means!! and with this code i can replay the hand too .. so what am i supposed to change then?? and yes i forgot to put that "." instruction in . :(...i have put it in the print statement asking for input now :)

OpenStudy (anonymous):

I read it to mean you are supposed to create a new hand rather than deducting from the letter count in the hand, but I could be wrong... If you can replay the hand and it all works, who cares? job done!:)

OpenStudy (anonymous):

:)...and BTW i don't even need the update_hand function in my play_game function :o

OpenStudy (anonymous):

I think the next problem set is based on this one if I remember right.

OpenStudy (anonymous):

IDK...i am yet to try the ghost game..then i will take some lectures ..then ps6 :)

OpenStudy (anonymous):

6 took me awhile. Its great going back and looking with a critical eye at other peoples solutions though. Really helps get the concepts down better than just doing the problems and forgetting about them and moving on.

OpenStudy (anonymous):

Oh, a workaholic amongst us.

OpenStudy (anonymous):

just comments and a couple of variations for iterating a sequence with a for loop i like to just extract the items without using the range function - alternative to lines 156-158 in update_hand. http://dpaste.com/569919/ but sometimes you need the item AND the index, so they have provided the enumerate() function: http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#index-item-2-enumerate shouldn't line 180 (in is_valid_word) be in the update_hand function? line 220-221 - the built-in sum() function works on sequences. you could use sum(hand.values()) in lines 243-245 you call get_word_score three times. a new variable would let u call it just once - http://dpaste.com/569928/ i would simplify lines 243 -254 to: http://dpaste.com/569930/

OpenStudy (anonymous):

About "Mutating the hand": This is a tricky thing about using dictionaries (and certain other data types). Normally, when you alter a variable WITHIN a function the effects are only within that function BUT when you "mutate" a variable rather than change it completely the effects happen outside of the function, too. For example, here is one of the functions from your code: def is_valid_word(word, hand, word_list): """ Returns True if word is in the word_list and is entirely composed of letters in the hand. Otherwise, returns False. Does not mutate hand or word_list. word: string hand: dictionary (string -> int) word_list: list of lowercase strings """ if word in word_list: for i in range (0,len(word)): if hand.has_key(word[i]): hand[word[i]]=hand[word[i]]-1 else:return False if hand[word[i]]==0:del hand[word[i]] return True else: return False 5 lines from the bottom you 'mutate' the dictionary 'hand' by typing hand[word[i]]=hand[word[i]]-1 This change will affect the dictionary 'hand' even outside of your function. For a simpler example try typing this in to the Shell: dict={'a':1, 'b':1, 'c':1} other=dict other[b]=other[b]-1 print dict You will find that dict was changed even though it doesn't seem like it should be!

OpenStudy (anonymous):

I dealt a few hands with your code to illustrate this problem and here it is: Enter n to deal a new hand, r to replay the last hand, or e to end game: n ur current hand is: a p p c e m s enter your word:pens plaese enter a valid word!!! enter your word:ape plaese enter a valid word!!! I first tried the word pens. The funtion is_valid_word went letter by letter to make sure I had them all in my hand. As it checked each letter it mutated the hand by subtracting one from that letter until it eventually got to the letter 'n' which was not in my hand and returned False. The problem is that the mutations that is_valid_word were not local because they were mutations. That's why when I tried ape, which should have been valid, it returned False. Because the mutations to hand persist. If you add a line that prints your hand after any invalid word you should be able to see this more clearly: Enter n to deal a new hand, r to replay the last hand, or e to end game: n ur current hand is: a p p c e m s enter your word:pens plaese enter a valid word!!! ur current hand is: a p c m s enter your word:ape plaese enter a valid word!!!

OpenStudy (anonymous):

The built-in copy function is the easiest way to avoid this problem. try using: test_hand=hand.copy() That will create a copy of hand so you can do your mutations on test_hand without affecting hand.

OpenStudy (anonymous):

OMG. VertigoJC !! thatnk u so much...during testing i got this error once! but i could never find it again..now i know where it came from and how mutation affects my code.. thank u again!! AND @bwCA thanks for the suggestion..i'll definitely try them..:) so for now enlightened_Minds=(bwCA, joz ,nessman) + (VertigoJC,) LOL :D

OpenStudy (anonymous):

Here is an updated code including some of the ps6 problems..please check it...i am not sure that is free of bugs http://ideone.com/YMZ01

OpenStudy (anonymous):

Nice solution! 1 question about the "if word in word_list" though: Was this in the lecture at all? I spent a long time trying to figure out how to set up this search in the word list, but could never figure it out. But it seems python does this automatically. Do you know if the algorithm used to find the "word" as an element of the list is linear or bisection?

OpenStudy (anonymous):

It was not in the lecture . I learned its use here on this forum. And yeah its is linear I think..

OpenStudy (anonymous):

cool, that was really helpful for me. And I don't feel so bad for not knowing if it wasn't in the lecture. Thanks so much!

OpenStudy (anonymous):

hey..u should not feel bad for not knowing something...even if it was in the lecture...u learned it at last...that's the big deal..be happy ..enjoy learning ..thats it..having fun is the most important thing :)

OpenStudy (anonymous):

http://wiki.python.org/moin/TimeComplexity

OpenStudy (anonymous):

...so is the if [element] in [list] an example of "get item?" That would mean it is of constant complexity, right?

OpenStudy (anonymous):

if so, do you know if that means the list search is accomplished using the hashing technique they mentioned in the lecture?

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!