if number is 1-2, 4 or 6-8 or 12-28, 30, 32...
tjreedy at udel.edu
Tue Jun 11 06:40:03 CEST 2002
"Gustaf Liljegren" <gustafl at algonet.se> wrote in message
news:Xns9229EE722467Fgustafl at 22.214.171.124...
> 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
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