Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8078157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:42:08+00:00 2026-06-05T15:42:08+00:00

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

  • 0

(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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T15:42:10+00:00Added an answer on June 5, 2026 at 3:42 pm

    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)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given an arbitrary input string I'm meant to find the sum of all numbers
Does Python offer a way to iterate over all consecutive sublists of a given
Given a two-dimensional list, I would like to find everything that contains a sublist.
Given are two python lists with strings in them (names of persons): list_1 =
I'm working on a python script that needs to run between two given times.
For example, given the two letters A and B, I'd like to generate all
Can i resize images in python to given height and width,i use python 2.5,
I was given this Python code that would calculate an MD5 value for any
Given: alphabet = ['a','b','c',...,'z'] i want python to enumerate every combination (starting from 1
Given a url to an image is there a way in Django/Python to pull

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.