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.1 KiB
Raw Blame History

id, title, challengeType, videoUrl, dashedName
id title challengeType videoUrl dashedName
587d8250367417b2b2512c5f 创建一个堆栈类 1 create-a-stack-class

--description--

在上一节中,我们讨论了堆栈是什么以及如何使用数组来表示堆栈。在本节中,我们将创建自己的堆栈类。虽然您可以使用数组来创建堆栈,但有时最好限制我们对堆栈的控制量。除了pushpop方法之外,堆栈还有其他有用的方法。让我们为我们的堆栈类添加一个peek isEmptyclear方法。说明编写一个push方法,将元素推送到堆栈顶部,一个pop方法删除堆栈顶部的元素,一个peek堆栈中第一个元素的peek方法,一个isEmpty方法用于检查是否存在stack是空的是一个clear堆栈中所有元素的方法。通常堆栈没有这个,但我们添加了一个控制台记录集合的print助手方法。

--hints--

你的Stack类应该有一个push方法。

assert(
  (function () {
    var test = new Stack();
    return typeof test.push === 'function';
  })()
);

你的Stack类应该有一个pop方法。

assert(
  (function () {
    var test = new Stack();
    return typeof test.pop === 'function';
  })()
);

你的Stack类应该有一个peek方法。

assert(
  (function () {
    var test = new Stack();
    return typeof test.peek === 'function';
  })()
);

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

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

你的Stack类应该有一个clear方法。

assert(
  (function () {
    var test = new Stack();
    return typeof test.clear === 'function';
  })()
);

peek方法应该返回堆栈的顶部元素

assert(
  (function () {
    var test = new Stack();
    test.push('CS50');
    return test.peek() === 'CS50';
  })()
);

pop方法应该删除并返回堆栈的顶部元素

assert(
  (function () {
    var test = new Stack();
    test.push('CS50');
    return test.pop() === 'CS50';
  })()
);

如果堆栈不包含任何元素,则isEmpty方法应返回true

assert(
  (function () {
    var test = new Stack();
    return test.isEmpty();
  })()
);

clear方法应该从堆栈中删除所有元素

assert(
  (function () {
    var test = new Stack();
    test.push('CS50');
    test.clear();
    return test.isEmpty();
  })()
);

--seed--

--seed-contents--

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

  // Only change code above this line
}

--solutions--

class Stack {
  constructor() {
    this.collection = [];
  }
  print() {
    console.log(this.collection);
  }
  push(val) {
    this.collection.push(val);
  }
  pop() {
    return this.collection.pop();
  }
  peek() {
    return this.collection[this.collection.length - 1];
  }
  isEmpty() {
    return this.collection.length === 0;
  }
  clear() {
    return (this.collection.length = 0);
  }
}