
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to generate first n lexicographic numbers in python
Suppose we have a number n, we have to find first n numbers that are sorted in lexicographic sequence.
So, if the input is like n = 15, then the output will be [1, 10, 11, 12, 13, 14, 15, 2, 3, 4, 5, 6, 7, 8, 9]
To solve this, we will follow these steps:
- count := 1
- ans := a list with single element count
- while size of ans < n, do
- count := count * 10
- while count > n , do
- count := quotient of count / 10
- count := count + 1
- while count mod 10 is same as 0, do
- count := quotient of count / 10
- insert count at the end of ans
- return ans
Let us see the following implementation to get better understanding:
Example Code
class Solution: def solve(self, n): count = 1 ans = [count] while len(ans) < n: count *= 10 while count > n: count = count // 10 count += 1 while count % 10 == 0: count = count // 10 ans.append(count) return ans ob = Solution() n = 15 print(ob.solve(n))
Input
15
Output
[1, 10, 11, 12, 13, 14, 15, 2, 3, 4, 5, 6, 7, 8, 9]
- Related Articles
- Program to find sum of first N odd numbers in Python
- Java Program to generate n distinct random numbers
- Program to find the sum of first n odd numbers in Python
- 8086 program to generate AP series of n numbers
- 8086 program to generate G.P. series of n numbers
- Program to find kth lexicographic sequence from 1 to n of size k Python
- Python Program for cube sum of first n natural numbers
- Program to find first N Iccanobif Numbers in C++
- JavaScript function to take a number n and generate an array with first n prime numbers
- Python Program for Sum of squares of first n natural numbers
- Python program to print decimal octal hex and binary of first n numbers
- Java Program to Display Numbers and Sum of First N Natural Numbers
- Program to create largest lexicographic number from a list of numbers in C++
- Program to find sum of first n natural numbers in C++
- Sum of first n natural numbers in C Program

Advertisements