Spaces:
Running
Running
Download algospaced-ui/src/services/catalogService.ts from iamfebin/algospaced-dsa: direct link, hf CLI and curl.
- Browser
- Download file 4.19 kB
-
https://huggingface.co/spaces/iamfebin/algospaced-dsa/resolve/main/algospaced-ui/src/services/catalogService.ts
- Command line
-
hf download hf://spaces/iamfebin/algospaced-dsa/algospaced-ui/src/services/catalogService.ts
-
curl -L -o catalogService.ts https://huggingface.co/spaces/iamfebin/algospaced-dsa/resolve/main/algospaced-ui/src/services/catalogService.ts
4.19 kB
| import { NEETCODE_150_DATA, type CatalogProblem } from '../data/neetcode150Data'; | |
| export type { CatalogProblem }; | |
| // Pre-built index maps for instant O(1) lookups | |
| const problemNumberMap = new Map<number, CatalogProblem>(); | |
| const problemIdMap = new Map<string, CatalogProblem>(); | |
| const patternMap = new Map<string, CatalogProblem[]>(); | |
| for (const prob of NEETCODE_150_DATA) { | |
| problemNumberMap.set(prob.problem_number, prob); | |
| problemIdMap.set(prob.id, prob); | |
| const patternList = patternMap.get(prob.pattern) || []; | |
| patternList.push(prob); | |
| patternMap.set(prob.pattern, patternList); | |
| } | |
| /** | |
| * Returns a catalog problem by its official LeetCode problem number. | |
| */ | |
| export function getCatalogProblemByNumber(problemNumber: number | string | undefined | null): CatalogProblem | undefined { | |
| if (problemNumber === undefined || problemNumber === null) return undefined; | |
| const num = typeof problemNumber === 'string' ? parseInt(problemNumber, 10) : problemNumber; | |
| if (isNaN(num)) return undefined; | |
| return problemNumberMap.get(num); | |
| } | |
| /** | |
| * Returns a catalog problem by its catalog ID (e.g. "nc-217", "prob-contains-duplicate", or "217"). | |
| */ | |
| export function getCatalogProblemById(id: string | undefined | null): CatalogProblem | undefined { | |
| if (!id) return undefined; | |
| const direct = problemIdMap.get(id); | |
| if (direct) return direct; | |
| // If numeric ID string (e.g. "217"), check problemNumberMap | |
| if (/^\d+$/.test(id)) { | |
| return problemNumberMap.get(parseInt(id, 10)); | |
| } | |
| // Normalize slug / id format (e.g. "prob-contains-duplicate" -> "containsduplicate") | |
| const cleanId = id.replace(/^prob-/, '').toLowerCase().replace(/[^a-z0-9]+/g, ''); | |
| for (const prob of NEETCODE_150_DATA) { | |
| const probNormalized = prob.name.toLowerCase().replace(/[^a-z0-9]+/g, ''); | |
| if (probNormalized === cleanId || prob.id.toLowerCase().replace(/[^a-z0-9]+/g, '') === cleanId) { | |
| return prob; | |
| } | |
| } | |
| return undefined; | |
| } | |
| /** | |
| * Returns a catalog problem by its problem name/title. | |
| */ | |
| export function getCatalogProblemByName(name: string | undefined | null): CatalogProblem | undefined { | |
| if (!name) return undefined; | |
| const cleanName = name.toLowerCase().replace(/\s*-\s*method.*$/i, '').replace(/[^a-z0-9]+/g, ''); | |
| for (const prob of NEETCODE_150_DATA) { | |
| const probNormalized = prob.name.toLowerCase().replace(/[^a-z0-9]+/g, ''); | |
| if (probNormalized === cleanName) { | |
| return prob; | |
| } | |
| } | |
| return undefined; | |
| } | |
| /** | |
| * Robust, unified catalog lookup across problem number, ID, and problem title. | |
| */ | |
| export function findCatalogProblem( | |
| problemNumber?: number | string | null, | |
| id?: string | null, | |
| name?: string | null | |
| ): CatalogProblem | undefined { | |
| if (problemNumber !== undefined && problemNumber !== null) { | |
| const p = getCatalogProblemByNumber(problemNumber); | |
| if (p) return p; | |
| } | |
| if (id) { | |
| const p = getCatalogProblemById(id); | |
| if (p) return p; | |
| } | |
| if (name) { | |
| const p = getCatalogProblemByName(name); | |
| if (p) return p; | |
| } | |
| return undefined; | |
| } | |
| /** | |
| * Returns all 150 curated problems in canonical expedition sequence. | |
| */ | |
| export function getAllCatalogProblems(): CatalogProblem[] { | |
| return NEETCODE_150_DATA; | |
| } | |
| /** | |
| * Returns all problems belonging to a specific algorithm pattern. | |
| */ | |
| export function getCatalogProblemsByPattern(pattern: string): CatalogProblem[] { | |
| return patternMap.get(pattern) || []; | |
| } | |
| /** | |
| * Returns all unique pattern categories across the 150 problems. | |
| */ | |
| export function getAllPatterns(): string[] { | |
| return Array.from(patternMap.keys()); | |
| } | |
| /** | |
| * Finds the next unsolved problem in the trail sequence given a set of completed numbers. | |
| */ | |
| export function getNextUnsolvedProblem( | |
| completedNumbers: Set<number>, | |
| sequence?: number[] | |
| ): CatalogProblem | undefined { | |
| if (sequence && sequence.length > 0) { | |
| for (const num of sequence) { | |
| if (!completedNumbers.has(num)) { | |
| const prob = problemNumberMap.get(num); | |
| if (prob) return prob; | |
| } | |
| } | |
| } | |
| // Fallback to iterating canonical catalog | |
| for (const prob of NEETCODE_150_DATA) { | |
| if (!completedNumbers.has(prob.problem_number)) { | |
| return prob; | |
| } | |
| } | |
| return undefined; | |
| } | |