• Re: A little puzzle.

    From David Brown@21:1/5 to Ben Bacarisse on Mon Nov 21 22:06:07 2022
    On 21/11/2022 21:45, Ben Bacarisse wrote:
    I wonder if there are any real posters here? Let's see...

    I came across a trivial programming task that must have been solved a thousand times by other programmers, but it had never crossed my path
    until yesterday. I must be feeling my age because I made a real hash of tackling it at first. Anyway, I thought it might be of interest.

    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 en 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. Do all the news
    readers used by programmers (or ex programmers) all respect the presence
    of a form-feed character...

    ... like this? Because that's my favourite way, rather than posting
    lots of dummy lines before the solution.


    Are there any restrictions, such as sticking to integers? The problem
    becomes quite difficult if your measure is the reals in [0, 1) and your
    "n" is, say, π/4...

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ben Bacarisse@21:1/5 to All on Mon Nov 21 20:45:28 2022
    I wonder if there are any real posters here? Let's see...

    I came across a trivial programming task that must have been solved a
    thousand times by other programmers, but it had never crossed my path
    until yesterday. I must be feeling my age because I made a real hash of tackling it at first. Anyway, I thought it might be of interest.

    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 en 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. Do all the news
    readers used by programmers (or ex programmers) all respect the presence
    of a form-feed character...

    ... like this? Because that's my favourite way, rather than posting
    lots of dummy lines before the solution.

    --
    Ben.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ben Bacarisse@21:1/5 to David Brown on Mon Nov 21 21:21:07 2022
    David Brown <david.brown@hesbynett.no> writes:

    On 21/11/2022 21:45, Ben Bacarisse wrote:
    I wonder if there are any real posters here? Let's see...
    I came across a trivial programming task that must have been solved a
    thousand times by other programmers, but it had never crossed my path
    until yesterday. I must be feeling my age because I made a real hash of
    tackling it at first. Anyway, I thought it might be of interest.
    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 en 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. Do all the news
    readers used by programmers (or ex programmers) all respect the presence
    of a form-feed character...

    ... like this? Because that's my favourite way, rather than posting
    lots of dummy lines before the solution.


    Are there any restrictions, such as sticking to integers? The problem becomes quite difficult if your measure is the reals in [0, 1) and
    your "n" is, say, π/4...

    I don't follow. What is "my" n? I did not mention an n.

    I don't see why the problem can't be naturally extended to a circular
    real interval [0, 1), subject to the fact that we'll use floating point
    numbers for practical purposes. But I don't think this is what you were talking about.

    --
    Ben.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Tim Rentsch@21:1/5 to Ben Bacarisse on Mon Nov 21 17:39:44 2022
    Ben Bacarisse <ben.usenet@bsb.me.uk> writes:

    I wonder if there are any real posters here? Let's see...

    I came across a trivial programming task that must have been solved a thousand times by other programmers, but it had never crossed my path
    until yesterday. I must be feeling my age because I made a real hash of tackling it at first. Anyway, I thought it might be of interest.

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

    I was specifically concerned with integer values where the sub-range
    includes the start value but excludes the end value.

    The question I think you're asking is to write a function like this

    /* is_circularly_between( a, b, c ) -
    * 1 if b is circularly between a and c,
    * 0 otherwise
    * interval [ a, c ) is closed at the 'a' end, 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 ){
    /* to be determined */
    }

    with T being some integer type. Assuming this is right I have
    written such a function (but am not posting it just yet).


    Though I am not sure this merits the term "puzzle", I suggest that
    solutions be posted with some spoiler protection.

    That's good, thank you for the reminder.

    Do all the news
    readers used by programmers (or ex programmers) all respect the presence
    of a form-feed character...
    Next page...

    Mine does, for some values of "respect". It does have the property
    that it hides what follows.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From David Brown@21:1/5 to Ben Bacarisse on Tue Nov 22 09:23:16 2022
    On 21/11/2022 22:21, Ben Bacarisse wrote:
    David Brown <david.brown@hesbynett.no> writes:

    On 21/11/2022 21:45, Ben Bacarisse wrote:
    I wonder if there are any real posters here? Let's see...
    I came across a trivial programming task that must have been solved a
    thousand times by other programmers, but it had never crossed my path
    until yesterday. I must be feeling my age because I made a real hash of >>> tackling it at first. Anyway, I thought it might be of interest.
    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 en 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. Do all the news
    readers used by programmers (or ex programmers) all respect the presence >>> of a form-feed character...

    ... like this? Because that's my favourite way, rather than posting
    lots of dummy lines before the solution.


    Are there any restrictions, such as sticking to integers? The problem
    becomes quite difficult if your measure is the reals in [0, 1) and
    your "n" is, say, π/4...

    I don't follow. What is "my" n? I did not mention an n.

    I don't see why the problem can't be naturally extended to a circular
    real interval [0, 1), subject to the fact that we'll use floating point numbers for practical purposes. But I don't think this is what you were talking about.


    Having now read Tim's post, I see I might have /completely/
    misinterpreted what you wrote. Your "en value" was not a step size "n",
    but a typo for "end value". But then your problem comes down to nothing
    more than a "modulo" function and a comparison, which sounds far too
    simple a "puzzle".

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From David Brown@21:1/5 to Ben Bacarisse on Tue Nov 22 09:17:54 2022
    On 21/11/2022 22:21, Ben Bacarisse wrote:
    David Brown <david.brown@hesbynett.no> writes:

    On 21/11/2022 21:45, Ben Bacarisse wrote:
    I wonder if there are any real posters here? Let's see...
    I came across a trivial programming task that must have been solved a
    thousand times by other programmers, but it had never crossed my path
    until yesterday. I must be feeling my age because I made a real hash of >>> tackling it at first. Anyway, I thought it might be of interest.
    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 en 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. Do all the news
    readers used by programmers (or ex programmers) all respect the presence >>> of a form-feed character...

    ... like this? Because that's my favourite way, rather than posting
    lots of dummy lines before the solution.


    Thunderbird seems to respect it when showing the posts. I am not sure
    of the most convenient way to add one. I'll try copy-and-pasting your
    FF character...



    Did that work?


    Are there any restrictions, such as sticking to integers? The problem
    becomes quite difficult if your measure is the reals in [0, 1) and
    your "n" is, say, π/4...

    I don't follow. What is "my" n? I did not mention an n.

    You referred to a "start value" and an "en value". I like to use names,
    so I'll call the "start value" "a", and use "n" for the "en value". So
    as far as I understand it, you are asking for a function that takes an
    input "x" and determines if there is an integer "i" such that

    x ≡ a + i.n

    where the congruence is over a "wrapping" set.


    I don't see why the problem can't be naturally extended to a circular
    real interval [0, 1), subject to the fact that we'll use floating point numbers for practical purposes. But I don't think this is what you were talking about.


    Well, the point is if "being specifically concerned with integers" means
    the puzzle is limited to integer ranges, or if that is just what you
    were thinking about first. I suppose you /do/ mean sticking to
    integers, because I'd be surprised if a solution were possible once you
    bring arbitrary real numbers into it. That kind of mathematics leads to
    things like the Banach-Tarski paradox, and watching far too many maths
    Youtube videos...

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Richard Heathfield@21:1/5 to David Brown on Tue Nov 22 11:08:01 2022
    On 22/11/2022 8:23 am, David Brown wrote:
    But then your problem comes down to nothing more than a "modulo"
    function and a comparison, which sounds far too simple a "puzzle".

    That's where I ended up too.

    --
    Richard Heathfield
    Email: rjh at cpax dot org dot uk
    "Usenet is a strange place" - dmr 29 July 1999
    Sig line 4 vacant - apply within

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Richard Heathfield@21:1/5 to David Brown on Tue Nov 22 11:07:03 2022
    On 22/11/2022 8:17 am, David Brown wrote:
    On 21/11/2022 22:21, Ben Bacarisse wrote:
    David Brown <david.brown@hesbynett.no> writes:

    On 21/11/2022 21:45, Ben Bacarisse wrote:
    Do all the
    news
    readers used by programmers (or ex programmers) all respect
    the presence
    of a form-feed character...

    ... like this?  Because that's my favourite way, rather than
    posting
    lots of dummy lines before the solution.


    Thunderbird seems to respect it when showing the posts.  I am not
    sure of the most convenient way to add one.  I'll try
    copy-and-pasting your FF character...



    Did that work?

    The answer "no" springs to mind.

    --
    Richard Heathfield
    Email: rjh at cpax dot org dot uk
    "Usenet is a strange place" - dmr 29 July 1999
    Sig line 4 vacant - apply within

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Richard Heathfield@21:1/5 to Ben Bacarisse on Tue Nov 22 11:04:03 2022
    On 21/11/2022 8:45 pm, Ben Bacarisse wrote:
    I wonder if there are any real posters here?

    Nobody here but us chickens.

    Let's see...

    I came across a trivial programming task that must have been solved a thousand times by other programmers, but it had never crossed my path
    until yesterday. I must be feeling my age because I made a real hash of tackling it at first. Anyway, I thought it might be of interest.

    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 en 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. Do all the news
    readers used by programmers (or ex programmers) all respect the presence
    of a form-feed character...

    Dunno. Let's find out:

    Ctrl-L coming up:



    ... like this? Because that's my favourite way, rather than posting
    lots of dummy lines before the solution.

    Your problem is closely related to the very first question I was
    ever posed (in early 1982), by a friend who needed to be able to
    establish cleanly in a single expression whether a keypress was a
    digit (ASCII 48-57). The relevant dialect of BASIC didn't have
    anything like an isdigit function.

    The friend was on the point of giving up on me when I handed him

    ABS(K-52.5)<4.5

    Nowadays we might render this in C as:

    int inrange(int lo, int hi, int k)
    {
    return (k-(lo+hi)/2)<((hi-lo)/2);
    }

    although of course in C the problem would be far better solved as:

    int inrange(int lo, int hi, int k)
    {
    return (lo <= k) && (k < hi);
    }

    or even as a macro.

    I must confess I'm not entirely certain I have correctly
    interpreted your puzzle, which I have taken to mean "is this a
    given value in the given range", but this seems just a bit too
    easy for you to make a hash of, but I'm sure I've made hashes of
    worse. I've missed something, haven't I?

    --
    Richard Heathfield
    Email: rjh at cpax dot org dot uk
    "Usenet is a strange place" - dmr 29 July 1999
    Sig line 4 vacant - apply within

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ben Bacarisse@21:1/5 to David Brown on Tue Nov 22 11:40:10 2022
    David Brown <david.brown@hesbynett.no> writes:

    On 21/11/2022 22:21, Ben Bacarisse wrote:
    David Brown <david.brown@hesbynett.no> writes:

    On 21/11/2022 21:45, Ben Bacarisse wrote:
    I wonder if there are any real posters here? Let's see...
    I came across a trivial programming task that must have been solved a
    thousand times by other programmers, but it had never crossed my path
    until yesterday. I must be feeling my age because I made a real hash of >>>> tackling it at first. Anyway, I thought it might be of interest.
    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 en 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. Do all the news
    readers used by programmers (or ex programmers) all respect the presence >>>> of a form-feed character...

    ... like this? Because that's my favourite way, rather than posting
    lots of dummy lines before the solution.


    Thunderbird seems to respect it when showing the posts. I am not sure of the most convenient way to add one. I'll try copy-and-pasting your
    FF character...



    Did that work?

    Yes, but for my newsreader a form feed only hides what's below it when
    at the start of a line.

    Are there any restrictions, such as sticking to integers? The problem
    becomes quite difficult if your measure is the reals in [0, 1) and
    your "n" is, say, π/4...
    I don't follow. What is "my" n? I did not mention an n.

    You referred to a "start value" and an "en value".

    Sorry, typo. End value.

    I like to use names, so I'll call the "start value" "a", and use "n"
    for the "en value". So as far as I understand it, you are asking for
    a function that takes an input "x" and determines if there is an
    integer "i" such that

    x ≡ a + i.n

    where the congruence is over a "wrapping" set.

    Hmm... I must have made a real hash of the description (and I was being deliberately a bit vague for reasons that should come out later) because
    there is always such an i.

    Here's an example. An event starts at 5 minutes to the hour (start =
    55) and ends at quarter past (end = 15). The x's 55, 58, 3, 12 and so
    on are in the range, 53, 17 and 33 are not.

    The fact that all data are integers is not really material. We could
    consider compass points in [0, 2*pi) and have arbitrary start and end
    bearings.

    I don't see why the problem can't be naturally extended to a circular
    real interval [0, 1), subject to the fact that we'll use floating point
    numbers for practical purposes. But I don't think this is what you were
    talking about.

    Well, the point is if "being specifically concerned with integers"
    means the puzzle is limited to integer ranges, or if that is just what
    you were thinking about first. I suppose you /do/ mean sticking to
    integers, because I'd be surprised if a solution were possible once
    you bring arbitrary real numbers into it. That kind of mathematics
    leads to things like the Banach-Tarski paradox, and watching far too
    many maths Youtube videos...

    I think I've not explained the problem well because there's not that
    rarefied about it!

    --
    Ben.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ben Bacarisse@21:1/5 to David Brown on Tue Nov 22 12:54:50 2022
    David Brown <david.brown@hesbynett.no> writes:

    On 21/11/2022 22:21, Ben Bacarisse wrote:
    David Brown <david.brown@hesbynett.no> writes:

    On 21/11/2022 21:45, Ben Bacarisse wrote:
    I wonder if there are any real posters here? Let's see...
    I came across a trivial programming task that must have been solved a
    thousand times by other programmers, but it had never crossed my path
    until yesterday. I must be feeling my age because I made a real hash of >>>> tackling it at first. Anyway, I thought it might be of interest.
    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 en 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. Do all the news
    readers used by programmers (or ex programmers) all respect the presence >>>> of a form-feed character...

    ... like this? Because that's my favourite way, rather than posting
    lots of dummy lines before the solution.


    Are there any restrictions, such as sticking to integers? The problem
    becomes quite difficult if your measure is the reals in [0, 1) and
    your "n" is, say, π/4...
    I don't follow. What is "my" n? I did not mention an n.
    I don't see why the problem can't be naturally extended to a circular
    real interval [0, 1), subject to the fact that we'll use floating point
    numbers for practical purposes. But I don't think this is what you were
    talking about.

    Having now read Tim's post, I see I might have /completely/
    misinterpreted what you wrote. Your "en value" was not a step size
    "n", but a typo for "end value". But then your problem comes down to
    nothing more than a "modulo" function and a comparison, which sounds
    far too simple a "puzzle".

    What's your solution?

    --
    Ben.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ben Bacarisse@21:1/5 to Tim Rentsch on Tue Nov 22 13:00:58 2022
    Tim Rentsch <tr.17687@z991.linuxsc.com> writes:

    The question I think you're asking is to write a function like this

    Yes, but I deliberately did not present the question with this degree of clarity! I wanted to present the problem as I came across it because
    what little fun it offers comes from clarifying the issue.

    Though I am not sure this merits the term "puzzle", I suggest that
    solutions be posted with some spoiler protection.

    That's good, thank you for the reminder.

    Your explanation of the issue is, to my mind, a teeny tiny spoiler put
    "above the fold".

    Do all the news
    readers used by programmers (or ex programmers) all respect the presence
    of a form-feed character...
    Next page...

    Mine does, for some values of "respect". It does have the property
    that it hides what follows.

    Yes. In the old days (which is only the late 80s in this case) I think
    all newsreaders would require some action to see that part of a message
    after a form feed, so that was how spoilers were put "below the fold".

    --
    Ben.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ben Bacarisse@21:1/5 to Richard Heathfield on Tue Nov 22 12:53:42 2022
    Richard Heathfield <rjh@cpax.org.uk> writes:

    On 21/11/2022 8:45 pm, Ben Bacarisse wrote:
    I wonder if there are any real posters here?

    Nobody here but us chickens.

    Let's see...
    I came across a trivial programming task that must have been solved a
    thousand times by other programmers, but it had never crossed my path
    until yesterday. I must be feeling my age because I made a real hash of
    tackling it at first. Anyway, I thought it might be of interest.
    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 en 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. Do all the news
    readers used by programmers (or ex programmers) all respect the presence
    of a form-feed character...

    Dunno. Let's find out:

    Ctrl-L coming up:




    Didn't see a form feed there. Here's one (I hope):



    although of course in C the problem would be far better solved as:

    int inrange(int lo, int hi, int k)
    {
    return (lo <= k) && (k < hi);
    }

    or even as a macro.

    I must confess I'm not entirely certain I have correctly interpreted
    your puzzle, which I have taken to mean "is this a given value in the
    given range", but this seems just a bit too easy for you to make a
    hash of, but I'm sure I've made hashes of worse. I've missed
    something, haven't I?

    The circular wrapping. On a clock, 55 is in the range of minutes that
    starts at 45 and ends at 5.

    --
    Ben.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Paul N@21:1/5 to Ben Bacarisse on Tue Nov 22 05:01:23 2022
    On Tuesday, November 22, 2022 at 12:53:47 PM UTC, Ben Bacarisse wrote:
    Richard Heathfield <r...@cpax.org.uk> writes:

    On 21/11/2022 8:45 pm, Ben Bacarisse wrote:
    I wonder if there are any real posters here?

    Nobody here but us chickens.

    Let's see...
    I came across a trivial programming task that must have been solved a
    thousand times by other programmers, but it had never crossed my path
    until yesterday. I must be feeling my age because I made a real hash of
    tackling it at first. Anyway, I thought it might be of interest.
    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 en 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. Do all the news
    readers used by programmers (or ex programmers) all respect the presence >> of a form-feed character...

    Dunno. Let's find out:

    Ctrl-L coming up:



    Didn't see a form feed there. Here's one (I hope):
    although of course in C the problem would be far better solved as:

    int inrange(int lo, int hi, int k)
    {
    return (lo <= k) && (k < hi);
    }

    or even as a macro.

    I must confess I'm not entirely certain I have correctly interpreted
    your puzzle, which I have taken to mean "is this a given value in the
    given range", but this seems just a bit too easy for you to make a
    hash of, but I'm sure I've made hashes of worse. I've missed
    something, haven't I?
    The circular wrapping. On a clock, 55 is in the range of minutes that
    starts at 45 and ends at 5.

    What's wrong with - subtract start from both end and value, add the modulus if either is negative, and compare?

    For example, in your example we subtract 45 from 5 (end) and 55 (value) to get -40 (end) and 10 (value). Adjusting gives 20 (end) and 10 (value). 10 is below 20 so we're in the range.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Tim Rentsch@21:1/5 to Ben Bacarisse on Tue Nov 22 05:31:59 2022
    Ben Bacarisse <ben.usenet@bsb.me.uk> writes:

    Tim Rentsch <tr.17687@z991.linuxsc.com> writes:

    The question I think you're asking is to write a function like this

    Yes, but I deliberately did not present the question with this degree of clarity! I wanted to present the problem as I came across it because
    what little fun it offers comes from clarifying the issue.

    Ah ha. I didn't understand that before.

    Though I am not sure this merits the term "puzzle", I suggest that
    solutions be posted with some spoiler protection.

    That's good, thank you for the reminder.

    Your explanation of the issue is, to my mind, a teeny tiny spoiler put
    "above the fold".

    Yes, I realized that only later and after the fact. My apologies.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Tim Rentsch@21:1/5 to Ben Bacarisse on Tue Nov 22 05:24:33 2022
    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)...

    (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;
    }

    This function works if T is any integer type, or any real
    floating-point type, or is a type pointer to any complete
    object type. (Disclaimer: I didn't think carefully about
    the case where T is a pointer to an array type.)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Tim Rentsch@21:1/5 to Paul N on Tue Nov 22 05:48:17 2022
    Paul N <gw7rib@aol.com> writes:

    On Tuesday, November 22, 2022 at 12:53:47 PM UTC, Ben Bacarisse wrote:

    Richard Heathfield <r...@cpax.org.uk> writes:

    [...]

    although of course in C the problem would be far better solved as:

    int inrange(int lo, int hi, int k)
    {
    return (lo <= k) && (k < hi);
    }

    or even as a macro.

    I must confess I'm not entirely certain I have correctly interpreted
    your puzzle, which I have taken to mean "is this a given value in the
    given range", but this seems just a bit too easy for you to make a
    hash of, but I'm sure I've made hashes of worse. I've missed
    something, haven't I?

    The circular wrapping. On a clock, 55 is in the range of minutes that
    starts at 45 and ends at 5.

    What's wrong with - subtract start from both end and value, add the
    modulus if either is negative, and compare?

    Suppose start is 9223372036854775800 and end is -9223372036854775800
    (or the corresponding values for type 'int', those values are for a
    64-bit signed integer type). The subtraction gives undefined behavior.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From David Brown@21:1/5 to Richard Heathfield on Tue Nov 22 15:17:16 2022
    On 22/11/2022 12:07, Richard Heathfield wrote:
    On 22/11/2022 8:17 am, David Brown wrote:
    On 21/11/2022 22:21, Ben Bacarisse wrote:
    David Brown <david.brown@hesbynett.no> writes:

    On 21/11/2022 21:45, Ben Bacarisse wrote:
    Do all the news
    readers used by programmers (or ex programmers) all respect the
    presence
    of a form-feed character...

    ... like this?  Because that's my favourite way, rather than posting >>>>> lots of dummy lines before the solution.


    Thunderbird seems to respect it when showing the posts.  I am not sure
    of the most convenient way to add one.  I'll try copy-and-pasting your
    FF character...



    Did that work?

    The answer "no" springs to mind.


    It worked fine for me, in the sense that regardless of the size of the Thunderbird window used to view the message, everything after each FF
    was just beyond view until I scrolled down. When replying to the
    message, Thunderbird compacts it all.

    What happens in your newsreader? You also have Thunderbird, albeit a
    slightly newer version than mine.


    But for Ben's newsreader to be happy, it seems the FF needs to be at the
    start of a line.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From David Brown@21:1/5 to Ben Bacarisse on Tue Nov 22 15:26:46 2022
    On 22/11/2022 12:40, Ben Bacarisse wrote:
    David Brown <david.brown@hesbynett.no> writes:

    On 21/11/2022 22:21, Ben Bacarisse wrote:
    David Brown <david.brown@hesbynett.no> writes:


    (We don't need secrets here, I think, so I'll snip the FF's.)

    Are there any restrictions, such as sticking to integers? The problem >>>> becomes quite difficult if your measure is the reals in [0, 1) and
    your "n" is, say, π/4...
    I don't follow. What is "my" n? I did not mention an n.

    You referred to a "start value" and an "en value".

    Sorry, typo. End value.

    I like to use names, so I'll call the "start value" "a", and use "n"
    for the "en value". So as far as I understand it, you are asking for
    a function that takes an input "x" and determines if there is an
    integer "i" such that

    x ≡ a + i.n

    where the congruence is over a "wrapping" set.

    Hmm... I must have made a real hash of the description (and I was being deliberately a bit vague for reasons that should come out later) because there is always such an i.

    Maybe I had formed an impression of the problem too early, and
    interpreted everything you wrote to fit that idea. Certainly for my interpretation, there is not always such an "i". (For example, use
    modulo 24, with a = 0, n = 4, and x = 1. No "i" can be found. It's all
    about cyclic groups and subgroups. Since I have no doubt at all that
    you know this, I assume I am misinterpreting you again :-) )


    Here's an example. An event starts at 5 minutes to the hour (start =
    55) and ends at quarter past (end = 15). The x's 55, 58, 3, 12 and so
    on are in the range, 53, 17 and 33 are not.


    OK. We are talking ranges, rather than points.

    The fact that all data are integers is not really material. We could consider compass points in [0, 2*pi) and have arbitrary start and end bearings.

    I don't see why the problem can't be naturally extended to a circular
    real interval [0, 1), subject to the fact that we'll use floating point
    numbers for practical purposes. But I don't think this is what you were >>> talking about.

    Well, the point is if "being specifically concerned with integers"
    means the puzzle is limited to integer ranges, or if that is just what
    you were thinking about first. I suppose you /do/ mean sticking to
    integers, because I'd be surprised if a solution were possible once
    you bring arbitrary real numbers into it. That kind of mathematics
    leads to things like the Banach-Tarski paradox, and watching far too
    many maths Youtube videos...

    I think I've not explained the problem well because there's not that
    rarefied about it!


    Surely it is not that unusual to enjoy things like this?

    <https://www.youtube.com/watch?v=s86-Z-CbaHA>

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From David Brown@21:1/5 to Tim Rentsch on Tue Nov 22 15:31:05 2022
    On 22/11/2022 14:48, Tim Rentsch wrote:
    Paul N <gw7rib@aol.com> writes:

    On Tuesday, November 22, 2022 at 12:53:47 PM UTC, Ben Bacarisse wrote:

    Richard Heathfield <r...@cpax.org.uk> writes:

    [...]

    although of course in C the problem would be far better solved as:

    int inrange(int lo, int hi, int k)
    {
    return (lo <= k) && (k < hi);
    }

    or even as a macro.

    I must confess I'm not entirely certain I have correctly interpreted
    your puzzle, which I have taken to mean "is this a given value in the
    given range", but this seems just a bit too easy for you to make a
    hash of, but I'm sure I've made hashes of worse. I've missed
    something, haven't I?

    The circular wrapping. On a clock, 55 is in the range of minutes that
    starts at 45 and ends at 5.

    What's wrong with - subtract start from both end and value, add the
    modulus if either is negative, and compare?

    Suppose start is 9223372036854775800 and end is -9223372036854775800
    (or the corresponding values for type 'int', those values are for a
    64-bit signed integer type). The subtraction gives undefined behavior.

    It's all modulo arithmetic - you can do it all as unsigned types.

    Or use Python and be happy - this is, after all, comp.programming and
    not comp.lang.c !

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ben Bacarisse@21:1/5 to Tim Rentsch on Tue Nov 22 15:23:56 2022
    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.

    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: fsxNet Usenet Gateway (21:1/5)
  • From Ben Bacarisse@21:1/5 to David Brown on Tue Nov 22 15:28:45 2022
    David Brown <david.brown@hesbynett.no> writes:

    OK. We are talking ranges, rather than points.

    Yes. So much confusion from a missing 'd'! Sorry.

    --
    Ben.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From David Brown@21:1/5 to Ben Bacarisse on Tue Nov 22 16:30:59 2022
    On 22/11/2022 16:28, Ben Bacarisse wrote:
    David Brown <david.brown@hesbynett.no> writes:

    OK. We are talking ranges, rather than points.

    Yes. So much confusion from a missing 'd'! Sorry.


    Yes, but it made the thread more fun!

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Tim Rentsch@21:1/5 to Ben Bacarisse on Wed Nov 23 08:04:34 2022
    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... :)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ben Bacarisse@21:1/5 to Tim Rentsch on Thu Nov 24 00:06:35 2022
    Tim Rentsch <tr.17687@z991.linuxsc.com> writes:

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

    I didn't make a good job of presenting it. It certainly didn't pique
    anyone else's interest, but then comp.programming is not well populated.

    One thing that struck me was that I had not come across this before. I
    was surprised that this was not one of those idioms that one absorbs
    along the way. I suppose it is of limited use.

    --
    Ben.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Richard Heathfield@21:1/5 to Ben Bacarisse on Thu Nov 24 04:06:01 2022
    On 24/11/2022 12:06 am, Ben Bacarisse wrote:
    Tim Rentsch <tr.17687@z991.linuxsc.com> writes:

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

    I didn't make a good job of presenting it. It certainly didn't pique
    anyone else's interest, but then comp.programming is not well populated.

    One thing that struck me was that I had not come across this before. I
    was surprised that this was not one of those idioms that one absorbs
    along the way. I suppose it is of limited use.

    The trouble is that it comes across as "is y >= x and <= z?",
    which is about as simple as it gets.

    --
    Richard Heathfield
    Email: rjh at cpax dot org dot uk
    "Usenet is a strange place" - dmr 29 July 1999
    Sig line 4 vacant - apply within

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From David Brown@21:1/5 to Richard Heathfield on Thu Nov 24 09:29:59 2022
    On 24/11/2022 05:06, Richard Heathfield wrote:
    On 24/11/2022 12:06 am, Ben Bacarisse wrote:
    Tim Rentsch <tr.17687@z991.linuxsc.com> writes:

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

    I didn't make a good job of presenting it.  It certainly didn't pique
    anyone else's interest, but then comp.programming is not well populated.

    One thing that struck me was that I had not come across this before.  I
    was surprised that this was not one of those idioms that one absorbs
    along the way.  I suppose it is of limited use.

    The trouble is that it comes across as "is y >= x and <= z?", which is
    about as simple as it gets.


    Some of us managed to misinterpret the post as being about as complex as
    it gets!

    I use circular buffers a great deal in my coding. For many of my
    systems, there are UART communication ports (such as for debugging
    output - when you don't have a screen or standard output, a UART does
    the job). There is typically a circular buffer which is piped out to
    the port via interrupt routines, and when the application code wants to
    "print" out a message, it gets pushed into the buffer.

    So the kind of thought needed for Ben's "puzzle" turns up in real code,
    and I've seen people get it wrong.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ben Bacarisse@21:1/5 to Richard Heathfield on Thu Nov 24 13:14:01 2022
    Richard Heathfield <rjh@cpax.org.uk> writes:

    On 24/11/2022 12:06 am, Ben Bacarisse wrote:
    Tim Rentsch <tr.17687@z991.linuxsc.com> writes:

    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... :)
    I didn't make a good job of presenting it. It certainly didn't pique
    anyone else's interest, but then comp.programming is not well populated.
    One thing that struck me was that I had not come across this before. I
    was surprised that this was not one of those idioms that one absorbs
    along the way. I suppose it is of limited use.

    The trouble is that it comes across as "is y >= x and <= z?", which is
    about as simple as it gets.

    I am saddened that you think I would have made a hash of that and amazed
    that you could think I had never have come across such a thing
    before. :-(

    I would have thought that

    "Consider any ordered measure that "wraps round" -- bearings in
    degrees, minutes in the hour, indeed hours in either the 12 or 24 hour
    clock."

    might have suggested it was not any old start <= x < end problem. How
    would you have phrased it so as to avoid the confusion?

    Anyway, the take-away is that the size of the range is not part of the
    problem and that no modulo operations are involved. I found that mildly interesting.

    --
    Ben.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Richard Heathfield@21:1/5 to Ben Bacarisse on Thu Nov 24 14:00:07 2022
    On 24/11/2022 1:14 pm, Ben Bacarisse wrote:
    Richard Heathfield <rjh@cpax.org.uk> writes:

    On 24/11/2022 12:06 am, Ben Bacarisse wrote:
    Tim Rentsch <tr.17687@z991.linuxsc.com> writes:

    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... :)
    I didn't make a good job of presenting it. It certainly didn't pique
    anyone else's interest, but then comp.programming is not well populated. >>> One thing that struck me was that I had not come across this before. I
    was surprised that this was not one of those idioms that one absorbs
    along the way. I suppose it is of limited use.

    The trouble is that it comes across as "is y >= x and <= z?", which is
    about as simple as it gets.

    I am saddened that you think I would have made a hash of that and amazed
    that you could think I had never have come across such a thing
    before. :-(

    Well, of course I don't think that. But that's how it reads,
    that's all. It couldn't be what you meant, but I'm not a mind reader.

    I would have thought that

    "Consider any ordered measure that "wraps round" -- bearings in
    degrees, minutes in the hour, indeed hours in either the 12 or 24 hour
    clock."

    might have suggested it was not any old start <= x < end problem.

    It suggested modulo to me.

    How
    would you have phrased it so as to avoid the confusion?

    That depends on what you mean, which is evidently now clear to
    others, but not yet to me.

    Anyway, the take-away is that the size of the range is not part of the problem and that no modulo operations are involved. I found that mildly interesting.


    Okay.

    --
    Richard Heathfield
    Email: rjh at cpax dot org dot uk
    "Usenet is a strange place" - dmr 29 July 1999
    Sig line 4 vacant - apply within

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ben Bacarisse@21:1/5 to David Brown on Thu Nov 24 13:23:51 2022
    David Brown <david.brown@hesbynett.no> writes:

    I use circular buffers a great deal in my coding. For many of my
    systems, there are UART communication ports (such as for debugging
    output - when you don't have a screen or standard output, a UART does
    the job). There is typically a circular buffer which is piped out to
    the port via interrupt routines, and when the application code wants
    to "print" out a message, it gets pushed into the buffer.

    Yes, and I've used circular buffers before, but always as queues so
    there was never a need to know if an index (or pointer) is in the filled
    or unfilled region. One just had to avoid confusing a full buffer with
    an empty one.

    So the kind of thought needed for Ben's "puzzle" turns up in real
    code, and I've seen people get it wrong.

    What's the modulo solution you had in mind?

    --
    Ben.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ben Bacarisse@21:1/5 to Richard Heathfield on Thu Nov 24 14:59:08 2022
    Richard Heathfield <rjh@cpax.org.uk> writes:

    On 24/11/2022 1:14 pm, Ben Bacarisse wrote:
    Richard Heathfield <rjh@cpax.org.uk> writes:

    On 24/11/2022 12:06 am, Ben Bacarisse wrote:
    Tim Rentsch <tr.17687@z991.linuxsc.com> writes:

    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... :)
    I didn't make a good job of presenting it. It certainly didn't pique
    anyone else's interest, but then comp.programming is not well populated. >>>> One thing that struck me was that I had not come across this before. I >>>> was surprised that this was not one of those idioms that one absorbs
    along the way. I suppose it is of limited use.

    The trouble is that it comes across as "is y >= x and <= z?", which is
    about as simple as it gets.
    I am saddened that you think I would have made a hash of that and amazed
    that you could think I had never have come across such a thing
    before. :-(

    Well, of course I don't think that. But that's how it reads, that's
    all. It couldn't be what you meant, but I'm not a mind reader.

    I hope you can help me express it better.

    I would have thought that
    "Consider any ordered measure that "wraps round" -- bearings in
    degrees, minutes in the hour, indeed hours in either the 12 or 24 hour
    clock."
    might have suggested it was not any old start <= x < end problem.

    It suggested modulo to me.

    That quote was from the first post -- the one that reads to you as if I
    meant a plain start <= x < end.

    How
    would you have phrased it so as to avoid the confusion?

    That depends on what you mean, which is evidently now clear to others,
    but not yet to me.

    There's only been one solution, so I'm not 100% anyone else knows what
    the question was!

    I'd like to find a way to pin it down in case I ever want to express the problem to someone else. The case in point related to timed events
    where I only know the start and end minutes. I needed to test if the
    current time's minutes was in any of the events. So if there were three
    events running from

    10 to 25, 30 to 50 and 55 to 05

    minutes past the hour then at 15 past we are in event 1. At 35 past we
    are not in any event, and at 03 we are in event 3.

    I really want to find a way of explaining this that avoids too many
    specifics because any one case is likely to raise a lot of questions.
    The generic part is that there is some measure, with a direction or
    order, that wraps round, and we want to test for sub-ranges of that
    measure.

    --
    Ben.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Richard Harnden@21:1/5 to Ben Bacarisse on Thu Nov 24 19:00:32 2022
    On 24/11/2022 14:59, Ben Bacarisse wrote:


    I'd like to find a way to pin it down in case I ever want to express the problem to someone else. The case in point related to timed events
    where I only know the start and end minutes. I needed to test if the
    current time's minutes was in any of the events. So if there were three events running from

    10 to 25, 30 to 50 and 55 to 05

    minutes past the hour then at 15 past we are in event 1. At 35 past we
    are not in any event, and at 03 we are in event 3.

    Isn't 35 within event 2? Or am I missing something?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ben Bacarisse@21:1/5 to Richard Harnden on Thu Nov 24 20:36:19 2022
    Richard Harnden <richard.nospam@gmail.com> writes:

    On 24/11/2022 14:59, Ben Bacarisse wrote:

    I'd like to find a way to pin it down in case I ever want to express the
    problem to someone else. The case in point related to timed events
    where I only know the start and end minutes. I needed to test if the
    current time's minutes was in any of the events. So if there were three
    events running from
    10 to 25, 30 to 50 and 55 to 05
    minutes past the hour then at 15 past we are in event 1. At 35 past we
    are not in any event, and at 03 we are in event 3.

    Isn't 35 within event 2? Or am I missing something?

    Yes! I stupidly went and edited the times because I'd chosen very odd
    ones and then didn't check.

    You clearly see the question now, so do you have advice on how I could
    word it so as to avid your initial view of what was asking?

    --
    Ben.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Tim Rentsch@21:1/5 to Ben Bacarisse on Thu Nov 24 14:51:21 2022
    Ben Bacarisse <ben.usenet@bsb.me.uk> writes:

    Tim Rentsch <tr.17687@z991.linuxsc.com> writes:

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

    I didn't make a good job of presenting it. It certainly didn't
    pique anyone else's interest, but then comp.programming is not
    well populated.

    I think part of the problem is that the question looks "easier"
    than it really is. People have an initial reaction (certainly I
    know I did) but don't continue to think through what the issues
    might be.

    For what it's worth, here is a stab at presenting the problem.
    It's longer than I would like it to be, but maybe it will be
    helpful.

    We are interested in writing a function to answer a more general
    version of the example problem stated below.

    Consider a plane flying easterly (and for simplicity, directly
    above the equator). We measure the plane's progress by its
    longitude, measured to the nearest second of arc (1/3600 of a
    degree), so from -180 degrees to 179 degrees 59 minutes 59
    seconds (or equivalently from -648000 seconds to 647999 seconds).
    We would like to know if a given longitude is between the plane's
    starting longitude and ending longitude. (Note that the
    longitude measurements below are given in degrees, minutes, and
    seconds, but we may assume that internally they are represented
    as an integer number of seconds.)

    For example, if the plane flies

    from Los Angeles, US (longitude -118 14' 37")
    to New York, US (longitude -73 56' 7")

    does it cross over the longitude lines of

    Denver, US (longitude -104 59' 30"), or
    Moscow, Russia (longitude 37 37' 6")?

    Answer: the plane does cross the longitude line of Denver but not
    that of Moscow.

    Similarly, if the plane flies

    from Beijing, China (longitude 116 23' 0")
    to Los Angeles, US (longitude -118 14' 37")

    does it cross over

    Tokyo, Japan (longitude 139 50' 32"),
    Waikiki, Hawaii, US (longitude -157 50' 4"), or
    Moscow, Russia (longitude 37 37' 6")?

    Answer: the plane does cross the longitude lines of Tokyo and
    Waikiki but not that of Moscow.

    For the general case we would like to handle any circular measure
    (for convenience having coordinates in some integer range).

    Exercise: write a function to answer this kind of question for
    circular measures in general. You may assume integer coordinates
    and intervals that include the starting point but do not include
    the end point. Give a suitable declaration for the function,
    and separately give a function definition to implement the given
    interface.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Dmitry A. Kazakov@21:1/5 to Tim Rentsch on Fri Nov 25 09:30:00 2022
    On 2022-11-24 23:51, Tim Rentsch wrote:

    For the general case we would like to handle any circular measure
    (for convenience having coordinates in some integer range).

    Not necessarily circular. The same issue arise with overflowing
    counters. E.g. the typical case when you have some real-time clock
    counter which periodically runs over and you have to detect overflows.

    Exercise: write a function to answer this kind of question for
    circular measures in general. You may assume integer coordinates
    and intervals that include the starting point but do not include
    the end point. Give a suitable declaration for the function,
    and separately give a function definition to implement the given
    interface.

    Usual technique is expanding the range this or that way. E.g.

    - Computing differences in the specified direction and complementing by
    the modulo when the difference turns negative.
    - Using large non-modular numbers that never overflow, e.g. with indices
    of a ring buffer. A 64-bit sequence number I is the new "index." To
    access elements I mod N is used.

    --
    Regards,
    Dmitry A. Kazakov
    http://www.dmitry-kazakov.de

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Tim Rentsch@21:1/5 to Dmitry A. Kazakov on Fri Nov 25 01:16:19 2022
    "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> writes:

    On 2022-11-24 23:51, Tim Rentsch wrote:

    For the general case we would like to handle any circular measure
    (for convenience having coordinates in some integer range).
    [...]
    Exercise: write a function to answer this kind of question for
    circular measures in general. You may assume integer coordinates
    and intervals that include the starting point but do not include
    the end point. Give a suitable declaration for the function,
    and separately give a function definition to implement the given
    interface.

    Usual technique is expanding the range this or that way. E.g.

    - Computing differences in the specified direction and complementing
    by the modulo when the difference turns negative.
    - Using large non-modular numbers that never overflow, e.g. with
    indices of a ring buffer. A 64-bit sequence number I is the new
    "index." To access elements I mod N is used.

    Even Edsgar Dijkstra wrote code to show solutions to
    programming exercises. Where is your code?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Dmitry A. Kazakov@21:1/5 to Tim Rentsch on Fri Nov 25 11:11:05 2022
    On 2022-11-25 10:16, Tim Rentsch wrote:
    "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> writes:

    On 2022-11-24 23:51, Tim Rentsch wrote:

    For the general case we would like to handle any circular measure
    (for convenience having coordinates in some integer range).
    [...]
    Exercise: write a function to answer this kind of question for
    circular measures in general. You may assume integer coordinates
    and intervals that include the starting point but do not include
    the end point. Give a suitable declaration for the function,
    and separately give a function definition to implement the given
    interface.

    Usual technique is expanding the range this or that way. E.g.

    - Computing differences in the specified direction and complementing
    by the modulo when the difference turns negative.
    - Using large non-modular numbers that never overflow, e.g. with
    indices of a ring buffer. A 64-bit sequence number I is the new
    "index." To access elements I mod N is used.

    Even Edsgar Dijkstra wrote code to show solutions to
    programming exercises. Where is your code?

    If you ask for code rather than for an algorithm, you must formalize you question, by providing written specifications in the programming
    language of choice. E.g. specify the ADT longtitude and the operations
    you want to implement on it, e.g. range comparisons.

    --
    Regards,
    Dmitry A. Kazakov
    http://www.dmitry-kazakov.de

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Tim Rentsch@21:1/5 to Dmitry A. Kazakov on Fri Nov 25 06:26:53 2022
    "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> writes:

    On 2022-11-25 10:16, Tim Rentsch wrote:

    "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> writes:

    On 2022-11-24 23:51, Tim Rentsch wrote:

    For the general case we would like to handle any circular measure
    (for convenience having coordinates in some integer range).

    [...]

    Exercise: write a function to answer this kind of question for
    circular measures in general. You may assume integer coordinates
    and intervals that include the starting point but do not include
    the end point. Give a suitable declaration for the function,
    and separately give a function definition to implement the given
    interface.

    Usual technique is expanding the range this or that way. E.g.

    - Computing differences in the specified direction and complementing
    by the modulo when the difference turns negative.
    - Using large non-modular numbers that never overflow, e.g. with
    indices of a ring buffer. A 64-bit sequence number I is the new
    "index." To access elements I mod N is used.

    Even Edsgar Dijkstra wrote code to show solutions to
    programming exercises. Where is your code?

    If you ask for code rather than for an algorithm, you must formalize
    you question, by providing written specifications in the programming
    language of choice. E.g. specify the ADT longtitude and the operations
    you want to implement on it, e.g. range comparisons.

    Apparently you don't realize that part of the problem is to
    define the interface, not just implement it. For the programming
    language, pick any common, widely used language, as for example
    Ada or C. For the type of the values involved, pick any standard
    integer data type that covers a large range of possible values,
    such as 'intmax_t' in C. "Give a suitable declaration for the
    function" means both (a) supplying an appropriate prototype
    (using C terminology), and (b) either writing a comment or
    choosing adequately descriptive parameters names for whatever
    argument values are needed. "Give a function definition to
    implement the given interface" means supplying a complete
    function definition that is compatible with the previously
    supplied declaration, and that computes the desired result.

    Now once again, where is your code?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Dmitry A. Kazakov@21:1/5 to Tim Rentsch on Fri Nov 25 15:59:03 2022
    On 2022-11-25 15:26, Tim Rentsch wrote:
    "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> writes:

    On 2022-11-25 10:16, Tim Rentsch wrote:

    "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> writes:

    On 2022-11-24 23:51, Tim Rentsch wrote:

    For the general case we would like to handle any circular measure
    (for convenience having coordinates in some integer range).

    [...]

    Exercise: write a function to answer this kind of question for
    circular measures in general. You may assume integer coordinates
    and intervals that include the starting point but do not include
    the end point. Give a suitable declaration for the function,
    and separately give a function definition to implement the given
    interface.

    Usual technique is expanding the range this or that way. E.g.

    - Computing differences in the specified direction and complementing
    by the modulo when the difference turns negative.
    - Using large non-modular numbers that never overflow, e.g. with
    indices of a ring buffer. A 64-bit sequence number I is the new
    "index." To access elements I mod N is used.

    Even Edsgar Dijkstra wrote code to show solutions to
    programming exercises. Where is your code?

    If you ask for code rather than for an algorithm, you must formalize
    you question, by providing written specifications in the programming
    language of choice. E.g. specify the ADT longtitude and the operations
    you want to implement on it, e.g. range comparisons.

    Apparently you don't realize that part of the problem is to
    define the interface, not just implement it.

    Why do you expect code if you cannot formulate the problem?

    For the programming
    language, pick any common, widely used language, as for example
    Ada or C. For the type of the values involved, pick any standard
    integer data type that covers a large range of possible values,
    such as 'intmax_t' in C. "Give a suitable declaration for the
    function" means both (a) supplying an appropriate prototype
    (using C terminology), and (b) either writing a comment or
    choosing adequately descriptive parameters names for whatever
    argument values are needed. "Give a function definition to
    implement the given interface" means supplying a complete
    function definition that is compatible with the previously
    supplied declaration, and that computes the desired result.

    Now once again, where is your code?

    Since you didn't said what you want, here is an implementation of an
    advanced ring buffer with a defined order on its elements that survives overriding them in the buffer:

    http://www.dmitry-kazakov.de/ada/components.htm#10.2

    The source is freely available.

    (Ordering elements in a ring of module is the general problem of which
    what you wanted is a special case, I guess)

    --
    Regards,
    Dmitry A. Kazakov
    http://www.dmitry-kazakov.de

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ben Bacarisse@21:1/5 to Tim Rentsch on Sat Nov 26 16:25:29 2022
    Tim Rentsch <tr.17687@z991.linuxsc.com> writes:

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

    Tim Rentsch <tr.17687@z991.linuxsc.com> writes:

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

    I didn't make a good job of presenting it. It certainly didn't
    pique anyone else's interest, but then comp.programming is not
    well populated.

    I think part of the problem is that the question looks "easier"
    than it really is. People have an initial reaction (certainly I
    know I did) but don't continue to think through what the issues
    might be.

    I think so. I am, to be honest, rather disappointed by the reception
    this problem has had. Even after various examples and two explanations,
    some posters still don't know what's being asked, but at the time it is
    claimed that it's trivial and not worth coding up. Sometimes both
    claims are made by the same poster.

    For what it's worth, here is a stab at presenting the problem.
    It's longer than I would like it to be, but maybe it will be
    helpful.

    We are interested in writing a function to answer a more general
    version of the example problem stated below.

    Consider a plane flying easterly (and for simplicity, directly
    above the equator). We measure the plane's progress by its
    longitude, measured to the nearest second of arc (1/3600 of a
    degree), so from -180 degrees to 179 degrees 59 minutes 59
    seconds (or equivalently from -648000 seconds to 647999 seconds).
    We would like to know if a given longitude is between the plane's
    starting longitude and ending longitude. (Note that the
    longitude measurements below are given in degrees, minutes, and
    seconds, but we may assume that internally they are represented
    as an integer number of seconds.)

    For example, if the plane flies

    from Los Angeles, US (longitude -118 14' 37")
    to New York, US (longitude -73 56' 7")

    does it cross over the longitude lines of

    Denver, US (longitude -104 59' 30"), or
    Moscow, Russia (longitude 37 37' 6")?

    Answer: the plane does cross the longitude line of Denver but not
    that of Moscow.

    Similarly, if the plane flies

    from Beijing, China (longitude 116 23' 0")
    to Los Angeles, US (longitude -118 14' 37")

    does it cross over

    Tokyo, Japan (longitude 139 50' 32"),
    Waikiki, Hawaii, US (longitude -157 50' 4"), or
    Moscow, Russia (longitude 37 37' 6")?

    Answer: the plane does cross the longitude lines of Tokyo and
    Waikiki but not that of Moscow.

    For the general case we would like to handle any circular measure
    (for convenience having coordinates in some integer range).

    Exercise: write a function to answer this kind of question for
    circular measures in general. You may assume integer coordinates
    and intervals that include the starting point but do not include
    the end point. Give a suitable declaration for the function,
    and separately give a function definition to implement the given
    interface.

    I think this is good, though the example with seconds of arc may appear
    fiddly to some people. Degrees would do, but then the examples would
    become a bit fuzzy.

    I would probably use times of day. For example, my cheap "off-peak" electricity is from 22:00 to 00:30 and from 02:30 to 7:30 but there are
    all sorts possible examples: times of day when I do not want to be
    disturbed, times when particular parking rules apply and so on.

    The problem would then be to determine, given a time of day (in minutes
    past midnight), if that time is within a particular period.

    I don't think it's a spoiler to give, first, a very concrete example,
    even including the function interface. A second part of the question
    would be by ask about generalising this interface to any "circular"
    measure.

    So I don't think it would matter to help DAK by giving

    with Ada.Text_IO;
    use Ada.Text_IO;

    procedure Longitude is

    function Flight_East_Crosses_Longitude
    (Start_Seconds, End_Seconds, Longitude : Integer) return Boolean is
    begin
    -- Your code here
    end Flight_East_Crosses_Longitude;

    Beijing : constant Integer := ((+116 * 60) + 23) * 60 + 0;
    Los_Angeles : constant Integer := ((-118 * 60) + 14) * 60 + 37;
    Tokyo : constant Integer := ((+139 * 60) + 50) * 60 + 32;
    Waikiki : constant Integer := ((-157 * 60) + 50) * 60 + 4;
    Moscow : constant Integer := ((+037 * 60) + 37) * 60 + 6;

    begin
    Put_Line(Flight_East_Crosses_Longitude(Beijing, Los_Angeles, Tokyo)'img);
    Put_Line(Flight_East_Crosses_Longitude(Beijing, Los_Angeles, Waikiki)'img);
    Put_Line(Flight_East_Crosses_Longitude(Beijing, Los_Angeles, Moscow)'img);
    end Longitude;

    Of course, Ada is one of the few languages where one can actually
    implement largely type-generic functions, so it's a shame to be this
    specific, but as I say, that could come later.

    --
    Ben.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Dmitry A. Kazakov@21:1/5 to Ben Bacarisse on Sat Nov 26 19:36:30 2022
    On 2022-11-26 17:25, Ben Bacarisse wrote:
    Tim Rentsch <tr.17687@z991.linuxsc.com> writes:

    Consider a plane flying easterly (and for simplicity, directly
    above the equator). We measure the plane's progress by its
    longitude, measured to the nearest second of arc (1/3600 of a
    degree), so from -180 degrees to 179 degrees 59 minutes 59
    seconds (or equivalently from -648000 seconds to 647999 seconds).
    We would like to know if a given longitude is between the plane's
    starting longitude and ending longitude. (Note that the
    longitude measurements below are given in degrees, minutes, and
    seconds, but we may assume that internally they are represented
    as an integer number of seconds.)

    For example, if the plane flies

    from Los Angeles, US (longitude -118 14' 37")
    to New York, US (longitude -73 56' 7")

    does it cross over the longitude lines of

    Denver, US (longitude -104 59' 30"), or
    Moscow, Russia (longitude 37 37' 6")?

    Answer: the plane does cross the longitude line of Denver but not
    that of Moscow.

    Similarly, if the plane flies

    from Beijing, China (longitude 116 23' 0")
    to Los Angeles, US (longitude -118 14' 37")

    does it cross over

    Tokyo, Japan (longitude 139 50' 32"),
    Waikiki, Hawaii, US (longitude -157 50' 4"), or
    Moscow, Russia (longitude 37 37' 6")?

    Answer: the plane does cross the longitude lines of Tokyo and
    Waikiki but not that of Moscow.

    So I don't think it would matter to help DAK by giving

    with Ada.Text_IO;
    use Ada.Text_IO;

    procedure Longitude is

    function Flight_East_Crosses_Longitude
    (Start_Seconds, End_Seconds, Longitude : Integer) return Boolean is
    begin
    -- Your code here
    end Flight_East_Crosses_Longitude;

    Beijing : constant Integer := ((+116 * 60) + 23) * 60 + 0;
    Los_Angeles : constant Integer := ((-118 * 60) + 14) * 60 + 37;
    Tokyo : constant Integer := ((+139 * 60) + 50) * 60 + 32;
    Waikiki : constant Integer := ((-157 * 60) + 50) * 60 + 4;
    Moscow : constant Integer := ((+037 * 60) + 37) * 60 + 6;

    begin
    Put_Line(Flight_East_Crosses_Longitude(Beijing, Los_Angeles, Tokyo)'img);
    Put_Line(Flight_East_Crosses_Longitude(Beijing, Los_Angeles, Waikiki)'img);
    Put_Line(Flight_East_Crosses_Longitude(Beijing, Los_Angeles, Moscow)'img);
    end Longitude;

    Of course, Ada is one of the few languages where one can actually
    implement largely type-generic functions, so it's a shame to be this specific, but as I say, that could come later.

    Here is an implementation based on an integer type. Usually one would
    deploy a fixed-point type instead, but I don't want to mud the details.
    Also a ring buffer would use a modular type, but then it must start at
    0, and here we start at -180 * 3600 seconds. --------------------------------------------------
    with Ada.Text_IO; use Ada.Text_IO;

    procedure Test_Longtitude is
    --
    -- Longtitude in seconds
    --
    type Longtitude is range -180 * 3600 .. 180 * 3600 - 1;

    type Degree is range -180..180;
    type Minute is range 0..59;
    type Second is range 0..59;

    function Compose (D : Degree; M : Minute; S : Second)
    return Longtitude is
    Result : constant Integer :=
    (Integer (D) * 60 + Integer (M)) * 60 + Integer (S);
    begin
    if Result <= Integer (Longtitude'Last) then
    return Longtitude (Result);
    else
    return Longtitude (Result - 360 * 3600);
    end if;
    end Compose;

    Beijing : constant Longtitude := Compose (+116, 23, 0);
    Los_Angeles : constant Longtitude := Compose (-118, 14, 37);
    Denver : constant Longtitude := Compose (-104, 59, 30);
    New_York : constant Longtitude := Compose ( -73, 56, 7);
    Tokyo : constant Longtitude := Compose (+139, 50, 32);
    Waikiki : constant Longtitude := Compose (-157, 50, 4);
    Moscow : constant Longtitude := Compose (+037, 37, 6);

    function Flight_East_Crosses_Longitude
    ( Start, Stop, X : Longtitude
    ) return Boolean is
    begin
    if Start <= Stop then
    return X in Start..Stop;
    else
    return X <= Stop or else X >= Start;
    end if;
    end Flight_East_Crosses_Longitude;
    begin
    Put_Line
    ( "Los_Angeles to New_York over Denver "
    & Flight_East_Crosses_Longitude (Los_Angeles, New_York, Denver)'Image
    );
    Put_Line
    ( "Los_Angeles to New_York over Moscow "
    & Flight_East_Crosses_Longitude (Los_Angeles, New_York, Moscow)'Image
    );
    Put_Line
    ( "Beijing to Los_Angeles over Tokyo "
    & Flight_East_Crosses_Longitude (Beijing, Los_Angeles, Tokyo)'Image
    );
    Put_Line
    ( "Beijing to Los_Angeles over Waikiki "
    & Flight_East_Crosses_Longitude (Beijing, Los_Angeles, Waikiki)'Image
    );
    Put_Line
    ( "Beijing to Los_Angeles over Moscow "
    & Flight_East_Crosses_Longitude (Beijing, Los_Angeles, Moscow)'Image
    );
    end Test_Longtitude;
    ------------------------------

    It should print:

    Los_Angeles to New_York over Denver TRUE
    Los_Angeles to New_York over Moscow FALSE
    Beijing to Los_Angeles over Tokyo TRUE
    Beijing to Los_Angeles over Waikiki TRUE
    Beijing to Los_Angeles over Moscow FALSE


    --
    Regards,
    Dmitry A. Kazakov
    http://www.dmitry-kazakov.de

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ben Bacarisse@21:1/5 to Andreas on Sun Nov 27 00:11:49 2022
    Andreas <nobody@me.com> writes:

    On 24.11.22 14:14, Ben Bacarisse wrote:
    Richard Heathfield <rjh@cpax.org.uk> writes:

    On 24/11/2022 12:06 am, Ben Bacarisse wrote:
    Tim Rentsch <tr.17687@z991.linuxsc.com> writes:

    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... :)
    I didn't make a good job of presenting it. It certainly didn't pique
    anyone else's interest, but then comp.programming is not well populated. >>>> One thing that struck me was that I had not come across this before. I >>>> was surprised that this was not one of those idioms that one absorbs
    along the way. I suppose it is of limited use.

    The trouble is that it comes across as "is y >= x and <= z?", which is
    about as simple as it gets.
    I am saddened that you think I would have made a hash of that and amazed
    that you could think I had never have come across such a thing
    before. :-(
    I would have thought that
    "Consider any ordered measure that "wraps round" -- bearings in
    degrees, minutes in the hour, indeed hours in either the 12 or 24 hour
    clock."
    might have suggested it was not any old start <= x < end problem. How
    would you have phrased it so as to avoid the confusion?
    Anyway, the take-away is that the size of the range is not part of the
    problem and that no modulo operations are involved. I found that mildly
    interesting.

    This still bothers me. Take your example of 55 minutes being between
    45 minutes and 5 minutes. That's certainly true, but what if the
    numbers where not minutes? If your numbers wrapped around at 54
    instead of at 60, there wouldn't be a 55. How is your program supposed
    to know it? Is it hard coded? Or do you want something flexible enough
    to be usable with any "modulo"?

    The data are assumed to be valid -- i.e. in the expected range for the
    kind of data (0 to 59 for minutes for example).

    And when you need to do this test with potentially invalid data, I'd
    argue that the correction does not belong here. In some applications it
    might be appropriate to silently "correct" an invalid datum and in
    others you might need to hard error.

    The data validation should be somewhere else. That makes the simple
    test function re-usable.

    I agree that the problem specification should have made that as clear as possible.

    --
    Ben.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Andreas@21:1/5 to Ben Bacarisse on Sun Nov 27 00:38:26 2022
    On 24.11.22 14:14, Ben Bacarisse wrote:
    Richard Heathfield <rjh@cpax.org.uk> writes:

    On 24/11/2022 12:06 am, Ben Bacarisse wrote:
    Tim Rentsch <tr.17687@z991.linuxsc.com> writes:

    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... :)
    I didn't make a good job of presenting it. It certainly didn't pique
    anyone else's interest, but then comp.programming is not well populated. >>> One thing that struck me was that I had not come across this before. I
    was surprised that this was not one of those idioms that one absorbs
    along the way. I suppose it is of limited use.

    The trouble is that it comes across as "is y >= x and <= z?", which is
    about as simple as it gets.

    I am saddened that you think I would have made a hash of that and amazed
    that you could think I had never have come across such a thing
    before. :-(

    I would have thought that

    "Consider any ordered measure that "wraps round" -- bearings in
    degrees, minutes in the hour, indeed hours in either the 12 or 24 hour
    clock."

    might have suggested it was not any old start <= x < end problem. How
    would you have phrased it so as to avoid the confusion?

    Anyway, the take-away is that the size of the range is not part of the problem and that no modulo operations are involved. I found that mildly interesting.


    This still bothers me. Take your example of 55 minutes being between 45
    minutes and 5 minutes. That's certainly true, but what if the numbers
    where not minutes? If your numbers wrapped around at 54 instead of at
    60, there wouldn't be a 55. How is your program supposed to know it? Is
    it hard coded? Or do you want something flexible enough to be usable
    with any "modulo"?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ben Bacarisse@21:1/5 to Dmitry A. Kazakov on Sun Nov 27 02:03:24 2022
    "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> writes:

    On 2022-11-26 17:25, Ben Bacarisse wrote:
    Tim Rentsch <tr.17687@z991.linuxsc.com> writes:

    Consider a plane flying easterly (and for simplicity, directly
    above the equator). We measure the plane's progress by its
    longitude, measured to the nearest second of arc (1/3600 of a
    degree), so from -180 degrees to 179 degrees 59 minutes 59
    seconds (or equivalently from -648000 seconds to 647999 seconds).
    We would like to know if a given longitude is between the plane's
    starting longitude and ending longitude. (Note that the
    longitude measurements below are given in degrees, minutes, and
    seconds, but we may assume that internally they are represented
    as an integer number of seconds.)

    For example, if the plane flies

    from Los Angeles, US (longitude -118 14' 37")
    to New York, US (longitude -73 56' 7")

    does it cross over the longitude lines of

    Denver, US (longitude -104 59' 30"), or
    Moscow, Russia (longitude 37 37' 6")?

    Answer: the plane does cross the longitude line of Denver but not
    that of Moscow.

    Similarly, if the plane flies

    from Beijing, China (longitude 116 23' 0")
    to Los Angeles, US (longitude -118 14' 37")

    does it cross over

    Tokyo, Japan (longitude 139 50' 32"),
    Waikiki, Hawaii, US (longitude -157 50' 4"), or
    Moscow, Russia (longitude 37 37' 6")?

    Answer: the plane does cross the longitude lines of Tokyo and
    Waikiki but not that of Moscow.
    <cut my outline>

    Here is an implementation based on an integer type. Usually one would
    deploy a fixed-point type instead, but I don't want to mud the
    details.

    The original question was posed in terms of an arbitrary ordered type so
    I don't think that would have muddied the waters at all.

    Also a ring buffer would use a modular type, but then it must start at
    0, and here we start at -180 * 3600 seconds.

    I don't know enough Ada to know how generic it can be made in that
    language, but the Haskell version requires only that the type has an
    ordering. I think that can also be expressed (messily) in recent C++
    with template type constraints.

    --------------------------------------------------
    with Ada.Text_IO; use Ada.Text_IO;

    procedure Test_Longtitude is
    --
    -- Longtitude in seconds
    --
    type Longtitude is range -180 * 3600 .. 180 * 3600 - 1;

    type Degree is range -180..180;
    type Minute is range 0..59;
    type Second is range 0..59;

    function Compose (D : Degree; M : Minute; S : Second)
    return Longtitude is
    Result : constant Integer :=
    (Integer (D) * 60 + Integer (M)) * 60 + Integer (S);
    begin
    if Result <= Integer (Longtitude'Last) then
    return Longtitude (Result);
    else
    return Longtitude (Result - 360 * 3600);
    end if;
    end Compose;

    Beijing : constant Longtitude := Compose (+116, 23, 0);
    Los_Angeles : constant Longtitude := Compose (-118, 14, 37);
    Denver : constant Longtitude := Compose (-104, 59, 30);
    New_York : constant Longtitude := Compose ( -73, 56, 7);
    Tokyo : constant Longtitude := Compose (+139, 50, 32);
    Waikiki : constant Longtitude := Compose (-157, 50, 4);
    Moscow : constant Longtitude := Compose (+037, 37, 6);

    function Flight_East_Crosses_Longitude
    ( Start, Stop, X : Longtitude
    ) return Boolean is
    begin
    if Start <= Stop then
    return X in Start..Stop;
    else
    return X <= Stop or else X >= Start;
    end if;
    end Flight_East_Crosses_Longitude;

    Except for some boundary cases that have got lost in the telling, this
    is similar to Tim's solution. I chose to use a recursive call, because
    I though it explained the non-trivial case more clearly (but I bet I am
    pretty much the only one who thinks that).

    [The boundary cases of the original problem were that the region was half
    open: [start, end) so (in the specific case here)

    Flight_East_Crosses_Longitude(X, X, X)

    would be FALSE, and for any Y /= X

    Flight_East_Crosses_Longitude(X, Y, X)
    Flight_East_Crosses_Longitude(X, Y, Y)

    would be TRUE and FALSE respectively.]


    Tim's description was intended to clarify the problem, but it did not do
    so for you. How, in your opinion, could the question be posed (ideally
    in the general form) so that it can be readily understood?

    --
    Ben.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Dmitry A. Kazakov@21:1/5 to Ben Bacarisse on Sun Nov 27 11:02:08 2022
    On 2022-11-27 03:03, Ben Bacarisse wrote:
    Except for some boundary cases that have got lost in the telling, this
    is similar to Tim's solution. I chose to use a recursive call, because
    I though it explained the non-trivial case more clearly (but I bet I am pretty much the only one who thinks that).

    [The boundary cases of the original problem were that the region was half open: [start, end) so (in the specific case here)

    Flight_East_Crosses_Longitude(X, X, X)

    would be FALSE, and for any Y /= X

    Flight_East_Crosses_Longitude(X, Y, X)
    Flight_East_Crosses_Longitude(X, Y, Y)

    would be TRUE and FALSE respectively.]


    Tim's description was intended to clarify the problem, but it did not do
    so for you. How, in your opinion, could the question be posed (ideally
    in the general form) so that it can be readily understood?

    It depends on the background. E.g.

    implement tests on directed intervals in a ring of modulo K

    Is that readily understood? I don't know. Intervals in a ring are
    ambiguous. One needs to disambiguate, e.g. by introducing a direction.

    In practical cases rings represent some measurement process gone skewed
    due to wrapping, like longitude or an RT counter etc. You somehow need
    to restore the original contiguous value, e.g. when comparing them. It
    is sometimes impossible to do. I remember a nasty controller designed in
    a way that the order of messages in its buffer was impossible to restore.

    For example, by using unrolling. Consider interval [I1, I2] in a ring of K.

    if I1 > I2 then
    I2 := I2 + K; -- Unroll, now I1 <= I2
    end if;

    here we assume I1 > I2 indicates one single wrap happened. If multiple
    did, we are lost. (This is how you lose data/get garbage in I/O on data overrun)

    Now the inclusion test must take into account unrolled x:

    x in [I1, I2] or else x + K in [I1, I2]

    Graphically:
    K
    |### ####| --> 2K
    |### ####### ####|
    | |
    x x+K

    --
    Regards,
    Dmitry A. Kazakov
    http://www.dmitry-kazakov.de

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Julio Di Egidio@21:1/5 to Ben Bacarisse on Sun Nov 27 02:27:06 2022
    On Monday, 21 November 2022 at 21:45:34 UTC+1, Ben Bacarisse wrote:

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

    I don't think much better can be done of the obvious implementation:

    ```js
    function inOpenModRange(x: number, lo: number, hi: number, m: number): boolean {
    let x_ = MOD(x, m);
    let lo_ = MOD(lo, m);
    let hi_ = MOD(hi, m);
    return lo_ <= x_ && x_ < hi_;
    }
    ```

    where MOD is modulo, not remainder.

    In terms of remainder (as in JS), MOD looks like this:

    ```js
    function MOD(x: number, m: number): number {
    if (x * m === 0) { return 0; } // allow for MOD(x, 0)
    if (x > 0) { x = x % m; }
    else { x = (m + x % m) % m; }
    return x;
    }
    ```

    Beware of bugs.

    HTH,

    Julio

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Tim Rentsch@21:1/5 to Ben Bacarisse on Sun Nov 27 18:38:52 2022
    Ben Bacarisse <ben.usenet@bsb.me.uk> writes:

    Tim Rentsch <tr.17687@z991.linuxsc.com> writes:

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

    Tim Rentsch <tr.17687@z991.linuxsc.com> writes:

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

    I didn't make a good job of presenting it. It certainly didn't
    pique anyone else's interest, but then comp.programming is not
    well populated.

    I think part of the problem is that the question looks "easier"
    than it really is. People have an initial reaction (certainly I
    know I did) but don't continue to think through what the issues
    might be.

    I think so. I am, to be honest, rather disappointed by the
    reception this problem has had. Even after various examples and
    two explanations, some posters still don't know what's being
    asked, but at the time it is claimed that it's trivial and not
    worth coding up. Sometimes both claims are made by the same
    poster.

    I am somewhat baffled by the replies. I thought what was being
    sought was evident in your initial posting. Perhaps not obvious,
    but still clear enough after a bit of thought.

    For what it's worth, here is a stab at presenting the problem.
    It's longer than I would like it to be, but maybe it will be
    helpful.

    We are interested in writing a function to answer a more general
    version of the example problem stated below.

    Consider a plane flying easterly (and for simplicity, directly
    above the equator). We measure the plane's progress by its
    longitude, measured to the nearest second of arc (1/3600 of a
    degree), so from -180 degrees to 179 degrees 59 minutes 59
    seconds (or equivalently from -648000 seconds to 647999 seconds).
    We would like to know if a given longitude is between the plane's
    starting longitude and ending longitude. (Note that the
    longitude measurements below are given in degrees, minutes, and
    seconds, but we may assume that internally they are represented
    as an integer number of seconds.)

    For example, if the plane flies

    from Los Angeles, US (longitude -118 14' 37")
    to New York, US (longitude -73 56' 7")

    does it cross over the longitude lines of

    Denver, US (longitude -104 59' 30"), or
    Moscow, Russia (longitude 37 37' 6")?

    Answer: the plane does cross the longitude line of Denver but not
    that of Moscow.

    Similarly, if the plane flies

    from Beijing, China (longitude 116 23' 0")
    to Los Angeles, US (longitude -118 14' 37")

    does it cross over

    Tokyo, Japan (longitude 139 50' 32"),
    Waikiki, Hawaii, US (longitude -157 50' 4"), or
    Moscow, Russia (longitude 37 37' 6")?

    Answer: the plane does cross the longitude lines of Tokyo and
    Waikiki but not that of Moscow.

    For the general case we would like to handle any circular measure
    (for convenience having coordinates in some integer range).

    Exercise: write a function to answer this kind of question for
    circular measures in general. You may assume integer coordinates
    and intervals that include the starting point but do not include
    the end point. Give a suitable declaration for the function,
    and separately give a function definition to implement the given
    interface.

    I think this is good, though the example with seconds of arc may
    appear fiddly to some people. Degrees would do, but then the
    examples would become a bit fuzzy.

    I agree on both points. I didn't like the fiddly-ness, but no
    better example came to mind.

    I would probably use times of day. For example, my cheap
    "off-peak" electricity is from 22:00 to 00:30 and from 02:30 to
    7:30 but there are all sorts possible examples: times of day when
    I do not want to be disturbed, times when particular parking rules
    apply and so on.

    I was looking for a circular measure that is widely known and also
    has some negative values. Longitude was the best I could think of.

    The problem would then be to determine, given a time of day (in
    minutes past midnight), if that time is within a particular
    period.

    Hours of the day, especially on a 24-hour clock, is probably better
    than longitude for the initial example.

    I don't think it's a spoiler to give, first, a very concrete
    example, even including the function interface. A second part of
    the question would be by ask about generalising this interface to
    any "circular" measure.

    I was trying to be faithful (what I thought was) your not wanting
    to give away the simplicity of a general solution. I can't
    decide if your example below gives away too much or might be
    slightly misleading (because the modulus is inherent in the
    measure). It's hard to find a phrasing that is both specific
    enough to clearly state what is being sought and also not so
    specific that it gives away the nature of the problem. I was
    trying to strike a balance point, apparently not quite balanced
    enough. :)

    So I don't think it would matter to help DAK by giving

    with Ada.Text_IO;
    use Ada.Text_IO;

    procedure Longitude is

    function Flight_East_Crosses_Longitude
    (Start_Seconds, End_Seconds, Longitude : Integer) return Boolean is
    begin
    -- Your code here
    end Flight_East_Crosses_Longitude;

    Beijing : constant Integer := ((+116 * 60) + 23) * 60 + 0;
    Los_Angeles : constant Integer := ((-118 * 60) + 14) * 60 + 37;
    Tokyo : constant Integer := ((+139 * 60) + 50) * 60 + 32;
    Waikiki : constant Integer := ((-157 * 60) + 50) * 60 + 4;
    Moscow : constant Integer := ((+037 * 60) + 37) * 60 + 6;

    begin
    Put_Line(
    Flight_East_Crosses_Longitude(Beijing, Los_Angeles, Tokyo)'img
    );
    Put_Line(
    Flight_East_Crosses_Longitude(Beijing, Los_Angeles, Waikiki)'img
    );
    Put_Line(
    Flight_East_Crosses_Longitude(Beijing, Los_Angeles, Moscow)'img
    );
    end Longitude;

    (I reformatted the calls to Put_Line() so they wouldn't violate
    my newsreader's idea of how long lines should be.)

    Of course, Ada is one of the few languages where one can actually
    implement largely type-generic functions, so it's a shame to be
    this specific, but as I say, that could come later.

    I can't help feeling that your suggestion of using times of
    day -- especially if expressed in 4-digit military time format --
    would be a better choice for the example.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Tim Rentsch@21:1/5 to Julio Di Egidio on Sun Nov 27 19:23:21 2022
    Julio Di Egidio <julio@diegidio.name> writes:

    On Monday, 21 November 2022 at 21:45:34 UTC+1, Ben Bacarisse wrote:

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

    I don't think much better can be done of the obvious implementation:

    ```js
    function inOpenModRange(
    x: number, lo: number, hi: number, m: number
    ): boolean {
    let x_ = MOD(x, m);
    let lo_ = MOD(lo, m);
    let hi_ = MOD(hi, m);
    return lo_ <= x_ && x_ < hi_;
    }
    ```

    where MOD is modulo, not remainder.

    In terms of remainder (as in JS), MOD looks like this:

    ```js
    function MOD(x: number, m: number): number {
    if (x * m === 0) { return 0; } // allow for MOD(x, 0)
    if (x > 0) { x = x % m; }
    else { x = (m + x % m) % m; }
    return x;
    }
    ```

    This proposed function doesn't work. Consider a curfew that
    starts at 10 pm (2200) and goes until 5 am (0500). Is 3 am
    (0300) a curfew violation? The call to your function would be

    inOpenModRange( 0300, 2200, 0500, 2400 );

    which yields false. But it should yield true.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Julio Di Egidio@21:1/5 to Julio Di Egidio on Sun Nov 27 23:03:14 2022
    On Monday, 28 November 2022 at 07:59:31 UTC+1, Julio Di Egidio wrote:

    Moreover, notice that this MOD fails (in particular, does not
    return positive) for m <= 0. (!!) But I won't bother with that now.

    I meant, for m < 0. You get the point: that I won't get it totally
    right here ever...

    Julio

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Julio Di Egidio@21:1/5 to Tim Rentsch on Sun Nov 27 22:59:28 2022
    On Monday, 28 November 2022 at 04:23:28 UTC+1, Tim Rentsch wrote:
    Julio Di Egidio <ju...@diegidio.name> writes:
    On Monday, 21 November 2022 at 21:45:34 UTC+1, Ben Bacarisse wrote:

    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 en value.
    <snip>

    In terms of remainder (as in JS), MOD looks like this:

    ```js

    Should have been "ts", i.e. TypeScript (for the few type annotations:
    get rid of those to get the plain JS).

    ```js
    function MOD(x: number, m: number): number {
    if (x * m === 0) { return 0; } // allow for MOD(x, 0)

    BTW, better not do that multiplication:

    ```js
    if (m === 0) { return 0; } // allow for m = 0
    if (x === 0) { return 0; } // a short-cut
    ```

    Moreover, notice that this MOD fails (in particular, does not
    return positive) for m <= 0. (!!) But I won't bother with that now.

    if (x > 0) { x = x % m; }
    else { x = (m + x % m) % m; }
    return x;
    }
    ```
    This proposed function doesn't work. Consider a curfew that
    starts at 10 pm (2200) and goes until 5 am (0500). Is 3 am
    (0300) a curfew violation? The call to your function would be

    inOpenModRange( 0300, 2200, 0500, 2400 );

    which yields false. But it should yield true.

    Argh! But you should not have snipped that "beware
    of bugs", that was the most important part!! ;)

    This one should do the trick:

    ```ts
    /** Returns whether x in [lo, hi[ (mod m). */
    function inOpenModRange(
    x: number, lo: number, hi: number, m: number
    ): boolean {
    let x_ = MOD(x - lo, m);
    let hi_ = MOD(hi - lo, m);
    return x_ < hi_;
    }
    ```

    Still untested, I hope I have not botched it again...

    Julio

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Richard Harnden@21:1/5 to Ben Bacarisse on Mon Nov 28 11:20:28 2022
    On 21/11/2022 20:45, Ben Bacarisse wrote:
    I wonder if there are any real posters here? Let's see...

    I came across a trivial programming task that must have been solved a thousand times by other programmers, but it had never crossed my path
    until yesterday. I must be feeling my age because I made a real hash of tackling it at first. Anyway, I thought it might be of interest.

    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 en 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. Do all the news
    readers used by programmers (or ex programmers) all respect the presence
    of a form-feed character...

    ... like this? Because that's my favourite way, rather than posting
    lots of dummy lines before the solution.


    I think this works ...

    int in_subrange(int range, int start, int end, int check)
    {
    check %= range;

    if ( ( end < start && (
    (check >= 0 && check <= end)
    || (check >= start && check < range)
    )
    )
    || ( check >= start && check <= end )
    )
    return 1;

    return 0;
    }

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Tim Rentsch@21:1/5 to Richard Harnden on Mon Nov 28 07:30:11 2022
    Richard Harnden <richard.nospam@gmail.com> writes:

    On 21/11/2022 20:45, Ben Bacarisse wrote:

    I wonder if there are any real posters here? Let's see...

    I came across a trivial programming task that must have been solved a
    thousand times by other programmers, but it had never crossed my path
    until yesterday. I must be feeling my age because I made a real hash of
    tackling it at first. Anyway, I thought it might be of interest.

    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 en 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. Do all the news
    readers used by programmers (or ex programmers) all respect the presence
    of a form-feed character...

    ... like this? Because that's my favourite way, rather than posting
    lots of dummy lines before the solution.

    I think this works ...

    int in_subrange(int range, int start, int end, int check)
    {
    check %= range;

    if ( ( end < start && (
    (check >= 0 && check <= end)
    || (check >= start && check < range)
    )
    )
    || ( check >= start && check <= end )
    )
    return 1;

    return 0;
    }

    Have you thought about a case where the values of check, start,
    and end are chosen from the interval -9000000000 to 9000000000?
    How about the interval -18000000000 to 18000000000?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Tim Rentsch@21:1/5 to Julio Di Egidio on Mon Nov 28 07:22:33 2022
    Julio Di Egidio <julio@diegidio.name> writes:

    On Monday, 28 November 2022 at 04:23:28 UTC+1, Tim Rentsch wrote:

    Julio Di Egidio <ju...@diegidio.name> writes:

    I recommend switching away from google groups, to a posting service
    that is more well-behaved. Try eternal-september.org, which offers
    free accounts after registering, if you can't find anything else
    more to your liking.

    On Monday, 21 November 2022 at 21:45:34 UTC+1, Ben Bacarisse wrote:

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

    <snip>

    In terms of remainder (as in JS), MOD looks like this:

    ```js

    Should have been "ts", i.e. TypeScript (for the few type
    annotations: get rid of those to get the plain JS).

    ```js
    function MOD(x: number, m: number): number {
    if (x * m === 0) { return 0; } // allow for MOD(x, 0)

    BTW, better not do that multiplication:

    ```js
    if (m === 0) { return 0; } // allow for m = 0
    if (x === 0) { return 0; } // a short-cut
    ```

    Moreover, notice that this MOD fails (in particular, does not
    return positive) for m <= 0. (!!) But I won't bother with that
    now.

    if (x > 0) { x = x % m; }
    else { x = (m + x % m) % m; }
    return x;
    }
    ```

    This proposed function doesn't work. Consider a curfew that
    starts at 10 pm (2200) and goes until 5 am (0500). Is 3 am
    (0300) a curfew violation? The call to your function would be

    inOpenModRange( 0300, 2200, 0500, 2400 );

    which yields false. But it should yield true.

    Argh! But you should not have snipped that "beware
    of bugs", that was the most important part!! ;)

    It's your job to beware of bugs, not mine.

    This one should do the trick:

    ```ts
    /** Returns whether x in [lo, hi[ (mod m). */
    function inOpenModRange(
    x: number, lo: number, hi: number, m: number
    ): boolean {
    let x_ = MOD(x - lo, m);
    let hi_ = MOD(hi - lo, m);
    return x_ < hi_;
    }
    ```

    This scheme looks like it will work, as long as the values given
    don't get too near the edges of representation. JavaScript
    represents numeric values using floating point, and that choice
    leads to some unexpected results when working with large numbers.

    However, this approach is more complicated than it needs to be.
    Have you tried looking at the other answers?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Richard Harnden@21:1/5 to Tim Rentsch on Mon Nov 28 17:15:22 2022
    On 28/11/2022 15:30, Tim Rentsch wrote:
    Richard Harnden <richard.nospam@gmail.com> writes:

    On 21/11/2022 20:45, Ben Bacarisse wrote:

    I wonder if there are any real posters here? Let's see...

    I came across a trivial programming task that must have been solved a
    thousand times by other programmers, but it had never crossed my path
    until yesterday. I must be feeling my age because I made a real hash of >>> tackling it at first. Anyway, I thought it might be of interest.

    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 en 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. Do all the news
    readers used by programmers (or ex programmers) all respect the presence >>> of a form-feed character...

    ... like this? Because that's my favourite way, rather than posting
    lots of dummy lines before the solution.

    I think this works ...

    int in_subrange(int range, int start, int end, int check)
    {
    check %= range;

    if ( ( end < start && (
    (check >= 0 && check <= end)
    || (check >= start && check < range)
    )
    )
    || ( check >= start && check <= end )
    )
    return 1;

    return 0;
    }

    Have you thought about a case where the values of check, start,
    and end are chosen from the interval -9000000000 to 9000000000?
    How about the interval -18000000000 to 18000000000?

    Nope, I assume that start and end are between zero and range, and that
    check is greater that zero.

    I didn't think about overflows at all.

    With unsigned integers ...

    int in_subrange(uint_fast64_t range, uint_fast64_t start, uint_fast64_t
    end, uint_fast64_t check)
    {
    check %= range;

    if ( ( end < start && (
    check <= end
    || (check >= start && check < range)
    )
    )
    || ( check >= start && check <= end )
    )
    return 1;

    return 0;
    }

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Julio Di Egidio@21:1/5 to Tim Rentsch on Mon Nov 28 10:20:30 2022
    On Monday, 28 November 2022 at 16:22:38 UTC+1, Tim Rentsch wrote:
    Julio Di Egidio <ju...@diegidio.name> writes:
    On Monday, 21 November 2022 at 21:45:34 UTC+1, Ben Bacarisse wrote:

    On Monday, 28 November 2022 at 04:23:28 UTC+1, Tim Rentsch wrote:

    Julio Di Egidio <ju...@diegidio.name> writes:

    I recommend switching away from google groups,

    That is your prerogative, I suppose.

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

    Argh! But you should not have snipped that "beware
    of bugs", that was the most important part!! ;)

    It's your job to beware of bugs, not mine.

    I didn't know you were paying me for production level code:
    in that case, I'd have asked for the technical requirements,
    not just the functional ones...

    IOW, I have have given a mathematical spec written in some
    simply-typed functional language, can be functions on the real
    numbers. Which is often step one in functional/algorithmic
    design. Beside that point, one needs technical requirements,
    i.e. info on the concrete numeric types available as a minimum.

    This one should do the trick:

    ```ts
    /** Returns whether x in [lo, hi[ (mod m). */
    function inOpenModRange(
    x: number, lo: number, hi: number, m: number
    ): boolean {
    let x_ = MOD(x - lo, m);
    let hi_ = MOD(hi - lo, m);
    return x_ < hi_;
    }
    ```
    This scheme looks like it will work, as long as the values given
    don't get too near the edges of representation. JavaScript
    represents numeric values using floating point, and that choice
    leads to some unexpected results when working with large numbers.

    However, this approach is more complicated than it needs to be.
    Have you tried looking at the other answers?

    That is complicated?? Maybe I haven't looked well enough
    but, honestly, I have not seen anything that is even vaguely as
    clear, and efficient, and to the point. Have I missed it?

    Julio

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Tim Rentsch@21:1/5 to Richard Harnden on Tue Nov 29 04:03:46 2022
    Richard Harnden <richard.nospam@gmail.com> writes:

    On 28/11/2022 15:30, Tim Rentsch wrote:

    Richard Harnden <richard.nospam@gmail.com> writes:

    On 21/11/2022 20:45, Ben Bacarisse wrote:

    I wonder if there are any real posters here? Let's see...

    I came across a trivial programming task that must have been
    solved a thousand times by other programmers, but it had never
    crossed my path until yesterday. I must be feeling my age
    because I made a real hash of tackling it at first. Anyway, I
    thought it might be of interest.

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

    I was specifically concerned with integer values where the
    sub-range includes the start value but excludes the end value.
    [...]

    I think this works ...

    int in_subrange(int range, int start, int end, int check)
    {
    check %= range;

    if ( ( end < start && (
    (check >= 0 && check <= end)
    || (check >= start && check < range)
    )
    )
    || ( check >= start && check <= end )
    )
    return 1;

    return 0;
    }

    Have you thought about a case where the values of check, start,
    and end are chosen from the interval -9000000000 to 9000000000?
    How about the interval -18000000000 to 18000000000?

    Nope, I assume that start and end are between zero and range, and
    that check is greater that zero.

    This assumption doesn't match the problem statement (emphasis
    added):

    Consider >> any << ordered measure that "wraps round"

    What's being asked for is a function that will work with any
    ordered measure (that wraps), not just some such measures. An
    example of such a measure is longitude, which goes from -180
    to +180 (with one of the two extreme values omitted). Similarly
    there is no reason not to allow a measure that is only positive
    integers but does not include zero. An important part of the
    challenge is to come up with a solution that handles these cases
    as well as the more obvious ones.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Julio Di Egidio@21:1/5 to Tim Rentsch on Tue Nov 29 04:50:49 2022
    On Tuesday, 29 November 2022 at 13:29:48 UTC+1, Tim Rentsch wrote:
    Julio Di Egidio <ju...@diegidio.name> writes:
    On Monday, 28 November 2022 at 16:22:38 UTC+1, Tim Rentsch wrote:
    Julio Di Egidio <an unrepentant google groups user> writes:
    On Monday, 21 November 2022 at 21:45:34 UTC+1, Ben Bacarisse wrote: [...]
    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 en value.

    Argh! But you should not have snipped that "beware
    of bugs", that was the most important part!! ;)

    It's your job to beware of bugs, not mine.

    I didn't know you were paying me for production level code:

    You are under no obligation to post bug-free code. Similarly I
    am under no obligation to correct bugs in your code.

    That is not what I said: indeed "beware of bugs" is a paraphrase
    of Knuth for your info, and what that means. Morover, modulo
    my mistakes, which is another story, my code *is* bug free, just
    apparently you are confused about the whole what is what.

    But if you
    want your postings to be taken seriously it's important to put

    I am certainly not interested in any such cheap abusive "psychology".

    The question can be answered without using any of subtraction,
    remainder, or mod,

    No, it cannot. But feel free to prove me wrong.

    and more than one solution has been posted
    demonstrating that property. Perhaps you missed those answers.

    The answer by Paul N is equivalent to mine, but I have given a
    formalization, plus a courtesy implementation of MOD for those
    playing with a language that does not have it built-in.

    And my formalization was not so much to improve on Paul's
    answer anyway, it was more out of the irritation of the several
    posts by you and others with the deranged morals but not
    even able to state an interface, let alone write few lines of
    code that are not imply a horrible mess when at all correct.

    How long have you being doing this job? You come up as yet
    another clueless arrogant ass.

    Julio

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Tim Rentsch@21:1/5 to Julio Di Egidio on Tue Nov 29 04:29:42 2022
    Julio Di Egidio <julio@diegidio.name> writes:

    On Monday, 28 November 2022 at 16:22:38 UTC+1, Tim Rentsch wrote:

    Julio Di Egidio <an unrepentant google groups user> writes:

    On Monday, 21 November 2022 at 21:45:34 UTC+1, Ben Bacarisse wrote:

    [...]

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

    Argh! But you should not have snipped that "beware
    of bugs", that was the most important part!! ;)

    It's your job to beware of bugs, not mine.

    I didn't know you were paying me for production level code:

    You are under no obligation to post bug-free code. Similarly I
    am under no obligation to correct bugs in your code. But if you
    want your postings to be taken seriously it's important to put
    more thought into what is posted and make an effort to avoid
    careless mistakes.

    in that case, I'd have asked for the technical requirements,
    not just the functional ones...

    IOW, I have have given a mathematical spec written in some
    simply-typed functional language, can be functions on the real
    numbers. Which is often step one in functional/algorithmic
    design. Beside that point, one needs technical requirements,
    i.e. info on the concrete numeric types available as a minimum.

    Part of the challenge is to figure out what the interface should
    be. The problem is not just a coding exercise.

    This one should do the trick:

    ```ts
    /** Returns whether x in [lo, hi[ (mod m). */
    function inOpenModRange(
    x: number, lo: number, hi: number, m: number
    ): boolean {
    let x_ = MOD(x - lo, m);
    let hi_ = MOD(hi - lo, m);
    return x_ < hi_;
    }
    ```

    This scheme looks like it will work, as long as the values given
    don't get too near the edges of representation. JavaScript
    represents numeric values using floating point, and that choice
    leads to some unexpected results when working with large numbers.

    However, this approach is more complicated than it needs to be.
    Have you tried looking at the other answers?

    That is complicated?? Maybe I haven't looked well enough
    but, honestly, I have not seen anything that is even vaguely as
    clear, and efficient, and to the point. Have I missed it?

    The question can be answered without using any of subtraction,
    remainder, or mod, and more than one solution has been posted
    demonstrating that property. Perhaps you missed those answers.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Dmitry A. Kazakov@21:1/5 to Richard Harnden on Tue Nov 29 16:13:07 2022
    On 2022-11-29 16:03, Richard Harnden wrote:

    Okay, so how about this ... ?

    int in_subrange(int_fast64_t range_min, int_fast64_t range_max,
    int_fast64_t start, int_fast64_t end, int_fast64_t check)
    {
        while ( check < range_min )
            check += range_max - range_min;

    Ring modulo would be range_max - range_min + 1

    It's okay for check to have 'clocked', range_min, range_max, start and
    end have sensible values.

    If the language supports modular and ranged types there is no need to check/normalize arguments.

    --
    Regards,
    Dmitry A. Kazakov
    http://www.dmitry-kazakov.de

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Richard Heathfield@21:1/5 to Julio Di Egidio on Tue Nov 29 14:47:23 2022
    On 29/11/2022 12:50 pm, Julio Di Egidio wrote:
    How long have you being doing this job? You come up as yet
    another clueless arrogant ass.

    Your judgement seems overly hasty. I can assure you on the basis
    of many discussions in which he and I have both participated that
    Tim Rentsch is neither clueless nor an ass.

    --
    Richard Heathfield
    Email: rjh at cpax dot org dot uk
    "Usenet is a strange place" - dmr 29 July 1999
    Sig line 4 vacant - apply within

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Richard Harnden@21:1/5 to Tim Rentsch on Tue Nov 29 15:03:54 2022
    On 29/11/2022 12:03, Tim Rentsch wrote:
    Richard Harnden <richard.nospam@gmail.com> writes:

    On 28/11/2022 15:30, Tim Rentsch wrote:

    Richard Harnden <richard.nospam@gmail.com> writes:

    On 21/11/2022 20:45, Ben Bacarisse wrote:

    I wonder if there are any real posters here? Let's see...

    I came across a trivial programming task that must have been
    solved a thousand times by other programmers, but it had never
    crossed my path until yesterday. I must be feeling my age
    because I made a real hash of tackling it at first. Anyway, I
    thought it might be of interest.

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

    I was specifically concerned with integer values where the
    sub-range includes the start value but excludes the end value.
    [...]

    I think this works ...

    int in_subrange(int range, int start, int end, int check)
    {
    check %= range;

    if ( ( end < start && (
    (check >= 0 && check <= end)
    || (check >= start && check < range)
    )
    )
    || ( check >= start && check <= end )
    )
    return 1;

    return 0;
    }

    Have you thought about a case where the values of check, start,
    and end are chosen from the interval -9000000000 to 9000000000?
    How about the interval -18000000000 to 18000000000?

    Nope, I assume that start and end are between zero and range, and
    that check is greater that zero.

    This assumption doesn't match the problem statement (emphasis
    added):

    Consider >> any << ordered measure that "wraps round"

    What's being asked for is a function that will work with any
    ordered measure (that wraps), not just some such measures. An
    example of such a measure is longitude, which goes from -180
    to +180 (with one of the two extreme values omitted). Similarly > there is no reason not to allow a measure that is only positive
    integers but does not include zero. An important part of the
    challenge is to come up with a solution that handles these cases
    as well as the more obvious ones.

    Okay, so how about this ... ?

    int in_subrange(int_fast64_t range_min, int_fast64_t range_max,
    int_fast64_t start, int_fast64_t end, int_fast64_t check)
    {
    while ( check < range_min )
    check += range_max - range_min;

    while ( check > range_max )
    check -= range_max - range_min;

    if ( ( end < start && (
    ( check > range_min && check <= end )
    || (check >= start && check < range_max)
    )
    )
    || ( check >= start && check <= end )
    )
    return 1;

    return 0
    }

    It's okay for check to have 'clocked', range_min, range_max, start and
    end have sensible values.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Julio Di Egidio@21:1/5 to Ben Bacarisse on Tue Nov 29 09:50:29 2022
    On Tuesday, 29 November 2022 at 18:23:12 UTC+1, Ben Bacarisse wrote:
    Richard Heathfield <r...@cpax.org.uk> writes:
    On 29/11/2022 12:50 pm, Julio Di Egidio wrote:

    How long have you being doing this job? You come up as yet
    another clueless arrogant ass.

    Your judgement seems overly hasty. I can assure you on the basis of
    many discussions in which he and I have both participated that Tim
    Rentsch is neither clueless nor an ass.

    And on the basis of observing quite a discussions with Julio Di Egidio,
    I am amazed that it took this long get to crude insults. It's often the second reply, if not the fist.

    As usual, what a bunch of fucking morons...

    But you remain the champion of the fraudulent
    suckers around here.

    *Plonk*

    Julio

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ben Bacarisse@21:1/5 to Dmitry A. Kazakov on Tue Nov 29 17:37:35 2022
    "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> writes:

    On 2022-11-29 16:03, Richard Harnden wrote:

    Okay, so how about this ... ?
    int in_subrange(int_fast64_t range_min, int_fast64_t range_max,
    int_fast64_t start, int_fast64_t end, int_fast64_t check)
    {
        while ( check < range_min )
            check += range_max - range_min;

    Ring modulo would be range_max - range_min + 1

    It's okay for check to have 'clocked', range_min, range_max, start
    and end have sensible values.

    If the language supports modular and ranged types there is no need to check/normalize arguments.

    Part of the specification was that the inputs were intended to be valid,
    though maybe that was not very clear. In the case where this cropped
    up, everything was in minutes coming from internal clocks and calendars
    where the result were already in the 0-59 range.

    In my view, if you need to check the arguments, that's a separate
    function because what to do with bad data is so often
    application-specific. Just normalising and carrying on is only one
    strategy, and one that can delay detecting bugs. In your solution, you normalised the degrees, minutes and seconds into a longitude beforehand,
    which is all that was needed in that example.

    --
    Ben.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ben Bacarisse@21:1/5 to Richard Heathfield on Tue Nov 29 17:23:08 2022
    Richard Heathfield <rjh@cpax.org.uk> writes:

    On 29/11/2022 12:50 pm, Julio Di Egidio wrote:
    How long have you being doing this job? You come up as yet
    another clueless arrogant ass.

    Your judgement seems overly hasty. I can assure you on the basis of
    many discussions in which he and I have both participated that Tim
    Rentsch is neither clueless nor an ass.

    And on the basis of observing quite a discussions with Julio Di Egidio,
    I am amazed that it took this long get to crude insults. It's often the
    second reply, if not the fist.

    --
    Ben.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Dmitry A. Kazakov@21:1/5 to Ben Bacarisse on Tue Nov 29 19:02:13 2022
    On 2022-11-29 18:37, Ben Bacarisse wrote:

    In my view, if you need to check the arguments, that's a separate
    function because what to do with bad data is so often
    application-specific.

    Yes, validation is a part of the measurement process. Usually datatype
    in which sensors report data and ones used in computations are
    different. Validation happens upon conversion and then, ideally, invalid
    values are excluded per design.

    Just normalising and carrying on is only one
    strategy, and one that can delay detecting bugs.

    Sure. E.g. IEEE 754 float design is a perfect example of such bugs when computations with non-numbers just continue until too late.

    --
    Regards,
    Dmitry A. Kazakov
    http://www.dmitry-kazakov.de

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Julio Di Egidio@21:1/5 to Dmitry A. Kazakov on Tue Nov 29 10:10:58 2022
    On Tuesday, 29 November 2022 at 19:02:19 UTC+1, Dmitry A. Kazakov wrote:
    On 2022-11-29 18:37, Ben Bacarisse wrote:

    In my view, if you need to check the arguments, that's a separate
    function because what to do with bad data is so often
    application-specific.

    Yes, validation is a part of the measurement process. Usually datatype
    in which sensors report data and ones used in computations are
    different. Validation happens upon conversion and then, ideally, invalid values are excluded per design.

    Just normalising and carrying on is only one
    strategy, and one that can delay detecting bugs.

    Sure. E.g. IEEE 754 float design is a perfect example of such bugs when computations with non-numbers just continue until too late.

    A couple of very nonsensical statements.

    But congratulations especially for always
    fighting the good fight...

    Julio

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Tim Rentsch@21:1/5 to Richard Heathfield on Wed Nov 30 01:39:38 2022
    Richard Heathfield <rjh@cpax.org.uk> writes:

    On 29/11/2022 12:50 pm, Julio Di Egidio wrote:

    How long have you being doing this job? You come up as yet
    another clueless arrogant ass.

    Your judgement seems overly hasty. I can assure you on the basis of
    many discussions in which he and I have both participated that Tim
    Rentsch is neither clueless nor an ass.

    Thank you Richard. How the heck are you?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Richard Heathfield@21:1/5 to Tim Rentsch on Wed Nov 30 11:10:36 2022
    On 30/11/2022 9:39 am, Tim Rentsch wrote:
    Richard Heathfield <rjh@cpax.org.uk> writes:

    On 29/11/2022 12:50 pm, Julio Di Egidio wrote:

    How long have you being doing this job? You come up as yet
    another clueless arrogant ass.

    Your judgement seems overly hasty. I can assure you on the basis of
    many discussions in which he and I have both participated that Tim
    Rentsch is neither clueless nor an ass.

    Thank you Richard. How the heck are you?

    You're welcome. Well, haec olim meminisse juvabit (and I am sure
    I need not translate).

    --
    Richard Heathfield
    Email: rjh at cpax dot org dot uk
    "Usenet is a strange place" - dmr 29 July 1999
    Sig line 4 vacant - apply within

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Tim Rentsch@21:1/5 to Julio Di Egidio on Wed Nov 30 02:24:11 2022
    Julio Di Egidio <julio@diegidio.name> writes:

    [edited for brevity]

    On Tuesday, 29 November 2022 at 13:29:48 UTC+1, Tim Rentsch wrote:

    On Monday, 21 November 2022 at 21:45:34 UTC+1, Ben Bacarisse wrote:

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

    The question can be answered without using any of subtraction,
    remainder, or mod,

    No, it cannot. But feel free to prove me wrong.

    At this point I see no reason to try to convince you.

    and more than one solution has been posted
    demonstrating that property. Perhaps you missed those answers.

    The answer by Paul N is equivalent to mine, but I have given a
    formalization, plus a courtesy implementation of MOD for those
    playing with a language that does not have it built-in.

    And my formalization was not so much to improve on Paul's
    answer anyway, it was more out of the irritation of the several
    posts by you and others with the deranged morals but not
    even able to state an interface,

    I posted an interface in my initial response to the lead post of
    the thread. IIANM that response was in fact the first response
    of any in the thread.

    Which of my earlier posts irritated you, and what parts of them
    caused you irritation?

    let alone write few lines of
    code that are not imply a horrible mess when at all correct.

    I don't know how to make sense of the restrictive phrase here.

    How long have you being doing this job? You come up as yet
    another clueless arrogant ass.

    I first posted in net news (back when it was called usenet) back
    in the mid 1980s. My first experience with computer programming
    was in the late 1960s. My first exposure to C was in 1978,
    reading "The C Programming Language" by Kernighan and Ritchie.
    At that time C was roughly the tenth programming language I was
    familiar with.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Tim Rentsch@21:1/5 to Richard Harnden on Wed Nov 30 03:32:12 2022
    Richard Harnden <richard.nospam@gmail.com> writes:

    [edited for brevity]

    On 29/11/2022 12:03, Tim Rentsch wrote:

    On 21/11/2022 20:45, Ben Bacarisse wrote:

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

    I was specifically concerned with integer values where the
    sub-range includes the start value but excludes the end value.

    [responding to an earlier proposed solution] This assumption
    doesn't match the problem statement (emphasis added):

    Consider >> any << ordered measure that "wraps round"

    What's being asked for is a function that will work with any
    ordered measure (that wraps), not just some such measures. An
    example of such a measure is longitude, which goes from -180
    to +180 (with one of the two extreme values omitted). Similarly >
    there is no reason not to allow a measure that is only positive
    integers but does not include zero. An important part of the
    challenge is to come up with a solution that handles these cases
    as well as the more obvious ones.

    Okay, so how about this ... ?

    int in_subrange(int_fast64_t range_min, int_fast64_t range_max,
    int_fast64_t start, int_fast64_t end, int_fast64_t check)
    {
    while ( check < range_min )
    check += range_max - range_min;

    while ( check > range_max )
    check -= range_max - range_min;

    if ( ( end < start && (
    ( check > range_min && check <= end )
    || (check >= start && check < range_max)
    )
    )
    || ( check >= start && check <= end )
    )
    return 1;

    return 0
    }

    Several things stand out to me.

    The code seems confused about whether range_min and range_max are
    legal values or just outside of legal values. Normally I would
    expect them to be legal values, since the "just outside of legal
    values" choice might fall outside the range of the type being used.

    Assuming range_min and range_max are legal values, the code has an
    off-by-one error, namely, it should be range_max - range_min + 1
    that is added or subtracted to bring 'check' into the legal range.

    There is still the problem of potential overflow, because the value
    of the expression 'range_max - range_min' might exceed the bounds of
    the type being used.

    Minor comment: the choice of 'int_fast64_t' for the parameter type
    is kind of surprising. Why not 'int_least64_t' or maybe even just
    'long long'?

    The expressions 'check <= end' should be 'check < end' to conform to
    the problem statement that the interval being considered excludes
    the end value.

    If range_min and range_max are legal values, the tests for 'check'
    being inside the range become superfluous.

    Incidentally, since the problem statement doesn't say, to my way of
    thinking it would be fine simply to exclude values of 'check' that
    fall outside the legal range

    if( check < range_min || check > range_max ) return 0;

    which has the nice by-product of avoiding the overflow problem.

    Let's look again at the key test, simplified so as not to do the
    range tests (some redundant parentheses retained):

    if(
    (end < start && (check <= end || check >= start))
    || (check >= start && check <= end)
    ){
    return 1;
    }

    I think the logic of this test is right, but the form of it makes me
    nervous. The reason for that is the second line depends on the
    condition 'start <= end' being true, but that condition is not
    explicitly tested. The condition is /implied/ by the tests that are
    done against 'check', but that result is not immediately obvious. I
    think a better writing would be to use a ?: operator, as for example

    if(
    end < start
    ? start <= check || check < end
    : start <= check && check < end
    ){
    return 1;
    }

    Now it is immediately obvious that the two cases are mutually
    exclusive.

    Finally, consider the last two statements. Since the penultimate
    statement conditionally returns 1 (true) and the last statement
    simply returns 0 (false), these two statements can be combined into
    a single 'return' statement:

    return
    end < start
    ? start <= check || check < end
    : start <= check && check < end
    ;

    (I hope you will excuse my tendency to try to simplify almost any
    code that I see. I appreciate what you've done here.)

    It's okay for check to have 'clocked', range_min, range_max, start
    and end have sensible values.

    What I think you mean by this is that 'start' and 'end' can be
    assumed to have values within the legal range, and that range_min
    and range_max have sensible values (so range_min <= range_max),
    and that 'check' might fall outside the legal range, and that
    possibility must be accounted for. That makes sense.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Julio Di Egidio@21:1/5 to Tim Rentsch on Wed Nov 30 06:36:51 2022
    On Wednesday, 30 November 2022 at 12:32:18 UTC+1, Tim Rentsch wrote:

    Incidentally, since the problem statement doesn't say, to my way of
    thinking it would be fine simply to exclude values of 'check' that
    fall outside the legal range

    These threads are typically just *full* of misunderstandings
    and malpractices, and I won't even begin listing them all.
    For example, *that*, to the letter, is a recipe for *guaranteed
    disaster*. I know of a company that used to build robots like
    that, just assume all inputs are valid by a protocol, and at the
    first signal invalid by the protocol, the robots simply destroy
    whatever is range... or crash a plane or what have you...

    And that's a good place to stop. (EOD)

    Julio

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Julio Di Egidio@21:1/5 to Tim Rentsch on Wed Nov 30 06:28:36 2022
    On Wednesday, 30 November 2022 at 11:24:17 UTC+1, Tim Rentsch wrote:
    Julio Di Egidio <ju...@diegidio.name> writes:
    [edited for brevity]
    On Tuesday, 29 November 2022 at 13:29:48 UTC+1, Tim Rentsch wrote:
    On Monday, 21 November 2022 at 21:45:34 UTC+1, Ben Bacarisse wrote:

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

    The question can be answered without using any of subtraction,
    remainder, or mod,

    No, it cannot. But feel free to prove me wrong.

    At this point I see no reason to try to convince you.

    You have never had any reason, so that is hardly surprising.

    I first posted in net news (back when it was called usenet) back
    in the mid 1980s. My first experience with computer programming

    So you do not even have the age excuse... BTW, how do you think
    it's called now? A rhetorical question.

    Julio

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Richard Harnden@21:1/5 to Tim Rentsch on Wed Nov 30 15:21:30 2022
    On 30/11/2022 11:32, Tim Rentsch wrote:
    Richard Harnden <richard.nospam@gmail.com> writes:

    [edited for brevity]

    On 29/11/2022 12:03, Tim Rentsch wrote:

    On 21/11/2022 20:45, Ben Bacarisse wrote:

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

    I was specifically concerned with integer values where the
    sub-range includes the start value but excludes the end value.

    [responding to an earlier proposed solution] This assumption
    doesn't match the problem statement (emphasis added):

    Consider >> any << ordered measure that "wraps round"

    What's being asked for is a function that will work with any
    ordered measure (that wraps), not just some such measures. An
    example of such a measure is longitude, which goes from -180
    to +180 (with one of the two extreme values omitted). Similarly >
    there is no reason not to allow a measure that is only positive
    integers but does not include zero. An important part of the
    challenge is to come up with a solution that handles these cases
    as well as the more obvious ones.

    Okay, so how about this ... ?

    int in_subrange(int_fast64_t range_min, int_fast64_t range_max,
    int_fast64_t start, int_fast64_t end, int_fast64_t check)
    {
    while ( check < range_min )
    check += range_max - range_min;

    while ( check > range_max )
    check -= range_max - range_min;

    if ( ( end < start && (
    ( check > range_min && check <= end )
    || (check >= start && check < range_max)
    )
    )
    || ( check >= start && check <= end )
    )
    return 1;

    return 0
    }

    Several things stand out to me.

    The code seems confused about whether range_min and range_max are
    legal values or just outside of legal values. Normally I would
    expect them to be legal values, since the "just outside of legal
    values" choice might fall outside the range of the type being used.

    I think >= min and < max, so if min were 0 then mod max would be a valid
    value.


    Assuming range_min and range_max are legal values, the code has an
    off-by-one error, namely, it should be range_max - range_min + 1
    that is added or subtracted to bring 'check' into the legal range.

    Yes, Dmitry pointed this out too.


    There is still the problem of potential overflow, because the value
    of the expression 'range_max - range_min' might exceed the bounds of
    the type being used.

    Minor comment: the choice of 'int_fast64_t' for the parameter type
    is kind of surprising. Why not 'int_least64_t' or maybe even just
    'long long'?

    Yes, int_least64_t is probably better.


    The expressions 'check <= end' should be 'check < end' to conform to
    the problem statement that the interval being considered excludes
    the end value.

    Okay.


    If range_min and range_max are legal values, the tests for 'check'
    being inside the range become superfluous.

    Incidentally, since the problem statement doesn't say, to my way of
    thinking it would be fine simply to exclude values of 'check' that
    fall outside the legal range

    if( check < range_min || check > range_max ) return 0;

    which has the nice by-product of avoiding the overflow problem.

    Yes, that could/should be moved outside the function.


    Let's look again at the key test, simplified so as not to do the
    range tests (some redundant parentheses retained):

    if(
    (end < start && (check <= end || check >= start))
    || (check >= start && check <= end)
    ){
    return 1;
    }

    I think the logic of this test is right, but the form of it makes me
    nervous. The reason for that is the second line depends on the
    condition 'start <= end' being true, but that condition is not
    explicitly tested. The condition is /implied/ by the tests that are
    done against 'check', but that result is not immediately obvious. I
    think a better writing would be to use a ?: operator, as for example

    if(
    end < start
    ? start <= check || check < end
    : start <= check && check < end
    ){
    return 1;
    }

    Now it is immediately obvious that the two cases are mutually
    exclusive.

    Finally, consider the last two statements. Since the penultimate
    statement conditionally returns 1 (true) and the last statement
    simply returns 0 (false), these two statements can be combined into
    a single 'return' statement:

    return
    end < start
    ? start <= check || check < end
    : start <= check && check < end
    ;

    And I think that is the solution you orignally gave (?)


    (I hope you will excuse my tendency to try to simplify almost any
    code that I see. I appreciate what you've done here.)

    For me, vebose is simple and I'd only condense it down once I understand
    it / know its correct.

    It's okay for check to have 'clocked', range_min, range_max, start
    and end have sensible values.

    What I think you mean by this is that 'start' and 'end' can be
    assumed to have values within the legal range, and that range_min
    and range_max have sensible values (so range_min <= range_max),
    and that 'check' might fall outside the legal range, and that
    possibility must be accounted for. That makes sense.

    That is what I meant, yes.

    Thanks for taking your time.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Tim Rentsch@21:1/5 to Ben Bacarisse on Thu Dec 1 04:30:45 2022
    Ben Bacarisse <ben.usenet@bsb.me.uk> writes:

    "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> writes:

    [...]

    function Flight_East_Crosses_Longitude
    ( Start, Stop, X : Longtitude
    ) return Boolean is
    begin
    if Start <= Stop then
    return X in Start..Stop;
    else
    return X <= Stop or else X >= Start;
    end if;
    end Flight_East_Crosses_Longitude;

    Except for some boundary cases [ie, the region being half open] that
    have got lost in the telling, this is similar to Tim's solution. I
    chose to use a recursive call, because I though it explained the
    non-trivial case more clearly (but I bet I am pretty much the only
    one who thinks that).

    I want to add something here to my earlier comment. The idea of
    using a recursive call reflects a deeper understanding of what
    "circular regions" are. If one has already assimilated that
    understanding then I think the recursive call is "more obvious",
    in the sense that it takes less thought, or I might say less
    additional thought. I didn't have that background (and didn't
    develop it while solving the problem) so for me the cruder but
    more direct approach was easier to see. Bottom line, I don't
    think either formulation is uniformly "easier to understand" than
    the other; it depends on one's background (or ability to develop
    a suitable understanding on the fly, which in this case I did not
    possess).

    This problem provides an example where it helps to see both
    approaches to solving the problem, to see how they relate to each
    other, but also to give an appreciation for the power of having
    more advanced tools available in the mental toolbox.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Tim Rentsch@21:1/5 to Julio Di Egidio on Thu Dec 1 06:55:39 2022
    Julio Di Egidio <julio@diegidio.name> writes:

    On Wednesday, 30 November 2022 at 11:24:17 UTC+1, Tim Rentsch wrote:

    Julio Di Egidio <ju...@diegidio.name> writes:
    [edited for brevity]

    On Tuesday, 29 November 2022 at 13:29:48 UTC+1, Tim Rentsch wrote:
    On Monday, 21 November 2022 at 21:45:34 UTC+1, Ben Bacarisse wrote:

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

    The question can be answered without using any of subtraction,
    remainder, or mod,

    No, it cannot. But feel free to prove me wrong.

    At this point I see no reason to try to convince you.

    You have never had any reason, so that is hardly surprising.

    As you may have observed, I often put in time and effort to give
    further explanation of an earlier comment. I'm more likely to do
    that when I think the person I'm responding to is interested in
    hearing what I have to say.

    I hope you will understand if I choose not to read or respond to
    your future postings.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Tim Rentsch@21:1/5 to Richard Harnden on Thu Dec 1 06:51:20 2022
    Richard Harnden <richard.nospam@gmail.com> writes:

    On 30/11/2022 11:32, Tim Rentsch wrote:

    [...]

    Let's look again at the key test, simplified so as not to do the
    range tests (some redundant parentheses retained):

    if(
    (end < start && (check <= end || check >= start))
    || (check >= start && check <= end)
    ){
    return 1;
    }

    I think the logic of this test is right, but the form of it makes me
    nervous. The reason for that is the second line depends on the
    condition 'start <= end' being true, but that condition is not
    explicitly tested. The condition is /implied/ by the tests that are
    done against 'check', but that result is not immediately obvious. I
    think a better writing would be to use a ?: operator, as for example

    if(
    end < start
    ? start <= check || check < end
    : start <= check && check < end
    ){
    return 1;
    }

    Now it is immediately obvious that the two cases are mutually
    exclusive.

    Finally, consider the last two statements. Since the penultimate
    statement conditionally returns 1 (true) and the last statement
    simply returns 0 (false), these two statements can be combined into
    a single 'return' statement:

    return
    end < start
    ? start <= check || check < end
    : start <= check && check < end
    ;

    And I think that is the solution you orignally gave (?)

    Mathematically equivalent (my code used different operand orders).
    Notice though that my derivation above simply started with your
    code and went forward. I wasn't re-engineering the problem, just
    revising what you had written to put it in an easier-to-understand
    form. (I should add that I did modify the end boundary condition
    test, but that is incidental.) That the result was equivalent to
    what I wrote shows that your original code has the same behavior
    as mine, and only that; I didn't mean to compare them or offer
    any sort of correction (and in fact I wasn't thinking of what
    I had written while I was replying to your posting).

    (I hope you will excuse my tendency to try to simplify almost any
    code that I see. I appreciate what you've done here.)

    For me, vebose is simple and I'd only condense it down once I
    understand it / know its correct.

    When I say "simplify" I don't mean make shorter but make it easier
    for me to convince myself that the code is correct. A lot of
    times shorter code is easier to understand, but shortness is not
    the metric for simplicity. Code that makes it easier for me to
    convince myself that the code is correct is simpler, no matter
    how condensed or compact it is. What motivated me here was that
    the original code made it hard for me to convince myself that the
    code was correct.

    [...]

    Thanks for taking your time.

    Thank you, it's always pleasing to hear appreciation for my
    efforts.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ben Bacarisse@21:1/5 to Tim Rentsch on Fri Dec 2 00:22:08 2022
    Tim Rentsch <tr.17687@z991.linuxsc.com> writes:

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

    "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> writes:

    [...]

    function Flight_East_Crosses_Longitude
    ( Start, Stop, X : Longtitude
    ) return Boolean is
    begin
    if Start <= Stop then
    return X in Start..Stop;
    else
    return X <= Stop or else X >= Start;
    end if;
    end Flight_East_Crosses_Longitude;

    Except for some boundary cases [ie, the region being half open] that
    have got lost in the telling, this is similar to Tim's solution. I
    chose to use a recursive call, because I though it explained the
    non-trivial case more clearly (but I bet I am pretty much the only
    one who thinks that).

    I want to add something here to my earlier comment. The idea of
    using a recursive call reflects a deeper understanding of what
    "circular regions" are. If one has already assimilated that
    understanding then I think the recursive call is "more obvious",
    in the sense that it takes less thought, or I might say less
    additional thought. I didn't have that background (and didn't
    develop it while solving the problem) so for me the cruder but
    more direct approach was easier to see. Bottom line, I don't
    think either formulation is uniformly "easier to understand" than
    the other; it depends on one's background (or ability to develop
    a suitable understanding on the fly, which in this case I did not
    possess).

    For me, the negated recursive call was a sort of "ah ha!" moment. I was ploughing forwards trying to work out this and that case when a
    light-bulb went off.

    Above I say "it explained the non-trivial case more clearly" but that's
    lazy wording and does not capture what I meant. Rather than explaining anything, I want code that is easy to verify. I want, with just a
    little thought, to know it's right.

    This problem provides an example where it helps to see both
    approaches to solving the problem, to see how they relate to each
    other, but also to give an appreciation for the power of having
    more advanced tools available in the mental toolbox.

    I've got rather fond of it as a question. I don't conduct any
    interviews anymore or I would be tempted to use it.

    --
    Ben.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Julio Di Egidio@21:1/5 to Tim Rentsch on Fri Dec 2 01:54:18 2022
    On Thursday, 1 December 2022 at 15:55:43 UTC+1, Tim Rentsch wrote:
    Julio Di Egidio <ju...@diegidio.name> writes:
    On Wednesday, 30 November 2022 at 11:24:17 UTC+1, Tim Rentsch wrote:
    Julio Di Egidio <ju...@diegidio.name> writes:
    [edited for brevity]
    On Tuesday, 29 November 2022 at 13:29:48 UTC+1, Tim Rentsch wrote:
    On Monday, 21 November 2022 at 21:45:34 UTC+1, Ben Bacarisse wrote: >>>
    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 en value.

    The question can be answered without using any of subtraction,
    remainder, or mod,

    No, it cannot. But feel free to prove me wrong.

    At this point I see no reason to try to convince you.

    You have never had any reason, so that is hardly surprising.

    As you may have observed, I often put in time and effort to give
    further explanation of an earlier comment. I'm more likely to do
    that when I think the person I'm responding to is interested in
    hearing what I have to say.

    You insane retards and the end of the world... Of course I
    tell you about plane crashes and you even more so behave
    like a retarded queer. You do are an utter imbecille.

    I hope you will understand if I choose not to read or respond to
    your future postings.

    Sure I do, you umpteenth piece of fraudulent abusive retard.
    But I hope you will understand if every once in a while I still
    point out some of the bestiality of your and this bandwagon's
    insanity.

    After all, if you guys like selling, buying and eating shit, who
    am I to tell you not to... if you retarded cunts didn't shit all
    outside your box, that is.

    Enough said for now.

    Julio

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Tim Rentsch@21:1/5 to Ben Bacarisse on Fri Dec 2 22:30:39 2022
    Ben Bacarisse <ben.usenet@bsb.me.uk> writes:

    Tim Rentsch <tr.17687@z991.linuxsc.com> writes:

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

    For reference here is my earlier answer [with return type changed
    to 'bool']:

    bool
    is_circularly_between( T a, T b, T c ){
    return a <= c ? a <= b && b < c : a <= b || b < c;
    }

    [In contrast to the definition above] I
    chose to use a recursive call, because I though it explained the
    non-trivial case more clearly (but I bet I am pretty much the only
    one who thinks that).

    I want to add something here to my earlier comment. The idea of
    using a recursive call reflects a deeper understanding of what
    "circular regions" are. If one has already assimilated that
    understanding then I think the recursive call is "more obvious",
    in the sense that it takes less thought, or I might say less
    additional thought. I didn't have that background (and didn't
    develop it while solving the problem) so for me the cruder but
    more direct approach was easier to see. Bottom line, I don't
    think either formulation is uniformly "easier to understand" than
    the other; it depends on one's background (or ability to develop
    a suitable understanding on the fly, which in this case I did not
    possess).

    For me, the negated recursive call was a sort of "ah ha!" moment. I
    was ploughing forwards trying to work out this and that case when a light-bulb went off.

    For reference here is your recursive version:

    bool is_circularly_between(T start, T end, T i) {
    return start <= end ? start <= i && i < end
    : !is_circularly_between(end, start, i);
    }

    Above I say "it explained the non-trivial case more clearly" but
    that's lazy wording and does not capture what I meant. Rather than explaining anything, I want code that is easy to verify. I want,
    with just a little thought, to know it's right.

    Even after understanding the "ah ha!", I am still not entirely happy
    with this version. Even though I know the trick, there is still a
    mental slowdown around the negated recursive call. I can see that
    it works, but it takes a little bit of mental effort each time.

    Thinking a little more about how to write an answer, I came up with
    this:

    extern bool is_circularly_between( T, T, T );
    extern bool is_circularly_outside( T, T, T );

    bool
    is_circularly_between( T start, T limit, T x ){
    if( start <= limit ) return start <= x && x < limit;

    return is_circularly_outside( limit, start, x );
    }

    bool
    is_circularly_outside( T start, T limit, T x ){
    if( start <= limit ) return x < start || limit <= x;

    return is_circularly_between( limit, start, x );
    }

    For me this version is mentally smoother than the negated directly
    recursive call. Part of the reason for that is having the two
    complementary functions shows the duality more explicitly. Of
    course I never would have gotten here if not for your insight; even
    so, I'm inclined to prefer this version on the metric of which is
    easier to convince myself that it's right.


    This problem provides an example where it helps to see both
    approaches to solving the problem, to see how they relate to each
    other, but also to give an appreciation for the power of having
    more advanced tools available in the mental toolbox.

    I've got rather fond of it as a question. I don't conduct any
    interviews anymore or I would be tempted to use it.

    It's a great question. I'm glad you posted it.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Julio Di Egidio@21:1/5 to Tim Rentsch on Sat Dec 3 02:43:13 2022
    On Saturday, 3 December 2022 at 07:30:46 UTC+1, Tim Rentsch wrote:
    Ben Bacarisse <ben.u...@bsb.me.uk> writes:
    <snip>
    For reference here is my earlier answer [with return type changed
    to 'bool']:

    bool
    is_circularly_between( T a, T b, T c ){
    return a <= c ? a <= b && b < c : a <= b || b < c;
    }

    What mental gymnastics? To begin with, that is just
    a pain to read with those variables names plus their
    unconventional order. Written slightly better, that is:

    bool is_circularly_between(T x, T lo, T hi){
    return lo <= hi ? lo <= x && x < hi : lo <= x || x < hi;
    }

    That said, circular over what exactly? Namely, define
    constructors for T: how is someone, starting from the
    usual machine types, supposed to get to calling your
    function? (Try and see what you cannot but end up
    with: there are only two options...)

    Julio

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stefan Ram@21:1/5 to Richard Heathfield on Wed Dec 14 13:23:38 2022
    Richard Heathfield <rjh@cpax.org.uk> writes:
    Your problem is closely related to the very first question I was
    ever posed (in early 1982), by a friend who needed to be able to
    establish cleanly in a single expression whether a keypress was a
    digit (ASCII 48-57). The relevant dialect of BASIC didn't have
    anything like an isdigit function.
    The friend was on the point of giving up on me when I handed him
    ABS(K-52.5)<4.5

    "K >= 48 AND K <= 57" also is a single expression in BASIC.

    For the problem to find whether a point x is in an interval [a,b],
    I'd rotate the problem, so that the interval starts at 0.

    x =( x - a )% max
    b =( b - a )% max
    return 0 <= x <= b

    Here I have used Python's floored modulo operator and Python
    notation for "0 <= x and x <= b".

    For the case that only a and b are given, but not whether
    the interval is [a,b] or [b,a], I'd choose that interval
    the length of which is smaller than 12 (if possible).

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Richard Heathfield@21:1/5 to Stefan Ram on Wed Dec 14 13:57:18 2022
    On 14/12/2022 1:23 pm, Stefan Ram wrote:
    Richard Heathfield <rjh@cpax.org.uk> writes:
    Your problem is closely related to the very first question I was
    ever posed (in early 1982), by a friend who needed to be able to
    establish cleanly in a single expression whether a keypress was a
    digit (ASCII 48-57). The relevant dialect of BASIC didn't have
    anything like an isdigit function.
    The friend was on the point of giving up on me when I handed him
    ABS(K-52.5)<4.5

    "K >= 48 AND K <= 57" also is a single expression in BASIC.

    The available keywords were LET, PRINT, END, FOR...NEXT, GOTO,
    GOSUB...RETURN, IF...THEN line number, DEF, READ, DATA, DIM, and
    REM. AND was not amongst them. Also available were these
    functions: ABS, ATN (arc tan), COS, EXP, INT, LOG, RND, SIN, SQR
    (root).

    --
    Richard Heathfield
    Email: rjh at cpax dot org dot uk
    "Usenet is a strange place" - dmr 29 July 1999
    Sig line 4 vacant - apply within

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Richard Heathfield@21:1/5 to Richard Heathfield on Wed Dec 14 13:58:08 2022
    On 14/12/2022 1:57 pm, Richard Heathfield wrote:
    On 14/12/2022 1:23 pm, Stefan Ram wrote:
    Richard Heathfield <rjh@cpax.org.uk> writes:
    Your problem is closely related to the very first question I was
    ever posed (in early 1982), by a friend who needed to be able to
    establish cleanly in a single expression whether a keypress was a
    digit (ASCII 48-57). The relevant dialect of BASIC didn't have
    anything like an isdigit function.
    The friend was on the point of giving up on me when I handed him
                      ABS(K-52.5)<4.5

       "K >= 48 AND K <= 57" also is a single expression in BASIC.

    The available keywords were LET, PRINT, END, FOR...NEXT, GOTO, GOSUB...RETURN, IF...THEN line number, DEF, READ, DATA, DIM, and
    REM. AND was not amongst them. Also available were these
    functions: ABS, ATN (arc tan), COS, EXP, INT, LOG, RND, SIN, SQR
    (root).

    ...and I missed TAN.

    --
    Richard Heathfield
    Email: rjh at cpax dot org dot uk
    "Usenet is a strange place" - dmr 29 July 1999
    Sig line 4 vacant - apply within

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stefan Ram@21:1/5 to Richard Heathfield on Wed Dec 14 15:43:25 2022
    Richard Heathfield <rjh@cpax.org.uk> writes:
    On 14/12/2022 1:23 pm, Stefan Ram wrote:
    Richard Heathfield <rjh@cpax.org.uk> writes:
    Your problem is closely related to the very first question I was
    ever posed (in early 1982), by a friend who needed to be able to
    establish cleanly in a single expression whether a keypress was a
    digit (ASCII 48-57). The relevant dialect of BASIC didn't have
    anything like an isdigit function.
    The friend was on the point of giving up on me when I handed him
    ABS(K-52.5)<4.5
    "K >= 48 AND K <= 57" also is a single expression in BASIC.
    The available keywords were LET, PRINT, END, FOR...NEXT, GOTO, >GOSUB...RETURN, IF...THEN line number, DEF, READ, DATA, DIM, and
    REM. AND was not amongst them. Also available were these
    functions: ABS, ATN (arc tan), COS, EXP, INT, LOG, RND, SIN, SQR
    (root).

    Sometimes, multiplication can be used for "AND".
    I.e., "( K >= 48 )*( K <= 57 )".

    Some BASIC dialects use "-1" for "True" and "0"
    for "False", but accept any non-zero value for "True".

    Pet 2001 transcript (emulator):

    |LET K=50
    |
    |READY.
    |IF( K >= 48 )*( K <= 57 )THEN PRINT "YES."
    |YES.
    |
    |READY.

    .

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Richard Heathfield@21:1/5 to Stefan Ram on Wed Dec 14 16:22:04 2022
    On 14/12/2022 3:43 pm, Stefan Ram wrote:
    Richard Heathfield <rjh@cpax.org.uk> writes:
    On 14/12/2022 1:23 pm, Stefan Ram wrote:
    Richard Heathfield <rjh@cpax.org.uk> writes:
    Your problem is closely related to the very first question I was
    ever posed (in early 1982), by a friend who needed to be able to
    establish cleanly in a single expression whether a keypress was a
    digit (ASCII 48-57). The relevant dialect of BASIC didn't have
    anything like an isdigit function.
    The friend was on the point of giving up on me when I handed him
    ABS(K-52.5)<4.5
    "K >= 48 AND K <= 57" also is a single expression in BASIC.
    The available keywords were LET, PRINT, END, FOR...NEXT, GOTO,
    GOSUB...RETURN, IF...THEN line number, DEF, READ, DATA, DIM, and
    REM. AND was not amongst them. Also available were these
    functions: ABS, ATN (arc tan), COS, EXP, INT, LOG, RND, SIN, SQR
    (root).

    Sometimes, multiplication can be used for "AND".
    I.e., "( K >= 48 )*( K <= 57 )".

    Yes, I know. Barely a fortnight after using a mainframe computer
    for the first time, I didn't know it /then/. With 40 years of
    programming experience, it is not difficult to devise better
    solutions to the problems I encountered 40 years ago. But when
    you're there, you have to use what knowledge you have and what
    you can dredge up from the photostats that served as a manual.

    --
    Richard Heathfield
    Email: rjh at cpax dot org dot uk
    "Usenet is a strange place" - dmr 29 July 1999
    Sig line 4 vacant - apply within

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)