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

ps6, problem 4. My dictionary (with each key sorted), rearrange_dict, takes so long to create, it causes my program to crash. I've tried on smaller word_list lists, and everything works fine. I notice it get slower around word_list size 1,000 words. by 10,000 or so, it's getting ridiculous. And of course, 83,000 or whatever is actually in "words.txt", just doesn't come out. I've got a test program which includes the load_words stuff from the assignment, along with my get_word_rearrangements function. I modified load_words so that I could test on different size lists.

OpenStudy (anonymous):

If some people could run this code and tell me if they have a problem, that would be helpful, because I can't figure out what's going on. Remember to be in the directory with "words.txt" from the assignment. Also, the modification I made to load_words is documented line for line in the code with the comment 'added by me'. You can try different size lists by changing what is now 'n>=10000', to 'n>=whatever number you want'. Thanks for your help! http://dpaste.com/576675/

OpenStudy (anonymous):

ok, I may have been wrong about what the problem was...(although maybe not). I re-wrote your line 37 to turn the word into a list, sort the list, then turn the list back into a string. Then added ordered string to dictionary. Then...and I think this may actually be they key...I changed "print rearrange_dict" at the bottom to print only the the length of the dictionary. Completed smoothly in a couple of seconds. Maybe printing out a dictionary that big hurts idles brain?

OpenStudy (anonymous):

** deleted first two posts to avoid unnecessary reading and save face:) def load_word() runs fine by itself after removing your "n" 10000 limits. problem was in get_word_rearrangements() or...the prints statement at the bottom.

OpenStudy (anonymous):

concur - ran it as is without the last print statement and it was ok. definitely line 37 could be tweaked (although it does work) - the list comprehension is unnecessary, join will work on the entire list returned by sorted - ''.join(sorted(word))

OpenStudy (anonymous):

Thanks. Yeah, I guess creating something in order to refer to its elements and printing the whole thing are two different things. More different than I suppose I thought.

OpenStudy (anonymous):

So if I understand this correctly... this code takes the word list (all 83,000 words) and rearranges each word into its combinations? Essentially multiple that word_list into a permutation of each word? e.g. the and would look like this in your rearranged dictionary... and adn dna dan nad nda You are then using this dictionary to test the best words? I assumed it'd be easier to do this at the hand level (e.g. once a hand is dealt... rearrange it into all it's combinations and pick the best word from there using the points dictionary). Also - can you explain how the code in line 37 works?

OpenStudy (anonymous):

nope the instructions were to make a dictionary where the key is the letters of the word in sorted order and the value is the word - {sorted_word : word}

OpenStudy (anonymous):

bwCA has got it right, and that's what my code is doing. And you're right, you then use that by comparing all possible permutations of your hand (each permutation in sorted order) to entries in that dictionary, and then of those that match you choose the one with the best score (using points_dict). As far as line 37 goes, I actually got that from VertigoJC (a user on this site), and used it because it was a little more concise than the code I wrote to do the same thing. What it does: The function sorted(w) returns a list of the elements of 'w' in sorted order. So sorted('hello') would return [ 'e', 'h', 'l', 'l', 'o']. " ''.join(c for c in sorted(w)) " takes each letter in that list one at a time, and joins it to the empty string (represented by two single quotes with nothing between them.) Then you have a string of of the word 'w' except that the letters are in sorted order, and you add that as a key to a new dictionary with the associated value being the original word 'w'.

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!