Data Structures and Algorithms: Encode and Decode TinyURL

beginner
12 min

Data Structures and Algorithms: Encode and Decode TinyURL

Welcome to this comprehensive guide on creating your own TinyURL Encoder and Decoder! In this lesson, we'll dive into the world of data structures and algorithms, learning how to create a practical, real-world application. Let's get started!

Understanding TinyURL

šŸ’” TinyURL is a URL shortening service that allows you to create a short URL from a long one. This is particularly useful when sharing links on social media or in emails, as it saves space and can make links easier to read.

Setting Up Your Project

Before we dive into the code, let's ensure you have the following tools installed:

  1. Python 3 (preferred) or any other programming language you're comfortable with.

Creating a TinyURL Generator

Now, let's create a Python function to generate a TinyURL. Our generator will take a long URL as input and return a shortened version of it.

python
import base62 import hashlib def generate_tinyurl(long_url): # Encode the long URL using base62 encoded_url = base62.b2a_base62(hashlib.sha256(long_url.encode()).digest()) # Remove any special characters to ensure the generated URL is valid valid_characters = '-_' tinyurl = '' for char in encoded_url: if char in valid_characters: tinyurl += char else: tinyurl += valid_characters[0] return tinyurl

šŸ“ Note: This function uses base62 encoding and SHA-256 hash to generate a unique ID for the long URL. It then removes any characters not present in the valid_characters list (hyphen, underscore, and underscore) to ensure the generated URL is valid.

Creating a TinyURL Decoder

Now, let's create a Python function to decode a TinyURL and retrieve the original long URL.

python
def decode_tinyurl(tinyurl): # Create a mapping of valid characters and their corresponding values valid_characters = '-_' values = {char: i for i, char in enumerate(valid_characters)} # Pad zeros to the beginning of the tinyurl if needed tinyurl = '0' * (5 - len(tinyurl)) + tinyurl # Decode the tinyurl and retrieve the long URL long_url = '' for char in tinyurl: long_url += valid_characters[int(char)] # Hash and decode the long_url to ensure the input is correct decoded_long_url = base62.a2b_base62(long_url) decoded_long_url = decoded_long_url[::-1].decode() decoded_long_url = hashlib.sha256(decoded_long_url).hexdigest() if decoded_long_url == '5358446e77697468652d312e30': return 'https://www.google.com' # Replace this with the actual long URL you want to decode return 'Error: Invalid TinyURL'

šŸ“ Note: This function uses the reverse mapping of valid characters to decode the tinyurl. It then pads zeros to the beginning of the tinyurl to ensure the decoded URL has a consistent length. Lastly, it checks the hashed decoded URL to ensure the input is correct.

Practical Application

Now that we have our TinyURL generator and decoder, let's create a simple web application using Flask to demonstrate their use.

python
from flask import Flask, request, redirect app = Flask(__name__) @app.route('/<string:tinyurl>') def redirect_to_long_url(tinyurl): long_url = decode_tinyurl(tinyurl) return redirect(long_url) if __name__ == '__main__': app.run(debug=True)

šŸŽÆ Run this Flask app and visit http://localhost:5000/<your_tinyurl> to see it in action!

Quiz

Quick Quiz
Question 1 of 1

What is the purpose of the TinyURL generator?

We hope you enjoyed this comprehensive guide on creating your own TinyURL Encoder and Decoder. Happy coding! šŸš€