728x90
import hashlib
class URLShortener:
def __init__(self):
self.url_map = {}
self.short_to_long = {}
def _hash_url(self, url):
# Generate a hash for the given URL
return hashlib.md5(url.encode()).hexdigest()[:6]
def shorten_url(self, url: str) -> str:
# Check if the URL has already been shortened
if url in self.url_map:
return self.url_map[url]
# Generate a short URL
short_hash = self._hash_url(url)
short_url = self.base_url + short_hash
# Store the mappings
self.url_map[url] = short_url
self.short_to_long[short_url] = url
return short_url
def retrieve_url(self, short_url: str) -> str:
# Retrieve the original URL from the shortened version
return self.short_to_long.get(short_url, "URL not found")
# Example usage
url_shortener = URLShortener()
short_url = url_shortener.shorten_url(long_url)
print("Shortened URL:", short_url)
original_url = url_shortener.retrieve_url(short_url)
print("Original URL:", original_url)
728x90
'호그와트' 카테고리의 다른 글
췤췤 머신 그저 감사합니다 !! (0) | 2024.08.18 |
---|---|
포ㄹ트를 뽀트르 포트르 포트리스 뽀로로 (0) | 2024.08.18 |
tryhackme Boiler CTF privilege escalation (0) | 2024.07.27 |
판다의 고기는 원래 맛있다 (0) | 2024.07.26 |
Why did CrowdStrike IT outage happen ? (brief summary) (0) | 2024.07.24 |