'Most Efficient Way To Find Which Range an Input Falls into from a List of ranges (as string)
Here is the situation: My workplace files engineering drawing pdfs by drawing number and sorts them into folders. There are 200+ of these folders. The folder is named with the range of drawing numbers that will go in the folder. For example: "0.001.000 - 0.001.999"
Most of them are from x.xxx.000 - x.xxx.999, however, there are a few that are x.000.000 - x.500.999. Additionally, any with a first digit 9 are done in the format 9.xxx.x00 - 9.xxx.x99. So basically, the range they will fall into is not always consistent.
I currently have two ways I have figured out to tackle this: (1) I take the input and use substrings to create the x.xxx., and then add the 000 and 999 on the end. I use if statements to handle the first digit 9 case and the others that don't follow the usual format, since I already know which ones these are
(2) The other method I came up with seems more elegant, but it also seems to be slower: I get all the folder names into a list. Then I using a for loop, I loop through every folder in the list. In this loop I first take out the .'s from the input and the folder name, and then do foldername.split("-"c) to get it into a min and max. Then I use a select case to see if input is between those two numbers. If it is, I set that as the folder to look in, and exit the loop. If not, go to the next folder and repeat. The problem is because it loops through 200+ folders, this process seems to be slower if you're looking up 10 or 20 drawing numbers
Is there a better way to do this? Perhaps a way to go directly to which folder it is without having to loop through a bunch for every input? I have some ideas for reducing the number of folders the loop will have to go through (for example, only looping through folders with the same first digit would in some cases speed it up quite a bit), but I am not sure if it's possible to bypass the loop entirely without using the first method, which while simple also seems a little more brute force.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
