I have list of ranges which all of the ranges in that list has the same start and stop, but not the same step.
eg:
rr = [range(0, 10, 2), range(0, 10, 3)]
Of course, the list can contain more than only 2 ranges.
The sorted union of these ranges contains the following numbers:
u = [0, 2, 3, 4, 6, 8, 9]
I want to index into u (e.g., u[5] == 8).
The problem is that the range can be huge (millions) and I don't want to turn rr into u. I need somehow to calculate the value at an index without expanding the list.
Tried for several hours to think of an algorithm to do that, the best solution I found includes using binary search to do that, which I think it is not the ideal way of doing so.
You can make a new
rangewith the samestartandend, and pack allsteps to a new list. Now on each number from the range you can check if it matches any step. You can make this into a generator:Now you can loop on that generator until you reach the relevant index. According to your example:
And this will print out: