Skip to content

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
merged 14 commits into from
Oct 5, 2024
Merged
21 changes: 21 additions & 0 deletions data_structures/stacks/lexicographical_numbers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
def lexical_order(n: int) -> str:

ans = []
stack = [1]

while stack:
num = stack.pop()
if num > n:
continue

Check failure on line 10 in data_structures/stacks/lexicographical_numbers.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

data_structures/stacks/lexicographical_numbers.py:10:1: W293 Blank line contains whitespace
ans.append(str(num))
if (num % 10) != 9:

Check failure on line 12 in data_structures/stacks/lexicographical_numbers.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

data_structures/stacks/lexicographical_numbers.py:12:28: W291 Trailing whitespace
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)}")
Loading