-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-problem.js
More file actions
258 lines (198 loc) · 5.61 KB
/
create-problem.js
File metadata and controls
258 lines (198 loc) · 5.61 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
// Available topics
const topics = [
'Arrays',
'Strings',
'LinkedLists',
'Trees',
'Graphs',
'Stacks',
'Queues',
'Heaps',
'Hashing',
'Recursion',
'Backtracking',
'DynamicProgramming',
'Greedy',
'BinarySearch',
'Sorting',
'BitManipulation',
'Math',
'TwoPointers',
'SlidingWindow',
'Other'
];
function question(query) {
return new Promise(resolve => rl.question(query, resolve));
}
async function createProblemFolder() {
console.log('\n🚀 DSA Problem Generator\n');
// Get problem details
const problemNumber = await question('Problem Number (e.g., 001): ');
const problemName = await question('Problem Name (e.g., Two Sum): ');
const difficulty = await question('Difficulty (Easy/Medium/Hard): ');
console.log('\nAvailable Topics:');
topics.forEach((topic, index) => {
console.log(`${index + 1}. ${topic}`);
});
const topicIndex = await question('\nSelect Topic (enter number): ');
const topic = topics[parseInt(topicIndex) - 1] || 'Other';
const problemStatement = await question('\nProblem Statement (brief): ');
// Create folder structure
const folderName = `${problemNumber}-${problemName.toLowerCase().replace(/\s+/g, '-')}`;
const topicPath = path.join(process.cwd(), topic);
const problemPath = path.join(topicPath, folderName);
// Create directories
if (!fs.existsSync(topicPath)) {
fs.mkdirSync(topicPath, { recursive: true });
createTopicReadme(topicPath, topic);
}
if (fs.existsSync(problemPath)) {
console.log('\n❌ Error: This problem folder already exists!');
rl.close();
return;
}
fs.mkdirSync(problemPath, { recursive: true });
// Create files
createSolutionFile(problemPath, problemName);
createReadmeFile(problemPath, problemNumber, problemName, difficulty, topic, problemStatement);
createInputFile(problemPath);
createOutputFile(problemPath);
console.log('\n✅ Problem folder created successfully!');
console.log(`📁 Location: ${problemPath}`);
console.log('\nFiles created:');
console.log(' - solution.js');
console.log(' - README.md');
console.log(' - input.txt');
console.log(' - output.txt');
rl.close();
}
function createSolutionFile(problemPath, problemName) {
const content = `const fs = require('fs');
// Read input from file
const input = fs.readFileSync(process.platform === 'win32' ? 'input.txt' : '/dev/stdin', 'utf8').trim();
/**
* Problem: ${problemName}
*
* Approach:
* 1.
* 2.
* 3.
*
* Time Complexity: O(?)
* Space Complexity: O(?)
*/
function solve(input) {
const lines = input.split('\\n');
// Your solution here
return result;
}
// Main execution
const result = solve(input);
console.log(result);
`;
fs.writeFileSync(path.join(problemPath, 'solution.js'), content);
}
function createReadmeFile(problemPath, problemNumber, problemName, difficulty, topic, problemStatement) {
const today = new Date().toLocaleDateString('en-GB');
const content = `# ${problemNumber} - ${problemName}
## 📝 Problem Statement
${problemStatement}
## 🎯 Difficulty
**${difficulty}**
## 🏷️ Topic
${topic}
## 💡 Examples
### Example 1:
\`\`\`
Input:
[Add example input]
Output:
[Add expected output]
Explanation:
[Explain the example]
\`\`\`
### Example 2:
\`\`\`
Input:
[Add example input]
Output:
[Add expected output]
\`\`\`
## 🔍 Approach
### Initial Thoughts
- [What was your first approach?]
- [Any observations about the problem?]
### Solution Strategy
1. **Step 1:** [Describe]
2. **Step 2:** [Describe]
3. **Step 3:** [Describe]
### Algorithm
\`\`\`
[Write pseudocode or algorithm steps]
\`\`\`
## 📊 Complexity Analysis
- **Time Complexity:** O(?)
- [Explain why]
- **Space Complexity:** O(?)
- [Explain why]
## 🔑 Key Learnings
- [ ] [What concept did you learn or reinforce?]
- [ ] [Any common pitfalls or edge cases?]
- [ ] [Optimization techniques used?]
## 🐛 Mistakes & Debugging
- [Any mistakes you made while solving?]
- [Edge cases you initially missed?]
## 🔗 Related Problems
- [Link to similar problems]
- [Link to prerequisite problems]
## 📅 Date Solved
${today}
## 📌 Notes
[Any additional notes or alternative approaches]
---
**Status:** 🔴 Not Started | 🟡 In Progress | 🟢 Solved | ✅ Reviewed
`;
fs.writeFileSync(path.join(problemPath, 'README.md'), content);
}
function createInputFile(problemPath) {
const content = `# Add your test input here
# Example:
# 5
# 1 2 3 4 5
`;
fs.writeFileSync(path.join(problemPath, 'input.txt'), content);
}
function createOutputFile(problemPath) {
const content = `# Output will be generated here when you run the solution
`;
fs.writeFileSync(path.join(problemPath, 'output.txt'), content);
}
function createTopicReadme(topicPath, topic) {
const content = `# ${topic}
## Problems Solved
| # | Problem | Difficulty | Date | Status |
|---|---------|-----------|------|--------|
| | | | | |
## Key Concepts
- [ ] [Concept 1]
- [ ] [Concept 2]
## Common Patterns
1. **Pattern Name:** Description
## Resources
- [Add helpful links]
`;
fs.writeFileSync(path.join(topicPath, 'README.md'), content);
}
// Run the script
createProblemFolder().catch(err => {
console.error('Error:', err);
rl.close();
});