JadeNB,
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:

Chapter 1: 3 sentences Chapter 2: 5 sentences Chapter 3: 4 sentences Chapter 4: 6 sentences The chapter with the longest chapter is 4, so we pad all of them to 6 Chapter 1: 01, 02, 03 # 04, 05, 06 are padded real: 01, 02, 03 Chapter 2: 07, 08, 09, 10, 11 # 12 is padded real: 04, 05, 06, 07, 08 Chapter 3: 13, 14, 15, 16 # 17 and 18 are padded real: 09, 10, 11, 12 Chapter 4: 19, 20, 21, 22, 23, 24 # no padding real: 13, 14, 15, 16, 17, 18 Now let's say someone says, what chapter does 15 belong to? ceil(15 / 6) = 3 Now let's say someone wants to know the true ordinal overall value my %pad = (1 => 0, 2 => 3, 3 => 4, 4 => 6); 15 - $pad{ceil(15 / 6)} = 11 Now let's say someone wants to know what is the ordinal value of the s +entence within the chapter 15 mod 6 = 3

Cheers - L~R


In reply to Re^3: chapters,sentences, and arrays by Limbic~Region
in thread chapters,sentences, and arrays by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.