in reply to Re: chapters,sentences, and arrays
in thread chapters,sentences, and arrays
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: chapters,sentences, and arrays
by Limbic~Region (Chancellor) on Oct 22, 2008 at 14:46 UTC | |
In a nutshell, this problem boils down to where this number fits in a list. The obvious non-creative solution to this is a binary search because it is both space and time efficient. It would also be fairly trivial if each chapter had the same number of sentences. One trick might be then to find the chapter with the most number of sentences and pad each chapter to be equal to that chapter. When you "hand out" sentences and their ordinal value, there will be gaps representing the padding but you can then instantly take any sentence and find which chapter it belongs to. You can also find its true overall ordinal value in the book as well as the ordinal value in the chapter. Consider the following:
Cheers - L~R | [reply] [d/l] |
by ikegami (Patriarch) on Oct 22, 2008 at 18:22 UTC | |
Computed data:
Input: Outputs:
Downside: it went from O(1) to O(Chapter). That shouldn't matter unless you have an incredible amount of chapters. In that case, convert the hash to an array and use a binary search to drop it to O(log Chapter). Update: Expanded on the downside. | [reply] [d/l] [select] |
by Perlbotics (Archbishop) on Oct 23, 2008 at 19:40 UTC | |
It is interesting to see that there seem to be no way around a loop/compare- or table-lookup-stage to cope with the discrete non-linearity of this problem. The example below shows two alternatives that seem to compute the chapter from a given valid section directly without an algorithmic (loop/compare) stage: Other creative math solutions might involve neural networks (coefficients again) or series employing the si(x) function (coefficients again)... could be fun ... and impractical too ;-)
| [reply] [d/l] [select] |