Compare commits
10 Commits
73cfc7cf13
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| a4b8468cba | |||
| 2cb09552c2 | |||
| 49c7132863 | |||
| 0b2a2e5cd0 | |||
| 427e0bf72a | |||
| 9f3449353b | |||
| d54543b160 | |||
| b939e70084 | |||
| a45e1e9585 | |||
| a94ec30f08 |
12
.github/workflows/deploy-homelab.yml
vendored
12
.github/workflows/deploy-homelab.yml
vendored
@@ -1,4 +1,4 @@
|
|||||||
name: "Deploy to Homelab"
|
name: "Deploy to birb co. production"
|
||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
@@ -23,23 +23,21 @@ jobs:
|
|||||||
|
|
||||||
- name: Remove directory from server
|
- name: Remove directory from server
|
||||||
run: |
|
run: |
|
||||||
ssh -i ~/.ssh/id_ed25519 github@${{ vars.HOST }} << 'EOF'
|
ssh -i ~/.ssh/id_ed25519 ${{ vars.USERNAME }}@${{ vars.HOST }} << 'EOF'
|
||||||
rm -rf ~/homelab-dsa-tutoring
|
rm -rf ~/homelab-dsa-tutoring
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
# Avoid needing to set up SSH access to GitHub for this user
|
# Avoid needing to set up SSH access to GitHub for this user
|
||||||
- name: Transfer repository files to server
|
- name: Transfer repository files to server
|
||||||
run: |
|
run: |
|
||||||
scp -i ~/.ssh/id_ed25519 -r ./* github@${{ vars.HOST }}:~/homelab-dsa-tutoring
|
scp -i ~/.ssh/id_ed25519 -r ./* ${{ vars.USERNAME }}@${{ vars.HOST }}:~/homelab-dsa-tutoring
|
||||||
|
|
||||||
- name: Deploy on server with Docker
|
- name: Deploy on server with Docker
|
||||||
run: |
|
run: |
|
||||||
ssh -i ~/.ssh/id_ed25519 github@${{ vars.HOST }} << 'EOF'
|
ssh -i ~/.ssh/id_ed25519 ${{ vars.USERNAME }}@${{ vars.HOST }} << 'EOF'
|
||||||
cd ~/homelab-dsa-tutoring
|
cd ~/homelab-dsa-tutoring
|
||||||
export TS_AUTHKEY=${{ secrets.TS_CONTAINER_AUTHKEY }}
|
export DOCKER_HOST=unix://$XDG_RUNTIME_DIR/docker.sock
|
||||||
docker compose -f docker-compose.yml down
|
docker compose -f docker-compose.yml down
|
||||||
docker compose -f docker-compose.yml up -d --build
|
docker compose -f docker-compose.yml up -d --build
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
3
.gitignore
vendored
3
.gitignore
vendored
@@ -174,3 +174,6 @@ cython_debug/
|
|||||||
.pypirc
|
.pypirc
|
||||||
|
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
|
||||||
|
# Generated build output
|
||||||
|
public/
|
||||||
|
|||||||
32
CLAUDE.md
Normal file
32
CLAUDE.md
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
# homelab-dsa-tutoring
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
|
||||||
|
This is a collection of Python projects for a tutoring side-gig focused on Data Structures and Algorithms (DSA).
|
||||||
|
|
||||||
|
It was previously deployed to a personal homelab server as a Tailscale file server (see `.github/workflows/deploy-homelab.yml`). The workflow SCP'd the repo to the server and ran a Docker Compose stack that served the files.
|
||||||
|
|
||||||
|
## Structure
|
||||||
|
|
||||||
|
```
|
||||||
|
shared/
|
||||||
|
├── assignments/ # PDFs and other assignment materials
|
||||||
|
└── notes-and-examples/ # Lesson files organized by date (YYYY.MM.DD)
|
||||||
|
```
|
||||||
|
|
||||||
|
Each lesson folder under `notes-and-examples/` contains one or more Python files used during the tutoring session.
|
||||||
|
|
||||||
|
## Lessons
|
||||||
|
|
||||||
|
| Date | Topics |
|
||||||
|
|------|--------|
|
||||||
|
| 2026.01.21 | Sorting algorithms (bubble sort, insertion sort) |
|
||||||
|
| 2026.01.28 | Object references, shallow/deep copies, linked lists |
|
||||||
|
| 2026.02.04 | Queue data structure |
|
||||||
|
| 2026.02.11 | Stack data structure |
|
||||||
|
|
||||||
|
## Notes
|
||||||
|
|
||||||
|
- No external dependencies — pure Python, no `requirements.txt` needed.
|
||||||
|
- Student homework submissions go to `me@bchen.dev`.
|
||||||
|
- Lesson files often include skeleton classes with method stubs for students to implement.
|
||||||
13
Caddyfile
Normal file
13
Caddyfile
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
# DSA Tutoring microsite
|
||||||
|
#
|
||||||
|
# For local use, serves on port 8080
|
||||||
|
# Replace :80 with your domain (e.g. dsa-tutoring.example.com) for
|
||||||
|
# automatic HTTPS via Let's Encrypt.
|
||||||
|
#
|
||||||
|
# Run `python build.py` first to generate the public/ directory.
|
||||||
|
|
||||||
|
:8080 {
|
||||||
|
root * /srv/public
|
||||||
|
encode gzip
|
||||||
|
file_server
|
||||||
|
}
|
||||||
287
build.py
Normal file
287
build.py
Normal file
@@ -0,0 +1,287 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""Build script: scans shared/, generates zip downloads and public/index.html."""
|
||||||
|
|
||||||
|
import html
|
||||||
|
import os
|
||||||
|
import shutil
|
||||||
|
import zipfile
|
||||||
|
from dataclasses import dataclass, field
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
SHARED_DIR = Path("shared")
|
||||||
|
PUBLIC_DIR = Path("public")
|
||||||
|
ZIPS_DIR = PUBLIC_DIR / "zips"
|
||||||
|
|
||||||
|
EXCLUDE_NAMES = {".DS_Store", "__pycache__", ".git", "Thumbs.db"}
|
||||||
|
EXCLUDE_SUFFIXES = {".pyc"}
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class SubSection:
|
||||||
|
name: str
|
||||||
|
zip_url: str
|
||||||
|
files: list[str]
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Section:
|
||||||
|
title: str
|
||||||
|
subsections: list[SubSection] = field(default_factory=list)
|
||||||
|
zip_url: str | None = None
|
||||||
|
files: list[str] | None = None
|
||||||
|
|
||||||
|
|
||||||
|
# --- File filtering ---
|
||||||
|
|
||||||
|
def should_include(path: Path) -> bool:
|
||||||
|
return path.name not in EXCLUDE_NAMES and path.suffix not in EXCLUDE_SUFFIXES
|
||||||
|
|
||||||
|
|
||||||
|
# --- Zip and file listing ---
|
||||||
|
|
||||||
|
def create_zip(source_dir: Path, zip_path: Path) -> None:
|
||||||
|
"""Zip source_dir into zip_path; contents extract under a named folder."""
|
||||||
|
with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zf:
|
||||||
|
for file_path in sorted(source_dir.rglob("*")):
|
||||||
|
if not file_path.is_file():
|
||||||
|
continue
|
||||||
|
relative = file_path.relative_to(source_dir.parent)
|
||||||
|
if any(part in EXCLUDE_NAMES for part in relative.parts):
|
||||||
|
continue
|
||||||
|
if not should_include(file_path):
|
||||||
|
continue
|
||||||
|
zf.write(file_path, arcname=relative)
|
||||||
|
|
||||||
|
|
||||||
|
def list_files(source_dir: Path) -> list[str]:
|
||||||
|
"""Return sorted list of filtered relative file paths for display."""
|
||||||
|
results = []
|
||||||
|
for file_path in sorted(source_dir.rglob("*")):
|
||||||
|
if not file_path.is_file():
|
||||||
|
continue
|
||||||
|
relative = file_path.relative_to(source_dir)
|
||||||
|
if any(part in EXCLUDE_NAMES for part in relative.parts):
|
||||||
|
continue
|
||||||
|
if not should_include(file_path):
|
||||||
|
continue
|
||||||
|
results.append(str(relative))
|
||||||
|
return results
|
||||||
|
|
||||||
|
|
||||||
|
# --- Data model construction ---
|
||||||
|
|
||||||
|
def scan_shared() -> list[Section]:
|
||||||
|
sections = []
|
||||||
|
|
||||||
|
assignments_dir = SHARED_DIR / "assignments"
|
||||||
|
if assignments_dir.exists():
|
||||||
|
subdirs = sorted(
|
||||||
|
d for d in assignments_dir.iterdir()
|
||||||
|
if d.is_dir() and should_include(d)
|
||||||
|
)
|
||||||
|
if subdirs:
|
||||||
|
subsections = []
|
||||||
|
for subdir in subdirs:
|
||||||
|
zip_name = f"assignments-{subdir.name}.zip"
|
||||||
|
create_zip(subdir, ZIPS_DIR / zip_name)
|
||||||
|
subsections.append(SubSection(
|
||||||
|
name=subdir.name,
|
||||||
|
zip_url=f"zips/{zip_name}",
|
||||||
|
files=list_files(subdir),
|
||||||
|
))
|
||||||
|
sections.append(Section(title="Assignments", subsections=subsections))
|
||||||
|
else:
|
||||||
|
zip_name = "assignments.zip"
|
||||||
|
create_zip(assignments_dir, ZIPS_DIR / zip_name)
|
||||||
|
sections.append(Section(
|
||||||
|
title="Assignments",
|
||||||
|
zip_url=f"zips/{zip_name}",
|
||||||
|
files=list_files(assignments_dir),
|
||||||
|
))
|
||||||
|
|
||||||
|
notes_dir = SHARED_DIR / "notes-and-examples"
|
||||||
|
if notes_dir.exists():
|
||||||
|
date_dirs = sorted(
|
||||||
|
d for d in notes_dir.iterdir()
|
||||||
|
if d.is_dir() and should_include(d)
|
||||||
|
)
|
||||||
|
subsections = []
|
||||||
|
for date_dir in date_dirs:
|
||||||
|
zip_name = f"notes-and-examples-{date_dir.name}.zip"
|
||||||
|
create_zip(date_dir, ZIPS_DIR / zip_name)
|
||||||
|
subsections.append(SubSection(
|
||||||
|
name=date_dir.name,
|
||||||
|
zip_url=f"zips/{zip_name}",
|
||||||
|
files=list_files(date_dir),
|
||||||
|
))
|
||||||
|
sections.append(Section(title="Notes and Examples", subsections=subsections))
|
||||||
|
|
||||||
|
return sections
|
||||||
|
|
||||||
|
|
||||||
|
# --- HTML rendering ---
|
||||||
|
|
||||||
|
HTML_TEMPLATE = """\
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>DSA Tutoring</title>
|
||||||
|
<style>
|
||||||
|
*, *::before, *::after {{ box-sizing: border-box; }}
|
||||||
|
body {{
|
||||||
|
font-family: system-ui, sans-serif;
|
||||||
|
max-width: 800px;
|
||||||
|
margin: 0 auto;
|
||||||
|
padding: 2rem 1rem;
|
||||||
|
color: #1a1a1a;
|
||||||
|
background: #f5f5f5;
|
||||||
|
}}
|
||||||
|
h1 {{ font-size: 1.75rem; margin-bottom: 0.25rem; }}
|
||||||
|
.subtitle {{ color: #666; margin-bottom: 2rem; font-size: 0.95rem; }}
|
||||||
|
.section {{
|
||||||
|
background: white;
|
||||||
|
border: 1px solid #e0e0e0;
|
||||||
|
border-radius: 8px;
|
||||||
|
padding: 1.5rem;
|
||||||
|
margin-bottom: 1.5rem;
|
||||||
|
}}
|
||||||
|
.section > h2 {{
|
||||||
|
margin: 0 0 1rem 0;
|
||||||
|
font-size: 1.2rem;
|
||||||
|
padding-bottom: 0.75rem;
|
||||||
|
border-bottom: 1px solid #eee;
|
||||||
|
}}
|
||||||
|
.section-flat {{
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 0.75rem;
|
||||||
|
}}
|
||||||
|
.subsection {{
|
||||||
|
padding-top: 0.75rem;
|
||||||
|
border-top: 1px solid #f0f0f0;
|
||||||
|
}}
|
||||||
|
.subsection:first-child {{ border-top: none; padding-top: 0; }}
|
||||||
|
.subsection-header {{
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: space-between;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
gap: 0.5rem;
|
||||||
|
margin-bottom: 0.5rem;
|
||||||
|
}}
|
||||||
|
.subsection-header h3 {{
|
||||||
|
margin: 0;
|
||||||
|
font-size: 0.95rem;
|
||||||
|
font-family: monospace;
|
||||||
|
color: #333;
|
||||||
|
}}
|
||||||
|
.download-btn {{
|
||||||
|
display: inline-block;
|
||||||
|
padding: 0.35rem 0.85rem;
|
||||||
|
background: #2563eb;
|
||||||
|
color: white;
|
||||||
|
text-decoration: none;
|
||||||
|
border-radius: 5px;
|
||||||
|
font-size: 0.8rem;
|
||||||
|
white-space: nowrap;
|
||||||
|
}}
|
||||||
|
.download-btn:hover {{ background: #1d4ed8; }}
|
||||||
|
.file-list {{
|
||||||
|
list-style: none;
|
||||||
|
padding: 0;
|
||||||
|
margin: 0;
|
||||||
|
}}
|
||||||
|
.file-list li {{
|
||||||
|
font-family: monospace;
|
||||||
|
font-size: 0.82rem;
|
||||||
|
color: #555;
|
||||||
|
padding: 0.15rem 0;
|
||||||
|
}}
|
||||||
|
.file-list li::before {{ content: "— "; color: #bbb; }}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>DSA Tutoring</h1>
|
||||||
|
<p class="subtitle">Lesson files and assignments. Click a download button to get a .zip archive.</p>
|
||||||
|
{body}
|
||||||
|
</body>
|
||||||
|
</html>"""
|
||||||
|
|
||||||
|
|
||||||
|
def render_file_list(files: list[str]) -> str:
|
||||||
|
items = "".join(f" <li>{html.escape(f)}</li>\n" for f in files)
|
||||||
|
return f" <ul class=\"file-list\">\n{items} </ul>"
|
||||||
|
|
||||||
|
|
||||||
|
def render_download_btn(zip_url: str, label: str) -> str:
|
||||||
|
return (
|
||||||
|
f"<a class=\"download-btn\" href=\"{html.escape(zip_url)}\" download>"
|
||||||
|
f"Download {html.escape(label)} (.zip)</a>"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def render_section(section: Section) -> str:
|
||||||
|
if section.subsections:
|
||||||
|
parts = []
|
||||||
|
for sub in section.subsections:
|
||||||
|
parts.append(
|
||||||
|
f" <div class=\"subsection\">\n"
|
||||||
|
f" <div class=\"subsection-header\">\n"
|
||||||
|
f" <h3>{html.escape(sub.name)}</h3>\n"
|
||||||
|
f" {render_download_btn(sub.zip_url, sub.name)}\n"
|
||||||
|
f" </div>\n"
|
||||||
|
f"{render_file_list(sub.files)}\n"
|
||||||
|
f" </div>"
|
||||||
|
)
|
||||||
|
inner = "\n".join(parts)
|
||||||
|
else:
|
||||||
|
inner = (
|
||||||
|
f" <div class=\"section-flat\">\n"
|
||||||
|
f" {render_download_btn(section.zip_url, section.title)}\n"
|
||||||
|
f"{render_file_list(section.files or [])}\n"
|
||||||
|
f" </div>"
|
||||||
|
)
|
||||||
|
|
||||||
|
return (
|
||||||
|
f" <section class=\"section\">\n"
|
||||||
|
f" <h2>{html.escape(section.title)}</h2>\n"
|
||||||
|
f"{inner}\n"
|
||||||
|
f" </section>"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def render_html(sections: list[Section]) -> str:
|
||||||
|
body = "\n".join(render_section(s) for s in sections)
|
||||||
|
return HTML_TEMPLATE.format(body=body)
|
||||||
|
|
||||||
|
|
||||||
|
# --- Entry point ---
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
os.chdir(Path(__file__).parent)
|
||||||
|
|
||||||
|
if PUBLIC_DIR.exists():
|
||||||
|
shutil.rmtree(PUBLIC_DIR)
|
||||||
|
PUBLIC_DIR.mkdir()
|
||||||
|
ZIPS_DIR.mkdir()
|
||||||
|
|
||||||
|
sections = scan_shared()
|
||||||
|
html_content = render_html(sections)
|
||||||
|
|
||||||
|
output = PUBLIC_DIR / "index.html"
|
||||||
|
output.write_text(html_content, encoding="utf-8")
|
||||||
|
print(f"Built {output}")
|
||||||
|
|
||||||
|
robots = PUBLIC_DIR / "robots.txt"
|
||||||
|
robots.write_text("User-agent: *\nDisallow: /\n", encoding="utf-8")
|
||||||
|
print(f"Built {robots}")
|
||||||
|
|
||||||
|
print(f"Zips in {ZIPS_DIR}:")
|
||||||
|
for z in sorted(ZIPS_DIR.iterdir()):
|
||||||
|
print(f" {z.name}")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
{
|
|
||||||
"TCP": {
|
|
||||||
"443": {
|
|
||||||
"HTTPS": true
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"Web": {
|
|
||||||
"dsa-tutoring-1.tail8f43b.ts.net:443": {
|
|
||||||
"Handlers": {
|
|
||||||
"/": {
|
|
||||||
"Path": "/shared"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"AllowFunnel": {
|
|
||||||
"dsa-tutoring-1.tail8f43b.ts.net:443": true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,22 +1,24 @@
|
|||||||
services:
|
services:
|
||||||
tailscale:
|
builder:
|
||||||
image: tailscale/tailscale:stable
|
image: python:3-alpine
|
||||||
container_name: tailscale-dsa-tutoring
|
command: python /app/build.py
|
||||||
hostname: dsa-tutoring
|
|
||||||
environment:
|
|
||||||
- TS_AUTHKEY=${TS_AUTHKEY}
|
|
||||||
- TS_STATE_DIR=/var/lib/tailscale
|
|
||||||
- TS_SERVE_CONFIG=/config/tailscale-serve-config.json
|
|
||||||
volumes:
|
volumes:
|
||||||
- tailscale-dsa-tutoring-state:/var/lib/tailscale
|
- .:/app
|
||||||
- /dev/net/tun:/dev/net/tun # shared interface across all Tailscale instances
|
|
||||||
- ./config:/config
|
caddy:
|
||||||
- ./shared:/shared
|
image: caddy:2-alpine
|
||||||
cap_add:
|
|
||||||
- NET_ADMIN
|
|
||||||
- SYS_MODULE
|
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
|
depends_on:
|
||||||
|
builder:
|
||||||
|
condition: service_completed_successfully
|
||||||
|
ports:
|
||||||
|
- "8080:8080"
|
||||||
|
volumes:
|
||||||
|
- ./Caddyfile:/etc/caddy/Caddyfile
|
||||||
|
- ./public:/srv/public:ro
|
||||||
|
- caddy_data:/data
|
||||||
|
- caddy_config:/config
|
||||||
|
|
||||||
volumes:
|
volumes:
|
||||||
tailscale-dsa-tutoring-state:
|
caddy_data:
|
||||||
|
caddy_config:
|
||||||
|
|||||||
Binary file not shown.
2
shared/notes-and-examples/2026.02.28/homework.py
Normal file
2
shared/notes-and-examples/2026.02.28/homework.py
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
# Homework:
|
||||||
|
# See youtube-playlists under Assignments
|
||||||
71
shared/notes-and-examples/2026.02.28/linked_list_solution.py
Normal file
71
shared/notes-and-examples/2026.02.28/linked_list_solution.py
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
# Solution to 2026.01.28 homework
|
||||||
|
|
||||||
|
class Node:
|
||||||
|
prev_node: "Node | None" = None
|
||||||
|
next_node: "Node | None" = None
|
||||||
|
|
||||||
|
def __init__(self, value) -> None:
|
||||||
|
self.value = value
|
||||||
|
|
||||||
|
class LinkedList:
|
||||||
|
head: "Node | None" = None
|
||||||
|
tail: "Node | None" = None
|
||||||
|
|
||||||
|
def append(self, thing):
|
||||||
|
if self.head==None:
|
||||||
|
self.head=Node(value=thing)
|
||||||
|
self.tail=self.head
|
||||||
|
return
|
||||||
|
|
||||||
|
if self.tail is not None:
|
||||||
|
self.tail.next_node=Node(value=thing)
|
||||||
|
if self.tail.next_node is not None:
|
||||||
|
self.tail.next_node.prev_node = self.tail
|
||||||
|
self.tail=self.tail.next_node
|
||||||
|
|
||||||
|
def prepend(self, thing):
|
||||||
|
previous_head = self.head
|
||||||
|
self.head = Node(value=thing)
|
||||||
|
self.head.next_node = previous_head
|
||||||
|
|
||||||
|
if previous_head is not None:
|
||||||
|
previous_head.prev_node = self.head
|
||||||
|
|
||||||
|
def insert(self, thing, index):
|
||||||
|
current_node = self.head
|
||||||
|
if current_node is None:
|
||||||
|
self.append(thing)
|
||||||
|
return
|
||||||
|
|
||||||
|
if index == 0:
|
||||||
|
self.prepend(thing)
|
||||||
|
return
|
||||||
|
|
||||||
|
for _ in range(1, index):
|
||||||
|
if current_node is not None:
|
||||||
|
current_node = current_node.next_node
|
||||||
|
if current_node is None:
|
||||||
|
raise IndexError
|
||||||
|
|
||||||
|
prev_next = current_node.next_node
|
||||||
|
current_node.next_node = Node(thing)
|
||||||
|
current_node.next_node.prev_node = current_node
|
||||||
|
current_node.next_node.next_node = prev_next
|
||||||
|
|
||||||
|
|
||||||
|
def print_list(self):
|
||||||
|
current_node = self.head
|
||||||
|
while current_node is not None:
|
||||||
|
print(current_node.value)
|
||||||
|
current_node = current_node.next_node
|
||||||
|
|
||||||
|
example_list = LinkedList()
|
||||||
|
example_list.append("first")
|
||||||
|
example_list.append("second")
|
||||||
|
example_list.append("fourth")
|
||||||
|
example_list.insert("third", 2)
|
||||||
|
example_list.insert("zero", 0)
|
||||||
|
example_list.insert("last", 5)
|
||||||
|
|
||||||
|
example_list.print_list()
|
||||||
|
|
||||||
51
shared/notes-and-examples/2026.02.28/queues.py
Normal file
51
shared/notes-and-examples/2026.02.28/queues.py
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
class Node:
|
||||||
|
next_node: "None | Node" = None
|
||||||
|
prev_node: "None | Node" = None
|
||||||
|
|
||||||
|
def __init__(self, value):
|
||||||
|
self.value = value
|
||||||
|
|
||||||
|
class Queue:
|
||||||
|
front: "Node | None" = None
|
||||||
|
back: "Node | None" = None
|
||||||
|
|
||||||
|
def append(self, value):
|
||||||
|
if self.back is None:
|
||||||
|
self.front = Node(value)
|
||||||
|
self.back = self.front
|
||||||
|
else:
|
||||||
|
new_node = Node(value)
|
||||||
|
self.back.prev_node = new_node
|
||||||
|
new_node.next_node = self.back
|
||||||
|
self.back = new_node
|
||||||
|
|
||||||
|
def popleft(self):
|
||||||
|
if self.front is not None:
|
||||||
|
prev_front = self.front
|
||||||
|
|
||||||
|
prev_node_in_queue = self.front.prev_node
|
||||||
|
self.front.next_node = None
|
||||||
|
self.front.prev_node = None
|
||||||
|
if prev_node_in_queue is not None:
|
||||||
|
prev_node_in_queue.next_node = None
|
||||||
|
|
||||||
|
self.front = prev_node_in_queue
|
||||||
|
|
||||||
|
return prev_front
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
|
||||||
|
def print_queue(self):
|
||||||
|
current_node = self.front
|
||||||
|
while current_node is not None:
|
||||||
|
print(current_node.value)
|
||||||
|
current_node = current_node.prev_node
|
||||||
|
|
||||||
|
|
||||||
|
queue = Queue()
|
||||||
|
queue.append("first")
|
||||||
|
queue.append("second")
|
||||||
|
queue.append("third")
|
||||||
|
queue.popleft()
|
||||||
|
queue.popleft()
|
||||||
|
queue.print_queue()
|
||||||
Reference in New Issue
Block a user