|
|
| Welcome to the Monastery | |
| PerlMonks |
Re: Lookahead algorithm for IP subroutineby demerphq (Chancellor) |
| on May 11, 2005 at 12:07 UTC ( [id://456051]=note: print w/replies, xml ) | Need Help?? |
This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.
To the best of my understanding from the CB you can factor a lot of stuff out of your current approach. Here is what i came up with based on your scratchpad.
It expects a length and an array of increasing ordered numbers. It finds the start index and end index of the array such that, the sequence is the lowest contiguous sequence of the required size, or failing that a sequence where the difference between the highest and lowest value is the least. The trick with this is that you can tell if a sequence is contiguous by subtracting the beginnig from the end, likewise we can get the sequence with the least difference between begin and end by the same approach. We can remember the "best" sequence we've seen by storing its endpoint, and we can simply exit when we have encountered the first contiguous sequence (which will be the lowest since we start low and iterate up). Anyway, the idea here is to take something like this and adapt it to your exact requirements, the way you formatted the code and stuff made it fairly hard to read and work out. Alway use whitespace in your queries to make them readable, SQL is an ugly language to begin with so making it readable is really important. HTH The text in this node was added sometime after it was posted in an attempt to clarify the code for hok. Later Update: Heres a cleaner and annotated version of the code, same idea, but simplified further. Note the early termination when it finds a contiguous sequence which allows this to be worst case O(N) and best case lower.
--- $world=~s/war/peace/g
In Section
Seekers of Perl Wisdom
|
|
||||||||||||||||||||||||