Generate numbers in lexical order from 1 to max_number. >>> " ".join(map(str, lexical_order(13))) '1 10 11 12 13 2 3 4 5 6 7 8 9' >>> list(lexical_order(1)) [1] >>> " ".join(map(str, lexical_order(20))) '1 10 11 12 13 14 15 16 17 18 19 2 20 3 4 5 6 7 8 9' >>> " ".jo
(max_number: int)
| 2 | |
| 3 | |
| 4 | def lexical_order(max_number: int) -> Iterator[int]: |
| 5 | """ |
| 6 | Generate numbers in lexical order from 1 to max_number. |
| 7 | |
| 8 | >>> " ".join(map(str, lexical_order(13))) |
| 9 | '1 10 11 12 13 2 3 4 5 6 7 8 9' |
| 10 | >>> list(lexical_order(1)) |
| 11 | [1] |
| 12 | >>> " ".join(map(str, lexical_order(20))) |
| 13 | '1 10 11 12 13 14 15 16 17 18 19 2 20 3 4 5 6 7 8 9' |
| 14 | >>> " ".join(map(str, lexical_order(25))) |
| 15 | '1 10 11 12 13 14 15 16 17 18 19 2 20 21 22 23 24 25 3 4 5 6 7 8 9' |
| 16 | >>> list(lexical_order(12)) |
| 17 | [1, 10, 11, 12, 2, 3, 4, 5, 6, 7, 8, 9] |
| 18 | """ |
| 19 | |
| 20 | stack = [1] |
| 21 | |
| 22 | while stack: |
| 23 | num = stack.pop() |
| 24 | if num > max_number: |
| 25 | continue |
| 26 | |
| 27 | yield num |
| 28 | if (num % 10) != 9: |
| 29 | stack.append(num + 1) |
| 30 | |
| 31 | stack.append(num * 10) |
| 32 | |
| 33 | |
| 34 | if __name__ == "__main__": |
no test coverage detected