Created
January 24, 2017 13:58
-
-
Save ki-chi/fe8bf8d364d37561a8795eaca778d9bd to your computer and use it in GitHub Desktop.
The function for converting tricky-format digits
This file contains 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
## Convert strings of 2-digits number have the following tricky format to an integer. | |
## Tricky format: the last digit is described as decimal, and the first digit is chosen from {1, ..., 9, A, ..., Z, a, ..., z}. | |
## The min is 0(="00") and the max is 619(="z9"). | |
## Example: "15" -> 15, "A0" -> 100, "Z9" -> 359, "a0" -> 360, "c5" -> 385 | |
def num2int(num): | |
if num.isdigit(): | |
return int(num) | |
elif num[0].isupper(): | |
return int(num[0], 36) * 10 + int(num[1]) | |
else: | |
return int(num[0], 36) * 10 + 260 + int(num[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment