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
> I'm making a function that takes one integer and returns a boolean
> says if the integer matches a number in a certain group of numbers.
> group consists of single numbers aswell as ranges spanning thousands
> numbers. I'm looking for an elegant way to write this function.

After reading the other responses, I would try the following
  sort ranges and merge contiguous ranges so there is a gap between
  express each range (including single ranges) as half-open range
  put 2n endpoints in a single list # they *will* be distinct by step
  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 <=
  # 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