-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Added new algorithm to generate numbers in lexicographical order #11674
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
tianyizheng02
merged 14 commits into
TheAlgorithms:master
from
1227haran:lexicographical_numbers
Oct 5, 2024
Merged
Changes from 3 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
d2fc777
Added algorithm to generate numbers in lexicographical order
1227haran ed0553b
Removed the test cases
1227haran ae67331
Updated camelcase to snakecase
1227haran c6f2646
Added doctest
1227haran d62b61f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 17356b0
Added descriptive name for n
1227haran 2b355d3
Merge branch 'lexicographical_numbers' of https://github.com/1227hara…
1227haran 8762b51
Reduced the number of letters
1227haran 07edd68
Updated the return type
1227haran 4131323
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 628b47d
Updated import statement
1227haran b19c7d3
Merge branch 'lexicographical_numbers' of https://github.com/1227hara…
1227haran 8b4f4cd
Updated return type to Iterator[int]
1227haran 667bfc7
removed parentheses
1227haran File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
def lexical_order(n: int) -> str: | ||
1227haran marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
ans = [] | ||
stack = [1] | ||
|
||
while stack: | ||
num = stack.pop() | ||
if num > n: | ||
continue | ||
|
||
ans.append(str(num)) | ||
if (num % 10) != 9: | ||
stack.append(num + 1) | ||
|
||
stack.append(num * 10) | ||
|
||
return " ".join(ans) | ||
|
||
if __name__ == "__main__": | ||
|
||
print(f"Numbers from 1 to 25 in lexical order: {lexical_order(25)}") |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.