Last active
September 15, 2020 07:35
-
-
Save jn0/fdcdde128171a7d16da8b223670050ef to your computer and use it in GitHub Desktop.
integer to string at arbitrary (2..36) base
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
| import string | |
| def signum(n): | |
| return int(n / abs(n)) if n else 0 | |
| def i2s(n, b=10, d=string.digits+string.ascii_lowercase): | |
| assert 2 <= b <= len(d) | |
| return d[0] if n == 0 else ('-' + i2s(abs(n), b, d)) if n < 0 else (i2s(n//b, b, d) + d[n % b]).lstrip(d[0]) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Close-to-maximum length value: