Python ZigZag LeetCode Implementation with Flask Deployment using Docker and Kubernetes

Posted by

In this tutorial, we will be discussing how to solve the 3 ZigZag LeetCode problem in Python and then deploy the solution using Flask, Docker, and Kubernetes to showcase a complete end-to-end project.

Step 1: Solving the 3 ZigZag LeetCode Problem in Python

The 3 ZigZag LeetCode problem asks us to convert a given string into a zigzag pattern with a specified number of rows. The input string "PAYPALISHIRING" and number of rows 3 should yield the following zigzag pattern:

P   A   H   N
A P L S I I G
Y   I   R

To solve this problem in Python, we can use the following code snippet:

def convert(s: str, numRows: int) -> str:
    if numRows == 1:
        return s

    rows = [''] * min(numRows, len(s))
    current_row = 0
    going_down = False

    for c in s:
        rows[current_row] += c
        if current_row == 0 or current_row == numRows - 1:
            going_down = not going_down
        current_row += 1 if going_down else -1

    return ''.join(rows)

This function takes in a string s and number of rows numRows, and returns the zigzag pattern as a string.

Step 2: Setting up a Flask API for Deployment

Now that we have our solution for the 3 ZigZag problem, we can create a Flask API to serve this functionality. We can define a Flask route that takes in the input string and number of rows, and returns the zigzag pattern.

from flask import Flask, request
app = Flask(__name__)

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

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

Step 3: Dockerizing the Flask App

Next, we can create a Dockerfile to containerize our Flask app. We can start by creating a requirements.txt file that includes the necessary Python packages (Flask) and then create a Dockerfile with the following contents:

FROM python:3.8

WORKDIR /app

COPY requirements.txt .

RUN pip install -r requirements.txt

COPY . .

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

We can then build the Docker image using the following command:

docker build -t zigzag-flask .

Step 4: Deploying the Flask App on Kubernetes

Finally, we can deploy our Dockerized Flask app on Kubernetes. We can create a deployment and service YAML file to define our application:

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

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

We can then apply these YAML files using the following command:

kubectl apply -f deployment.yaml

Our Flask app should now be deployed and accessible through the LoadBalancer service IP.

This concludes our tutorial on solving the 3 ZigZag LeetCode problem in Python and deploying the solution using Flask, Docker, and Kubernetes. I hope you found this tutorial helpful and informative. Thank you for reading!