File tree Expand file tree Collapse file tree 1 file changed +6
-2
lines changed
Expand file tree Collapse file tree 1 file changed +6
-2
lines changed Original file line number Diff line number Diff line change 22
33CHARSET = string .digits + string .ascii_lowercase + string .ascii_uppercase
44
5+
56def base62_encode (num : int ) -> str :
67 """
78 Encodes a positive integer into a base62 string.
@@ -14,7 +15,7 @@ def base62_encode(num: int) -> str:
1415 """
1516 if num == 0 :
1617 return CHARSET [0 ]
17-
18+
1819 arr = []
1920 base = len (CHARSET )
2021 while num :
@@ -23,6 +24,7 @@ def base62_encode(num: int) -> str:
2324 arr .reverse ()
2425 return "" .join (arr )
2526
27+
2628def base62_decode (string_val : str ) -> int :
2729 """
2830 Decodes a base62 string into a positive integer.
@@ -36,14 +38,16 @@ def base62_decode(string_val: str) -> int:
3638 base = len (CHARSET )
3739 strlen = len (string_val )
3840 num = 0
39-
41+
4042 idx = 0
4143 for char in string_val :
4244 power = strlen - (idx + 1 )
4345 num += CHARSET .index (char ) * (base ** power )
4446 idx += 1
4547 return num
4648
49+
4750if __name__ == "__main__" :
4851 import doctest
52+
4953 doctest .testmod ()
You can’t perform that action at this time.
0 commit comments