Oliver Eyton-Williams ee1e8abd87
feat(curriculum): restore seed + solution to Chinese (#40683)
* feat(tools): add seed/solution restore script

* chore(curriculum): remove empty sections' markers

* chore(curriculum): add seed + solution to Chinese

* chore: remove old formatter

* fix: update getChallenges

parse translated challenges separately, without reference to the source

* chore(curriculum): add dashedName to English

* chore(curriculum): add dashedName to Chinese

* refactor: remove unused challenge property 'name'

* fix: relax dashedName requirement

* fix: stray tag

Remove stray `pre` tag from challenge file.

Signed-off-by: nhcarrigan <nhcarrigan@gmail.com>

Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
2021-01-12 19:31:00 -07:00

3.4 KiB
Raw Blame History

id, title, challengeType, videoUrl, dashedName
id title challengeType videoUrl dashedName
587d8250367417b2b2512c60 创建队列类 1 create-a-queue-class

--description--

与堆栈一样队列是元素的集合。但与堆栈不同队列遵循FIFO先入先出原则。添加到队列的元素将被推送到队列的尾部或末尾并且只允许删除队列前面的元素。我们可以使用数组来表示队列但就像堆栈一样我们希望限制我们对队列的控制量。队列类的两个主要方法是enqueue和dequeue方法。 enqueue方法将元素推送到队列的尾部dequeue方法移除并返回队列前面的元素。其他有用的方法是frontsize和isEmpty方法。说明编写一个将元素推送到队列尾部的入队方法一个删除并返回前面元素的出列方法一个让我们看到前面元素的前方法一个显示长度的大小方法以及一个isEmpty方法检查队列是否为空。

--hints--

您的Queue类应该有一个enqueue方法。

assert(
  (function () {
    var test = new Queue();
    return typeof test.enqueue === 'function';
  })()
);

您的Queue类应该有一个dequeue方法。

assert(
  (function () {
    var test = new Queue();
    return typeof test.dequeue === 'function';
  })()
);

您的Queue类应该有一个front方法。

assert(
  (function () {
    var test = new Queue();
    return typeof test.front === 'function';
  })()
);

您的Queue类应该有一个size方法。

assert(
  (function () {
    var test = new Queue();
    return typeof test.size === 'function';
  })()
);

您的Queue类应该有一个isEmpty方法。

assert(
  (function () {
    var test = new Queue();
    return typeof test.isEmpty === 'function';
  })()
);

dequeue方法应该删除并返回队列的前端元素

assert(
  (function () {
    var test = new Queue();
    test.enqueue('Smith');
    test.enqueue('John');
    return test.dequeue() === 'Smith';
  })()
);

front方法应该返回队列的front元素的值

assert(
  (function () {
    var test = new Queue();
    test.enqueue('Smith');
    test.enqueue('John');
    return test.front() === 'Smith';
  })()
);

size方法应该返回队列的长度

assert(
  (function () {
    var test = new Queue();
    test.enqueue('Smith');
    return test.size() === 1;
  })()
);

如果队列中有元素,则isEmpty方法应返回false

assert(
  (function () {
    var test = new Queue();
    test.enqueue('Smith');
    return !test.isEmpty();
  })()
);

--seed--

--seed-contents--

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

  // Only change code above this line
}

--solutions--

function Queue () { 
    var collection = [];
    this.print = function() {
        console.log(collection);
    };
    // Only change code below this line
    this.enqueue = function(item) {
        collection.push(item);
    }

    this.dequeue = function() {
        return collection.shift();
    }

    this.front = function() {
        return collection[0];
    }

    this.size = function(){
        return collection.length;
    }

    this.isEmpty = function() {
        return collection.length === 0 ? true : false;
    }
    // Only change code above this line
}