-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathexample.html
More file actions
221 lines (204 loc) · 6.89 KB
/
example.html
File metadata and controls
221 lines (204 loc) · 6.89 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
<!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">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.13/theme/dracula.min.css">
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f9f9f9;
transition: background 0.3s, color 0.3s;
}
.dark-mode {
background-color: #222;
color: #fff;
}
header {
background-color: #4CAF50;
color: white;
padding: 20px;
text-align: center;
}
.container {
padding: 20px;
}
select, button, input[type="file"] {
padding: 8px;
margin: 5px;
font-size: 1em;
}
.editor-container {
background: #ffffff;
padding: 20px;
border-radius: 5px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
.dark-mode .editor-container {
background: #333;
}
.editor-header {
font-size: 1.2em;
color: #4CAF50;
}
#codeEditor {
border: 1px solid #ddd;
height: 300px;
}
.dark-mode #codeEditor {
border: 1px solid #555;
}
button {
background-color: #4CAF50;
color: white;
border: none;
cursor: pointer;
}
button:hover {
background-color: #45a049;
}
pre {
background: #f1f1f1;
padding: 10px;
border-radius: 5px;
}
.dark-mode pre {
background: #444;
}
</style>
</head>
<body>
<header>
<h1>DSA Problems with Code Editor</h1>
</header>
<div class="container">
<!-- Problem Selection -->
<label for="problemSelect">Choose a Problem:</label>
<select id="problemSelect" onchange="loadProblem()">
<option value="reverseLinkedList">Reverse a Linked List</option>
<option value="fibonacci">Find Fibonacci Number</option>
<option value="factorial">Find Factorial</option>
</select>
<!-- Language Selection -->
<label for="languageSelect">Choose Language:</label>
<select id="languageSelect" onchange="changeLanguage()">
<option value="javascript">JavaScript</option>
<option value="python">Python</option>
<option value="cpp">C++</option>
</select>
<button onclick="toggleDarkMode()">Toggle Dark Mode</button>
<div class="editor-container">
<div class="editor-header">Code Editor</div>
<textarea id="codeEditor"></textarea>
<button onclick="runCode()">Run Code</button>
<button onclick="downloadCode()">Download Code</button>
<input type="file" id="uploadFile" onchange="uploadCode()" />
<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>
const editor = CodeMirror.fromTextArea(document.getElementById("codeEditor"), {
lineNumbers: true,
mode: "javascript",
theme: "dracula",
});
const problems = {
reverseLinkedList: {
javascript: `// Function to reverse a singly linked list
function ListNode(val) {
this.val = val;
this.next = null;
}
function reverseLinkedList(head) {
let prev = null, current = head;
while (current) {
let next = current.next;
current.next = prev;
prev = current;
current = next;
}
return prev;
}`,
python: `# Function to reverse a singly linked list
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def reverse_linked_list(head):
prev, current = None, head
while current:
next = current.next
current.next = prev
prev = current
current = next
return prev`,
cpp: `// Function to reverse a singly linked list
struct ListNode {
int val;
ListNode* next;
ListNode(int x) : val(x), next(nullptr) {}
};
ListNode* reverseLinkedList(ListNode* head) {
ListNode* prev = nullptr, *current = head;
while (current) {
ListNode* next = current->next;
current->next = prev;
prev = current;
current = next;
}
return prev;
}`
}
};
function loadProblem() {
const problem = document.getElementById("problemSelect").value;
const language = document.getElementById("languageSelect").value;
editor.setValue(problems[problem][language]);
}
function changeLanguage() {
const language = document.getElementById("languageSelect").value;
editor.setOption("mode", language === "cpp" ? "text/x-c++src" : language);
loadProblem();
}
function runCode() {
try {
const userCode = editor.getValue();
const output = eval(userCode);
document.getElementById("output").textContent = `Output: ${output}`;
} catch (error) {
document.getElementById("output").textContent = `Error: ${error.message}`;
}
}
function downloadCode() {
const code = editor.getValue();
const blob = new Blob([code], { type: "text/plain" });
const link = document.createElement("a");
link.href = URL.createObjectURL(blob);
link.download = "code.txt";
link.click();
}
function uploadCode() {
const file = document.getElementById("uploadFile").files[0];
if (!file) return;
const reader = new FileReader();
reader.onload = function (e) {
editor.setValue(e.target.result);
};
reader.readAsText(file);
}
function toggleDarkMode() {
document.body.classList.toggle("dark-mode");
editor.setOption("theme", document.body.classList.contains("dark-mode") ? "dracula" : "default");
}
loadProblem();
</script>
</body>
</html>