6.5 KiB
Raw Blame History

id, title, challengeType, forumTopicId, localeTitle
id title challengeType forumTopicId localeTitle
587d8255367417b2b2512c74 Create a Priority Queue Class 1 301630 Создание класса очереди приоритетов

Description

В этом вызове вы создадите очередь приоритетов. Приоритетная очередь - это особый тип очереди, в которой элементы могут иметь дополнительную информацию, которая определяет их приоритет. Это может быть просто представлено целым числом. Приоритет элемента переопределяет порядок размещения при определении элементов последовательности. Если элемент с более высоким приоритетом помещается в очередь после элементов с более низким приоритетом, элемент с более высоким приоритетом будет удален до всех остальных. Например, предположим, что у нас есть очередь приоритетов с тремя элементами: [['kitten', 2], ['dog', 2], ['rabbit', 2]] Здесь второе значение (целое число) представляет приоритет элемента , Если мы ставим в очередь ['human', 1] с приоритетом 1 (при условии, что более низкие приоритеты заданы приоритетом), тогда это будет первый элемент, который будет удален. Коллекция понравится: [['human', 1], ['kitten', 2], ['dog', 2], ['rabbit', 2]] . Мы начали писать PriorityQueue в редакторе кода. Вам нужно будет добавить метод enqueue для добавления элементов с приоритетом, метод dequeue для удаления элементов, метод size для возврата количества элементов в очереди, front метод для возврата элемента в передней части очереди и наконец, метод isEmpty , который вернет true если очередь пуста или false если это не так. enqueue должна принимать элементы с форматом, указанным выше ( ['human', 1] ), где 1 представляет приоритет. dequeue должен возвращать только текущий элемент, а не его приоритет.

Instructions

Tests

tests:
  - text: Your <code>Queue</code> class should have a <code>enqueue</code> method.
    testString: assert((function(){var test = new PriorityQueue();  return (typeof test.enqueue === 'function')}()));
  - text: Your <code>Queue</code> class should have a <code>dequeue</code> method.
    testString: assert((function(){var test = new PriorityQueue();  return (typeof test.dequeue === 'function')}()));
  - text: Your <code>Queue</code> class should have a <code>size</code> method.
    testString: assert((function(){var test = new PriorityQueue();  return (typeof test.size === 'function')}()));
  - text: Your <code>Queue</code> class should have an <code>isEmpty</code> method.
    testString: assert((function(){var test = new PriorityQueue();  return (typeof test.isEmpty === 'function')}()));
  - text: Your PriorityQueue 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>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.
    testString: assert((function(){var test = new PriorityQueue(); test.enqueue(['A', 5]); test.enqueue(['B', 5]); test.enqueue(['C', 5]); test.enqueue(['D', 3]); test.enqueue(['E', 1]); test.enqueue(['F', 7]); var result = []; result.push(test.dequeue()); result.push(test.dequeue()); result.push(test.dequeue()); result.push(test.dequeue()); result.push(test.dequeue()); result.push(test.dequeue()); return result.join('') === 'EDABCF';}()));

Challenge Seed

function PriorityQueue () {
    this.collection = [];
    this.printCollection = function() {
      console.log(this.collection);
    };
    // Only change code below this line

    // Only change code above this line
}

Solution

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);
  }

  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.'
 }
 };
 }