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


Groups > comp.programming > #15974

Re: A little puzzle.

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.programming
Subject Re: A little puzzle.
Date 2022-11-27 02:03 +0000
Organization A noiseless patient Spider
Message-ID <875yf1i26r.fsf@bsb.me.uk> (permalink)
References (3 earlier) <86sfi98xnx.fsf@linuxsc.com> <87leo1i5bo.fsf@bsb.me.uk> <86k03k7yqe.fsf@linuxsc.com> <87o7stisxy.fsf@bsb.me.uk> <tltmbd$su3$1@gioia.aioe.org>

Show all headers | View raw


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

> On 2022-11-26 17:25, Ben Bacarisse wrote:
>> Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
>> 
>>> Consider a plane flying easterly (and for simplicity, directly
>>> above the equator).  We measure the plane's progress by its
>>> longitude, measured to the nearest second of arc (1/3600 of a
>>> degree), so from -180 degrees to 179 degrees 59 minutes 59
>>> seconds (or equivalently from -648000 seconds to 647999 seconds).
>>> We would like to know if a given longitude is between the plane's
>>> starting longitude and ending longitude.  (Note that the
>>> longitude measurements below are given in degrees, minutes, and
>>> seconds, but we may assume that internally they are represented
>>> as an integer number of seconds.)
>>>
>>> For example, if the plane flies
>>>
>>>     from Los Angeles, US (longitude -118 14' 37")
>>>     to New York, US (longitude -73 56' 7")
>>>
>>> does it cross over the longitude lines of
>>>
>>>     Denver, US (longitude -104 59' 30"), or
>>>     Moscow, Russia (longitude 37 37' 6")?
>>>
>>> Answer: the plane does cross the longitude line of Denver but not
>>> that of Moscow.
>>>
>>> Similarly, if the plane flies
>>>
>>>     from Beijing, China (longitude 116 23' 0")
>>>     to Los Angeles, US (longitude -118 14' 37")
>>>
>>> does it cross over
>>>
>>>     Tokyo, Japan (longitude 139 50' 32"),
>>>     Waikiki, Hawaii, US (longitude -157 50' 4"), or
>>>     Moscow, Russia (longitude 37 37' 6")?
>>>
>>> Answer: the plane does cross the longitude lines of Tokyo and
>>> Waikiki but not that of Moscow.
<cut my outline>

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

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

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

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

> --------------------------------------------------
> with Ada.Text_IO;  use Ada.Text_IO;
>
> procedure Test_Longtitude is
> --
> -- Longtitude in seconds
> --
>    type Longtitude is range -180 * 3600 .. 180 * 3600 - 1;
>
>    type Degree is range -180..180;
>    type Minute is range 0..59;
>    type Second is range 0..59;
>
>    function Compose (D : Degree; M : Minute; S : Second)
>       return Longtitude is
>       Result : constant Integer :=
>                   (Integer (D) * 60 + Integer (M)) * 60 + Integer (S);
>    begin
>       if Result <= Integer (Longtitude'Last) then
>          return Longtitude (Result);
>       else
>          return Longtitude (Result - 360 * 3600);
>       end if;
>    end Compose;
>
>    Beijing     : constant Longtitude := Compose (+116, 23,  0);
>    Los_Angeles : constant Longtitude := Compose (-118, 14, 37);
>    Denver      : constant Longtitude := Compose (-104, 59, 30);
>    New_York    : constant Longtitude := Compose ( -73, 56,  7);
>    Tokyo       : constant Longtitude := Compose (+139, 50, 32);
>    Waikiki     : constant Longtitude := Compose (-157, 50,  4);
>    Moscow      : constant Longtitude := Compose (+037, 37,  6);
>
>    function Flight_East_Crosses_Longitude
>             (  Start, Stop, X : Longtitude
>             )  return Boolean is
>    begin
>       if Start <= Stop then
>          return X in Start..Stop;
>       else
>          return X <= Stop or else X >= Start;
>       end if;
>    end Flight_East_Crosses_Longitude;

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

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

  Flight_East_Crosses_Longitude(X, X, X)

would be FALSE, and for any Y /= X

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

would be TRUE and FALSE respectively.]


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

-- 
Ben.

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


Thread

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

csiph-web