Disambiguate "Priority Queue" instructions (#38805)

* Disambiguate "Priority Queue" instructions

* Add front() to solution

* Add tests for front() method

* Fix test typo

Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com>

Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com>
This commit is contained in:
Ty Mick 2020-07-15 12:49:56 -04:00 committed by GitHub
parent 31f9211016
commit cd90da13f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -13,8 +13,8 @@ For instance, lets imagine we have a priority queue with three items:
<code>[['kitten', 2], ['dog', 2], ['rabbit', 2]]</code>
Here the second value (an integer) represents item priority. If we enqueue <code>['human', 1]</code> with a priority of <code>1</code> (assuming lower priorities are given precedence) it would then be the first item to be dequeued. The collection would look like this:
<code>[['human', 1], ['kitten', 2], ['dog', 2], ['rabbit', 2]]</code>.
Weve started writing a <code>PriorityQueue</code> in the code editor. You will need to add an <code>enqueue</code> method for adding items with a priority, a <code>dequeue</code> method for removing items, a <code>size</code> method to return the number of items in the queue, a <code>front</code> method to return the element at the front of the queue, and finally an <code>isEmpty</code> method that will return <code>true</code> if the queue is empty or <code>false</code> if it is not.
The <code>enqueue</code> should accept items with the format shown above (<code>['human', 1]</code>) where <code>1</code> represents the priority. The <code>dequeue</code> should return only the current item, not its priority.
Weve started writing a <code>PriorityQueue</code> in the code editor. You will need to add an <code>enqueue</code> method for adding items with a priority, a <code>dequeue</code> method for removing and returning items, a <code>size</code> method to return the number of items in the queue, a <code>front</code> method to return the element at the front of the queue, and finally an <code>isEmpty</code> method that will return <code>true</code> if the queue is empty or <code>false</code> if it is not.
The <code>enqueue</code> should accept items with the format shown above (<code>['human', 1]</code>) where <code>1</code> represents the priority. <code>dequeue</code> and <code>front</code> should return only the item's name, not its priority.
</section>
## Instructions
@ -33,10 +33,14 @@ tests:
testString: assert((function(){var test = new PriorityQueue(); return (typeof test.dequeue === 'function')}()));
- text: Your <code>PriorityQueue</code> class should have a <code>size</code> method.
testString: assert((function(){var test = new PriorityQueue(); return (typeof test.size === 'function')}()));
- text: Your <code>PriorityQueue</code> class should have a <code>front</code> method.
testString: assert((function(){var test = new PriorityQueue(); return (typeof test.front === 'function')}()));
- text: Your <code>PriorityQueue</code> class should have an <code>isEmpty</code> method.
testString: assert((function(){var test = new PriorityQueue(); return (typeof test.isEmpty === 'function')}()));
- text: Your <code>PriorityQueue</code> class should correctly keep track of the current number of items using the <code>size</code> method as items are enqueued and dequeued.
testString: assert((function(){var test = new PriorityQueue(); test.enqueue(['David Brown', 2]); test.enqueue(['Jon Snow', 1]); var size1 = test.size(); test.dequeue(); var size2 = test.size(); test.enqueue(['A', 3]); test.enqueue(['B', 3]); test.enqueue(['C', 3]); return (size1 === 2 && size2 === 1 && test.size() === 4)}()));
- text: The <code>front</code> method should return the correct item at the front of the queue as items are enqueued and dequeued.
testString: assert((function(){var test = new PriorityQueue(); test.enqueue(["David Brown", 2]); var front1 = test.front(); test.enqueue(["Jon Snow", 1]); var front2 = test.front(); test.dequeue(); test.enqueue(["A", 3]); var front3 = test.front(); test.enqueue(["B", 3]); test.enqueue(["C", 3]); test.dequeue(); var front4 = test.front(); return (front1 === "David Brown" && front2 === "Jon Snow" && front3 === "David Brown" && front4 === "A");})());
- text: The <code>isEmpty</code> method should return <code>true</code> when the queue is empty.
testString: assert((function(){var test = new PriorityQueue(); test.enqueue(['A', 1]); test.enqueue(['B', 1]); test.dequeue(); var first = test.isEmpty(); test.dequeue(); return (!first && test.isEmpty()); }()));
- text: The priority queue should return items with a higher priority before items with a lower priority and return items in first-in-first-out order otherwise.
@ -73,41 +77,46 @@ function PriorityQueue () {
```js
function PriorityQueue () {
this.collection = [];
this.printCollection = function(){
console.log(this.collection);
};
this.size = function() {
return this.collection.length;
};
this.isEmpty = function() {
return this.size() > 0 ? false : true;
};
this.enqueue = function (newitem) {
if (this.isEmpty()) {
return this.collection.push(newitem);
}
function PriorityQueue() {
this.collection = [];
this.printCollection = function () {
console.log(this.collection);
};
// Only change code below this line
this.enqueue = function (newitem) {
if (this.isEmpty()) {
return this.collection.push(newitem);
}
this.collection = this.collection.reverse();
var found_index = this.collection.findIndex(function (item) {
return newitem[1] >= item[1];
});
if (found_index === -1) {
this.collection.push(newitem);
} else {
this.collection.splice(found_index, 0, newitem);
}
this.collection = this.collection.reverse();
};
this.dequeue = function() {
if (!this.isEmpty()) {
return this.collection.shift()[0];
} else {
return 'The queue is empty.'
}
};
}
this.collection = this.collection.reverse();
var found_index = this.collection.findIndex(function (item) {
return newitem[1] >= item[1];
});
if (found_index === -1) {
this.collection.push(newitem);
} else {
this.collection.splice(found_index, 0, newitem);
}
this.collection = this.collection.reverse();
};
this.dequeue = function () {
if (!this.isEmpty()) {
return this.collection.shift()[0];
} else {
return "The queue is empty.";
}
};
this.size = function () {
return this.collection.length;
};
this.front = function () {
return this.collection[0][0];
};
this.isEmpty = function () {
return this.size() > 0 ? false : true;
};
// Only change code above this line
}
```
</section>