good chemistry is complicated, and a little bit messy -LW |
|
PerlMonks |
Challenge: Find median string in a listby Limbic~Region (Chancellor) |
on Jul 04, 2007 at 18:10 UTC ( [id://624919]=perlquestion: print w/replies, xml ) | Need Help?? |
Limbic~Region has asked for the wisdom of the Perl Monks concerning the following question:
All,
This challenge was posed last night in #perl IRC on freenode. Given:
For the purposes of this challenge, distance is the Levenshtein Distance. The median string is the string where the distance between itself and the furthest away string in the list is minimized. In other words, if you were to calculate the distance of the string furthest away for every string in the list, the median string would be the one with the shortest distance. I recognize that my use of median is likely incorrect. In the event of a conflict, refer to the definition here as the intended one. There may be more than one string that fits the defintion of median string, in this case only 1 such string is required. If you come up with a great solution that is not very memory efficient feel free to post it but if you can, limit memory consumption to 750MB. I have a feeling a trie may provide for a better solution, but the best I can come up with so far is below: <Reveal this spoiler or all in this thread>
Update: ysth suggested a number of optimizations that I had already tried. He had the sense to try them together which makes it blazingly fast. While the algorithm is mine, credit for the optimizations go to him. Cheers - L~R
Back to
Seekers of Perl Wisdom
|
|