
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find All Contiguously Increasing Numbers in a 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
- for j in range i + 1 to 9, do
- 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]
Advertisements