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: Mon, 21 Nov 2022 17:39:44 -0800 Organization: A noiseless patient Spider Lines: 51 Message-ID: <86ilj7ahsv.fsf@linuxsc.com> References: <875yf8nijb.fsf@bsb.me.uk> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader01.eternal-september.org; posting-host="0b8f1b00ad451c4da87815129da31114"; logging-data="4122223"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+lmjQ9D1lUtHT+YK5Fm8zQauK4uB8fdPw=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:u4TUnODr55Suu55DAUwuvoPPp8M= sha1:ps7xqSJfzrqxuw701/fRF2zaFLc= Xref: csiph.com comp.programming:15920 Ben Bacarisse writes: > 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. The question I think you're asking is to write a function like this /* is_circularly_between( a, b, c ) - * 1 if b is circularly between a and c, * 0 otherwise * interval [ a, c ) is closed at the 'a' end, 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 ){ /* to be determined */ } with T being some integer type. Assuming this is right I have written such a function (but am not posting it just yet). > 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.