Hi, I'm on problem set 5 problem 6: implementing the game 'ghost'. I have two questions. 1) The 'Helper Code' includes a function to convert the string to a dictionary, is it easier to work with dictionaires in this problem and why? 2) A rule in the game is that players must create word fragments that can become words, how do I search the wordlist to check this?
MIT OCW 2008?? my solution does not use the frequency dictionary function the startswith() string method may be usefull. I used a bisection search of the wordlist to see if a fragment could make a word http://docs.python.org/2.7/library/stdtypes.html#str.startswith
I used the str.startswith and it worked. I haven't done a bisection of the wordlist because I found that an answer was return straight away with "any (w.startswith(frag) for w in wordlist)". although I imagine a bisect seach would be more efficient
yeh OCW 2008
keep it simple, if it works and its not to slow it's ok. nice generator.
1) Dictionaries are a mapping between a key and a value. Your variables are actually a dictionary: foo = 3 foo is the key, 3 is the value. When you want foo's value, you just refer to foo. Most of the rest of it is just syntax stuff. One thing that isn't much fun with dictionaries, if you refer to a key that doesn't exist, they raise an exception. The good news, is that dictionaries have method to query them that won't raise an exception: foo = dict.get( key, sentinalValue ) if( foo == sentinalValue ): print "shucks!" else: print "you got key's value"
Join our real-time social learning platform and learn together with your friends!