Download lib/code-templates.ts from brightmind-ai/codepractice-ai: direct link, hf CLI and curl.
- Browser
- Download file 3.83 kB
-
https://huggingface.co/spaces/brightmind-ai/codepractice-ai/resolve/main/lib/code-templates.ts
- Command line
-
hf download hf://spaces/brightmind-ai/codepractice-ai/lib/code-templates.ts
-
curl -L -o code-templates.ts https://huggingface.co/spaces/brightmind-ai/codepractice-ai/resolve/main/lib/code-templates.ts
3.83 kB
| import { type SupportedLanguage } from './llmChecker' | |
| import { type Question } from './questions' | |
| // Extract function name and parameters from Python starter code | |
| function parsePythonFunction(starterCode: string): { functionName: string; params: string[] } | null { | |
| const match = starterCode.match(/def\s+(\w+)\s*\(([^)]*)\)/) | |
| if (!match) return null | |
| const functionName = match[1] | |
| const paramsStr = match[2].trim() | |
| const params = paramsStr ? paramsStr.split(',').map(p => p.trim().split(':')[0].trim()) : [] | |
| return { functionName, params } | |
| } | |
| // Generate starter code for different languages | |
| export function generateStarterCode(question: Question, language: SupportedLanguage): string { | |
| const parsed = parsePythonFunction(question.starterCode) | |
| if (!parsed) { | |
| // Fallback to Python if parsing fails | |
| return language === 'python' ? question.starterCode : question.starterCode | |
| } | |
| const { functionName, params } = parsed | |
| const paramsStr = params.join(', ') | |
| switch (language) { | |
| case 'python': | |
| return question.starterCode | |
| case 'java': | |
| // Infer types from test cases to match exactly | |
| // Check first test case to determine parameter and return types | |
| const firstTestCase = question.testCases && question.testCases.length > 0 ? question.testCases[0] : null | |
| const firstInput = firstTestCase?.input | |
| const firstOutput = firstTestCase?.expectedOutput | |
| // Infer parameter types from test case input | |
| const javaParams = params.map((p, index) => { | |
| if (Array.isArray(firstInput)) { | |
| const inputValue = firstInput[index] | |
| if (Array.isArray(inputValue)) { | |
| return `int[] ${p}` | |
| } else if (typeof inputValue === 'number') { | |
| return `int ${p}` | |
| } else if (typeof inputValue === 'string') { | |
| return `String ${p}` | |
| } | |
| } | |
| // Fallback: infer from parameter name | |
| if (p.includes('nums') || p.includes('arr') || p.includes('list') || p.includes('prices')) { | |
| return `int[] ${p}` | |
| } | |
| if (p.includes('target') || p.includes('val') || p.includes('num')) { | |
| return `int ${p}` | |
| } | |
| return `int ${p}` // Default to int | |
| }).join(', ') | |
| // Infer return type from test case expected output | |
| let javaReturnType = 'int' | |
| if (firstOutput !== undefined && firstOutput !== null) { | |
| if (Array.isArray(firstOutput)) { | |
| javaReturnType = 'int[]' | |
| } else if (typeof firstOutput === 'number') { | |
| javaReturnType = 'int' | |
| } else if (typeof firstOutput === 'string') { | |
| javaReturnType = 'String' | |
| } else if (typeof firstOutput === 'boolean') { | |
| javaReturnType = 'boolean' | |
| } | |
| } else { | |
| // Fallback: infer from function name | |
| if (functionName.includes('Sum') || functionName.includes('List') || functionName.includes('Array') || functionName.includes('Intersection') || functionName.includes('Union')) { | |
| javaReturnType = 'int[]' | |
| } else if (functionName.includes('Profit') || functionName.includes('Max') || functionName.includes('Min') || functionName.includes('Length') || functionName.includes('Count')) { | |
| javaReturnType = 'int' | |
| } | |
| } | |
| // Add default return based on return type | |
| let defaultReturn = '' | |
| if (javaReturnType === 'int[]') { | |
| defaultReturn = 'return new int[0];' | |
| } else if (javaReturnType === 'int') { | |
| defaultReturn = 'return 0;' | |
| } else { | |
| defaultReturn = 'return null;' | |
| } | |
| return `class Solution { | |
| public ${javaReturnType} ${functionName}(${javaParams}) { | |
| // Write your code here | |
| ${defaultReturn} | |
| } | |
| }` | |
| default: | |
| return question.starterCode | |
| } | |
| } | |