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

Terry Reedy tjreedy at udel.edu
Tue Jun 11 12:44:08 EDT 2002


> "Gustaf Liljegren" <gustafl at algonet.se>
wants elegant function matching int to any of multiple numbers and
ranges

My very-pseudo code had parity error at end.  Real Python code:

import bisect

# noncontiguous and sorted numbers and ranges: 1, 3, 10-300, 2999,
12345-98765
# expanded to (lo, hi+1) ranges and flattened to list - exercise for
OP
rangenums = (1,2, 3,4, 10,301, 2999,3000, 12345,98766)

for i in (0,1,2, 3,4, 10,20, 300,301, 90000,98765,98766,100000):
  print bisect.bisect(rangenums, i) %2,

0 1 0 1 0 1 1 1 0 1 1 0 0
>>>






More information about the Python-list mailing list