Coding challenge:
Convert a given long URL into a Tiny URL. The length of this Tiny URL should be fixed and the same for all URLs. You should return the full URL if given a shortened URL.
I am aware this code is technically flawed as there will be clashes owing to the limitations of the hash function in Python.
Therefore I am not specifically looking for coding suggestions on this specific library. Instead what I am curious to know is if this can be improved to use an even shorter URL extension (e.g: 6 characters) that is unique.
I considered using a different hashing function from the import hashing library but the output of these are usually 32-64 characters long and therefore certainly not a "Tiny" URL.
class TinyURL:
def __init__(self):
self.storedURLS = {}
def encode(self, long):
tiny = hex(hash(long))
self.storedURLS[tiny] = long
return tiny
def decode(self, tiny):
if tiny in self.storedURLS:
return self.storedURLS[tiny]
else:
return "Tiny URL is not in the database"
t = TinyURL()
encoded = t.encode("google.com")
print(encoded)
decoded = t.decode(encoded)
print(decoded)