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.
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 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...
Next page...
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.
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.
But then your problem comes down to nothing more than a "modulo"
function and a comparison, which sounds far too simple a "puzzle".
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?
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.
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 problemI don't follow. What is "my" n? I did not mention an n.
becomes quite difficult if your measure is the reals in [0, 1) and
your "n" is, say, π/4...
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...
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 don't follow. What is "my" n? I did not mention an n.
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 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".
The question I think you're asking is to write a function like this
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.
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:
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 <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 interpretedThe circular wrapping. On a clock, 55 is in the range of minutes that
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?
starts at 45 and ends at 5.
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".
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.
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?
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.
David Brown <david.brown@hesbynett.no> writes:
On 21/11/2022 22:21, Ben Bacarisse wrote:
David Brown <david.brown@hesbynett.no> writes:
Are there any restrictions, such as sticking to integers? The problem >>>> becomes quite difficult if your measure is the reals in [0, 1) andI don't follow. What is "my" n? I did not mention an n.
your "n" is, say, π/4...
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!
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.
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;
}
OK. We are talking ranges, rather than points.
David Brown <david.brown@hesbynett.no> writes:
OK. We are talking ranges, rather than points.
Yes. So much confusion from a missing 'd'! Sorry.
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);
}
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.
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... :)
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.
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.
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,I didn't make a good job of presenting it. It certainly didn't pique
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... :)
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 <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,I didn't make a good job of presenting it. It certainly didn't pique
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... :)
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.
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.
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:I am saddened that you think I would have made a hash of that and amazed
Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
I think this problem would make a good interview question,I didn't make a good job of presenting it. It certainly didn't pique
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... :)
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.
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.
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.
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?
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.
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.
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.
"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?
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.
"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?
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.
Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
Consider a plane flying easterly (and for simplicity, directlySo I don't think it would matter to help DAK by giving
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.
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.
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:I am saddened that you think I would have made a hash of that and amazed
Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
I think this problem would make a good interview question,I didn't make a good job of presenting it. It certainly didn't pique
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... :)
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.
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"?
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,I didn't make a good job of presenting it. It certainly didn't pique
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... :)
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.
On 2022-11-26 17:25, Ben Bacarisse wrote:<cut my outline>
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.
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;
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?
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.
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.
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;
}
```
Moreover, notice that this MOD fails (in particular, does not
return positive) for m <= 0. (!!) But I won't bother with that now.
Julio Di Egidio <ju...@diegidio.name> writes:<snip>
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.
In terms of remainder (as in JS), MOD looks like this:
```js
```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; }This proposed function doesn't work. Consider a curfew that
else { x = (m + x % m) % m; }
return x;
}
```
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.
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.
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;
}
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_;
}
```
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?
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,
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.
This one should do the trick:
```tsThis scheme looks like it will work, as long as the values given
/** 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_;
}
```
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?
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.
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.
But if you
want your postings to be taken seriously it's important to put
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.
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:
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?
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;
It's okay for check to have 'clocked', range_min, range_max, start and
end have sensible values.
How long have you being doing this job? You come up as yet
another clueless arrogant ass.
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.
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.
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.
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.
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.
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.
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 <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?
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.
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.
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
}
It's okay for check to have 'clocked', range_min, range_max, start
and end have sensible values.
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
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.
I first posted in net news (back when it was called usenet) back
in the mid 1980s. My first experience with computer programming
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.
"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).
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.
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 (?)
(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.
[...]
Thanks for taking your time.
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.
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.
I hope you will understand if I choose not to read or respond to
your future postings.
Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
[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.
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 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;
}
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
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.
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).
On 14/12/2022 1:23 pm, Stefan Ram wrote:
Richard Heathfield <rjh@cpax.org.uk> writes:The available keywords were LET, PRINT, END, FOR...NEXT, GOTO, >GOSUB...RETURN, IF...THEN line number, DEF, READ, DATA, DIM, and
Your problem is closely related to the very first question I was"K >= 48 AND K <= 57" also is a single expression in BASIC.
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
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 <rjh@cpax.org.uk> writes:
On 14/12/2022 1:23 pm, Stefan Ram wrote:
Richard Heathfield <rjh@cpax.org.uk> writes:The available keywords were LET, PRINT, END, FOR...NEXT, GOTO,
Your problem is closely related to the very first question I was"K >= 48 AND K <= 57" also is a single expression in BASIC.
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
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 )".
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 491 |
Nodes: | 16 (2 / 14) |
Uptime: | 105:44:02 |
Calls: | 9,684 |
Calls today: | 5 |
Files: | 13,725 |
Messages: | 6,175,398 |
Posted today: | 1 |