Last active
July 10, 2025 13:46
-
-
Save sloanlance/436b71fef25aa1ca7f984eb4b2efc15a to your computer and use it in GitHub Desktop.
Python: A small function to make a string of digits for numbering columns in the output.
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
def numbar(n: int) -> str: | |
""" | |
Make a string of digits for numbering columns in the output. Column | |
numbering begins with zero. If the number of columns require more | |
place values, newlines are used to make multiple rows in the output. | |
E.g., if `n <= 10`, there will be one row of output, but if | |
`10 < n <= 100`, there will be two rows of output, etc. | |
""" | |
rows = len(str(n - 1)) | |
return '\n'.join( | |
''.join(j[i] for j in | |
[f'{m:0{rows}}' for m in range(n)]) | |
for i in range(rows)) | |
print(numbar(10)) | |
# 0123456789 | |
print(numbar(42)) | |
# 000000000011111111112222222222333333333344 | |
# 012345678901234567890123456789012345678901 | |
print(numbar(101)) | |
# 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001 | |
# 00000000001111111111222222222233333333334444444444555555555566666666667777777777888888888899999999990 | |
# 01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment