-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithm.js
More file actions
312 lines (290 loc) · 9.63 KB
/
algorithm.js
File metadata and controls
312 lines (290 loc) · 9.63 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
//Creating palindrome checker
{
let palindrome = function(value) {
let emptyStack = [];
let word = value;
let reverseWord = "";
let wLen = word.length;
for(let a = 0; a < wLen; a++) {
emptyStack.push(word[a]);
}
for(let b = 0; b < wLen; b++) {
reverseWord += emptyStack.pop();
}
if(word === reverseWord) {
console.log(`${word} is a palindrome.`);
}
else {
console.log(`${word} is not a palindrome.`)
}
}
//Creaking the palindrome
palindrome("bob")
}
{
//Creating a 'stack' data structure using "class data type"
//We know for creating a class, we need 4 things such as push, pop, peek, length
class STACK {
constructor() {
this.store = [];
this.count = 0;
}
//Checking, if the stack is empty or not!
isEmpty() {
return this.count === 0;
}
//this method will push element to the end of the stack
push(element) {
this.store[this.count] = element;
this.count++;
}
//this method will removes element from the top of the stack
pop() {
if(this.isEmpty() === false) {
this.count = this.count-1;
return this.store.pop();
}
}
//this method will peek the top element from the stack
peek() {
return this.store[this.count-1];
}
//this method will returns the size of the stack
size() {
return this.count;
}
}
//Creating variable for checking all the stack method
let stAck = new STACK();
stAck.push("Bmw");
stAck.push("Audi");
console.log(stAck);
console.log(stAck.pop());
console.log(stAck.isEmpty());
}
{
//Creating "stack data structure" using "function data type"
let STaCK = function() {
this.storage = {};
this.count = 0;
//this method will adds an element to the end of the stack
this.push = function(elememt) {
this.storage[this.count] = elememt;
this.count++;
}
//this method will removes element from the top of the stack
this.pop = function() {
if(this.count === 0) {
return undefined;
}
else {
this.count--;
let result = this.storage[this.count];
delete this.storage[this.count];
return result;
}
}
//this method will peek the top element from the stack
this.peek = function() {
return this.storage[this.count-1];
}
//this method will returns the size of the stack
this.size = function() {
return this.count;
}
}
//Creating variable to check all the stack methods
let sTACk = new STaCK();
console.log(sTACk.pop());
}
{
//Creating a "set data structure" using "function data type"
let SET = function() {
//this empty array will hold the set values
let collection = [];
//This method will check for the presence of element,
//if found any matches then returns "true" otherwise returns "false"
this.has = function(elememt) {
return (collection.indexOf(elememt) !== -1);
}
//this method will returns the full set
this.values = function() {
return collection;
}
//this method will adds element to the set
this.add = function(elememt) {
if(!this.has(elememt)) {
collection.push(elememt);
return true;
}
else {
return false;
}
}
//this method will removes element from the set
this.remove = function() {
if(this.has(elememt)) {
let index = collection.indexOf(elememt);
collection.splice(index, 1);
return true;
}
else {
return false;
}
}
//this method will returns the size of the set
this.size = function() {
return collection.length;
}
//this method will returns the union of two set as a new set
this.union = function(otherSet) {
let unionSet = new SET();
let firstSet = this.values();
let secondSet = otherSet.values();
firstSet.forEach(function(e) {
unionSet.add(e);
})
secondSet.forEach(function(e) {
unionSet.add(e);
})
return unionSet;
}
//this method will returns the intersection of two set as a new set
this.intersection = function(otherSet) {
let intersectionSet = new SET();
let firstSet = this.values();
firstSet.forEach(function(e) {
if(otherSet.has(e)) {
intersectionSet.add(e);
}
})
return intersectionSet;
}
//this method will returns the difference of two set as a new set
this.difference = function(otherSet) {
let differenceSet = new SET();
let firstSet = this.values();
firstSet.forEach(function(e) {
if(!otherSet.has(e)) {
differenceSet.add(e);
}
})
return differenceSet;
}
//this method will check if one set is the subset of otherset then returns "true" otherwise "false"
this.subset = function(otherSet) {
let firstSet = this.values();
return firstSet.every(function(value) {
return otherSet.has(value);
})
}
}
//Creating variables to check the set methods
let setA = new SET();
let setB = new SET();
setA.add("a");
setA.add("b");
setB.add("a");
setB.add("b");
setB.add("c");
console.log(setA.subset(setB));
console.log(setB.difference(setA).values());
console.log(setA.union(setB).values());
}
{
//Creating "queue data structure"
//We know that, "queue" similar to "stack" data structure
//but the only difference is "stack follow 'LIFO'" and "queue follows 'FIFO'"
let QUEUE = function() {
//this empty array will hold the queue values
let collection = [];
//this method will check if the queue is empty or not
this.isEmpty = function() {
return collection.length === 0;
}
//this method will returns the full queue
this.print = function() {
console.log(collection);
}
//this method will adds an element at the top of the queue
this.enqueue = function(elememt) {
return collection.push(elememt);
}
//this method will removes element from the top of front of the queue
this.dequeue = function() {
return collection.shift();
}
//this method will returns the size of the queue
this.size = function() {
return collection.length;
}
//this method will peek the top element from the queue
this.front = function() {
return collection[0];
}
}
}
{
//Creating "priority queue" data structure
let PRIORITYqueue = function() {
let collection = [];
//this will returns the full queue
this.print = function() {
console.log(collection);
}
//Checking if the collection array is empty or not!
this.isEmpty = function() {
return collection.length === 0;
}
//here is the main part of "priority queue"
//We can add values to the collection array according to its priority index
this.enqueue = function(elememt) {
//Checking if empty then push element
if(this.isEmpty()) {
collection.push(elememt);
}
else {
//Set a flag to check if element is added
let added = false;
//this is to determine is the for loop found a match
for(let i = 0; i < collection.length; i++) {
//if the element at the index "i" in the collection has
//higher priority you need to splice the array at that
//index and insert the element there
if(elememt[1] < collection[i][1]) {
//splice the array at that index and insert the element there
collection.splice(i, 0, elememt);
//Satisfy the added condition
added = true;
//breaking the for loop here
break;
}
}
//if element not added in for loop
if(!added) {
collection.push(elememt);
}
}
}
//this method will removes element from the front of the queue
this.dequeue = function() {
let value = collection.shift();
return value[0];
}
//this method will returns the front element from the queue
this.front = function() {
return collection[0];
}
//this method will returns the size of the queue
this.size = function() {
return collection.length;
}
}
}
{
//leetcode problem: brute force algorithm
//Expected output: [0, 1]
var twoSum = function(nums, target) {
};
twoSum([2, 7, 11, 15], 9);
}