-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNEW.py
34 lines (31 loc) · 916 Bytes
/
NEW.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
import random
import math
import string
full_tiny = {}
tiny_full = {}
letters = string.ascii_letters + string.digits
print(random.randint(0,6200)%62)
class Codec:
def encode(self,longurl):
ans = ''
one = ''
for i in range(0,6):
one = letters[random.randint(0,6200)%62]
ans = ans + one
shorturl = "http://tinyurl.com/" + ans
if longurl in full_tiny:
pass
else:
full_tiny[longurl] = shorturl
tiny_full[shorturl] = longurl
return(full_tiny[longurl])
def decode(self,shorturl):
if shorturl in tiny_full:
return tiny_full[shorturl]
else:
print('no such short url existed')
pass
url = 'https://leetcode.com/problems/design-tinyurl'
codec = Codec()
short_url = codec.encode(url)
print(codec.decode(short_url))