Skip to content

Commit 07edd68

Browse files
committed
Updated the return type
1 parent 8762b51 commit 07edd68

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,37 @@
1-
def lexical_order(max_number: int) -> str:
1+
from typing import Iterator
2+
3+
def lexical_order(max_number: int) -> Iterator[str]:
24
"""
35
Generate numbers in lexical order from 1 to max_number.
46
5-
>>> lexical_order(13)
7+
>>> " ".join(lexical_order(13))
68
'1 10 11 12 13 2 3 4 5 6 7 8 9'
7-
>>> lexical_order(1)
8-
'1'
9-
>>> lexical_order(20)
9+
>>> list(lexical_order(1))
10+
['1']
11+
>>> " ".join(lexical_order(20))
1012
'1 10 11 12 13 14 15 16 17 18 19 2 20 3 4 5 6 7 8 9'
11-
>>> lexical_order(25)
13+
>>> " ".join(lexical_order(25))
1214
'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'
15+
>>> list(lexical_order(12))
16+
['1', '10', '11', '12', '2', '3', '4', '5', '6', '7', '8', '9']
1317
"""
1418

15-
ans = []
1619
stack = [1]
1720

1821
while stack:
1922
num = stack.pop()
2023
if num > max_number:
2124
continue
2225

23-
ans.append(str(num))
26+
yield(str(num))
2427
if (num % 10) != 9:
2528
stack.append(num + 1)
2629

2730
stack.append(num * 10)
2831

29-
return " ".join(ans)
30-
3132

3233
if __name__ == "__main__":
3334
from doctest import testmod
3435

3536
testmod()
36-
print(f"Numbers from 1 to 25 in lexical order: {lexical_order(25)}")
37+
print(f"Numbers from 1 to 25 in lexical order: {list(lexical_order(26))}")

0 commit comments

Comments
 (0)