if number is 1-2, 4 or 6-8 or 12-28, 30, 32...

Terry Reedy tjreedy at udel.edu
Tue Jun 11 00:40:03 EDT 2002


"Gustaf Liljegren" <gustafl at algonet.se> wrote in message
news:Xns9229EE722467Fgustafl at 195.100.94.182...
> I'm making a function that takes one integer and returns a boolean
that
> says if the integer matches a number in a certain group of numbers.
This
> group consists of single numbers aswell as ranges spanning thousands
of
> numbers. I'm looking for an elegant way to write this function.

After reading the other responses, I would try the following
(untested):
  sort ranges and merge contiguous ranges so there is a gap between
each
  express each range (including single ranges) as half-open range
[min,max+1)
  put 2n endpoints in a single list # they *will* be distinct by step
1
  test target number against first and last int in list
  if < first or >= last, no match; (if = first, match # not sure if
need this)
  if within that range, run binsort to get index of largest num <=
target
  # modify binsort or next statement if necessary
  if even(index) match = true else false

Terry J. Reedy








More information about the Python-list mailing list