[QUIZ] Perl 'Medium' Quiz of the Week #2005-05-06 - Ranges' Lookup

Shlomi Fish <shlomif-ik1l9ssToec+JF/[email protected]> Fri, 6 May 2005 23:16:36 +0300
Newsgroups gmane.comp.lang.perl.qotw.discuss
Message-ID <[email protected]>
IMPORTANT: Please do not post solutions, hints, or other spoilers
        until at least 60 hours after the date of this message.  
        Thanks.

Well, the previous quiz did not seem to be very popular. Even I started 
writing it, and then abandoned. I also reached the conclusion that I'd be 
better off adapting Template Toolkit or whatever for this.

In any case here's a fresh new quiz inspired by a question someone asked on 
the IRC. (Freenode #perl's channel today)

You are given a large number of ranges (A[i],B[i]) where B[i] > A[i] for every 
i, and A[i] and B[i] are real. The ranges are not supplied in any particular 
order. You are then given a sequence of real numbers X[0], X[1], X[2] one by 
one. For each number, you have to determine whether it falls inside at least 
one of the ranges. 

For bonus points, you need to supply the user with a set of the indices of 
matching ranges.

Phrasing it Perlishly:

Write the following functions:

1. my $handle = prepare_ranges_handle($array_ref);

$array_ref is an array ref of array refs each containing two elements, being 
the numbers of the ranges.

It returns $handle, which is a reference used for further querying.

2. my $ret = lookup_ranges($handle, $x);

Looks up $x in the ranges which were input to $handle.

$ret is a hash ref that should contain a field 'verdict' that is a boolean 
specifying if any of the ranges contain $x. It may also contain 'match_set' 
which points to a reference to a hash whose keys are the indexes of the 
matching ranges.

Have fun!

Regards,

	Shlomi Fish

---------------------------------------------------------------------
Shlomi Fish      shlomif-ik1l9ssToec+JF/[email protected]
Homepage:        http://www.shlomifish.org/

Hacker sees bug. Hacker fixes bug.