Ассистент сам перечислил «LeetCode 45 — Jump Game II», а на просьбу дать код ответил, что такой задачи нет: разбор знал задачи LeetCode только по слагу. Теперь понимает «45», «lc:45», «LC 45», «LeetCode 45 — …», а строку без кода ищет по точному названию, если оно в каталоге одно. Это общий разбор: работает и во вставке списка на странице, и в get_problem. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
201 lines
8.1 KiB
Python
201 lines
8.1 KiB
Python
"""Разбор списка задач, вставленного преподавателем текстом.
|
|
|
|
Принимает вперемешку ссылки, слаги и коды задач — по строке на задачу:
|
|
|
|
https://leetcode.com/problems/two-sum/
|
|
two-sum
|
|
lc:binary-search
|
|
https://codeforces.com/problemset/problem/4/A
|
|
cf:4A
|
|
1352G
|
|
LeetCode 45 — Jump Game II
|
|
Jump Game II
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import re
|
|
from dataclasses import dataclass, field
|
|
|
|
from sqlalchemy import String, cast, func, select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.models import Platform, Problem
|
|
|
|
LC_URL = re.compile(r"leetcode\.com/problems/([a-z0-9-]+)", re.I)
|
|
CF_URL = re.compile(
|
|
r"codeforces\.com/(?:contest|problemset/problem)/(\d+)/?(?:problem/)?([A-Za-z]\d*)",
|
|
re.I,
|
|
)
|
|
CF_CODE = re.compile(r"^(\d{1,6})([A-Za-z]\d*)$")
|
|
LC_SLUG = re.compile(r"^[a-z0-9]+(?:-[a-z0-9]+)*$")
|
|
# Номер LeetCode: «45», «lc:45», «LC 45», «LeetCode #45 — Jump Game II».
|
|
# После номера не может идти буква или дефис: «lc:1-bit-and-2-bit-characters»
|
|
# — это слаг, а не задача номер 1.
|
|
LC_NUMBER = re.compile(r"^(?:(?:lc|leetcode)\s*[:#№]?\s*)?(\d+)(?![\w-])", re.I)
|
|
|
|
|
|
@dataclass(slots=True)
|
|
class ParseResult:
|
|
problems: list[Problem] = field(default_factory=list)
|
|
unresolved: list[str] = field(default_factory=list)
|
|
|
|
|
|
@dataclass(slots=True)
|
|
class _Ref:
|
|
platform: Platform | None
|
|
key: str
|
|
raw: str
|
|
by_number: bool = False
|
|
|
|
|
|
def _classify(line: str) -> _Ref | None:
|
|
line = line.strip().strip(",;")
|
|
if not line:
|
|
return None
|
|
|
|
if match := LC_URL.search(line):
|
|
return _Ref(Platform.leetcode, match.group(1).lower(), line)
|
|
if match := CF_URL.search(line):
|
|
return _Ref(Platform.codeforces, f"{match.group(1)}{match.group(2).upper()}", line)
|
|
|
|
lowered = line.lower()
|
|
if match := LC_NUMBER.match(line):
|
|
rest = line[match.end() :].strip()
|
|
# Голое число — номер, а «45 ...» без приставки — уже не наш случай.
|
|
if not rest or not match.group(0).isdigit():
|
|
return _Ref(Platform.leetcode, match.group(1).lstrip("0") or "0", line, by_number=True)
|
|
# Свои задачи пишутся с приставкой: голый слаг неотличим от LeetCode,
|
|
# а угадывать, какую из двух задач имели в виду, — плохая идея.
|
|
for prefix in ("task:", "local:"):
|
|
if lowered.startswith(prefix):
|
|
return _Ref(Platform.local, line[len(prefix) :].strip().lower(), line)
|
|
for prefix, platform in (("lc:", Platform.leetcode), ("cf:", Platform.codeforces)):
|
|
if lowered.startswith(prefix):
|
|
rest = line[len(prefix) :].strip()
|
|
if platform == Platform.codeforces:
|
|
match = CF_CODE.match(rest)
|
|
if match:
|
|
return _Ref(platform, f"{match.group(1)}{match.group(2).upper()}", line)
|
|
return _Ref(platform, rest, line)
|
|
return _Ref(platform, rest.lower(), line)
|
|
|
|
if match := CF_CODE.match(line):
|
|
return _Ref(Platform.codeforces, f"{match.group(1)}{match.group(2).upper()}", line)
|
|
if LC_SLUG.match(lowered):
|
|
return _Ref(Platform.leetcode, lowered, line)
|
|
return _Ref(None, line, line)
|
|
|
|
|
|
async def parse_problem_list(session: AsyncSession, text: str) -> ParseResult:
|
|
refs = [ref for ref in (_classify(line) for line in text.splitlines()) if ref is not None]
|
|
result = ParseResult()
|
|
if not refs:
|
|
return result
|
|
|
|
cf_keys = {r.key for r in refs if r.platform == Platform.codeforces}
|
|
lc_keys = {r.key for r in refs if r.platform == Platform.leetcode and not r.by_number}
|
|
lc_numbers = {r.key for r in refs if r.platform == Platform.leetcode and r.by_number}
|
|
titles = {r.key.lower() for r in refs if r.platform is None}
|
|
local_keys = {r.key for r in refs if r.platform == Platform.local}
|
|
|
|
found: dict[tuple[Platform, str], Problem] = {}
|
|
if cf_keys:
|
|
rows = await session.execute(
|
|
select(Problem).where(
|
|
Problem.platform == Platform.codeforces,
|
|
func.upper(Problem.external_id).in_({k.upper() for k in cf_keys}),
|
|
)
|
|
)
|
|
for problem in rows.scalars():
|
|
found[(Platform.codeforces, problem.external_id.upper())] = problem
|
|
if lc_keys:
|
|
rows = await session.execute(
|
|
select(Problem).where(Problem.platform == Platform.leetcode, Problem.slug.in_(lc_keys))
|
|
)
|
|
for problem in rows.scalars():
|
|
found[(Platform.leetcode, problem.slug)] = problem
|
|
if lc_numbers:
|
|
rows = await session.execute(
|
|
select(Problem).where(
|
|
Problem.platform == Platform.leetcode, Problem.external_id.in_(lc_numbers)
|
|
)
|
|
)
|
|
for problem in rows.scalars():
|
|
found[(Platform.leetcode, "#" + problem.external_id)] = problem
|
|
by_title: dict[str, list[Problem]] = {}
|
|
if titles:
|
|
# Только площадки: своя задача пишется с приставкой (см. выше), а
|
|
# название, которое есть у двух задач, не угадываем.
|
|
rows = await session.execute(
|
|
select(Problem).where(
|
|
Problem.platform.in_((Platform.leetcode, Platform.codeforces)),
|
|
func.lower(Problem.title).in_(titles),
|
|
)
|
|
)
|
|
for problem in rows.scalars():
|
|
by_title.setdefault(problem.title.lower(), []).append(problem)
|
|
if local_keys:
|
|
rows = await session.execute(
|
|
select(Problem).where(Problem.platform == Platform.local, Problem.slug.in_(local_keys))
|
|
)
|
|
for problem in rows.scalars():
|
|
found[(Platform.local, problem.slug)] = problem
|
|
|
|
seen: set[int] = set()
|
|
for ref in refs:
|
|
problem = None
|
|
if ref.platform == Platform.codeforces:
|
|
problem = found.get((Platform.codeforces, ref.key.upper()))
|
|
elif ref.platform == Platform.leetcode:
|
|
key = "#" + ref.key if ref.by_number else ref.key
|
|
problem = found.get((Platform.leetcode, key))
|
|
elif ref.platform == Platform.local:
|
|
problem = found.get((Platform.local, ref.key))
|
|
else:
|
|
same = by_title.get(ref.key.lower(), [])
|
|
problem = same[0] if len(same) == 1 else None
|
|
if problem is None:
|
|
result.unresolved.append(ref.raw)
|
|
elif problem.id not in seen:
|
|
seen.add(problem.id)
|
|
result.problems.append(problem)
|
|
return result
|
|
|
|
|
|
async def search_problems(
|
|
session: AsyncSession,
|
|
query: str,
|
|
platform: Platform | None = None,
|
|
limit: int = 40,
|
|
*,
|
|
tag: str | None = None,
|
|
difficulty: str | None = None,
|
|
min_rating: int | None = None,
|
|
max_rating: int | None = None,
|
|
) -> list[Problem]:
|
|
stmt = select(Problem)
|
|
if platform:
|
|
stmt = stmt.where(Problem.platform == platform)
|
|
if tag and tag.strip():
|
|
# Метки лежат JSON-списком строк. Ищем метку целиком, в кавычках:
|
|
# иначе «dp» нашёлся бы и внутри «dp on trees», и внутри чужих слов.
|
|
needle = '%"' + tag.strip().lower() + '"%'
|
|
stmt = stmt.where(func.lower(cast(Problem.tags, String)).like(needle))
|
|
if difficulty and difficulty.strip():
|
|
stmt = stmt.where(func.lower(Problem.difficulty) == difficulty.strip().lower())
|
|
if min_rating is not None:
|
|
stmt = stmt.where(Problem.rating >= min_rating)
|
|
if max_rating is not None:
|
|
stmt = stmt.where(Problem.rating <= max_rating)
|
|
query = query.strip()
|
|
if query:
|
|
pattern = f"%{query.lower()}%"
|
|
stmt = stmt.where(
|
|
func.lower(Problem.title).like(pattern)
|
|
| func.lower(Problem.slug).like(pattern)
|
|
| func.lower(Problem.external_id).like(pattern)
|
|
)
|
|
stmt = stmt.order_by(Problem.platform, Problem.title).limit(limit)
|
|
return list((await session.execute(stmt)).scalars().all())
|