Program to find all contiguously increasing numbers in start end range in Python


Suppose we have two numbers start and end, we have to find a sorted list of integers such that every number e in range [start, end] both inclusive and the digits of e are contiguously increasing. An example of continuously increasing number is 5678, but 169 is not.

So, if the input is like start = 10 end = 150, then the output will be [12, 23, 34, 45, 56, 67, 78, 89, 123]

To solve this, we will follow these steps −

  • s := all 9 digits as a string "123456789"
  • a := a new list
  • for i in range 0 to 8, do
    • for j in range i + 1 to 9, do
      • x := (substring of s from index i to j-1) as number
      • if start <= x <= end, then
        • insert x into a
  • sort the list a and return

Example

Let us see the following implementation to get better understanding −

def solve(start, end):
   s = "123456789"
   a = []
   for i in range(9):
      for j in range(i + 1, 10):
         x = int(s[i:j])
         if start <= x <= end:
            a += (x,)
   return sorted(a)

start = 10
end = 150
print(solve(start, end))

Input

10, 150

Output

[12, 23, 34, 45, 56, 67, 78, 89, 123]

Updated on: 18-Oct-2021

314 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements