Skip to content

Commit 0154d40

Browse files
committed
Day 17
1 parent a9f96b9 commit 0154d40

File tree

2 files changed

+300
-14
lines changed

2 files changed

+300
-14
lines changed

Day 15 closuser.js

+14-14
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function createCounter() {
3333
const counter = createCounter();
3434
counter.increment();
3535
counter.increment();
36-
console.log(counter.getValue()); // Logs: 2
36+
console.log(counter.getValue()); // 2
3737

3838
//Activity 2: Practical Closures
3939

@@ -49,8 +49,8 @@ function createIdGenerator() {
4949
}
5050

5151
const generateId = createIdGenerator();
52-
console.log(generateId()); // Logs: 1
53-
console.log(generateId()); // Logs: 2
52+
console.log(generateId()); // 1
53+
console.log(generateId()); // 2
5454

5555
//Task 4: Create a closure that captures a user’s name and returns a function that greets the user by name.
5656

@@ -81,11 +81,11 @@ function createFunctions() {
8181
}
8282

8383
const functions = createFunctions();
84-
functions[0](); // Logs: 0
85-
functions[1](); // Logs: 1
86-
functions[2](); // Logs: 2
87-
functions[3](); // Logs: 3
88-
functions[4](); // Logs: 4
84+
functions[0](); // 0
85+
functions[1](); // 1
86+
functions[2](); // 2
87+
functions[3](); // 3
88+
functions[4](); // 4
8989

9090

9191
// Activity 4: Module Pattern
@@ -113,9 +113,9 @@ const itemManager = (function() {
113113

114114
itemManager.addItem("item1");
115115
itemManager.addItem("item2");
116-
console.log(itemManager.listItems()); // Logs: ["item1", "item2"]
116+
console.log(itemManager.listItems()); // ["item1", "item2"]
117117
itemManager.removeItem("item1");
118-
console.log(itemManager.listItems()); // Logs: ["item2"]
118+
console.log(itemManager.listItems()); // ["item2"]
119119

120120
// Activity 5: Memoization
121121

@@ -138,8 +138,8 @@ function add(a, b) {
138138
}
139139

140140
const memoizedAdd = memoize(add);
141-
console.log(memoizedAdd(1, 2)); // Logs: 3
142-
console.log(memoizedAdd(1, 2)); // Logs: 3 (from cache)
141+
console.log(memoizedAdd(1, 2)); // 3
142+
console.log(memoizedAdd(1, 2)); // 3 (from cache)
143143

144144
// Task 8:Create a memoized version of a function that calculates the factorial of a number.
145145

@@ -163,5 +163,5 @@ function factorial(n) {
163163
}
164164

165165
const memoizedFactorial = memoize(factorial);
166-
console.log(memoizedFactorial(5)); // Logs: 120
167-
console.log(memoizedFactorial(5)); // Logs: 120 (from cache)
166+
console.log(memoizedFactorial(5)); // 120
167+
console.log(memoizedFactorial(5)); // 120 (from cache)

Day 17 dsa.js

+286
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,286 @@
1+
//Activity 1: Linked List
2+
3+
// Task 1: Implement a `Node` class
4+
5+
class Node {
6+
constructor(value = null) {
7+
this.value = value;
8+
this.next = null;
9+
}
10+
}
11+
12+
13+
// Task 2: Implement a `LinkedList` class
14+
15+
16+
class LinkedList {
17+
constructor() {
18+
this.head = null;
19+
}
20+
21+
add(value) {
22+
const newNode = new Node(value);
23+
if (!this.head) {
24+
this.head = newNode;
25+
} else {
26+
let current = this.head;
27+
while (current.next) {
28+
current = current.next;
29+
}
30+
current.next = newNode;
31+
}
32+
}
33+
34+
remove(value) {
35+
if (!this.head) return;
36+
37+
if (this.head.value === value) {
38+
this.head = this.head.next;
39+
return;
40+
}
41+
42+
let current = this.head;
43+
while (current.next && current.next.value !== value) {
44+
current = current.next;
45+
}
46+
47+
if (current.next) {
48+
current.next = current.next.next;
49+
}
50+
}
51+
52+
display() {
53+
let current = this.head;
54+
const nodes = [];
55+
while (current) {
56+
nodes.push(current.value);
57+
current = current.next;
58+
}
59+
console.log(nodes.join(' -> '));
60+
}
61+
}
62+
63+
64+
// Activity 2: Stack
65+
66+
// Task 3: Implement a `Stack` class
67+
68+
class Stack {
69+
constructor() {
70+
this.items = [];
71+
}
72+
73+
push(element) {
74+
this.items.push(element);
75+
}
76+
77+
pop() {
78+
if (this.items.length === 0) return "Underflow";
79+
return this.items.pop();
80+
}
81+
82+
peek() {
83+
if (this.items.length === 0) return "No elements in Stack";
84+
return this.items[this.items.length - 1];
85+
}
86+
87+
isEmpty() {
88+
return this.items.length === 0;
89+
}
90+
}
91+
92+
93+
// Task 4: Use the `Stack` class to reverse a string
94+
95+
function reverseString(str) {
96+
const stack = new Stack();
97+
for (let i = 0; i < str.length; i++) {
98+
stack.push(str[i]);
99+
}
100+
101+
let reversedStr = '';
102+
while (!stack.isEmpty()) {
103+
reversedStr += stack.pop();
104+
}
105+
106+
return reversedStr;
107+
}
108+
109+
console.log(reverseString("hello")); // Output: "olleh"
110+
111+
112+
// Activity 3: Queue
113+
114+
// Task 5: Implement a `Queue` class
115+
116+
117+
class Queue {
118+
constructor() {
119+
this.items = [];
120+
}
121+
122+
enqueue(element) {
123+
this.items.push(element);
124+
}
125+
126+
dequeue() {
127+
if (this.isEmpty()) return "Underflow";
128+
return this.items.shift();
129+
}
130+
131+
front() {
132+
if (this.isEmpty()) return "No elements in Queue";
133+
return this.items[0];
134+
}
135+
136+
isEmpty() {
137+
return this.items.length === 0;
138+
}
139+
}
140+
141+
//Task 6: Use the `Queue` class to simulate a simple printer queue
142+
143+
144+
function printerQueue(jobs) {
145+
const queue = new Queue();
146+
jobs.forEach(job => queue.enqueue(job));
147+
148+
while (!queue.isEmpty()) {
149+
console.log(`Printing job: ${queue.dequeue()}`);
150+
}
151+
}
152+
153+
printerQueue(['job1', 'job2', 'job3']);
154+
// Output:
155+
// Printing job: job1
156+
// Printing job: job2
157+
// Printing job: job3
158+
159+
160+
// Activity 4: Binary Tree
161+
162+
//Task 7: Implement a `TreeNode` class
163+
164+
165+
class TreeNode {
166+
constructor(value = null) {
167+
this.value = value;
168+
this.left = null;
169+
this.right = null;
170+
}
171+
}
172+
173+
174+
//Task 8: Implement a `BinaryTree` class
175+
176+
class BinaryTree {
177+
constructor() {
178+
this.root = null;
179+
}
180+
181+
insert(value) {
182+
const newNode = new TreeNode(value);
183+
if (!this.root) {
184+
this.root = newNode;
185+
return;
186+
}
187+
188+
let current = this.root;
189+
while (true) {
190+
if (value < current.value) {
191+
if (!current.left) {
192+
current.left = newNode;
193+
return;
194+
}
195+
current = current.left;
196+
} else {
197+
if (!current.right) {
198+
current.right = newNode;
199+
return;
200+
}
201+
current = current.right;
202+
}
203+
}
204+
}
205+
206+
inOrderTraversal(node = this.root, result = []) {
207+
if (node) {
208+
this.inOrderTraversal(node.left, result);
209+
result.push(node.value);
210+
this.inOrderTraversal(node.right, result);
211+
}
212+
return result;
213+
}
214+
}
215+
216+
217+
// Activity 5: Graph (Optional)
218+
219+
//Task 9: Implement a `Graph` class
220+
221+
222+
class Graph {
223+
constructor() {
224+
this.adjList = new Map();
225+
}
226+
227+
addVertex(vertex) {
228+
if (!this.adjList.has(vertex)) {
229+
this.adjList.set(vertex, []);
230+
}
231+
}
232+
233+
addEdge(vertex1, vertex2) {
234+
if (this.adjList.has(vertex1)) {
235+
this.adjList.get(vertex1).push(vertex2);
236+
}
237+
if (this.adjList.has(vertex2)) {
238+
this.adjList.get(vertex2).push(vertex1);
239+
}
240+
}
241+
242+
bfs(startingNode) {
243+
let visited = new Set();
244+
let queue = [startingNode];
245+
246+
while (queue.length > 0) {
247+
let currentNode = queue.shift();
248+
249+
if (!visited.has(currentNode)) {
250+
console.log(currentNode);
251+
visited.add(currentNode);
252+
253+
let neighbors = this.adjList.get(currentNode);
254+
for (let neighbor of neighbors) {
255+
if (!visited.has(neighbor)) {
256+
queue.push(neighbor);
257+
}
258+
}
259+
}
260+
}
261+
}
262+
}
263+
264+
265+
// Task 10: Use the `Graph` class to represent a simple network and perform BFS
266+
267+
268+
const graph = new Graph();
269+
graph.addVertex('A');
270+
graph.addVertex('B');
271+
graph.addVertex('C');
272+
graph.addVertex('D');
273+
graph.addVertex('E');
274+
275+
graph.addEdge('A', 'B');
276+
graph.addEdge('A', 'C');
277+
graph.addEdge('B', 'D');
278+
graph.addEdge('C', 'E');
279+
280+
graph.bfs('A');
281+
// Output:
282+
// A
283+
// B
284+
// C
285+
// D
286+
// E

0 commit comments

Comments
 (0)