in reply to Re^3: chapters,sentences, and arrays
in thread chapters,sentences, and arrays
Computed data:
my %pad = ( 1 => 0, 4 => 3, 9 => 4, 13 => 6 ); my $longest = 6;
Input:
Outputs:my $sentence = 11;
my $padded = sum map $pad{$_}, grep $sentence>=$_, keys %pad; my $chapter = int( $padded / $longuest ) + 1; my $in_chapter = $padded % $longuest + 1
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.
|
|---|