Path: csiph.com!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.programming Subject: Re: A little puzzle. Date: Tue, 29 Nov 2022 04:03:46 -0800 Organization: A noiseless patient Spider Lines: 63 Message-ID: <86lenu6k7x.fsf@linuxsc.com> References: <875yf8nijb.fsf@bsb.me.uk> <86pmd76qrg.fsf@linuxsc.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader01.eternal-september.org; posting-host="5ea699727033ad6d8619c5ade6627e46"; logging-data="2388541"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18CtBIS/i2o+J5cQk/6tOVwkJnWfWmCoIk=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:5GK8BK0Cww8xgqccSjbuTIwAUUU= sha1:nlD3onbn260fESMiH7yOsRqhajQ= Xref: csiph.com comp.programming:15989 Richard Harnden writes: > On 28/11/2022 15:30, Tim Rentsch wrote: > >> Richard Harnden 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.