-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule-4-programming-assignment.html
470 lines (384 loc) · 10.6 KB
/
module-4-programming-assignment.html
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>CMPS 260: Module 4 Programming Assignment</title>
<style>* { font-family: monospace; }</style>
<script>
// NOTE: You must implement the data structures using the no prototype approach.
// This is what the book uses, so you can copy it.
// See also: https://it.pointpark.edu/tutorials/no-prototype-vs-prototype/
// NOTE: Please review the following links regularly:
// https://it.pointpark.edu/tutorials/arrays-vs-objects/
// https://it.pointpark.edu/tutorials/no-prototype-vs-prototype/
// https://it.pointpark.edu/tutorials/implementation-vs-interface/
//----------------//
// Creating a set //
//----------------//
console.log("Creating a set");
// 1. Finish the implemention of the set data structure below. Note that this
// implementation does not use the prototype (see homework).
function Set() {
var items = {}; // note that this is an object instead of an array
this.add = function(value) {
if (!this.has(value)) {
items[value] = value;
return true;
}
return false;
};
this.delete = function(value) {
if (this.has(value)) {
delete items[value];
return true;
}
return false;
};
this.has = function(value) {
return items.hasOwnProperty(value);
};
this.clear = function() {
items = {};
};
this.size = function() {
return Object.keys(items).length;
};
this.values = function() {
var values = [];
for (var i = 0; keys=Object.keys(items); i<keys.length; i++) {
values.push(items[keys[i]]);
}
return values;
};
}
//----------------//
// Set operations //
//----------------//
console.log("Set operations");
// 1. Implement the union set function in Set above.
this.union = function(otherSet) {
var unionSet = new Set();
var values = this.values();
for (var i=0; i<values.length; i++) {
unionSet.add(values[i]);
}
values = otherSet.values();
for (var i=0; i<values.length; i++) {
unionSet.add(values[i]);
}
return unionSet;
};
// 2. Implement the intersection set function in Set above.
this.intersection = function otherSet() {
var intersectionSet = new Set();
var values = this.values();
for (var i=0; i<values.length; i++) {
if (otherSet.has(values[i])) {
intersectionSet.add(values[i]);
}
}
return intersectionSet;
};
// 3. Implement the set difference function in Set above.
this.difference = function(otherSet) {
var differenceSet = new Set();
var values = this.values();
for (var i=0; i<values.length; i++) {
if (!otherSet.has(values[i])) {
differenceSet.add(values[i]);
}
}
return differenceSet;
};
// 4. Implement the subset function in Set above.
this.subset = function(otherSet) {
if (this.size() > otherSet.size()) {
return false;
} else {
var values = this.values;
for (var i=0; i<values.length; i++) {
if (!otherSet.has(values[i])) {
return false;
}
}
return true;
}
};
//--------------//
// Dictionaries //
//--------------//
console.log("Dictionaries");
// 1. Finish the implemention of the dictionary data structure below. Note that
// this implementation does not use the prototype (see homework).
// NOTE: This is very similar to the set data structure.
function Dictionary() {
// store all elements in the items object
var items = {};
this.set = function(key, value) {
items[key] = value;
};
this.delete = function(key) {
if (this.has(key)) {
delete items[key];
return true;
}
return false;
};
this.has = function(key) {
return key in items;
};
this.get = function(key) {
return this.has(key) ? items[key] : undefined;
};
this.clear = function() {
items = {};
};
this.size = function() {
return Object.keys(items).length;
};
this.keys = function() {
return Object.keys(items);
};
this.values = function() {
var values = [];
for (var k in items) {
if (this.has(k)) {
values.push(items[k]);
}
}
return values;
};
this.getItems = function() {
return items;
};
}
// 2. Write some tests that show that your code works.
var newDictionary = new Dictionary();
newDictionary.set('Cooper', 'dog');
newDictionary.set('Jude', 'dog');
newDictionary.set('Johnny', 'cat');
newDictionary.set('Cupcake', 'cat');
console.log(newDictionary.size());
console.log(newDictionary.values());
console.log(newDictionary.keys());
console.log(newDictionary.delete('Cupcake'));
console.log(newDictionary.size());
console.log(newDictionary.values());
//----------------//
// The hash table //
//----------------//
console.log("The hash table");
// NOTE: In the Dictionary we used items to store our key-value pairs. This is
// cheating a little bit because we do not exactly know how the browser
// implements storing these mappings (e.g., the approach from the slides
// or something more sophisticated like a hash table). We will now
// explicitly use hash tables.
//-----------------------------------------------------------------------------
// we need this below, scroll down for questions
//-----------------------------------------------------------------------------
function ValuePair(key, value) {
this.key = key;
this.value = value;
this.toString = function() {
return "[" + this.key + " - " + this.value + "]";
};
};
function Node(element) {
this.element = element;
this.next = null;
}
function LinkedList() {
this.length = 0;
this.head = null;
}
LinkedList.prototype.append = function(element) {
var node = new Node(element);
if (this.head === null) {
this.head = node;
}
else {
var tmp = this.head;
while (tmp.next !== null) {
tmp = tmp.next;
}
tmp.next = node;
}
this.length++;
};
LinkedList.prototype.insert = function(position, element) {
var node = new Node(element);
if (position === 0) {
var oldHead = this.head;
this.head = node;
this.head.next = oldHead;
}
else {
var tmp = this.head;
for (var i = 0; i < position-1; i++) {
tmp = tmp.next;
}
node.next = tmp.next;
tmp.next = node;
}
this.length++;
};
LinkedList.prototype.removeAt = function(position) {
var element = null;
if (position === 0) {
element = this.head.element;
this.head = this.head.next;
}
else {
var tmp = this.head;
for (var i = 0; i < position-1; i++) {
tmp = tmp.next;
}
element = tmp.next.element;
var prev = tmp;
var middle = tmp.next;
var next = middle.next;
prev.next = next;
}
this.length--;
return element;
};
LinkedList.prototype.remove = function(element) {
var position = this.indexOf(element);
return this.removeAt(position);
};
LinkedList.prototype.indexOf = function(element) {
var tmp = this.head;
for (var i = 0; i < this.length; i++) {
if (tmp.element === element) {
return i;
}
tmp = tmp.next;
}
return -1;
};
LinkedList.prototype.isEmpty = function() {
return this.length === 0;
};
LinkedList.prototype.size = function() {
var length = 0;
var tmp = this.head;
while (tmp !== null) {
length++;
tmp = tmp.next;
}
return length;
};
LinkedList.prototype.getHead = function() {
return this.head;
}
LinkedList.prototype.toString = function() {
var current = this.head;
var string = "List: ";
while (current) {
string += current.element + (current.next ? " -> " : "");
current = current.next;
}
return string;
};
LinkedList.prototype.print = function() {
console.log(this.toString());
};
//-----------------------------------------------------------------------------
// 1. Finish the implemention of the hash table data structure below. Note that
// this implementation does not use the prototype (see project). You also
// do not yet have to resolve collisions (see question 3).
function HashTable() {
// store all elements in the table array
var table = [];
var ValuePair = function(key, value) {
this.key = key;
this.value = value;
this.toString = function() {
return '[' + this.key + ' - ' + this.value + ']';
}
};
// define hash function
function loseloseHashCode(key) {
var hash = 0;
for (var i = 0; i < key.length; i++) {
hash += key.charCodeAt(i);
}
return hash % 37;
}
this.put = function(key, value) {
var position = loseloseHashCode(key);
if (table[position] == undefined) {
table[position] = new LinedList();
}
//console.log(position + ' - ' + key); first implementation
table[position].append(new ValuePair(key, value));;
};
this.remove = function(key) {
//table[loseloseHashCode (key)] = undefined; first implementation
var position = loseloseHashCode(key);
if (table[position] !== undefined) {
var current = table[position].getHead();
while (current.next) {
if (current.element.value === key) {
table[position].remove(current.element);
if (table[position].isEmpty()) {
table[position] = undefined;
}
return true;
}
current = current.next;
}
if (current.element.key === key) {
table[position].remove(current.elemet);
if (table[position].isEmpty()) {
table[position] = undefined;
}
return true;
}
}
return false;
};
this.get = function(key) {
var position = loseloseHashCode(key);
if (table[position] !== undefined) {
var current = table[position].getHead();
while (current.next) {
if (current.element.value === key) {
return current.element.value;
}
current = current.next;
}
if (current.element.key === key) {
return current.element.value;
}
}
return undefined;
//return table[loseloseHasCode(key)]; first implementation
};
this.print = function() {
// print the content of the hash table
for (var i = 0; i < table.length; i++) {
if (table[i] !== undefined) {
console.log(i + ": " + table[i]);
}
}
};
}
// 2. Write some tests that show that your code works.
var hash = new HasTable();
hash.put('Brian', 'November');
hash.put('Jaime', 'June');
hash.put('Logan', 'February');
// 3. Modify your implementation to use separate chaining (for linked list, see
// above) for resolving collisions. We also need ValuePair, which is provided
// above.
// See implementation above
// 4. We will skip linear probing because the implementation in the book has
// quite a serious bug in it. Can you find the bug (don't spend much time)?
// LESSON: Be careful with trusting other people's code.
</script>
</head>
<body>
See console!
</body>
</html>