LeetCode w/ Python Jupyter kernels

Preface

C++ was rather painful, but technically worked with this configuration. Hopefully python will make this much less painful:

Last updated: 2023-10-21T17:23:58.134068+00:00

Python implementation: CPython
Python version       : 3.11.6
IPython version      : 8.16.1

Compiler    : GCC 12.2.0
OS          : Linux
Release     : 5.15.90.1-microsoft-standard-WSL2
Machine     : x86_64
Processor   : 
CPU cores   : 8
Architecture: 64bit
Source: Testing Python Jupyter Features

I attempted two hard problems, and realized that it really has been a while for Python. Working backwards through daily easies until they become clockwork, before progressing with Medium and then hards. Will probably only document hards, and since I will only do 10-20 of them, will try and make them worth reading in the end.

TODO : Also make C++ ones readable again, and re-do the problem with sophisticated solutions?

Notebooks

A quick list of notebooks:

Requirements

This ended up being installed through the Dockerfile in .devcontainer/blog-codingInterview/Dockerfile and is available as a dev-container option:

# Comes with base requirements to build Quarto docs, as well as Python 3.11, gcc 12.2.0
# hadolint ignore= DL3007
FROM ghcr.io/cameronrutherford/quarto-ci:latest

# Install poetry
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
RUN curl -sSL https://install.python-poetry.org | POETRY_HOME=/etc/poetry python3 -
ENV PATH=$PATH:/etc/poetry/bin
RUN echo "poetry shell" >> /home/vscode/.bashrc

# Install poetry environment
WORKDIR /home

# Configure pyproject.toml with heredoc
# That way changes to the config force a re-build with correct deps
# Also eliminates filepath issues copying between projects...
COPY <<-"end_toml" ./pyproject.toml
[tool.poetry]
name = "python_coding_interview"
description = "Minimal Python environment for Jupyter Python practice"
version = "0.1.0"
authors = [
    "Cameron Rutherford"
]

[tool.poetry.dependencies]
python = ">=3.11"
jupyterlab = "*"
watermark = "*"
end_toml

# `cache-dir` is critical here to have user `vscode` have things installed
ENV POETRY_CACHE_DIR=/home/vscode/poetry-cache

# Since we aren't packaging anything, just adding dependencies
RUN poetry install --no-root --no-interaction --no-directory && \
  # Also install ipyknernel so it's visible in jupyter
  "poetry run python3 -m ipykernel install \
  --name py311-interview \
  --display-name \"LeetCode\" \
  --prefix=$(which jupyter)"

Notes

Python

Poetry

For a while, mamba was being used in the quarto-ci base image. That was downgrading Python to 3.10, and I didn’t want that. Instead we are using Poetry to manage pure python deps. The configuration for our image is stored as a heredoc within the Dockerfile to ensure that modifications to the pyproject.toml “file” actually triggers a rebuild when appropriate, since there is no way to chain a COPY/ADD command with a RUN in Docker.

Algorithms

Jupyter Specific

ipykernel

You can have a python environment run jupyter in VSCode, but really what you want from a venv is an ipykernel environment installed. We had to do that in our Dockerfile, and we also make it a “Jupyter Kernel” detectable by VSCode.

Bugs

VSCode

By default, kernel cells with really long lines do not look good, so we will have to embed that setting into the dev-container later