Created
February 22, 2020 00:50
-
-
Save js51/56fc6d5742bc52a8045f63e247309a21 to your computer and use it in GitHub Desktop.
Convert an integer to a string representation in base b
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 string(i, b): | |
| """represent any integer i as a base b string where 2<=b<=10""" | |
| import math | |
| start = b**int(math.log(i,b)) | |
| def _string(i,b,val): | |
| digit = int(i/val) | |
| return str(digit) + (_string(i-digit*val, b, val/b) if i-digit != 0 else "") | |
| return _string(i,b,start) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment