File size: 3,829 Bytes
bb11aeb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1e00060
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bb11aeb
 
 
 
 
 
 
 
 
1e00060
ea151a1
1e00060
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ea151a1
 
1a96512
 
 
 
 
 
 
 
 
 
bb11aeb
ea151a1
bb11aeb
1a96512
bb11aeb
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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
  }
}