Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.programming > #15951

Re: A little puzzle.

From Tim Rentsch <tr.17687@z991.linuxsc.com>
Newsgroups comp.programming
Subject Re: A little puzzle.
Date 2022-11-23 08:04 -0800
Organization A noiseless patient Spider
Message-ID <86sfi98xnx.fsf@linuxsc.com> (permalink)
References <875yf8nijb.fsf@bsb.me.uk> <865yf79l66.fsf@linuxsc.com> <87wn7nj9mb.fsf@bsb.me.uk>

Show all headers | View raw


Ben Bacarisse <ben.usenet@bsb.me.uk> writes:

> Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
>
>> Ben Bacarisse <ben.usenet@bsb.me.uk> 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.  [parameters in a
> different order to help with currying]
>
>   bool is_circularly_between(T start, T end, T i) {
>       return start <= end ? start <= i && i < end
>                           : !is_circularly_between(end, start, i);
>   }

Very clever!  It never occurred to me to consider the complement
of the interval.  This idea works only because the interval is
half open, which means its complement is also half open.  I'm not
sure which approach is "more obvious";  I think both formulations
need some not-completely-trivial thinking to see how (and that)
they work.  I think I find it easier to convince myself that the
non-recursive version works, but of course that's the one I
thought of so naturally I would be likely to think it's easier.

> 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.

Now I see the important point of your earlier comment.  My
attempt at clarifying the problem did not have a parameter for
the modulus, implicitly giving away that the modulus is not
needed.  Probably I should have tried to define the interface
first, and then discover a solution, rather than coding up a
solution and then asking about its interface.  I'll try to
remember that for future reference.

In my case, where I started was thinking of ranges in an unsigned
type, with some upper bound M.  Then we can simply add M-start to
the 'i' and 'end' parameters (using your names), and compare the
residues:

    unsigned delta = M - start;
    return  (i+delta)%M < (end+delta)%M;

Unfortunately this idea doesn't work if M is too close to the
upper limit of the type used.  I briefly considered at a few
variations using comparisons, subtractions, the range of the
type, and so forth, but it was all getting too messy.  Then I
thought, well, either the upper bound is above the lower bound or
it isn't, and I know how to do the case when it is, now how about
the case then it isn't?  I wondered about how to deal with the
potential problem of values (either 'start' or 'end' or 'i')
being "out of range", and finally decided not to care, at which
point I realized that the range matters only for validity
checking, not for getting an answer.  Thus I arrived at the
answer shown above.

I think this problem would make a good interview question,
provided care were taken to phrase it so the subtleties were
still there, but possible points of confusion were reduced.
Not that I know how to do that... :)

Back to comp.programming | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

