Utilizing ZigZag in Python with LeetCode Problems and Deployment using Flask, Docker, and Kubernetes

Posted by

ZigZag Conversion is a popular LeetCode problem that involves converting a given string into a zigzag pattern, and then reading that pattern row by row. In this tutorial, we will discuss how to solve the ZigZag LeetCode problem in Python, and then deploy the solution using Flask, Docker, and Kubernetes.

Step 1: Solving the ZigZag LeetCode Problem in Python

To solve the ZigZag Conversion problem in Python, we will first create a function that takes in the input string and the number of rows in the zigzag pattern, and then generates the output in the desired format. Here is a sample Python code snippet that achieves this:

def convert(s, numRows):
    if numRows == 1 or numRows >= len(s):
        return s

    zigzag = ['' for _ in range(numRows)]
    row, step = 0, 1

    for c in s:
        zigzag[row] += c
        if row == 0:
            step = 1
        elif row == numRows - 1:
            step = -1
        row += step

    return ''.join(zigzag)

Step 2: Setting up Flask for Deployment

Now that we have our ZigZag Conversion function ready, we can set up a simple Flask web application to deploy it. First, create a new Python file (e.g., app.py) and add the following code:

from flask import Flask, request
from zigzag_conversion import convert

app = Flask(__name__)

@app.route('/convert', methods=['POST'])
def zigzag_convert():
    data = request.json
    s = data['string']
    numRows = data['numRows']
    result = convert(s, numRows)
    return {'result': result}

if __name__ == '__main__':
    app.run(debug=True)

Step 3: Creating a Docker Image for the Flask Application

To containerize our Flask application, we need to create a Dockerfile in the project directory with the following content:

FROM python:3.9

WORKDIR /app

COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt

COPY . .

CMD ["python", "app.py"]

Next, create a requirements.txt file with the following content:

Flask

Now, build the Docker image by running the following command in the terminal:

docker build -t zigzag-converter .

Step 4: Running the Flask Application using Docker

To run the Flask application using the Docker image we just created, execute the following command in the terminal:

docker run -d -p 5000:5000 zigzag-converter

Now, the Flask application will be accessible at http://localhost:5000/convert.

Step 5: Deploying the Flask Application with Kubernetes

To deploy the Flask application using Kubernetes, first create a deployment YAML file (e.g., app-deployment.yaml) with the following content:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: zigzag-converter
spec:
  replicas: 2
  selector:
    matchLabels:
      app: zigzag-converter
  template:
    metadata:
      labels:
        app: zigzag-converter
    spec:
      containers:
      - name: zigzag-converter
        image: zigzag-converter:latest
        ports:
        - containerPort: 5000

Apply the deployment configuration by running the following command:

kubectl apply -f app-deployment.yaml

Next, create a service YAML file (e.g., app-service.yaml) to expose the Flask application:

apiVersion: v1
kind: Service
metadata:
  name: zigzag-converter
spec:
  selector:
    app: zigzag-converter
  ports:
    - protocol: TCP
      port: 80
      targetPort: 5000
  type: LoadBalancer

Apply the service configuration by running the following command:

kubectl apply -f app-service.yaml

Now, the Flask application will be accessible via the external IP address provided by the LoadBalancer service.

In conclusion, in this tutorial, we discussed how to solve the ZigZag LeetCode problem in Python, and then deploy the solution using Flask, Docker, and Kubernetes. This demonstrates how to create a simple web application, containerize it, and deploy it using container orchestration tools like Kubernetes.