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,650 of 57,431    |
|    Richard Heathfield to Ben Bacarisse    |
|    Re: A little puzzle.    |
|    22 Nov 22 11:04:03    |
   
   From: rjh@cpax.org.uk   
      
   On 21/11/2022 8:45 pm, Ben Bacarisse wrote:   
   > I wonder if there are any real posters here?   
      
   Nobody here but us chickens.   
      
   > 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.   
   >   
   > Though I am not sure this merits the term "puzzle", I suggest that   
   > solutions be posted with some spoiler protection. Do all the news   
   > readers used by programmers (or ex programmers) all respect the presence   
   > of a form-feed character...   
      
   Dunno. Let's find out:   
      
   Ctrl-L coming up:   
      
      
      
   > ... like this? Because that's my favourite way, rather than posting   
   > lots of dummy lines before the solution.   
      
   Your problem is closely related to the very first question I was   
   ever posed (in early 1982), by a friend who needed to be able to   
   establish cleanly in a single expression whether a keypress was a   
   digit (ASCII 48-57). The relevant dialect of BASIC didn't have   
   anything like an isdigit function.   
      
   The friend was on the point of giving up on me when I handed him   
      
    ABS(K-52.5)<4.5   
      
   Nowadays we might render this in C as:   
      
   int inrange(int lo, int hi, int k)   
   {   
    return (k-(lo+hi)/2)<((hi-lo)/2);   
   }   
      
   although of course in C the problem would be far better solved as:   
      
   int inrange(int lo, int hi, int k)   
   {   
    return (lo <= k) && (k < hi);   
   }   
      
   or even as a macro.   
      
   I must confess I'm not entirely certain I have correctly   
   interpreted your puzzle, which I have taken to mean "is this a   
   given value in the given range", but this seems just a bit too   
   easy for you to make a hash of, but I'm sure I've made hashes of   
   worse. I've missed something, haven't I?   
      
   --   
   Richard Heathfield   
   Email: rjh at cpax dot org dot uk   
   "Usenet is a strange place" - dmr 29 July 1999   
   Sig line 4 vacant - apply within   
      
   --- 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