A little puzzle. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-11-21 20:45 +0000
  Re: A little puzzle. David Brown <david.brown@hesbynett.no> - 2022-11-21 22:06 +0100
    Re: A little puzzle. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-11-21 21:21 +0000
      Re: A little puzzle. David Brown <david.brown@hesbynett.no> - 2022-11-22 09:17 +0100
        Re: A little puzzle. Richard Heathfield <rjh@cpax.org.uk> - 2022-11-22 11:07 +0000
          Re: A little puzzle. David Brown <david.brown@hesbynett.no> - 2022-11-22 15:17 +0100
        Re: A little puzzle. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-11-22 11:40 +0000
          Re: A little puzzle. David Brown <david.brown@hesbynett.no> - 2022-11-22 15:26 +0100
            Re: A little puzzle. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-11-22 15:28 +0000
              Re: A little puzzle. David Brown <david.brown@hesbynett.no> - 2022-11-22 16:30 +0100
      Re: A little puzzle. David Brown <david.brown@hesbynett.no> - 2022-11-22 09:23 +0100
        Re: A little puzzle. Richard Heathfield <rjh@cpax.org.uk> - 2022-11-22 11:08 +0000
        Re: A little puzzle. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-11-22 12:54 +0000
  Re: A little puzzle. Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-11-21 17:39 -0800
    Re: A little puzzle. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-11-22 13:00 +0000
      Re: A little puzzle. Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-11-22 05:31 -0800
  Re: A little puzzle. Richard Heathfield <rjh@cpax.org.uk> - 2022-11-22 11:04 +0000
    Re: A little puzzle. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-11-22 12:53 +0000
      Re: A little puzzle. Paul N <gw7rib@aol.com> - 2022-11-22 05:01 -0800
        Re: A little puzzle. Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-11-22 05:48 -0800
          Re: A little puzzle. David Brown <david.brown@hesbynett.no> - 2022-11-22 15:31 +0100
      FFs [was Re: A little puzzle] Richard Harnden <richard.nospam@gmail.com> - 2022-11-22 16:29 +0000
    Re: A little puzzle. Richard Heathfield <rjh@cpax.org.uk> - 2022-12-14 13:57 +0000
      Re: A little puzzle. Richard Heathfield <rjh@cpax.org.uk> - 2022-12-14 13:58 +0000
      Re: A little puzzle. Richard Heathfield <rjh@cpax.org.uk> - 2022-12-14 16:22 +0000
  Re: A little puzzle. Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-11-22 05:24 -0800
    Re: A little puzzle. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-11-22 15:23 +0000
      Re: A little puzzle. Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-11-23 08:04 -0800
        Re: A little puzzle. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-11-24 00:06 +0000
          Re: A little puzzle. Richard Heathfield <rjh@cpax.org.uk> - 2022-11-24 04:06 +0000
            Re: A little puzzle. David Brown <david.brown@hesbynett.no> - 2022-11-24 09:29 +0100
              Re: A little puzzle. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-11-24 13:23 +0000
            Re: A little puzzle. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-11-24 13:14 +0000
              Re: A little puzzle. Richard Heathfield <rjh@cpax.org.uk> - 2022-11-24 14:00 +0000
                Re: A little puzzle. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-11-24 14:59 +0000
                Re: A little puzzle. Richard Harnden <richard.nospam@gmail.com> - 2022-11-24 19:00 +0000
                Re: A little puzzle. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-11-24 20:36 +0000
              Re: A little puzzle. Andreas <nobody@me.com> - 2022-11-27 00:38 +0100
                Re: A little puzzle. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-11-27 00:11 +0000
          Re: A little puzzle. Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-11-24 14:51 -0800
            Re: A little puzzle. "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> - 2022-11-25 09:30 +0100
              Re: A little puzzle. Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-11-25 01:16 -0800
                Re: A little puzzle. "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> - 2022-11-25 11:11 +0100
                Re: A little puzzle. Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-11-25 06:26 -0800
                Re: A little puzzle. "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> - 2022-11-25 15:59 +0100
            Re: A little puzzle. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-11-26 16:25 +0000
              Re: A little puzzle. "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> - 2022-11-26 19:36 +0100
                Re: A little puzzle. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-11-27 02:03 +0000
                Re: A little puzzle. "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> - 2022-11-27 11:02 +0100
                Re: A little puzzle. Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-12-01 04:30 -0800
                Re: A little puzzle. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-12-02 00:22 +0000
                Re: A little puzzle. Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-12-02 22:30 -0800
                Re: A little puzzle. Julio Di Egidio <julio@diegidio.name> - 2022-12-03 02:43 -0800
              Re: A little puzzle. Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-11-27 18:38 -0800
  Re: A little puzzle. Julio Di Egidio <julio@diegidio.name> - 2022-11-27 02:27 -0800
    Re: A little puzzle. Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-11-27 19:23 -0800
      Re: A little puzzle. Julio Di Egidio <julio@diegidio.name> - 2022-11-27 22:59 -0800
        Re: A little puzzle. Julio Di Egidio <julio@diegidio.name> - 2022-11-27 23:03 -0800
        Re: A little puzzle. Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-11-28 07:22 -0800
          Re: A little puzzle. Julio Di Egidio <julio@diegidio.name> - 2022-11-28 10:20 -0800
            Re: A little puzzle. Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-11-29 04:29 -0800
              Re: A little puzzle. Julio Di Egidio <julio@diegidio.name> - 2022-11-29 04:50 -0800
                Re: A little puzzle. Richard Heathfield <rjh@cpax.org.uk> - 2022-11-29 14:47 +0000
                Re: A little puzzle. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-11-29 17:23 +0000
                Re: A little puzzle. Julio Di Egidio <julio@diegidio.name> - 2022-11-29 09:50 -0800
                Re: A little puzzle. Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-11-30 01:39 -0800
                Re: A little puzzle. Richard Heathfield <rjh@cpax.org.uk> - 2022-11-30 11:10 +0000
                Re: A little puzzle. Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-11-30 02:24 -0800
                Re: A little puzzle. Julio Di Egidio <julio@diegidio.name> - 2022-11-30 06:28 -0800
                Re: A little puzzle. Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-12-01 06:55 -0800
                Re: A little puzzle. Julio Di Egidio <julio@diegidio.name> - 2022-12-02 01:54 -0800
  Re: A little puzzle. Richard Harnden <richard.nospam@gmail.com> - 2022-11-28 11:20 +0000
    Re: A little puzzle. Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-11-28 07:30 -0800
      Re: A little puzzle. Richard Harnden <richard.nospam@gmail.com> - 2022-11-28 17:15 +0000
        Re: A little puzzle. Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-11-29 04:03 -0800
          Re: A little puzzle. Richard Harnden <richard.nospam@gmail.com> - 2022-11-29 15:03 +0000
            Re: A little puzzle. "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> - 2022-11-29 16:13 +0100
              Re: A little puzzle. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-11-29 17:37 +0000
                Re: A little puzzle. "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> - 2022-11-29 19:02 +0100
                Re: A little puzzle. Julio Di Egidio <julio@diegidio.name> - 2022-11-29 10:10 -0800
            Re: A little puzzle. Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-11-30 03:32 -0800
              Re: A little puzzle. Julio Di Egidio <julio@diegidio.name> - 2022-11-30 06:36 -0800
              Re: A little puzzle. Richard Harnden <richard.nospam@gmail.com> - 2022-11-30 15:21 +0000
                Re: A little puzzle. Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-12-01 06:51 -0800

csiph-web