-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule-2-programming-assignment.html
390 lines (299 loc) · 8.87 KB
/
module-2-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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>CMPS 260: Module 2 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/
//new comment
//--------------------------//
// The stack data structure //
//--------------------------//
console.log("The stack data structure");
// 1. Implement the stack data structure described in the book. Instead of
// using 'let items = [];' use 'var items = []'. Note that this
// implementation does not use the prototype (see project).
function Stack() {
var items = [];
this.push = function(element) {
items.push(element);
}
this.pop = function() {
return items.pop();
}
this.size = function() {
return items.length;
}
this.isEmpty = function() {
return items.length === 0;
}
this.clear = function() {
items = [];
}
}
// 2. Write a simple test program that shows your stack works.
var stack = new Stack();
stack.push(5);
stack.push(6);
stack.push(10);
console.log("The stack size is " + stack.size());
console.log(stack.isEmpty());
var stackItem = stack.pop();
console.log(stackItem);
//----------------------------------//
// ECMAScript 6 and the Stack class //
//----------------------------------//
console.log("ECMAScript 6 and the Stack class");
// Skip.
//-------------------------------//
// Solving problems using stacks //
//-------------------------------//
console.log("Solving problems using stacks");
// 1. Use the prompt to ask for a decimal number. Then write a loop that divides
// the number by two in every iteration and prints the remainder (0 or 1).
// For example, if the number is 5 the remainder is 1 and the number in the
// next iteration should be 2 (5/2 rounded down).
var num = prompt("Please enter a decimal number such as 10.4.")
var rmdr;
var result;
while (num > 0) {
var rmdr = num%2;
console.log("Remainder " + rmdr);
num = Math.floor(num / 2);
console.log("The number is: " + num)
}
//-------------------------------//
// Tutors Code //
//-------------------------------//
//while (num > 0) {
// var remainder = number % 2;
// console.log("Remainder: " + remainder);
// result = result + remainder;
// stack.push(remainder);
// number = Math.floor(number / 2);
// console.log("Number: " + number);
//}
// 2. The algorithm in (1) can be used to convert a decimal number to a binary
// number but there is one issue. What is the problem?
// 3. Solve the problem in (2) using a stack.
function Stack() {
var items = [];
this.push = function(element) {
items.push(element);
}
this.pop = function() {
return items.pop();
}
this.size = function() {
return items.length;
}
this.isEmpty = function() {
return items.length === 0;
}
this.clear = function() {
items = [];
}
}
var rmdr = new Stack();
var num = 2; //prompt("Please enter a decimal number such as 10.4.")
var result = '';
while (num > 0) {
rmdr.push(num%2);
num = Math.floor(num / 2);
//console.log("The number is " + num);
}
while (rmdr.size() > 0) {
console.log("Size is " + rmdr.size());
result = result + rmdr.pop();
//console.log();
}
console.log("The result is: " + result);
//--------------------------//
// The queue data structure //
//--------------------------//
console.log("The queue data structure");
// 1. Describe the difference between a stack and a queue. Give one example
// where a stack is appropriate and one example where a queue is
// appropriate (that has not yet been discussed in class).
Console.log("A stack is LIFO and a queue FIFO.");
Console.log("FIFO would be suitable to inventory, such as food items with an expiration.
The First items placed on the shelf should be the first items removed from the shelf.");
Console.log("LIFO is suitable to ")
//------------------//
// Creating a queue //
//------------------//
console.log("Creating a queue");
// 1. Implement the queue data structure described in the book. Instead of
// using 'let items = [];' use 'var items = []'. Note that this
// implementation does not use the prototype (see project).
function Queue() {
// properties and methods go here
var count = 0;
var lowestCount = 0;
var items = {};
this.isEmpty = function() {
return count - lowestCount === 0;
}
this.enqueue = function(element) {
items[count] = element;
count++;
}
this.dequeue = function() {
if (this.isEmpty()) {
return undefined;
}
const result = items[lowestCount];
delete items[lowestCount];
lowestCount++;
return result;
}
this.peek = function() {
if (this.isEmpty()) {
return undefined;
}
return items[lowestCount];
}
this.size = function() {
return count - lowestCount;
}
this.clear = function() {
items = [];
count = 0;
lowestCount = 0;
}
} //end of Queue object
// 2. Write a simple test program that shows your queue works.
var name1 = '';
var name2 = '';
var queue = new Queue();
console.log(queue.isEmpty());
queue.enqueue('Brian');
queue.enqueue('Quigley');
name1 = queue.dequeue();
name2 = queue.dequeue();
console.log(name1 + " " + name2);
//--------------------//
// The priority queue //
//--------------------//
console.log("The priority queue");
// The following class is used below.
function QueueElement(element, priority) {
this.element = element;
this.priority = priority;
}
// 1. Finish the implementation below for the priority queue. Note that this
// implementation does not use the prototype.
function PriorityQueue() {
var count = 0;
var lowestCount = 0
var items = [];
this.enqueue = function(element, priority) {
var newQ = new QueueElement(element, priority);
var added = false;
for (var i = 0; i < items.length; i++) {
if (items[i].priority > newQ.priority) {
items.splice(i, 0, newQ);
count++;
added = true;
break;
}
}
if (!added) {
items.push(newQ);
count++;
}
}
this.print = function() {
for (var i = 0; i < items.length; i++) {
console.log(`${items[i].element} - ${items[i].priority}`);
}
};
this.isEmpty = function() {
return count - lowestCount === 0;
}
this.dequeue = function() {
if (this.isEmpty()) {
return undefined;
}
const result = items[lowestCount];
delete items[lowestCount];
lowestCount++;
return result;
}
} //end of PriorityQueue
// 2. Write a simple test that makes sure the priority queue works as expected.
var newqueue = new PriorityQueue();
newqueue.enqueue('Pickles', 4);
newqueue.enqueue('Tomatoes', 2);
newqueue.enqueue('Beets', 1);
newqueue.isEmpty();
newqueue.print();
console.log(newqueue.dequeue());
console.log(newqueue.dequeue());
//---------------------------------//
// The circular queue - Hot Potato //
//---------------------------------//
console.log("The circular queue - Hot Potato");
// 1. Finish the implementation for the hot potato game. nameList contains the
// names of the participants, and num is how many times the potato is passed
// before a participant is ejected from the game (the one that holds the
// potato). Note that this is effectively a simulation of the game.
function pQueue() {
var count = 0;
var lowestCount = 0;
var items = [];
this.enqueue = function(element) {
items[count] = element;
count++;
}
this.dequeue = function() {
if (this.isEmpty()) {
return undefined;
}
const result = items[lowestCount];
delete items[lowestCount];
lowestCount++;
return result;
}
this.isEmpty = function() {
return count - lowestCount === 0;
}
this.size = function() {
return count - lowestCount;
}
} //end of pQueue object
function hotPotato(nameList, num) {
var newQueue = new pQueue();
var eliminatedList = [];
for (var i = 0; i < nameList.length; i++) {
newQueue.enqueue(nameList[i]);
console.log(nameList[i]);
}
while (newQueue.size() > 1) {
for (var i = 0; i < num; i++) {
newQueue.enqueue(newQueue.dequeue());
}
eliminatedList.push(newQueue.dequeue());
}
return {
eliminated: eliminatedList,
winner: newQueue.dequeue()
};
}
var names = ['Brian', 'Jaime', 'Sophia', 'Logan', 'Cooper'];
var results = new hotPotato(names, 7);
results.eliminated.forEach(name => {console.log('${name} was eliminated.')});
console.log('The winner is: ${results.winner}');
</script>
</head>
<body>
See console!
</body>
</html>