Path: csiph.com!news.mixmin.net!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.programming Subject: Re: A little puzzle. Date: Thu, 24 Nov 2022 14:51:21 -0800 Organization: A noiseless patient Spider Lines: 72 Message-ID: <86k03k7yqe.fsf@linuxsc.com> References: <875yf8nijb.fsf@bsb.me.uk> <865yf79l66.fsf@linuxsc.com> <87wn7nj9mb.fsf@bsb.me.uk> <86sfi98xnx.fsf@linuxsc.com> <87leo1i5bo.fsf@bsb.me.uk> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader01.eternal-september.org; posting-host="8ee213438f0e6ab517cae470e775d09b"; logging-data="795816"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18ZEozvJ+JXQuf4FNmF2A/fWD0Zf/ui6Yw=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:hmWE8gDl9cUXwru9aAsoRYqlTQs= sha1:sp5db/ZSMO/SptYZJPwp18sq4jQ= Xref: csiph.com comp.programming:15964 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. 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.