From: ben.usenet@bsb.me.uk   
      
   Tim Rentsch writes:   
      
   > Ben Bacarisse writes:   
   >   
   > [...]   
   >   
   >> Consider any ordered measure that "wraps round" -- bearings in degrees,   
   >> minutes in the hour, indeed hours in either the 12 or 24 hour clock.   
   >> The problem is to determine if a given value is in the sub-range   
   >> specified by a start and an [end] value.   
   >>   
   >> I was specifically concerned with integer values where the sub-range   
   >> includes the start value but excludes the end value.   
   >>   
   >> Though I am not sure this merits the term "puzzle", I suggest that   
   >> solutions be posted with some spoiler protection.   
   >   
   > My answer below (forgive me for resorting to "low tech" spoiler   
   > protection)...   
      
   I think this is the safest option.   
      
   > (spoiler alert)   
   > (spoiler alert)   
   > (spoiler alert)   
   > (spoiler alert)   
   > (spoiler alert)   
   > (spoiler alert)   
   > (spoiler alert)   
   > (spoiler alert)   
   > (spoiler alert)   
   > (spoiler alert)   
   > (spoiler alert)   
   > (spoiler alert)   
   > (spoiler alert)   
   > (spoiler alert)   
   > (spoiler alert)   
   > (spoiler alert)   
   > (spoiler alert)   
   > (spoiler alert)   
   > (spoiler alert)   
   > (spoiler alert)   
   > (spoiler alert)   
   > (spoiler alert)   
   > (spoiler alert)   
   > (spoiler alert)   
   > (spoiler alert)   
   > (spoiler alert)   
   > (spoiler alert)   
   > (spoiler alert)   
   > (spoiler alert)   
   > (spoiler alert)   
   > (spoiler alert)   
   > (spoiler alert)   
   > (spoiler alert)   
   > (spoiler alert)   
   > (spoiler alert)   
   > (spoiler alert)   
   > (spoiler alert)   
   > (spoiler alert)   
   > (spoiler alert)   
   > (spoiler alert)   
   > (spoiler alert)   
   > (spoiler alert)   
   > (spoiler alert)   
   > (spoiler alert)   
   > (spoiler alert)   
   > (spoiler alert)   
   > (spoiler alert)   
   > (spoiler alert)   
   > (spoiler alert)   
   > (spoiler alert)   
   >   
   > /* is_circularly_between( a, b, c ) -   
   > * 1 if b is circularly between a and c,   
   > * 0 otherwise   
   > * the interval of interest [ a, c ) is understood to be   
   > * closed at the 'a' end, and   
   > * open at the 'c' end   
   > *   
   > * The parameters a, b, and c are all of a single type T,   
   > * where T allows relational (ordering) comparisons.   
   > *   
   > * Assumes a, b, and c all have legitimate values.   
   > */   
   >   
   > int   
   > is_circularly_between( T a, T b, T c ){   
   > return a <= c ? a <= b && b < c : a <= b || b < c;   
   > }   
      
   I am sure you know this is correct! My version is recursive, because I   
   think it adds some clarity, but whether it really does add anything   
   probably depends on how one arrives at the answer.   
      
    bool is_circularly_between(T start, T end, T i) {   
    return start <= end ? start <= i && i < end   
    : !is_circularly_between(end, start, i);   
    }   
      
   (I put the parameters in a different order because I was using Haskell,   
   and with Curried functions, is_circularly_between x y is a useful   
   function in its own right.)   
      
   The only reason I thought it worth mentioning was my failure! For the   
   best part of an hour I thought the size of the circular range (the   
   modulus) had to be involved.   
      
   --   
   Ben.   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|