-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path55_Microsoft_shortenURL.py
executable file
·44 lines (31 loc) · 1.2 KB
/
55_Microsoft_shortenURL.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
30
31
32
33
34
35
36
37
38
39
40
41
42
"""
This problem was asked by Microsoft.
Implement a URL shortener with the following methods:
shorten(url), which shortens the url into a six-character alphanumeric string, such as zLg6wl.
restore(short), which expands the shortened string into the original url.
If no such shortened string exists, return null.
Hint: What if we enter the same URL twice?
"""
import hashlib
class tinyURL:
def __init__(self):
self.m = hashlib.sha3_256
self.prefix = "rafay.ak/"
self.key_to_URL = dict()
def shorten(self, url):
key = self.m(url.encode()).hexdigest()[:6]
self.key_to_URL[key] = url
return self.prefix+key
def get_url(self, shortened_url):
key = shortened_url.replace(self.prefix, "")
return self.key_to_URL[key]
if __name__ == '__main__':
url = "https://arxiv.org/pdf/1606.05340v2.pdf"
print("Original URL: " + url)
tinyURL = tinyURL()
short_1 = tinyURL.shorten(url)
print("Shortened URL_1: " + short_1, end=" ---- ")
print("Original URL: " + tinyURL.get_url(short_1))
short_2 = tinyURL.shorten(url)
print("Shortened URL: " + short_2, end=" ---- ")
print("Original URL: " + tinyURL.get_url(short_2))