Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get index of concatenated range list

Tags:

python

range

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:

[range(0, 10, 2), range(0, 10, 3)]

Of course, the list can contain more than only 2 ranges.
The concatenated range list represents the following numbers:

[0, 2, 3, 4, 6, 8, 9]

I want to get the x index of the concatenated range list.
For example, the 5 index of that last example will be 8.

The problem is that the range can be huge (millions) and I don't want to turn that range into a list to get the x index. I need somehow to calculate the value of that x index without "opening" that range 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.

Any idea of how can I acheive that?

like image 330
UdiM Avatar asked May 06 '20 14:05

UdiM


People also ask

How do I concatenate values in an array in Excel?

The picture above shows an array formula in cell F3 that looks for value "Fruit" in column B and concatenates corresponding values in column C. The TEXTJOIN function introduced in Excel 2019 allows you to easily concatenate values, it also accepts arrays and nested functions.

How to look up multiple values within a date range?

Lookup within a date range and return multiple values concatenated into one cell The following formula use a start and end date to filter dates in col C (table2) and return corresponding items on the same row in col A to cell D3. Array formula in cell D3 (first worksheet above):

How to use the index/match function with the match function?

And the area set up for the INDEX/MATCH formula also separates the Year and Quarter: Well, the key is to CONCATENATE the lookup values of the MATCH function along with CONCATENATING the ranges or arrays that the MATCH function searches, and then enter the formula using CTRL + SHIFT + ENTER to make it an array formula.

How to get the index of an element in a list?

Python List Comprehension can be used to avail the list of indices of all the occurrences of a particular element in a List. Using List comprehension, we can get the index values i.e. position of all the occurrences of an item in the List. Python’s inbuilt index () method can be used to get the index value of a particular element of the List.


1 Answers

You can make a new range with the same start and end, and pack all steps 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:

def steps_range(start, end, steps):
    for i in range(start, end):
        if any(i % x == 0 for x in steps):
            yield i

Now you can loop on that generator until you reach the relevant index. According to your example:

ranges = [range(0, 10, 2), range(0, 10, 3)]

start = ranges[0].start
end = ranges[0].stop
steps = [r.step for r in ranges]

target_index = 5

for i, num in enumerate(steps_range(start, end, steps)):
    print(num)
    if i == target_index:
        break

And this will print out:

0
2
3
4
6
8
like image 119
Tomerikoo Avatar answered Nov 15 '22 05:11

Tomerikoo