chore(i18n,curriculum): update translations (#43881)
This commit is contained in:
@@ -13,7 +13,7 @@ dashedName: use-the-strong-tag-to-make-text-bold
|
||||
|
||||
# --instructions--
|
||||
|
||||
给 `p` 标签里的 `Stanford University` 内容文本添加 `strong` 标签。
|
||||
给 `p` 标签里的 `Stanford University` 内容文本外面添加 `strong` 标签(不包括句号)。
|
||||
|
||||
# --hints--
|
||||
|
||||
|
@@ -35,6 +35,18 @@ assert($('input[type="radio"]').prop('checked'));
|
||||
assert($('input[type="checkbox"]').prop('checked'));
|
||||
```
|
||||
|
||||
不应该更改 `Indoor` 标签的内部文本。
|
||||
|
||||
```js
|
||||
assert.equal(document.querySelector('label[for="indoor"]')?.innerText?.trim(), 'Indoor');
|
||||
```
|
||||
|
||||
不应该更改 `Loving` 标签的内部文本。
|
||||
|
||||
```js
|
||||
assert.equal(document.querySelector('label[for="loving"]')?.innerText?.trim(), 'Loving');
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
@@ -60,7 +60,7 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
`footer` 元素的 `id` 属性值应为 `footer`。
|
||||
`a` 元素的文本应该是 “Jump to Bottom”。
|
||||
|
||||
```js
|
||||
assert(
|
||||
|
@@ -13,9 +13,9 @@ dashedName: record-collection
|
||||
以 `updateRecords` 函数开始,这个函数需要一个对象 `records`,包含一个音乐专辑集合,一个 `id`,一个 `prop`(如 `artist` 或 `tracks`),和一个 `value`。 使用下面的规则完成函数来修改传递给函数的对象。
|
||||
|
||||
- 函数必须始终返回整个音乐专辑集合对象。
|
||||
- 如果 `prop` 不是 `tracks` 并且 `value` 不是一个空字符串, 将相册的 `prop` 更新或设置为 `value`。
|
||||
- 如果 `prop` 不是 `tracks` 并且 `value` 不是一个空字符串, 将专辑的 `prop` 更新或设置为 `value`。
|
||||
- 如果 `prop` 是 `tracks` 但专辑没有 `tracks` 属性,则应创建空数组并为其添加 `value`。
|
||||
- 如果 `prop` 是 `tracks` 并且 `value` 不是一个空字符串,将 `value` 添加到相册现有 `tracks` 数组的末尾。
|
||||
- 如果 `prop` 是 `tracks` 并且 `value` 不是一个空字符串,将 `value` 添加到专辑现有 `tracks` 数组的末尾。
|
||||
- 如果 `value` 是空字符串,从专辑里删除指定的 `prop`。
|
||||
|
||||
**注意:** 用 `recordCollection` 对象做为测试参数对象。
|
||||
|
@@ -23,10 +23,19 @@ setFullName(firstAndLast)
|
||||
|
||||
# --hints--
|
||||
|
||||
`Object.keys(bob).length` 应返回 6。
|
||||
不应添加属性。 `Object.keys(bob).length` 应返回 6。
|
||||
|
||||
```js
|
||||
assert.deepEqual(Object.keys(bob).length, 6);
|
||||
assert.strictEqual(
|
||||
Object.keys((function () {
|
||||
let bob = new Person('Bob Ross');
|
||||
bob.setFirstName('Haskell');
|
||||
bob.setLastName('Curry');
|
||||
bob.setFullName('John Smith');
|
||||
return bob;
|
||||
})()).length,
|
||||
6
|
||||
);
|
||||
```
|
||||
|
||||
`bob instanceof Person` 应返回 `true`。
|
||||
|
@@ -36,6 +36,12 @@ assert(!code.match(/\.?[\s\S]*?trim/));
|
||||
assert(!code.match(/result\s*=\s*["'`].*?["'`]/));
|
||||
```
|
||||
|
||||
`hello` 变量的值不应更改。
|
||||
|
||||
```js
|
||||
assert(hello === ' Hello, World! ');
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
@@ -8,7 +8,7 @@ dashedName: set-state-with-this-setstate
|
||||
|
||||
# --description--
|
||||
|
||||
前面的挑战涵盖了组件的 `state` 以及如何在 `constructor` 中初始化 state。 还有一种方法可以更改组件的 `state`, React 提供了 `setState` 方法来更新组件的 `state`。 在组件类中调用 `setState` 方法如下所示:`this.setState()`,传入键值对的对象, 其中键是 state 属性,值是更新后的 state 数据。 例如,如果我们在 state 中存储 `username`,并想要更新它,代码如下所示:
|
||||
前面的挑战涵盖了组件的 `state` 以及如何在 `constructor` 中初始化 state。 还有一种方法可以更改组件的 `state`。 React 提供了 `setState` 方法来更新组件的 `state`。 在组件类中调用 `setState` 方法如下所示:`this.setState()`,传入键值对的对象, 其中键是 state 属性,值是更新后的 state 数据。 例如,如果我们在 state 中存储 `username`,并想要更新它,代码如下所示:
|
||||
|
||||
```jsx
|
||||
this.setState({
|
||||
@@ -16,11 +16,11 @@ this.setState({
|
||||
});
|
||||
```
|
||||
|
||||
React 要求永远不要直接修改 `state`,而是在 state 发生改变时始终使用 `this.setState()`。 此外,应该注意,React 可以批量处理多个 state 更新以提高性能。 这意味着通过 `setState` 方法进行的 state 更新可以是异步的。 `setState` 方法有一种替代语法可以解决异步问题, 虽然这很少用到,但是最好还是记住它! 有关详细信息,请参阅[React 文档](https://facebook.github.io/react/docs/state-and-lifecycle.html)。
|
||||
React 要求永远不要直接修改 `state`,而是在 state 发生改变时始终使用 `this.setState()`。 此外,应该注意,React 可以批量处理多个 state 更新以提高性能。 这意味着通过 `setState` 方法进行的 state 更新可以是异步的。 `setState` 方法有一种替代语法可以解决异步问题, 虽然这很少用到,但是最好还是记住它! 有关详细信息,请参阅[React 文档](https://reactjs.org/docs/state-and-lifecycle.html#state-updates-may-be-asynchronous)。
|
||||
|
||||
# --instructions--
|
||||
|
||||
代码编辑器中有一个 `button` 元素,它有一个 `onClick()` 处理程序。 当 `button` 在浏览器中接收到单击事件时触发此处理程序,并运行 `MyComponent` 中定义的 `handleClick` 方法。 在 `handleClick` 方法中,使用 `this.setState()` 更新组件的 `state`。 设置 `state` 中的 `name` 属性为字符串 `React Rocks!`。
|
||||
代码编辑器中有一个 `button` 元素,它有一个 `onClick()` handler。 当 `button` 在浏览器中接收到单击事件时触发此 handler,并运行 `MyComponent` 中定义的 `handleClick` 方法。 在 `handleClick` 方法中,使用 `this.setState()` 更新组件的 `state`。 设置 `state` 中的 `name` 属性为字符串 `React Rocks!`。
|
||||
|
||||
单击按钮查看渲染的 state 的更新。 如果不完全理解单击处理程序代码在此时的工作方式,请不要担心。 在接下来的挑战中会有讲述。
|
||||
|
||||
|
@@ -16,7 +16,7 @@ React 提供了有用的类型检查特性,以验证组件是否接收了正
|
||||
MyComponent.propTypes = { handleClick: PropTypes.func.isRequired }
|
||||
```
|
||||
|
||||
在上面的示例中,`PropTypes.func` 部分检查 `handleClick` 是否为函数。 添加 `isRequired`,告诉 React `handleClick` 是该组件的必需属性。 如果没有那个属性,将出现警告。 还要注意 `func` 代表 `function` 。 在 7 种 JavaScript 原语类型中, `function` 和 `boolean` (写为 `bool` )是唯一使用异常拼写的两种类型。 除了原始类型,还有其他类型可用。 例如,你可以检查 prop 是否为 React 元素。 请参阅[文档](https://reactjs.org/docs/jsx-in-depth.html#specifying-the-react-element-type)以获取所有选项。
|
||||
在上面的示例中,`PropTypes.func` 部分检查 `handleClick` 是否为函数。 添加 `isRequired`,告诉 React `handleClick` 是该组件的必需属性。 如果没有那个属性,将出现警告。 还要注意 `func` 代表 `function` 。 在 7 种 JavaScript 原语类型中, `function` 和 `boolean` (写为 `bool` )是唯一使用异常拼写的两种类型。 除了原始类型,还有其他类型可用。 例如,你可以检查 prop 是否为 React 元素。 请参阅[文档](https://reactjs.org/docs/typechecking-with-proptypes.html#proptypes)以获取所有选项。
|
||||
|
||||
**注意:**在 React v15.5.0 中, `PropTypes` 可以从 React 中单独引入,例如:`import PropTypes from 'prop-types';`。
|
||||
|
||||
|
@@ -27,7 +27,9 @@ dashedName: stock-price-checker
|
||||
3. 添加安全功能到 `server.js`。
|
||||
4. 在 `tests/2_functional-tests.js` 中创建所有的功能测试
|
||||
|
||||
在 `tests/2_functional-tests.js` 中编写下以下测试:
|
||||
**注意** 隐私考虑:由于每个 IP 只能接受一个赞(like),你必须保存 IP 地址。 必须遵守数据隐私法规,例如《通用数据保护条例》。 一个选项是获得保存用户数据的权限,但是匿名化则要简单得多。 对于此挑战,请记住在将 IP 地址保存到数据库之前对其进行匿名化。 如果你想知道如何做到这一点,你可以选择散列数据、截断它、或将 IP 地址的一部分设置为 0。
|
||||
|
||||
在 `tests/2_functional-tests.js` 中编写以下测试:
|
||||
|
||||
- 查看股价:发送 GET 请求到 `/api/stock-prices/`
|
||||
- 查看一个股票并关注它:发送 GET 请求到 `/api/stock-prices/`
|
||||
@@ -37,7 +39,7 @@ dashedName: stock-price-checker
|
||||
|
||||
# --hints--
|
||||
|
||||
提交自己的项目,而不是示例的 URL。
|
||||
提交你自己的项目,而不是示例的 URL。
|
||||
|
||||
```js
|
||||
(getUserInput) => {
|
||||
@@ -74,7 +76,7 @@ async (getUserInput) => {
|
||||
};
|
||||
```
|
||||
|
||||
`stockData` 属性包括字符串 `stock` 代码、数字 `price`,以及数字 `likes`。
|
||||
`stockData` 属性包括字符串 `stock`、数字 `price`,以及数字 `likes`。
|
||||
|
||||
```js
|
||||
async (getUserInput) => {
|
||||
@@ -89,7 +91,7 @@ async (getUserInput) => {
|
||||
};
|
||||
```
|
||||
|
||||
你也可以用作为 `true`(布尔值)来传递 `like` 字段,让你的偏好添加到股票中。 每个 IP 应该只接受 1 个赞。
|
||||
你也可以将 `like` 字段作为 `true`(布尔值)传递,让你的偏好添加到股票中。 每个 IP 应该只接受 1 个赞(like)。
|
||||
|
||||
```js
|
||||
|
||||
|
@@ -1,6 +1,6 @@
|
||||
---
|
||||
id: 8d5123c8c441eddfaeb5bdef
|
||||
title: Implement Bubble Sort
|
||||
title: 实现冒泡排序
|
||||
challengeType: 1
|
||||
forumTopicId: 301612
|
||||
dashedName: implement-bubble-sort
|
||||
@@ -8,23 +8,23 @@ dashedName: implement-bubble-sort
|
||||
|
||||
# --description--
|
||||
|
||||
This is the first of several challenges on sorting algorithms. Given an array of unsorted items, we want to be able to return a sorted array. We will see several different methods to do this and learn some tradeoffs between these different approaches. While most modern languages have built-in sorting methods for operations like this, it is still important to understand some of the common basic approaches and learn how they can be implemented.
|
||||
这是排序算法的几个挑战中的第一个。 给定一组未排序的元素数组,我们希望能够返回已排序的数组。 我们将看到几种不同的方法来实现这一点,并学习这些不同方法之间的一些权衡。 虽然大多数现代语言都有内置的排序方法来完成这样的操作,但了解一些常见的基本方法并了解如何实现它们仍然很重要。
|
||||
|
||||
Here we will see bubble sort. The bubble sort method starts at the beginning of an unsorted array and 'bubbles up' unsorted values towards the end, iterating through the array until it is completely sorted. It does this by comparing adjacent items and swapping them if they are out of order. The method continues looping through the array until no swaps occur at which point the array is sorted.
|
||||
在这里我们将看到冒泡排序。 冒泡排序方法从未排序数组的开头开始,并向末端“冒泡”未排序的值,遍历数组直到它完全排序。 它是这样实现的:比较相邻的元素,如果它们的顺序不对,就交换它们。 该方法继续循环遍历数组,直到没有发生交换,此时数组已被排序。
|
||||
|
||||
This method requires multiple iterations through the array and for average and worst cases has quadratic time complexity. While simple, it is usually impractical in most situations.
|
||||
这种方法需要对数组进行多次循环,对于平均和最坏的情况来说,其时间复杂性为二次方。 虽然简单,但在大多数情况下通常不切实际。
|
||||
|
||||
**Instructions:** Write a function `bubbleSort` which takes an array of integers as input and returns an array of these integers in sorted order from least to greatest.
|
||||
**说明:**写一个 `bubbleSort` 函数,它将一个整数数组作为输入,并返回一个数组,其中的整数元素以从小到大的顺序排列。
|
||||
|
||||
# --hints--
|
||||
|
||||
`bubbleSort` should be a function.
|
||||
`bubbleSort` 应该是一个函数。
|
||||
|
||||
```js
|
||||
assert(typeof bubbleSort == 'function');
|
||||
```
|
||||
|
||||
`bubbleSort` should return a sorted array (least to greatest).
|
||||
`bubbleSort` 应该返回一个已排序的数组(从小到大)。
|
||||
|
||||
```js
|
||||
assert(
|
||||
@@ -52,7 +52,7 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
`bubbleSort` should return an array that is unchanged except for order.
|
||||
`bubbleSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92])` 应返回一个除顺序外没有变化的数组。
|
||||
|
||||
```js
|
||||
assert.sameMembers(
|
||||
@@ -79,7 +79,7 @@ assert.sameMembers(
|
||||
);
|
||||
```
|
||||
|
||||
`bubbleSort` should not use the built-in `.sort()` method.
|
||||
`bubbleSort` 不应使用内置的 `.sort()` 方法。
|
||||
|
||||
```js
|
||||
assert(isBuiltInSortUsed());
|
||||
@@ -113,8 +113,6 @@ function bubbleSort(array) {
|
||||
return array;
|
||||
// Only change code above this line
|
||||
}
|
||||
|
||||
bubbleSort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
@@ -1,6 +1,6 @@
|
||||
---
|
||||
id: 587d8259367417b2b2512c86
|
||||
title: Implement Insertion Sort
|
||||
title: 实现插入排序
|
||||
challengeType: 1
|
||||
forumTopicId: 301613
|
||||
dashedName: implement-insertion-sort
|
||||
@@ -8,19 +8,19 @@ dashedName: implement-insertion-sort
|
||||
|
||||
# --description--
|
||||
|
||||
The next sorting method we'll look at is insertion sort. This method works by building up a sorted array at the beginning of the list. It begins the sorted array with the first element. Then it inspects the next element and swaps it backwards into the sorted array until it is in sorted position. It continues iterating through the list and swapping new items backwards into the sorted portion until it reaches the end. This algorithm has quadratic time complexity in the average and worst cases.
|
||||
我们将要研究的下一个排序方法是插入排序。 此方法的工作原理是通过在数组的开头构建排序数组。 它从第一个元素开始排序数组。 然后,它检查下一个元素并将其向后交换到已排序的数组中,直到它处于已排序的位置为止。 它继续遍历列表,并将新项目向后交换到已排序的部分中,直到到达末尾为止。 这种算法在平均和最坏的情况下都有二次方的时间复杂性。
|
||||
|
||||
**Instructions:** Write a function `insertionSort` which takes an array of integers as input and returns an array of these integers in sorted order from least to greatest.
|
||||
**说明:** 编写一个函数`insertionSort` ,它将一个整数数组作为输入,并按照从最小到最大的排序顺序返回这些整数的数组。
|
||||
|
||||
# --hints--
|
||||
|
||||
`insertionSort` should be a function.
|
||||
`insertionSort` 应该是一个函数。
|
||||
|
||||
```js
|
||||
assert(typeof insertionSort == 'function');
|
||||
```
|
||||
|
||||
`insertionSort` should return a sorted array (least to greatest).
|
||||
`insertionSort` 应该返回一个排序的数组(从最小到最大)。
|
||||
|
||||
```js
|
||||
assert(
|
||||
@@ -48,7 +48,7 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
`insertionSort` should return an array that is unchanged except for order.
|
||||
`insertionSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92])` 应返回一个除顺序外没有变化的数组。
|
||||
|
||||
```js
|
||||
assert.sameMembers(
|
||||
@@ -75,7 +75,13 @@ assert.sameMembers(
|
||||
);
|
||||
```
|
||||
|
||||
`insertionSort` should not use the built-in `.sort()` method.
|
||||
`insertionSort([5, 4, 33, 2, 8])` 应返回 `[2, 4, 5, 8, 33]`。
|
||||
|
||||
```js
|
||||
assert.deepEqual(insertionSort([5, 4, 33, 2, 8]), [2, 4, 5, 8, 33])
|
||||
```
|
||||
|
||||
`insertionSort` 不应使用内置的 `.sort()` 方法。
|
||||
|
||||
```js
|
||||
assert(isBuiltInSortUsed());
|
||||
@@ -109,8 +115,6 @@ function insertionSort(array) {
|
||||
return array;
|
||||
// Only change code above this line
|
||||
}
|
||||
|
||||
insertionSort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
@@ -1,6 +1,6 @@
|
||||
---
|
||||
id: 587d825c367417b2b2512c8f
|
||||
title: Implement Merge Sort
|
||||
title: 实现归并排序
|
||||
challengeType: 1
|
||||
forumTopicId: 301614
|
||||
dashedName: implement-merge-sort
|
||||
@@ -8,27 +8,27 @@ dashedName: implement-merge-sort
|
||||
|
||||
# --description--
|
||||
|
||||
Another common intermediate sorting algorithm is merge sort. Like quick sort, merge sort also uses a divide-and-conquer, recursive methodology to sort an array. It takes advantage of the fact that it is relatively easy to sort two arrays as long as each is sorted in the first place. But we'll start with only one array as input, so how do we get to two sorted arrays from that? Well, we can recursively divide the original input in two until we reach the base case of an array with one item. A single-item array is naturally sorted, so then we can start combining. This combination will unwind the recursive calls that split the original array, eventually producing a final sorted array of all the elements. The steps of merge sort, then, are:
|
||||
另一种常见的中间排序算法是归并排序。 像快速排序一样,合并排序也使用分而治之的递归方法对数组进行排序。 它基于这样一个事实:要将两个已经排好序的数组排在一起,是相对容易的。 但是我们只从一个数组作为输入开始,那么我们如何从中获得两个已排序的数组呢? 好吧,我们可以递归地将原始输入分成两部分,直到我们到达每个数组只有一个元素的基本情况。 单项数组是自然排序的,因此我们可以开始组合。 这个组合将展开拆分原始数组的递归调用,最终生成所有元素的最终排序数组。 合并排序的步骤如下:
|
||||
|
||||
**1)** Recursively split the input array in half until a sub-array with only one element is produced.
|
||||
**1)** 将输入数组递归地分成两部分,直到生成仅包含一个元素的子数组。
|
||||
|
||||
**2)** Merge each sorted sub-array together to produce the final sorted array.
|
||||
**2)** 将每个已排序的子数组合并在一起以生成最终的排序的数组。
|
||||
|
||||
Merge sort is an efficient sorting method, with time complexity of *O(nlog(n))*. This algorithm is popular because it is performant and relatively easy to implement.
|
||||
合并排序是一种有效的排序方法,时间复杂度为 *O(nlog(n))* 。 该算法很受欢迎,因为它性能高且易于实现。
|
||||
|
||||
As an aside, this will be the last sorting algorithm we cover here. However, later in the section on tree data structures we will describe heap sort, another efficient sorting method that requires a binary heap in its implementation.
|
||||
顺便说一句,这将是我们在此处介绍的最后一种排序算法。 但是,稍后在关于树型数据结构的部分中,我们将描述堆排序,这是另一种在其实现中需要二进制堆的有效排序方法。
|
||||
|
||||
**Instructions:** Write a function `mergeSort` which takes an array of integers as input and returns an array of these integers in sorted order from least to greatest. A good way to implement this is to write one function, for instance `merge`, which is responsible for merging two sorted arrays, and another function, for instance `mergeSort`, which is responsible for the recursion that produces single-item arrays to feed into merge. Good luck!
|
||||
**说明:** 编写一个函数 `mergeSort`,它以整数数组作为输入,并按从最小到最大的排序顺序返回这些整数的数组。 实现这一点的一个好方法是编写一个函数,例如 `merge`,它负责合并两个已排序的数组;以及另一个函数,例如 `mergeSort`,它负责递归,生成单项数组以提供给 merge。 祝你好运!
|
||||
|
||||
# --hints--
|
||||
|
||||
`mergeSort` should be a function.
|
||||
`mergeSort` 应该是一个函数。
|
||||
|
||||
```js
|
||||
assert(typeof mergeSort == 'function');
|
||||
```
|
||||
|
||||
`mergeSort` should return a sorted array (least to greatest).
|
||||
`mergeSort` 应该返回一个已排序的数组(从小到大)。
|
||||
|
||||
```js
|
||||
assert(
|
||||
@@ -56,7 +56,7 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
`mergeSort` should return an array that is unchanged except for order.
|
||||
`mergeSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92])` 应返回一个除顺序外没有变化的数组。
|
||||
|
||||
```js
|
||||
assert.sameMembers(
|
||||
@@ -83,7 +83,7 @@ assert.sameMembers(
|
||||
);
|
||||
```
|
||||
|
||||
`mergeSort` should not use the built-in `.sort()` method.
|
||||
`mergeSort` 不应使用内置的 `.sort()` 方法。
|
||||
|
||||
```js
|
||||
assert(isBuiltInSortUsed());
|
||||
@@ -117,8 +117,6 @@ function mergeSort(array) {
|
||||
return array;
|
||||
// Only change code above this line
|
||||
}
|
||||
|
||||
mergeSort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
@@ -1,6 +1,6 @@
|
||||
---
|
||||
id: 587d8259367417b2b2512c85
|
||||
title: Implement Selection Sort
|
||||
title: 实现选择排序
|
||||
challengeType: 1
|
||||
forumTopicId: 301616
|
||||
dashedName: implement-selection-sort
|
||||
@@ -8,19 +8,19 @@ dashedName: implement-selection-sort
|
||||
|
||||
# --description--
|
||||
|
||||
Here we will implement selection sort. Selection sort works by selecting the minimum value in a list and swapping it with the first value in the list. It then starts at the second position, selects the smallest value in the remaining list, and swaps it with the second element. It continues iterating through the list and swapping elements until it reaches the end of the list. Now the list is sorted. Selection sort has quadratic time complexity in all cases.
|
||||
这里我们将实现选择排序。 选择排序的工作原理是选择列表中的最小值并与列表中的第一个值进行交换。 然后它从第二个位置开始,选择剩余列表中的最小值,并将其与第二个元素交换。 它继续遍历列表并交换元素,直到它到达列表的末尾。 现在列表已排序。 在所有情况下,选择排序都具有二次方的时间复杂度。
|
||||
|
||||
**Instructions**: Write a function `selectionSort` which takes an array of integers as input and returns an array of these integers in sorted order from least to greatest.
|
||||
**说明**:编写一个 `selectionSort` 函数,它将一个整数数组作为输入,并按照从最小到最大的排序顺序返回这些整数的数组。
|
||||
|
||||
# --hints--
|
||||
|
||||
`selectionSort` should be a function.
|
||||
`selectionSort` 应该是一个函数。
|
||||
|
||||
```js
|
||||
assert(typeof selectionSort == 'function');
|
||||
```
|
||||
|
||||
`selectionSort` should return a sorted array (least to greatest).
|
||||
`selectionSort` 应该返回一个排序的数组(从最小到最大)。
|
||||
|
||||
```js
|
||||
assert(
|
||||
@@ -48,7 +48,7 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
`selectionSort` should return an array that is unchanged except for order.
|
||||
`selectionSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92])` 应返回一个除顺序外没有变化的数组。
|
||||
|
||||
```js
|
||||
assert.sameMembers(
|
||||
@@ -75,7 +75,7 @@ assert.sameMembers(
|
||||
);
|
||||
```
|
||||
|
||||
`selectionSort` should not use the built-in `.sort()` method.
|
||||
`selectionSort` 不应使用内置的 `.sort()` 方法。
|
||||
|
||||
```js
|
||||
assert(isBuiltInSortUsed());
|
||||
@@ -109,9 +109,6 @@ function selectionSort(array) {
|
||||
return array;
|
||||
// Only change code above this line
|
||||
}
|
||||
|
||||
|
||||
selectionSort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
@@ -1,6 +1,6 @@
|
||||
---
|
||||
id: a7bf700cd123b9a54eef01d5
|
||||
title: No Repeats Please
|
||||
title: 请不要重复
|
||||
challengeType: 5
|
||||
forumTopicId: 16037
|
||||
dashedName: no-repeats-please
|
||||
@@ -8,67 +8,67 @@ dashedName: no-repeats-please
|
||||
|
||||
# --description--
|
||||
|
||||
Return the number of total permutations of the provided string that don't have repeated consecutive letters. Assume that all characters in the provided string are each unique.
|
||||
返回没有重复连续字母的提供字符串的总排列数。 假设提供的字符串中的所有字符都是唯一的。
|
||||
|
||||
For example, `aab` should return 2 because it has 6 total permutations (`aab`, `aab`, `aba`, `aba`, `baa`, `baa`), but only 2 of them (`aba` and `aba`) don't have the same letter (in this case `a`) repeating.
|
||||
例如,`aab` 应该返回 2,因为它有 6 个排列(`aab`、`aab`、`aba`、`aba`、`baa` 和 `baa`),但其中只有 2 个(`aba` 和 `aba`)没有重复相同字符(即 `a`)。
|
||||
|
||||
# --hints--
|
||||
|
||||
`permAlone("aab")` should return a number.
|
||||
`permAlone("aab")` 应返回 2。
|
||||
|
||||
```js
|
||||
assert.isNumber(permAlone('aab'));
|
||||
```
|
||||
|
||||
`permAlone("aab")` should return 2.
|
||||
`permAlone("aab")` 应该返回 2。
|
||||
|
||||
```js
|
||||
assert.strictEqual(permAlone('aab'), 2);
|
||||
```
|
||||
|
||||
`permAlone("aaa")` should return 0.
|
||||
`permAlone("aaa")` 应该返回 0。
|
||||
|
||||
```js
|
||||
assert.strictEqual(permAlone('aaa'), 0);
|
||||
```
|
||||
|
||||
`permAlone("aabb")` should return 8.
|
||||
`permAlone("aabb")`应返回 8。
|
||||
|
||||
```js
|
||||
assert.strictEqual(permAlone('aabb'), 8);
|
||||
```
|
||||
|
||||
`permAlone("abcdefa")` should return 3600.
|
||||
`permAlone("abcdefa")`应返回 3600。
|
||||
|
||||
```js
|
||||
assert.strictEqual(permAlone('abcdefa'), 3600);
|
||||
```
|
||||
|
||||
`permAlone("abfdefa")` should return 2640.
|
||||
`permAlone("abfdefa")`应该返回 2640。
|
||||
|
||||
```js
|
||||
assert.strictEqual(permAlone('abfdefa'), 2640);
|
||||
```
|
||||
|
||||
`permAlone("zzzzzzzz")` should return 0.
|
||||
`permAlone("zzzzzzzz")` 应返回 0。
|
||||
|
||||
```js
|
||||
assert.strictEqual(permAlone('zzzzzzzz'), 0);
|
||||
```
|
||||
|
||||
`permAlone("a")` should return 1.
|
||||
`permAlone("a")` 应该返回 1。
|
||||
|
||||
```js
|
||||
assert.strictEqual(permAlone('a'), 1);
|
||||
```
|
||||
|
||||
`permAlone("aaab")` should return 0.
|
||||
`permAlone("aaab")` 应该返回 0。
|
||||
|
||||
```js
|
||||
assert.strictEqual(permAlone('aaab'), 0);
|
||||
```
|
||||
|
||||
`permAlone("aaabb")` should return 12.
|
||||
`permAlone("aaabb")` 应返回 12.
|
||||
|
||||
```js
|
||||
assert.strictEqual(permAlone('aaabb'), 12);
|
||||
|
@@ -1,6 +1,6 @@
|
||||
---
|
||||
id: a3f503de51cfab748ff001aa
|
||||
title: Pairwise
|
||||
title: 成对
|
||||
challengeType: 5
|
||||
forumTopicId: 301617
|
||||
dashedName: pairwise
|
||||
@@ -8,57 +8,57 @@ dashedName: pairwise
|
||||
|
||||
# --description--
|
||||
|
||||
Given an array `arr`, find element pairs whose sum equal the second argument `arg` and return the sum of their indices.
|
||||
给定一个数组 `arr` ,找到其中总和等于第二个参数 `arg` 的元素对,并返回它们的索引之和。
|
||||
|
||||
You may use multiple pairs that have the same numeric elements but different indices. Each pair should use the lowest possible available indices. Once an element has been used it cannot be reused to pair with another element. For instance, `pairwise([1, 1, 2], 3)` creates a pair `[2, 1]` using the 1 at index 0 rather than the 1 at index 1, because 0+2 < 1+2.
|
||||
你可以使用具有相同数字元素但索引不同的多个对。 每对应使用尽可能低的索引。 一旦元素被使用,它就不能被重用来与另一个元素配对。 例如, `pairwise([1, 1, 2], 3)` 使用索引为 0 的 1,而不是索引为 1 的 1 来创建一对 `[2, 1]`,因为 0 + 2 < 1 + 2。
|
||||
|
||||
For example `pairwise([7, 9, 11, 13, 15], 20)` returns `6`. The pairs that sum to 20 are `[7, 13]` and `[9, 11]`. We can then write out the array with their indices and values.
|
||||
例如, `pairwise([7, 9, 11, 13, 15], 20)` 返回 `6`。 总和为 20 的对是 `[7, 13]` 和 `[9, 11]`。 然后我们可以用它们的索引和值写出数组。
|
||||
|
||||
<div style='margin-left: 2em;'>
|
||||
|
||||
| Index | 0 | 1 | 2 | 3 | 4 |
|
||||
| 索引 | 0 | 1 | 2 | 3 | 4 |
|
||||
| ----- | - | - | -- | -- | -- |
|
||||
| Value | 7 | 9 | 11 | 13 | 15 |
|
||||
| 值 | 7 | 9 | 11 | 13 | 15 |
|
||||
|
||||
</div>
|
||||
|
||||
Below we'll take their corresponding indices and add them.
|
||||
接下来,我们将获取它们的相应索引并添加它们。
|
||||
|
||||
<div style='margin-left: 2em;'>
|
||||
|
||||
7 + 13 = 20 → Indices 0 + 3 = 3
|
||||
9 + 11 = 20 → Indices 1 + 2 = 3
|
||||
3 + 3 = 6 → Return `6`
|
||||
7 + 13 = 20 → 索引 0 + 3 = 3
|
||||
9 + 11 = 20 → 索引 1 + 2 = 3
|
||||
3 + 3 = 6 →返回 `6`
|
||||
|
||||
</div>
|
||||
|
||||
# --hints--
|
||||
|
||||
`pairwise([1, 4, 2, 3, 0, 5], 7)` should return 11.
|
||||
`pairwise([1, 4, 2, 3, 0, 5], 7)` 应该返回 11。
|
||||
|
||||
```js
|
||||
assert.deepEqual(pairwise([1, 4, 2, 3, 0, 5], 7), 11);
|
||||
```
|
||||
|
||||
`pairwise([1, 3, 2, 4], 4)` should return 1.
|
||||
`pairwise([1, 3, 2, 4], 4)` 应返回 1。
|
||||
|
||||
```js
|
||||
assert.deepEqual(pairwise([1, 3, 2, 4], 4), 1);
|
||||
```
|
||||
|
||||
`pairwise([1, 1, 1], 2)` should return 1.
|
||||
`pairwise([1, 1, 1], 2)` 应该返回 1。
|
||||
|
||||
```js
|
||||
assert.deepEqual(pairwise([1, 1, 1], 2), 1);
|
||||
```
|
||||
|
||||
`pairwise([0, 0, 0, 0, 1, 1], 1)` should return 10.
|
||||
`pairwise([0, 0, 0, 0, 1, 1], 1)` 应该返回10。
|
||||
|
||||
```js
|
||||
assert.deepEqual(pairwise([0, 0, 0, 0, 1, 1], 1), 10);
|
||||
```
|
||||
|
||||
`pairwise([], 100)` should return 0.
|
||||
`pairwise([], 100)` 应该返回 0。
|
||||
|
||||
```js
|
||||
assert.deepEqual(pairwise([], 100), 0);
|
||||
|
@@ -1,6 +1,6 @@
|
||||
---
|
||||
id: 587d8257367417b2b2512c7b
|
||||
title: Add a New Element to a Binary Search Tree
|
||||
title: 将新元素添加到二叉搜索树
|
||||
challengeType: 1
|
||||
forumTopicId: 301618
|
||||
dashedName: add-a-new-element-to-a-binary-search-tree
|
||||
@@ -8,27 +8,27 @@ dashedName: add-a-new-element-to-a-binary-search-tree
|
||||
|
||||
# --description--
|
||||
|
||||
This series of challenges will introduce the tree data structure. Trees are an important and versatile data structure in computer science. Of course, their name comes from the fact that when visualized they look much like the trees we are familiar with in the natural world. A tree data structure begins with one node, typically referred to as the root, and from here branches out to additional nodes, each of which may have more child nodes, and so on and so forth. The data structure is usually visualized with the root node at the top; you can think of it as a natural tree flipped upside down.
|
||||
这一系列的挑战将介绍树形数据结构。 树是计算机科学中一个重要的、通用的数据结构。 当然,它们的名称来自这样一个事实:当可视化时,它们看起来很像我们在自然界中熟悉的树木。 树数据结构从一个节点(通常称为根)开始,并从此处分支到其他节点,每个节点可能具有更多的子节点,依此类推。 数据结构通常以根节点为顶点进行可视化;你可以把它想象成一棵倒过来的自然树。
|
||||
|
||||
First, let's describe some common terminology we will encounter with trees. The root node is the top of the tree. Data points in the tree are called nodes. Nodes with branches leading to other nodes are referred to as the parent of the node the branch leads to (the child). Other more complicated familial terms apply as you might expect. A subtree refers to all the descendants of a particular node, branches may be referred to as edges, and leaf nodes are nodes at the end of the tree that have no children. Finally, note that trees are inherently recursive data structures. That is, any children of a node are parents of their own subtree, and so on. The recursive nature of trees is important to understand when designing algorithms for common tree operations.
|
||||
首先,让我们描述一下我们将遇到的关于树的一些常见术语。 根节点(root)是树的顶部。 树中的数据点称为节点(node)。 分支通向其他节点的节点称为分支通向的节点(即子节点)的父节点。 如你所料,其他更复杂的家庭术语也适用。 子树指的是某一特定节点的所有后代,分支可称为边,而叶子节点是位于树的末端的且没有子节点的节点。 最后,请注意,树本质上是递归的数据结构。 也就是说,一个节点的任何子节点都是其自己的子树的父节点,依此类推。 在为常见的树操作设计算法时,树的递归性质很重要。
|
||||
|
||||
To begin, we will discuss a particular type of a tree, the binary tree. In fact, we will actually discuss a particular binary tree, a binary search tree. Let's describe what this means. While the tree data structure can have any number of branches at a single node, a binary tree can only have two branches for every node. Furthermore, a binary search tree is ordered with respect to the child subtrees, such that the value of each node in the left subtree is less than or equal to the value of the parent node, and the value of each node in the right subtree is greater than or equal to the value of the parent node. It's very helpful to visualize this relationship in order to understand it better:
|
||||
首先,我们将讨论树的一个特殊类型,即二叉树。 实际上,我们将讨论特定的二叉树,即二叉搜索树。 让我们来看看这意味着什么。 虽然树形数据结构在一个节点上可以有任意数量的分支,但二叉树每个节点只能有两个分支。 此外,一个二叉搜索树相对于其子子树是有序的,即对于一个节点而言,其左子树中每个节点的值都小于或等于该节点的值,而其右子树中每个节点的值都大于或等于该节点的值。 为了更好地理解这种关系,将这种关系形象化是非常有帮助的:
|
||||
|
||||
<div style='width: 100%; display: flex; justify-content: center; align-items: center;'><img style='width: 100%; max-width: 350px; background-color: var(--gray-05);' src='https://user-images.githubusercontent.com/18563015/32136009-1e665d98-bbd6-11e7-9133-63184f9f9182.png'></div>
|
||||
|
||||
Now this ordered relationship is very easy to see. Note that every value to the left of 8, the root node, is less than 8, and every value to the right is greater than 8. Also notice that this relationship applies to each of the subtrees as well. For example, the first left child is a subtree. 3 is the parent node, and it has exactly two child nodes — by the rules governing binary search trees, we know without even looking that the left child of this node (and any of its children) will be less than 3, and the right child (and any of its children) will be greater than 3 (but also less than the structure's root value), and so on.
|
||||
现在,这种有序的关系是非常容易看到的。 注意,根节点 8 左边的每个值都小于 8,右边的每个值都大于 8。 还要注意的是,这种关系也适用于每个子树。 例如,第一个左孩子节点是一个子树。 3 是父节点,它正好有两个子节点——根据二进制搜索树的规则,我们甚至不用看就知道这个节点的左子节点(以及它的任何子节点)都将小于 3,右子节点(以及它的任何子节点)都将大于 3(但也小于根结点的值),依此类推。
|
||||
|
||||
Binary search trees are very common and useful data structures because they provide logarithmic time in the average case for several common operations such as lookup, insertion, and deletion.
|
||||
二叉搜索树是非常常见且有用的数据结构,因为它们在几种常见操作(例如查找、插入和删除)的平均情况下提供对数的时间复杂度。
|
||||
|
||||
# --instructions--
|
||||
|
||||
We'll start simple. We've defined the skeleton of a binary search tree structure here in addition to a function to create nodes for our tree. Observe that each node may have a left and right value. These will be assigned child subtrees if they exist. In our binary search tree, you will create a method to add new values to our binary search tree. The method should be called `add` and it should accept an integer value to add to the tree. Take care to maintain the invariant of a binary search tree: the value in each left child should be less than or equal to the parent value, and the value in each right child should be greater than or equal to the parent value. Here, let's make it so our tree cannot hold duplicate values. If we try to add a value that already exists, the method should return `null`. Otherwise, if the addition is successful, `undefined` should be returned.
|
||||
我们将从简单的内容开始。 我们在这里定义了一个二叉搜索树结构的骨架,此外还有一个为我们的树创建节点的函数。 注意观察每个节点可能有一个左值和右值。 如果子树存在,它们将被分配给对应的子树。 在我们的二叉搜索树中,你将创建一个方法来向我们的二叉搜索树添加新的值。 该方法应该被称为`add` ,它应该接受一个整数值来添加到树中。 注意保持二叉搜索树的不变量:每个左子项中的值应小于或等于父值,并且每个右子项中的值应大于或等于父值。 在这里,让我们确保我们的树不会含有重复的值。 如果我们尝试添加已存在的值,则该方法应返回`null` 。 否则,如果添加成功,则应返回`undefined` 。
|
||||
|
||||
**Hint:** trees are naturally recursive data structures!
|
||||
**提示:** 树是自然的递归数据结构!
|
||||
|
||||
# --hints--
|
||||
|
||||
The `BinarySearchTree` data structure should exist.
|
||||
存在 `BinarySearchTree` 的数据结构。
|
||||
|
||||
```js
|
||||
assert(
|
||||
@@ -42,7 +42,7 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
The binary search tree should have a method called `add`.
|
||||
二叉搜索树应该有一个名为 `add` 的方法。
|
||||
|
||||
```js
|
||||
assert(
|
||||
@@ -58,7 +58,7 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
The add method should add elements according to the binary search tree rules.
|
||||
添加的方法应该根据二叉搜索树的规则来添加元素。
|
||||
|
||||
```js
|
||||
assert(
|
||||
@@ -87,7 +87,7 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
Adding an element that already exists should return `null`.
|
||||
添加一个已经存在的元素应该返回 `null`。
|
||||
|
||||
```js
|
||||
assert(
|
||||
|
Reference in New Issue
Block a user