home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.programming      Programming issues that transcend langua      57,431 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 56,682 of 57,431   
   Ben Bacarisse to Tim Rentsch   
   Re: A little puzzle.   
   26 Nov 22 16:25:29   
   
   From: ben.usenet@bsb.me.uk   
      
   Tim Rentsch  writes:   
      
   > Ben Bacarisse  writes:   
   >   
   >> Tim Rentsch  writes:   
   >>   
   >>> I think this problem would make a good interview question,   
   >>> provided care were taken to phrase it so the subtleties were   
   >>> still there, but possible points of confusion were reduced.   
   >>> Not that I know how to do that... :)   
   >>   
   >> I didn't make a good job of presenting it.  It certainly didn't   
   >> pique anyone else's interest, but then comp.programming is not   
   >> well populated.   
   >   
   > I think part of the problem is that the question looks "easier"   
   > than it really is.  People have an initial reaction (certainly I   
   > know I did) but don't continue to think through what the issues   
   > might be.   
      
   I think so.  I am, to be honest, rather disappointed by the reception   
   this problem has had.  Even after various examples and two explanations,   
   some posters still don't know what's being asked, but at the time it is   
   claimed that it's trivial and not worth coding up.  Sometimes both   
   claims are made by the same poster.   
      
   > For what it's worth, here is a stab at presenting the problem.   
   > It's longer than I would like it to be, but maybe it will be   
   > helpful.   
   >   
   > We are interested in writing a function to answer a more general   
   > version of the example problem stated below.   
   >   
   > Consider a plane flying easterly (and for simplicity, directly   
   > above the equator).  We measure the plane's progress by its   
   > longitude, measured to the nearest second of arc (1/3600 of a   
   > degree), so from -180 degrees to 179 degrees 59 minutes 59   
   > seconds (or equivalently from -648000 seconds to 647999 seconds).   
   > We would like to know if a given longitude is between the plane's   
   > starting longitude and ending longitude.  (Note that the   
   > longitude measurements below are given in degrees, minutes, and   
   > seconds, but we may assume that internally they are represented   
   > as an integer number of seconds.)   
   >   
   > For example, if the plane flies   
   >   
   >    from Los Angeles, US (longitude -118 14' 37")   
   >    to New York, US (longitude -73 56' 7")   
   >   
   > does it cross over the longitude lines of   
   >   
   >    Denver, US (longitude -104 59' 30"), or   
   >    Moscow, Russia (longitude 37 37' 6")?   
   >   
   > Answer: the plane does cross the longitude line of Denver but not   
   > that of Moscow.   
   >   
   > Similarly, if the plane flies   
   >   
   >    from Beijing, China (longitude 116 23' 0")   
   >    to Los Angeles, US (longitude -118 14' 37")   
   >   
   > does it cross over   
   >   
   >    Tokyo, Japan (longitude 139 50' 32"),   
   >    Waikiki, Hawaii, US (longitude -157 50' 4"), or   
   >    Moscow, Russia (longitude 37 37' 6")?   
   >   
   > Answer: the plane does cross the longitude lines of Tokyo and   
   > Waikiki but not that of Moscow.   
   >   
   > For the general case we would like to handle any circular measure   
   > (for convenience having coordinates in some integer range).   
   >   
   > Exercise: write a function to answer this kind of question for   
   > circular measures in general.  You may assume integer coordinates   
   > and intervals that include the starting point but do not include   
   > the end point.  Give a suitable declaration for the function,   
   > and separately give a function definition to implement the given   
   > interface.   
      
   I think this is good, though the example with seconds of arc may appear   
   fiddly to some people.  Degrees would do, but then the examples would   
   become a bit fuzzy.   
      
   I would probably use times of day.  For example, my cheap "off-peak"   
   electricity is from 22:00 to 00:30 and from 02:30 to 7:30 but there are   
   all sorts possible examples: times of day when I do not want to be   
   disturbed, times when particular parking rules apply and so on.   
      
   The problem would then be to determine, given a time of day (in minutes   
   past midnight), if that time is within a particular period.   
      
   I don't think it's a spoiler to give, first, a very concrete example,   
   even including the function interface.  A second part of the question   
   would be by ask about generalising this interface to any "circular"   
   measure.   
      
   So I don't think it would matter to help DAK by giving   
      
     with Ada.Text_IO;   
     use Ada.Text_IO;   
      
     procedure Longitude is   
      
        function Flight_East_Crosses_Longitude   
          (Start_Seconds, End_Seconds, Longitude : Integer) return Boolean is   
        begin   
           -- Your code here   
        end Flight_East_Crosses_Longitude;   
      
        Beijing     : constant Integer := ((+116 * 60) + 23) * 60 +  0;   
        Los_Angeles : constant Integer := ((-118 * 60) + 14) * 60 + 37;   
        Tokyo       : constant Integer := ((+139 * 60) + 50) * 60 + 32;   
        Waikiki     : constant Integer := ((-157 * 60) + 50) * 60 +  4;   
        Moscow      : constant Integer := ((+037 * 60) + 37) * 60 +  6;   
      
     begin   
        Put_Line(Flight_East_Crosses_Longitude(Beijing, Los_Angeles,     
   Tokyo)'img);   
        Put_Line(Flight_East_Crosses_Longitude(Beijing, Los_Angeles,   
   Waikiki)'img);   
        Put_Line(Flight_East_Crosses_Longitude(Beijing, Los_Angeles,    
   Moscow)'img);   
     end Longitude;   
      
   Of course, Ada is one of the few languages where one can actually   
   implement largely type-generic functions, so it's a shame to be this   
   specific, but as I say, that could come later.   
      
   --   
   Ben.   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca