-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdsa-problems.html
More file actions
193 lines (181 loc) · 6.4 KB
/
dsa-problems.html
File metadata and controls
193 lines (181 loc) · 6.4 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>DSA Problems with Code Editor</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.13/codemirror.min.css">
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f9f9f9;
}
header {
background-color: #4CAF50;
color: white;
padding: 20px;
text-align: center;
}
header h1 {
margin: 0;
font-size: 2.5em;
}
.container {
padding: 20px;
}
.topic {
margin-bottom: 30px;
}
.topic h2 {
margin-bottom: 10px;
color: #4CAF50;
border-bottom: 2px solid #4CAF50;
display: inline-block;
}
.problems {
list-style-type: none;
padding: 0;
}
.problems li {
margin: 5px 0;
padding: 10px;
background: white;
border-radius: 5px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
cursor: pointer;
}
.problems li a {
text-decoration: none;
color: #333;
font-weight: bold;
}
.problems li a:hover {
color: #4CAF50;
}
.editor-container {
margin-top: 20px;
background: #ffffff;
padding: 20px;
border-radius: 5px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
.editor-header {
margin-bottom: 10px;
font-size: 1.2em;
color: #4CAF50;
}
#codeEditor {
border: 1px solid #ddd;
height: 300px;
}
button {
margin-top: 10px;
padding: 10px 15px;
font-size: 1em;
color: white;
background-color: #4CAF50;
border: none;
border-radius: 5px;
cursor: pointer;
}
button:hover {
background-color: #45a049;
}
.select-lang, .time-complexity {
margin-top: 10px;
}
#output {
margin-top: 10px;
background: #f1f1f1;
padding: 10px;
border-radius: 5px;
}
</style>
</head>
<body>
<header>
<h1>DSA Problems with Code Editor</h1>
</header>
<div class="container">
<div class="topic">
<h2>Problem 1: Find the Maximum Subarray Sum</h2>
<p>Write a function to find the maximum sum of a contiguous subarray.</p>
</div>
<div class="editor-container">
<div class="editor-header">Code Editor</div>
<label for="langSelect">Select Language:</label>
<select id="langSelect" class="select-lang">
<option value="javascript">JavaScript</option>
<option value="python">Python</option>
<option value="java">Java</option>
</select>
<label for="timeComplexity">Time Complexity:</label>
<input type="text" id="timeComplexity" class="time-complexity" placeholder="Enter Time Complexity">
<textarea id="codeEditor">// Example: Find the maximum subarray sum
function maxSubarraySum(arr) {
let maxSum = arr[0];
let currentSum = arr[0];
for (let i = 1; i < arr.length; i++) {
currentSum = Math.max(arr[i], currentSum + arr[i]);
maxSum = Math.max(maxSum, currentSum);
}
return maxSum;
}
maxSubarraySum([1, 2, 3, -2, 5]);</textarea>
<button onclick="runCode()">Run Code</button>
<pre id="output"></pre>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.13/codemirror.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.13/mode/javascript/javascript.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.13/mode/python/python.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.13/mode/clike/clike.min.js"></script>
<script>
// Initialize CodeMirror editor
const editor = CodeMirror.fromTextArea(document.getElementById("codeEditor"), {
lineNumbers: true,
mode: "javascript", // Default language mode
theme: "default",
});
// Function to update editor mode based on selected language
document.getElementById("langSelect").addEventListener("change", function() {
const lang = this.value;
let mode = "javascript"; // Default mode
if (lang === "python") {
mode = "python";
} else if (lang === "java") {
mode = "clike";
}
editor.setOption("mode", mode);
});
// Function to run the code
function runCode() {
const userCode = editor.getValue();
const lang = document.getElementById("langSelect").value;
let output = '';
// Evaluate the code based on the selected language
try {
if (lang === "javascript") {
output = eval(userCode);
} else if (lang === "python") {
// For Python, we'll simulate with a simple example (use actual Python eval on server side)
output = "Python code execution is not supported in the browser.";
} else if (lang === "java") {
// For Java, we would need a backend solution to execute Java code
output = "Java code execution is not supported in the browser.";
}
document.getElementById("output").textContent = `Output: ${output}`;
} catch (error) {
document.getElementById("output").textContent = `Error: ${error.message}`;
}
// Display Time Complexity
const timeComplexity = document.getElementById("timeComplexity").value;
if (timeComplexity) {
document.getElementById("output").textContent += `\nTime Complexity: ${timeComplexity}`;
}
}
</script>
</body>
</html>