-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtinyUrl.py
29 lines (22 loc) · 1022 Bytes
/
tinyUrl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# _*_ coding = utf-8 _*_
'''
TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/problems/design-tinyurl and it returns a short URL such as http://tinyurl.com/4e9iAk.
Design the encode and decode methods for the TinyURL service. There is no restriction on how your encode/decode algorithm should work. You just need to ensure that a URL can be encoded to a tiny URL and the tiny URL can be decoded to the original URL.
'''
import base64
class Codec:
def encode(self, longUrl):
return "http://tinyurl.com/" + base64.b32encode(longUrl)
"""Encodes a URL to a shortened URL.
:type longUrl: str
:rtype: str
"""
def decode(self, shortUrl):
return base64.b32decode(shortUrl.split('/')[-1])
"""Decodes a shortened URL to its original URL.
:type shortUrl: str
:rtype: str
"""
if __name__ == "__main__":
codec = Codec()
print(codec.decode(codec.encode('https://google.com')))