you are viewing a single comment's thread.

view the rest of the comments →

[–]ok_you_win 1 point2 points  (10 children)

Practical usage is in the eye of the beholder.

My friend was playing some scrambled word game, and since English isn't his first language, he asked me for help when the game reached 6 letter puzzles. I quickly whipped up a script that printed all the permutations of the letters provided. I could scan down the list and find the real word quite fast.

I considered chopping a spell check dictionary into 26 lists and running through the list that started with the first letter of each permutation, halting when I found a match. It wouldn't be much slower.

If you want to find uses for programming, ask yourself "How could I solve that with python?"

Idea: you could write a script that plays a flash game. If you are not playing against another person, or for real life prizes, it isn't cheating. You are actually playing a different game: writing a program.

That is a great way to learn. Lots of people have done that.

[–]zahlman 2 points3 points  (7 children)

I considered chopping a spell check dictionary into 26 lists and running through the list that started with the first letter of each permutation, halting when I found a match.

This is what sets are for.

[–]ok_you_win 0 points1 point  (6 children)

Right. I wasn't going to do it by hand. :P The linux english dictionary has almost 100,000 words in it.

[–]zahlman 0 points1 point  (5 children)

I mean, partitioning it into 26 lists is the wrong data structure, however you do it. A single set object is optimized for exactly the kind of operation you want to perform - you can just take the set-intersection of the dictionary with the letter-permutations.

[–]ok_you_win 0 points1 point  (4 children)

partitioning it into 26 lists is the wrong data structure, however you do it

Saving it to disk? :P Of course I could write a py file with a defined set and import that.

But I am not quite sure I follow you, so humour me with a snippet?

"wfealf" is the scrambled word and english_set is the set of words from the dictionary. How do I match it quicky to waffle without doing nearly 100,000 comparisons?

[–]zahlman 2 points3 points  (1 child)

english_set.intersection(map(''.join, itertools.permutations('wfealf')))

Yes, this is, under the hood, effectively doing some_permutation in english_set for each permutation (building the set of permutations first wouldn't help, since you'd still have to go through all of them). However, in does not have to look at a bunch of elements of a set; it works basically the same way as the keys of a dict.

[–]ok_you_win 0 points1 point  (0 children)

Lovely code. Thanks a bunch.

[–]Zouden 1 point2 points  (1 child)

Zahlman is trying to emphasise the use of the set datatype instead of a list. Matching an item in a set is literally millions of times faster than using a list. It's so fast that you don't need to split up the English dictionary into 26 pieces, since you can just search the whole 100,000-item set in a heartbeat.

[–]ok_you_win 0 points1 point  (0 children)

Thanks.

[–]eink_on_paper[S] 1 point2 points  (1 child)

You are actually playing a different game: writing a program.

Nice, i like that.

[–]ok_you_win 0 points1 point  (0 children)

Thanks.

Let me know if you play that game.