Every snippet below was written by the Flash-Next model running on llama.cpp. Styling & palette fronted by the same model.
Typed, documented, O(n).
def merge_sorted(a: list[T], b: list[T]) -> list[T]:
"""Merge two sorted lists into one sorted list in O(n)."""
result: list[T] = []
i = j = 0
while i < len(a) and j < len(b):
if a[i] <= b[j]:
result.append(a[i]); i += 1
else:
result.append(b[j]); j += 1
result.extend(a[i:]); result.extend(b[j:])
return result
from collections import OrderedDict
from typing import Generic, TypeVar, Optional
K = TypeVar("K"); V = TypeVar("V")
class LRUCache(Generic[K, V]):
"""LRU cache with O(1) get/put."""
def __init__(self, capacity: int) -> None:
if capacity <= 0: raise ValueError("capacity must be > 0")
self.capacity = capacity
self._cache: OrderedDict[K, V] = OrderedDict()
def get(self, key: K) -> Optional[V]:
if key not in self._cache: return None
self._cache.move_to_end(key)
return self._cache[key]
def put(self, key: K, value: V) -> None:
if key in self._cache: self._cache.move_to_end(key)
self._cache[key] = value
if len(self._cache) > self.capacity:
self._cache.popitem(last=False)
Debounce.
const debounce = (fn, delay = 100) => {
let t;
return (...args) => {
clearTimeout(t);
t = setTimeout(() => fn(...args), delay);
};
};
Function component with useState.
import { useState } from 'react';
export default function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<h1>Count: {count}</h1>
<button onClick={() => setCount(c => c - 1)}>-</button>
<button onClick={() => setCount(c => c + 1)}>+</button>
</div>
);
}
Top 5 most-purchased products.
SELECT oi.product_id, SUM(oi.quantity) AS total_quantity
FROM order_items oi
JOIN orders o ON o.order_id = oi.order_id
GROUP BY oi.product_id
ORDER BY total_quantity DESC
LIMIT 5;
Largest file in a tree.
largest_file() {
find "${1:-.}" -type f -printf '%s\t%p\n' \
| sort -nr | head -n1 | cut -f2
}