Question

(Python) Given two numbers A and B. I need to find all nested "groups" of numbers:

range(2169800, 2171194)

leading numbers: 21698XX, 21699XX, 2170XX, 21710XX, 217110X, 217111X, 
217112X, 217113X, 217114X, 217115X, 217116X, 217117X, 217118X, 2171190X, 
2171191X, 2171192X, 2171193X, 2171194X

or like this:

range(1000, 1452)

leading numbers: 10XX, 11XX, 12XX, 13XX, 140X, 141X, 142X, 143X, 
144X, 1450, 1451, 1452
Was it helpful?

Solution

Harder than it first looked - pretty sure this is solid and will handle most boundary conditions. :) (There are few!!)

def leading(a, b):
    # generate digit pairs a=123, b=456 -> [(1, 4), (2, 5), (3, 6)]
    zip_digits = zip(str(a), str(b))
    zip_digits = map(lambda (x,y):(int(x), int(y)), zip_digits)

    # this ignores problems where the last matching digits are 0 and 9
    # leading (12000, 12999) is same as leading(12, 12)
    while(zip_digits[-1] == (0,9)):         
        zip_digits.pop()            

    # start recursion
    return compute_leading(zip_digits)

def compute_leading(zip_digits):
    if(len(zip_digits) == 1):   # 1 digit case is simple!! :)
        (a,b) = zip_digits.pop()
        return range(a, b+1)

    #now we partition the problem
    # given leading(123,456) we decompose this into 3 problems
    # lows    -> leading(123,129)
    # middle  -> leading(130,449) which we can recurse to leading(13,44)
    # highs   -> leading(450,456)

    last_digits = zip_digits.pop()
    low_prefix  = reduce(lambda x, y : 10 * x + y, [tup[0] for tup in zip_digits]) * 10     # base for lows e.g. 120
    high_prefix = reduce(lambda x, y : 10 * x + y, [tup[1] for tup in zip_digits]) * 10     # base for highs e.g. 450
    lows = range(low_prefix + last_digits[0], low_prefix + 10)
    highs = range(high_prefix + 0, high_prefix + last_digits[1] + 1)

    #check for boundary cases where lows or highs have all ten digits
    (a,b) = zip_digits.pop()    # pop last digits of middle so they can be adjusted
    if len(lows) == 10:
        lows = []
    else:
        a = a + 1

    if len(highs) == 10:
        highs = []
    else:
        b = b - 1

    zip_digits.append((a,b))    # push back last digits of middle after adjustments

    return lows + compute_leading(zip_digits) + highs       # and recurse - woohoo!!



print leading(199,411)

print leading(2169800, 2171194)

print leading(1000, 1452)

OTHER TIPS

def foo(start, end):
    index = 0
    is_lower = False
    while index < len(start):
        if is_lower and start[index] == '0':
            break
        if not is_lower and start[index] < end[index]:
            first_lower = index
            is_lower = True
        index += 1
    return index-1, first_lower


start = '2169800'
end = '2171194'
result = []
while int(start) < int(end):
    index, first_lower = foo(start, end)
    range_end = index > first_lower and 10 or int(end[first_lower])
    for x in range(int(start[index]), range_end):
        result.append(start[:index] + str(x) + 'X'*(len(start)-index-1))
    if range_end == 10:
        start = str(int(start[:index])+1)+'0'+start[index+1:]
    else:
        start = start[:index] + str(range_end) + start[index+1:]

result.append(end)
print "Leading numbers:"
print result

I test the examples you've given, it is right. Hope this will help you

This should give you a good starting point :

def leading(start, end):

    leading = []
    hundreds = start // 100

    while (end - hundreds * 100) > 100:
        i = hundreds * 100
        leading.append(range(i,i+100))
        hundreds += 1

    c = hundreds * 100
    tens = 1

    while (end - c - tens * 10) > 10:
        i = c + tens * 10
        leading.append(range(i, i + 10))
        tens += 1

    c += tens * 10
    ones = 1

    while (end - c - ones) > 0:
        i = c + ones
        leading.append(i)
        ones += 1

    leading.append(end)

    return leading

Ok, the whole could be one loop-level deeper. But I thought it might be clearer this way. Hope, this helps you...

Update : Now I see what you want. Furthermore, maria's code doesn't seem to be working for me. (Sorry...) So please consider the following code :

def leading(start, end):

    depth = 2
    while 10 ** depth > end : depth -=1
    leading = []
    const = 0
    coeff = start // 10 ** depth

    while depth >= 0:
        while (end - const - coeff * 10 ** depth) >= 10 ** depth:
            leading.append(str(const / 10 ** depth + coeff) + "X" * depth)
            coeff += 1

        const += coeff * 10 ** depth
        coeff = 0
        depth -= 1

    leading.append(end)

    return leading

print leading(199,411)

print leading(2169800, 2171194)

print leading(1000, 1453)

print leading(1,12)

Now, let me try to explain the approach here. The algorithm will try to find "end" starting from value "start" and check whether "end" is in the next 10^2 (which is 100 in this case). If it fails, it will make a leap of 10^2 until it succeeds. When it succeeds it will go one depth level lower. That is, it will make leaps one order of magnitude smaller. And loop that way until the depth is equal to zero (= leaps of 10^0 = 1). The algorithm stops when it reaches the "end" value.

You may also notice that I have the implemented the wrapping loop I mentioned so it is now possible to define the starting depth (or leap size) in a variable.

The first while loop makes sure the first leap does not overshoot the "end" value.

If you have any questions, just feel free to ask.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top