Skip to content
DeepForgeby svx
All problems
MediumAlgorithmsal-292

Lexicographic Numbers up to N

Problem

Return the integers from 1 through n in lexicographic order as strings compared character by character.

Start at 1 and repeatedly either descend by appending a zero when that stays within n, or move to the next sibling by incrementing and falling back to a parent. Stop after n numbers.

Hint

Falling back to the parent happens whenever the next sibling would exceed n.

Test cases

case 1
in: [13]
expected: [1,10,11,12,13,2,3,4,5,6,7,8,9]
case 2
in: [2]
expected: [1,2]
case 3
in: [1]
expected: [1]
case 4
in: [10]
expected: [1,10,2,3,4,5,6,7,8,9]
case 5
in: [20]
expected: [1,10,11,12,13,14,15,16,17,18,19,2,20,3,4,5,6,7,8,9]
Starter codePython
def lexicographic_numbers(n):
    # Your code here
    pass

5 test cases · run your code against them in the editor