Sequential Digits Solution || Understanding Sequential Digits: A Pythonic Approach || A Deep Dive into Generating Sequential Numbers using Python ||

Sequential Digits

An integer has sequential digits if and only if each digit in the number is one more than the previous digit.

Return a sorted list of all the integers in the range [low, high] inclusive that have sequential digits.

Example 1:

Input: low = 100, high = 300 

Output: [123,234]

Example 2:

Input: low = 1000, high = 13000

Output: [1234, 2345, 3456, 4567,5678,6789,12345]


Constraints:

10 <= low <= high <= 10^9


Sample input

100

300


Sample output

123,234



Program:

def sequential_digits(low, high):

    # Base numbers for sequences

    nums = ['1','2','3','4','5','6','7','8','9']

    result = []

    

    # While there are numbers to process

    while nums:

        num = nums.pop(0) # Remove and get the first number

        n = int(num)

        

        # Check if this number is in the range

        if n >= low and n <= high:

            result.append(n)

        

        # If the last digit is not 9, we can add the next digit to form a sequence

        if num[-1] != '9':

            next_num = num + str(int(num[-1])+1)

            nums.append(next_num)

    

    return result


# Take input from user

low = int(input())

high = int(input())


results = sequential_digits(low, high)

print(",".join(map(str, results)))









Explanation:

Function Definition:
1)The function sequential_digits(low, high) is defined to generate sequential digits between the provided low and high values.

Initialization:
1)A list called nums is initialized with single-digit numbers as strings. These will serve as the starting points for generating sequential digits.
2)An empty list result is initialized to store numbers with sequential digits.

Generating Sequential Digits:
1)A while loop runs as long as the nums list is not empty.
2)The first item from the nums list is popped and stored in the variable num. This number will be checked and extended to generate sequential numbers.
3)num is converted to an integer and stored in the variable n.

Checking Range:
1)An if condition checks if the integer n is within the low and high range. If it is, then n is appended to the result list.

Generating the Next Sequential Number:
1)Another if condition checks if the last digit of the number (as a string) is not '9'. If it's not '9', it means we can extend the sequence further.
2)The next sequential digit is calculated by taking the last digit of num, converting it to an integer, adding 1 to it, and then converting it back to a string. This new digit is then concatenated to num to form the next sequential number.
3)This newly formed number is added back to the nums list for further processing.

Function Return:
1)Once the while loop is exhausted and all sequences have been generated, the function returns the result list containing all the sequential numbers within the given range.

Taking User Input:
1)The program takes two integer inputs from the user: the lower limit low and the upper limit high.

Calling the Function and Displaying Output:
1)The function sequential_digits is called with low and high as arguments.
2)The returned list of numbers is then converted to strings and joined using commas to form a single string. This string is printed out as the final result.

Post a Comment

0 Comments