I do not know how correct the title itself looks, but I will describe it in more detail here.
So, the essence is this: there is, for example, a certain range and a couple of conditions:
$startand$endalways have the same character length (although this is not so important);And if there is a range that includes the element
555123456(9characters long), then there is no other range that would include a number that is longer than the existing one and starts from555- i.e. The number 555123456 7 (length - 10 characters) does not exist by definition.The last digits in
$startand$end0and9respectively, which also makes the task easier.
Those. (hereinafter I write conditionally, without reference to any programming language or regexp standard)
555000000 - 555999999 // Т.е. 555* 666000000 - 666999999 // Т.е. 666* 5550000000 - 5559999999 // Не существует в списке диапазонов по определению задачи, игнорируем. 6660000000 - 6669999999 // Не существует в списке диапазонов по определению задачи, игнорируем. In other words: if 555000000 - 555999999 is specified, then no 555000000 0 - 555999999 9
Take for example
555000000 - 555999999 Task: convert to 555* . It’s easy to figure out this example - you need to subtract $start from $end to get a certain $result variable, which is equal to 999999 . Then use the result value of the $result.length() operation relative to, say, $start , to get the line 555*
$shortrec = substr($start,0,-strlen($result)) . "*"; But if the range looks like
777120000 - 777259999 then the difference will be 139999 . Here it is more difficult, but also understandable - in addition to partly indicated above, we compare positions 0 and 9 in both variables, “exit” by 12 and process 12 additionally, increasing it 13 times and getting the following sequence in a loop:
77712* 77713* 77714* ... 77725* But what if there is a range,
888125120 - 888959599 (here we will recall condition number 3 facilitating logic, so as not to operate with an expression like [5-9] if $start would be 88812512 5 ) for which you need to get
88812512* 8882* 8883* ... 8888* 88890* 88891* 88892* ... 88894* 888950* 888951* 888952* ... 888958* 8889590* 8889591* 8889592* ... 8889595* - I ask you to judge whether I follow the correct logic from the very beginning or not?
- And is there some universal solution (algorithm) for such problems?
- What would you do?
PS Programming language is not important - the approach itself and the algorithm are important. I hope I did not break the brain reader, but the task is not from the training, but the real one. So if there are ideas, I will be very grateful.