short_link/encode.py

38 lines
1.2 KiB
Python

def main():
link_id_converted = input("Please input converted result: ")
try:
link_id_converted = int(link_id_converted)
except:
raise ValueError("Invalid converted result")
field_map = {
'A': 0, 'a': 1, 'B': 2, 'b': 3,
'C': 4, 'c': 5, 'D': 6, 'd': 7,
'1': 8, 'E': 9, 'e': 10, 'F': 11,
'f': 12, 'G': 13, 'g': 14, 'H': 15,
'h': 16, '2': 17, 'I': 18, 'i': 19,
'J': 20, 'j': 21, 'K': 22, 'k': 23,
'L': 24, 'l': 25, '3': 26, 'M': 27,
'm': 28, 'N': 29, 'n': 30, 'O': 31,
'o': 32, 'P': 33, 'p': 34, '4': 35,
'Q': 36, 'q': 37, 'R': 38, 'r': 39,
'S': 40, 's': 41, 'T': 42, 't': 43,
'5': 44, 'U': 45, 'u': 46, 'V': 47,
'v': 48, 'W': 49, 'w': 50, 'X': 51,
'x': 52, '6': 53, 'Y': 54, 'y': 55,
'Z': 56, 'z': 57, '7': 58, '8': 59,
'9': 60, '0': 61}
field_map = {v:k for k, v in field_map.items()}
link_id = []
while link_id_converted > 0:
remainder = link_id_converted % 62
link_id.append(field_map[remainder])
link_id_converted = link_id_converted // 62
original_link_id = ''.join(link_id[::-1])
print("Link ID is: {}".format(original_link_id))
if __name__ == "__main__":
main()