fix: QA/Infosec update and python to chinese
This commit is contained in:
committed by
Mrugesh Mohapatra
parent
2c78402837
commit
1cfa09adc4
@ -0,0 +1,80 @@
|
||||
---
|
||||
id: a3f503de51cf954ede28891d
|
||||
title: Find the Symmetric Difference
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
localeTitle: 找到对称差异
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">创建一个带有两个或更多数组的函数,并返回所提供数组的<dfn>对称差</dfn> ( <code>△</code>或<code>⊕</code> )数组。给定两个集合(例如集合<code>A = {1, 2, 3}</code>并且集合<code>B = {2, 3, 4}</code> ),两个集合的数学术语“对称差异”是在任一集合中的元素集合。两组,但两者都没有( <code>A △ B = C = {1, 4}</code> )。对于你所采取的每一个额外的对称差异(比如在集合<code>D = {2, 3}</code> ),你应该得到具有两个集合中的任何一个但不是两个集合的元素的集合( <code>C △ D = {1, 4} △ {2, 3} = {1, 2, 3, 4}</code> )。结果数组必须仅包含唯一值( <em>不重复</em> )。如果卡住,请记得使用<a href="https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514" target="_blank">Read-Search-Ask</a> 。尝试配对程序。编写自己的代码。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: '<code>sym([1, 2, 3], [5, 2, 1, 4])</code>应返回<code>[3, 4, 5]</code> 。'
|
||||
testString: assert.sameMembers(sym([1, 2, 3], [5, 2, 1, 4]), [3, 4, 5]);
|
||||
- text: '<code>sym([1, 2, 3], [5, 2, 1, 4])</code>应仅包含三个元素。'
|
||||
testString: assert.equal(sym([1, 2, 3], [5, 2, 1, 4]).length, 3);
|
||||
- text: '<code>sym([1, 2, 3, 3], [5, 2, 1, 4])</code>应该返回<code>[3, 4, 5]</code> 。'
|
||||
testString: assert.sameMembers(sym([1, 2, 3, 3], [5, 2, 1, 4]), [3, 4, 5]);
|
||||
- text: '<code>sym([1, 2, 3, 3], [5, 2, 1, 4])</code>应仅包含三个元素。'
|
||||
testString: assert.equal(sym([1, 2, 3, 3], [5, 2, 1, 4]).length, 3);
|
||||
- text: '<code>sym([1, 2, 3], [5, 2, 1, 4, 5])</code>应返回<code>[3, 4, 5]</code> 。'
|
||||
testString: assert.sameMembers(sym([1, 2, 3], [5, 2, 1, 4, 5]), [3, 4, 5]);
|
||||
- text: '<code>sym([1, 2, 3], [5, 2, 1, 4, 5])</code>应仅包含三个元素。'
|
||||
testString: assert.equal(sym([1, 2, 3], [5, 2, 1, 4, 5]).length, 3);
|
||||
- text: '<code>sym([1, 2, 5], [2, 3, 5], [3, 4, 5])</code>应该返回<code>[1, 4, 5]</code>'
|
||||
testString: assert.sameMembers(sym([1, 2, 5], [2, 3, 5], [3, 4, 5]), [1, 4, 5]);
|
||||
- text: '<code>sym([1, 2, 5], [2, 3, 5], [3, 4, 5])</code>应仅包含三个元素。'
|
||||
testString: assert.equal(sym([1, 2, 5], [2, 3, 5], [3, 4, 5]).length, 3);
|
||||
- text: '<code>sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5])</code>应该返回<code>[1, 4, 5]</code> 。'
|
||||
testString: assert.sameMembers(sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]), [1, 4, 5]);
|
||||
- text: '<code>sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5])</code>应仅包含三个元素。'
|
||||
testString: assert.equal(sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]).length, 3);
|
||||
- text: '<code>sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3])</code>应该返回<code>[2, 3, 4, 6, 7]</code> <code>sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3])</code> <code>[2, 3, 4, 6, 7]</code> 。'
|
||||
testString: assert.sameMembers(sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]), [2, 3, 4, 6, 7]);
|
||||
- text: '<code>sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3])</code>应仅包含五个元素。'
|
||||
testString: assert.equal(sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]).length, 5);
|
||||
- text: '<code>sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1])</code>应该返回<code>[1, 2, 4, 5, 6, 7, 8, 9]</code> 。'
|
||||
testString: assert.sameMembers(sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1]), [1, 2, 4, 5, 6, 7, 8, 9]);
|
||||
- text: '<code>sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1])</code>应该只包含八个元素。'
|
||||
testString: assert.equal(sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1]).length, 8);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function sym(args) {
|
||||
return args;
|
||||
}
|
||||
|
||||
sym([1, 2, 3], [5, 2, 1, 4]);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,72 @@
|
||||
---
|
||||
id: 8d5123c8c441eddfaeb5bdef
|
||||
title: Implement Bubble Sort
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 实施冒泡排序
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">这是排序算法的几个挑战中的第一个。给定一组未排序的项目,我们希望能够返回已排序的数组。我们将看到几种不同的方法来实现这一点,并学习这些不同方法之间的一些权衡。虽然大多数现代语言都有这样的操作的内置排序方法,但了解一些常见的基本方法并了解如何实现它们仍然很重要。在这里我们将看到冒泡排序。冒泡排序方法从未排序数组的开头开始,并向末端“冒泡”未排序的值,遍历数组直到它完全排序。它通过比较相邻的项目并在它们出现故障时交换它们来完成此操作。该方法继续循环遍历数组,直到没有发生交换,此时数组被排序。该方法需要通过阵列进行多次迭代,并且对于平均和最差情况,具有二次时间复杂度。虽然简单,但在大多数情况下通常都是不切实际的。 <strong>说明:</strong>编写一个函数<code>bubbleSort</code> ,它将整数数组作为输入,并按从最小到最大的排序顺序返回这些整数的数组。 <strong>注意:</strong> <br>我们从幕后调用这个功能;我们使用的测试数组在编辑器中被注释掉了。尝试记录<code>array</code>以查看您的排序算法! </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>bubbleSort</code>是一个功能。
|
||||
testString: assert(typeof bubbleSort == 'function');
|
||||
- text: <code>bubbleSort</code>返回一个已排序的数组(从最小到最大)。
|
||||
testString: assert(isSorted(bubbleSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92])));
|
||||
- text: <code>bubbleSort</code>返回一个除订单外没有变化的数组。
|
||||
testString: assert.sameMembers(bubbleSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]), [1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]);
|
||||
- text: <code>bubbleSort</code>不应使用内置的<code>.sort()</code>方法。
|
||||
testString: assert.strictEqual(code.search(/\.sort\(/), -1);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function bubbleSort(array) {
|
||||
// change code below this line
|
||||
|
||||
// change code above this line
|
||||
return array;
|
||||
}
|
||||
|
||||
// test array:
|
||||
// [1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,72 @@
|
||||
---
|
||||
id: 587d8259367417b2b2512c86
|
||||
title: Implement Insertion Sort
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 实现插入排序
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">我们将看到的下一个排序方法是插入排序。此方法通过在列表的开头构建排序数组来工作。它以第一个元素开始排序数组。然后它检查下一个元素并将其向后交换到已排序的数组,直到它处于排序位置。它继续遍历列表并将新项目向后交换到已排序的部分,直到它到达结尾。该算法在平均和最差情况下具有二次时间复杂度。 <strong>说明:</strong>编写一个函数<code>insertionSort</code> ,它将一个整数数组作为输入,并按照从最小到最大的排序顺序返回这些整数的数组。 <strong>注意:</strong> <br>我们从幕后调用这个功能;我们使用的测试数组在编辑器中被注释掉了。尝试记录<code>array</code>以查看您的排序算法! </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>insertionSort</code>是一个函数。
|
||||
testString: assert(typeof insertionSort == 'function');
|
||||
- text: <code>insertionSort</code>返回一个排序数组(从最小到最大)。
|
||||
testString: assert(isSorted(insertionSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92])));
|
||||
- text: <code>insertionSort</code>返回一个除订单外没有变化的数组。
|
||||
testString: assert.sameMembers(insertionSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]), [1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]);
|
||||
- text: <code>insertionSort</code>不应使用内置的<code>.sort()</code>方法。
|
||||
testString: assert.strictEqual(code.search(/\.sort\(/), -1);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function insertionSort(array) {
|
||||
// change code below this line
|
||||
|
||||
// change code above this line
|
||||
return array;
|
||||
}
|
||||
|
||||
// test array:
|
||||
// [1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,72 @@
|
||||
---
|
||||
id: 587d825c367417b2b2512c8f
|
||||
title: Implement Merge Sort
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 实现合并排序
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">另一种非常常见的中间排序算法是合并排序。像快速排序一样,合并排序也使用分而治之的递归方法对数组进行排序。它利用了这样一个事实:只要每个数组首先排序,就可以相对容易地对两个数组进行排序。但是我们只从一个数组作为输入开始,那么我们如何从中获得两个排序的数组呢?好吧,我们可以递归地将原始输入分成两部分,直到我们到达具有一个项目的数组的基本情况。单项数组是自然排序的,因此我们可以开始组合。这个组合将展开拆分原始数组的递归调用,最终生成所有元素的最终排序数组。然后,合并排序的步骤是: <strong>1)</strong>递归地将输入数组拆分为一半,直到产生仅具有一个元素的子数组。 <strong>2)将</strong>每个排序的子数组合并在一起以产生最终的排序数组。合并排序是一种有效的排序方法,时间复杂度为<i>O(nlog(n))</i> 。该算法很受欢迎,因为它性能高且易于实现。顺便说一句,这将是我们在此处介绍的最后一种排序算法。但是,稍后在关于树数据结构的部分中,我们将描述堆排序,这是另一种在其实现中需要二进制堆的有效排序方法。 <strong>说明:</strong>编写一个函数<code>mergeSort</code> ,它以整数数组作为输入,并按从最小到最大的排序顺序返回这些整数的数组。实现这一点的一个好方法是编写一个函数,例如<code>merge</code> ,它负责合并两个已排序的数组,另一个函数,例如<code>mergeSort</code> ,它负责递归,生成单项数组以提供给merge。祝你好运! <strong>注意:</strong> <br>我们从幕后调用这个功能;我们使用的测试数组在编辑器中被注释掉了。尝试记录<code>array</code>以查看您的排序算法! </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>mergeSort</code>是一个函数。
|
||||
testString: assert(typeof mergeSort == 'function');
|
||||
- text: <code>mergeSort</code>返回一个排序数组(从最小到最大)。
|
||||
testString: assert(isSorted(mergeSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92])));
|
||||
- text: <code>mergeSort</code>返回一个除订单外没有变化的数组。
|
||||
testString: assert.sameMembers(mergeSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]), [1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]);
|
||||
- text: <code>mergeSort</code>不应使用内置的<code>.sort()</code>方法。
|
||||
testString: assert.strictEqual(code.search(/\.sort\(/), -1);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function mergeSort(array) {
|
||||
// change code below this line
|
||||
|
||||
// change code above this line
|
||||
return array;
|
||||
}
|
||||
|
||||
// test array:
|
||||
// [1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,72 @@
|
||||
---
|
||||
id: 587d825a367417b2b2512c89
|
||||
title: Implement Quick Sort
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 实施快速排序
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">在这里,我们将继续讨论中间排序算法:快速排序。快速排序是对数组进行排序的一种有效的,递归的分而治之的方法。在此方法中,在原始数组中选择了一个数据透视值。然后将该数组分成两个小于和大于数值的子数组。然后,我们在两个子阵列上结合递归调用快速排序算法的结果。这一直持续到达到空或单项数组的基本情况,我们返回。递归调用的展开将返回已排序的数组。快速排序是一种非常有效的排序方法,平均提供<i>O(nlog(n))</i>性能。它也相对容易实现。这些属性使其成为一种流行且有用的排序方法。 <strong>说明:</strong>编写一个函数<code>quickSort</code> ,它将整数数组作为输入,并按从最小到最大的排序顺序返回这些整数的数组。虽然枢轴值的选择很重要,但任何支点都可以用于我们的目的。为简单起见,可以使用第一个或最后一个元素。 <strong>注意:</strong> <br>我们从幕后调用这个功能;我们使用的测试数组在编辑器中被注释掉了。尝试记录<code>array</code>以查看您的排序算法! </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>quickSort</code>是一个功能。
|
||||
testString: assert(typeof quickSort == 'function');
|
||||
- text: <code>quickSort</code>返回一个排序数组(从最小到最大)。
|
||||
testString: assert(isSorted(quickSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92])));
|
||||
- text: <code>quickSort</code>返回一个除订单外没有变化的数组。
|
||||
testString: assert.sameMembers(quickSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]), [1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]);
|
||||
- text: <code>quickSort</code>不应使用内置的<code>.sort()</code>方法。
|
||||
testString: assert.strictEqual(code.search(/\.sort\(/), -1);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function quickSort(array) {
|
||||
// change code below this line
|
||||
|
||||
// change code above this line
|
||||
return array;
|
||||
}
|
||||
|
||||
// test array:
|
||||
// [1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,72 @@
|
||||
---
|
||||
id: 587d8259367417b2b2512c85
|
||||
title: Implement Selection Sort
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 实施选择排序
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">这里我们将实现选择排序。选择排序的工作原理是选择列表中的最小值并使用列表中的第一个值进行交换。然后它从第二个位置开始,选择剩余列表中的最小值,并将其与第二个元素交换。它继续遍历列表并交换元素,直到它到达列表的末尾。现在列表已排序。在所有情况下,选择排序都具有二次时间复杂度。 <strong>说明</strong> :编写一个函数<code>selectionSort</code> ,它将一个整数数组作为输入,并按照从最小到最大的排序顺序返回这些整数的数组。 <strong>注意:</strong> <br>我们从幕后调用这个功能;我们使用的测试数组在编辑器中被注释掉了。尝试记录<code>array</code>以查看您的排序算法! </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>selectionSort</code>是一个函数。
|
||||
testString: assert(typeof selectionSort == 'function');
|
||||
- text: <code>selectionSort</code>返回一个排序数组(从最小到最大)。
|
||||
testString: assert(isSorted(selectionSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92])));
|
||||
- text: <code>selectionSort</code>返回一个除订单外没有变化的数组。
|
||||
testString: assert.sameMembers(selectionSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]), [1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]);
|
||||
- text: <code>selectionSort</code>不应使用内置的<code>.sort()</code>方法。
|
||||
testString: assert.strictEqual(code.search(/\.sort\(/), -1);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function selectionSort(array) {
|
||||
// change code below this line
|
||||
|
||||
// change code above this line
|
||||
return array;
|
||||
}
|
||||
|
||||
// test array:
|
||||
// [1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,80 @@
|
||||
---
|
||||
id: a56138aff60341a09ed6c480
|
||||
title: Inventory Update
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
localeTitle: 库存更新
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">比较并更新存储在2D阵列中的库存与新交付的第二个2D阵列。更新当前现有库存物料数量(在<code>arr1</code> )。如果找不到商品,请将新商品和数量添加到库存数组中。返回的库存数组应按项目的字母顺序排列。如果卡住,请记得使用<a href="https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514" target="_blank">Read-Search-Ask</a> 。尝试配对程序。编写自己的代码。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 函数<code>updateInventory</code>应该返回一个数组。
|
||||
testString: assert.isArray(updateInventory([[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]], [[2, "Hair Pin"], [3, "Half-Eaten Apple"], [67, "Bowling Ball"], [7, "Toothpaste"]]));
|
||||
- text: <code>updateInventory([[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]], [[2, "Hair Pin"], [3, "Half-Eaten Apple"], [67, "Bowling Ball"], [7, "Toothpaste"]])</code>应该返回一个长度为6的数组。
|
||||
testString: assert.equal(updateInventory([[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]], [[2, "Hair Pin"], [3, "Half-Eaten Apple"], [67, "Bowling Ball"], [7, "Toothpaste"]]).length, 6);
|
||||
- text: <code>updateInventory([[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]], [[2, "Hair Pin"], [3, "Half-Eaten Apple"], [67, "Bowling Ball"], [7, "Toothpaste"]])</code>应返回<code>[[88, "Bowling Ball"], [2, "Dirty Sock"], [3, "Hair Pin"], [3, "Half-Eaten Apple"], [5, "Microphone"], [7, "Toothpaste"]]</code> 。
|
||||
testString: assert.deepEqual(updateInventory([[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]], [[2, "Hair Pin"], [3, "Half-Eaten Apple"], [67, "Bowling Ball"], [7, "Toothpaste"]]), [[88, "Bowling Ball"], [2, "Dirty Sock"], [3, "Hair Pin"], [3, "Half-Eaten Apple"], [5, "Microphone"], [7, "Toothpaste"]]);
|
||||
- text: <code>updateInventory([[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]], [])</code>应该返回<code>[[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]]</code> 。
|
||||
testString: assert.deepEqual(updateInventory([[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]], []), [[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]]);
|
||||
- text: <code>updateInventory([], [[2, "Hair Pin"], [3, "Half-Eaten Apple"], [67, "Bowling Ball"], [7, "Toothpaste"]])</code>应该返回<code>[[67, "Bowling Ball"], [2, "Hair Pin"], [3, "Half-Eaten Apple"], [7, "Toothpaste"]]</code> 。
|
||||
testString: assert.deepEqual(updateInventory([], [[2, "Hair Pin"], [3, "Half-Eaten Apple"], [67, "Bowling Ball"], [7, "Toothpaste"]]), [[67, "Bowling Ball"], [2, "Hair Pin"], [3, "Half-Eaten Apple"], [7, "Toothpaste"]]);
|
||||
- text: <code>updateInventory([[0, "Bowling Ball"], [0, "Dirty Sock"], [0, "Hair Pin"], [0, "Microphone"]], [[1, "Hair Pin"], [1, "Half-Eaten Apple"], [1, "Bowling Ball"], [1, "Toothpaste"]])</code>应返回<code>[[1, "Bowling Ball"], [0, "Dirty Sock"], [1, "Hair Pin"], [1, "Half-Eaten Apple"], [0, "Microphone"], [1, "Toothpaste"]]</code> 。
|
||||
testString: assert.deepEqual(updateInventory([[0, "Bowling Ball"], [0, "Dirty Sock"], [0, "Hair Pin"], [0, "Microphone"]], [[1, "Hair Pin"], [1, "Half-Eaten Apple"], [1, "Bowling Ball"], [1, "Toothpaste"]]), [[1, "Bowling Ball"], [0, "Dirty Sock"], [1, "Hair Pin"], [1, "Half-Eaten Apple"], [0, "Microphone"], [1, "Toothpaste"]]);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function updateInventory(arr1, arr2) {
|
||||
// All inventory must be accounted for or you're fired!
|
||||
return arr1;
|
||||
}
|
||||
|
||||
// Example inventory lists
|
||||
var curInv = [
|
||||
[21, "Bowling Ball"],
|
||||
[2, "Dirty Sock"],
|
||||
[1, "Hair Pin"],
|
||||
[5, "Microphone"]
|
||||
];
|
||||
|
||||
var newInv = [
|
||||
[2, "Hair Pin"],
|
||||
[3, "Half-Eaten Apple"],
|
||||
[67, "Bowling Ball"],
|
||||
[7, "Toothpaste"]
|
||||
];
|
||||
|
||||
updateInventory(curInv, newInv);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,72 @@
|
||||
---
|
||||
id: a7bf700cd123b9a54eef01d5
|
||||
title: No Repeats Please
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
localeTitle: 请不要重复
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">返回没有重复连续字母的提供字符串的总排列数。假设提供的字符串中的所有字符都是唯一的。例如, <code>aab</code>应该返回2,因为它总共有6个排列( <code>aab</code> , <code>aab</code> , <code>aba</code> , <code>aba</code> , <code>baa</code> , <code>baa</code> ),但只有2个( <code>aba</code>和<code>aba</code> )没有相同的字母(在这种情况下为<code>a</code> )重复。如果卡住,请记得使用<a href="https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514" target="_blank">Read-Search-Ask</a> 。尝试配对程序。编写自己的代码。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>permAlone("aab")</code>应返回一个数字。
|
||||
testString: assert.isNumber(permAlone('aab'));
|
||||
- text: <code>permAlone("aab")</code>应返回2。
|
||||
testString: assert.strictEqual(permAlone('aab'), 2);
|
||||
- text: <code>permAlone("aaa")</code>应该返回0。
|
||||
testString: assert.strictEqual(permAlone('aaa'), 0);
|
||||
- text: <code>permAlone("aabb")</code>应该返回8。
|
||||
testString: assert.strictEqual(permAlone('aabb'), 8);
|
||||
- text: <code>permAlone("abcdefa")</code>应返回3600。
|
||||
testString: assert.strictEqual(permAlone('abcdefa'), 3600);
|
||||
- text: <code>permAlone("abfdefa")</code>应返回2640。
|
||||
testString: assert.strictEqual(permAlone('abfdefa'), 2640);
|
||||
- text: <code>permAlone("zzzzzzzz")</code>应该返回0。
|
||||
testString: assert.strictEqual(permAlone('zzzzzzzz'), 0);
|
||||
- text: <code>permAlone("a")</code>应返回1。
|
||||
testString: assert.strictEqual(permAlone('a'), 1);
|
||||
- text: <code>permAlone("aaab")</code>应该返回0。
|
||||
testString: assert.strictEqual(permAlone('aaab'), 0);
|
||||
- text: <code>permAlone("aaabb")</code>应该返回12。
|
||||
testString: assert.strictEqual(permAlone('aaabb'), 12);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function permAlone(str) {
|
||||
return str;
|
||||
}
|
||||
|
||||
permAlone('aab');
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,62 @@
|
||||
---
|
||||
id: a3f503de51cfab748ff001aa
|
||||
title: Pairwise
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
localeTitle: 成对
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">给定一个数组<code>arr</code> ,找到其总和等于第二个参数<code>arg</code>元素对,并返回它们的索引之和。您可以使用具有相同数字元素但索引不同的多个对。每对应使用尽可能低的可用指数。一旦元素被使用,它就不能被重用来与另一个元素配对。例如, <code>pairwise([1, 1, 2], 3)</code>使用indice 0处的1而不是indice 1处的1创建一对<code>[2, 1]</code> ,因为0 + 2 <1 + 2。例如, <code>pairwise([7, 9, 11, 13, 15], 20)</code>返回<code>6</code> 。总和为20的对是<code>[7, 13]</code>和<code>[9, 11]</code> 。然后我们可以用它们的索引和值写出数组。 <table class="table"><tbody><tr><th> <strong>指数</strong> </th><th> 0 </th><th> 1 </th><th> 2 </th><th> 3 </th><th> 4 </th></tr><tr><td>值</td><td> 7 </td><td> 9 </td><td> 11 </td><td> 13 </td><td> 15 </td></tr></tbody></table>下面我们将采用相应的索引并添加它们。 7 + 13 = 20→指数0 + 3 = 3 <br> 9 + 11 = 20→指数1 + 2 = 3 <br> 3 + 3 = 6→返回<code>6</code>如果卡住,请记住使用<a href="https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514" target="_blank">Read-Search-Ask</a> 。尝试配对程序。编写自己的代码。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: '<code>pairwise([1, 4, 2, 3, 0, 5], 7)</code>应该返回11。'
|
||||
testString: assert.deepEqual(pairwise([1, 4, 2, 3, 0, 5], 7), 11);
|
||||
- text: '<code>pairwise([1, 3, 2, 4], 4)</code>应该返回1。'
|
||||
testString: assert.deepEqual(pairwise([1, 3, 2, 4], 4), 1);
|
||||
- text: '<code>pairwise([1, 1, 1], 2)</code>应该返回1。'
|
||||
testString: assert.deepEqual(pairwise([1, 1, 1], 2), 1);
|
||||
- text: '<code>pairwise([0, 0, 0, 0, 1, 1], 1)</code>应返回10。'
|
||||
testString: assert.deepEqual(pairwise([0, 0, 0, 0, 1, 1], 1), 10);
|
||||
- text: '<code>pairwise([], 100)</code>应该返回0。'
|
||||
testString: assert.deepEqual(pairwise([], 100), 0);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function pairwise(arr, arg) {
|
||||
return arg;
|
||||
}
|
||||
|
||||
pairwise([1,4,2,3,0,5], 7);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,74 @@
|
||||
---
|
||||
id: 587d8257367417b2b2512c7b
|
||||
title: Add a New Element to a Binary Search Tree
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 将新元素添加到二叉搜索树
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">现在我们已经了解了基础知识,让我们编写一个更复杂的方法。在此挑战中,我们将创建一个向二叉搜索树添加新值的方法。该方法应该被称为<code>add</code> ,它应该接受一个整数值来添加到树中。注意保持二叉搜索树的不变量:每个左子项中的值应小于或等于父值,并且每个右子项中的值应大于或等于父值。在这里,让我们这样做,以便我们的树不能容纳重复的值。如果我们尝试添加已存在的值,则该方法应返回<code>null</code> 。否则,如果添加成功,则应返回<code>undefined</code> 。提示:树是自然递归的数据结构! </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 存在<code>BinarySearchTree</code>数据结构。
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() }; return (typeof test == 'object')})());
|
||||
- text: 二叉搜索树有一个名为<code>add</code>的方法。
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; return (typeof test.add == 'function')})());
|
||||
- text: add方法根据二叉搜索树规则添加元素。
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; if (typeof test.add !== 'function') { return false; }; test.add(4); test.add(1); test.add(7); test.add(87); test.add(34); test.add(45); test.add(73); test.add(8); const expectedResult = [ 1, 4, 7, 8, 34, 45, 73, 87 ]; const result = test.inOrder(); return (expectedResult.toString() === result.toString()); })());
|
||||
- text: 添加已存在的元素将返回<code>null</code>
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; if (typeof test.add !== 'function') { return false; }; test.add(4); return test.add(4) == null; })());
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
var displayTree = (tree) => console.log(JSON.stringify(tree, null, 2));
|
||||
function Node(value) {
|
||||
this.value = value;
|
||||
this.left = null;
|
||||
this.right = null;
|
||||
}
|
||||
function BinarySearchTree() {
|
||||
this.root = null;
|
||||
// change code below this line
|
||||
// change code above this line
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,91 @@
|
||||
---
|
||||
id: 587d8252367417b2b2512c67
|
||||
title: Add Elements at a Specific Index in a Linked List
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 在链接列表中的特定索引处添加元素
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">让我们创建一个addAt(index,element)方法,在给定的索引处添加一个元素。就像我们如何删除给定索引处的元素一样,我们需要在遍历链表时跟踪currentIndex。当currentIndex与给定索引匹配时,我们需要重新分配上一个节点的下一个属性以引用新添加的节点。并且新节点应该引用currentIndex中的下一个节点。回到康加线的例子,一个新人想加入这条线,但他想加入中间。你处于中间位置,所以你要把手从你前面的人身上移开。新人走过去,把手放在你曾经牵过手的那个人身上,现在你已经掌握了新人。说明创建addAt(index,element)方法,该方法在给定索引处添加元素。如果无法添加元素,则返回false。注意请记住检查给定索引是否为负数或者是否长于链接列表的长度。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 当给定索引为0时, <code>addAt</code>方法应重新分配<code>head</code>到新节点。
|
||||
testString: assert((function(){var test = new LinkedList(); test.add('cat'); test.add('dog'); test.addAt(0,'cat'); return test.head().element === 'cat'}()));
|
||||
- text: 对于添加到链接列表的每个新节点, <code>addAt</code>方法应该将链表的长度增加一。
|
||||
testString: assert((function(){var test = new LinkedList(); test.add('cat'); test.add('dog'); test.addAt(0,'cat'); return test.size() === 3}()));
|
||||
- text: 如果无法添加节点,则<code>addAt</code>方法应返回<code>false</code> 。
|
||||
testString: assert((function(){var test = new LinkedList(); test.add('cat'); test.add('dog'); return (test.addAt(4,'cat') === false); }()));
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function LinkedList() {
|
||||
var length = 0;
|
||||
var head = null;
|
||||
|
||||
var Node = function(element){
|
||||
this.element = element;
|
||||
this.next = null;
|
||||
};
|
||||
|
||||
this.size = function(){
|
||||
return length;
|
||||
};
|
||||
|
||||
this.head = function(){
|
||||
return head;
|
||||
};
|
||||
|
||||
this.add = function(element){
|
||||
var node = new Node(element);
|
||||
if (head === null){
|
||||
head = node;
|
||||
} else {
|
||||
var currentNode = head;
|
||||
|
||||
while (currentNode.next) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
|
||||
currentNode.next = node;
|
||||
}
|
||||
length++;
|
||||
};
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
// Only change code above this line
|
||||
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,56 @@
|
||||
---
|
||||
id: 587d8256367417b2b2512c77
|
||||
title: Adjacency List
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 邻接名单
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">图表可以以不同方式表示。这里我们描述一种方法,称为<dfn>邻接列表</dfn> 。邻接列表本质上是项目符号列表,其中左侧是节点,右侧列出它所连接的所有其他节点。下面是邻接列表的表示。 <blockquote> Node1:Node2,Node3 <br> Node2:Node1 <br> Node3:Node1 </blockquote>以上是无向图,因为<code>Node1</code>连接到<code>Node2</code>和<code>Node3</code> ,并且该信息与<code>Node2</code>和<code>Node3</code>显示的连接一致。有向图的邻接列表意味着列表的每一行都显示方向。如果指示上述内容,那么<code>Node2: Node1</code>将意味着有向边缘从<code>Node2</code>指向<code>Node1</code> 。我们可以将上面的无向图表示为邻接列表,方法是将其放在JavaScript对象中。 <blockquote> var undirectedG = { <br>节点1:[“Node2”,“Node3”], <br> Node2:[“Node1”], <br> Node3:[“Node1”] <br> }; </blockquote>这也可以更简单地表示为一个数组,其中节点只有数字而不是字符串标签。 <blockquote> var undirectedGArr = [ <br> [1,2],#Node1 <br> [0],#Node2 <br> [0] #Node3 <br> ]。 </blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">创建一个社交网络作为无向图,其中有4个节点/人名为<code>James</code> , <code>Jill</code> , <code>Jenny</code>和<code>Jeff</code> 。詹姆斯和杰夫,吉尔和珍妮以及杰夫和珍妮之间存在边缘/关系。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>undirectedAdjList</code>应该只包含四个节点。
|
||||
testString: assert(Object.keys(undirectedAdjList).length === 4);
|
||||
- text: <code>Jeff</code>和<code>James</code>之间应该有优势。
|
||||
testString: assert(undirectedAdjList.James.indexOf("Jeff") !== -1 && undirectedAdjList.Jeff.indexOf("James") !== -1);
|
||||
- text: <code>Jill</code>和<code>Jenny</code>之间应该有一个优势。
|
||||
testString: assert(undirectedAdjList.Jill.indexOf("Jenny") !== -1 && undirectedAdjList.Jill.indexOf("Jenny") !== -1);
|
||||
- text: <code>Jeff</code>和<code>Jenny</code>之间应该有优势。
|
||||
testString: assert(undirectedAdjList.Jeff.indexOf("Jenny") !== -1 && undirectedAdjList.Jenny.indexOf("Jeff") !== -1);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
var undirectedAdjList = {
|
||||
};
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,58 @@
|
||||
---
|
||||
id: 587d8256367417b2b2512c78
|
||||
title: Adjacency Matrix
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 邻接矩阵
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">表示图形的另一种方法是将其置于<dfn>邻接矩阵中</dfn> 。 <dfn>邻接矩阵</dfn>是二维(2D)阵列,其中每个嵌套数组具有与外部数组相同数量的元素。换句话说,它是数字的矩阵或网格,其中数字代表边缘。零意味着没有边缘或关系。 <blockquote> 1 2 3 <br> ------ <br> 1 | 0 1 1 <br> 2 | 1 0 0 <br> 3 | 1 0 0 </blockquote>上面是一个非常简单的无向图,其中有三个节点,第一个节点连接到第二个和第三个节点。 <strong>注意</strong> :矩阵顶部和左侧的数字只是节点的标签。下面是同一件事的JavaScript实现。 <blockquote> var adjMat = [ <br> [0,1,1], <br> [1,0,0], <br> [1,0,0] <br> ]。 </blockquote>与邻接列表不同,矩阵的每个“行”必须具有与图中的节点相同数量的元素。这里我们有一个三乘三矩阵,这意味着我们的图中有三个节点。有向图看起来很相似。下面是第一节点具有指向第二节点的边缘,然后第二节点具有指向第三节点的边缘的图。 <blockquote> var adjMatDirected = [ <br> [0,1,0], <br> [0,0,1], <br> [0,0,0] <br> ]。 </blockquote>图形的边缘也可以有权<dfn>重</dfn> 。到目前为止,我们有<dfn>未加权的</dfn>边缘,只有存在和缺少边是二进制( <code>0</code>或<code>1</code> )。根据您的应用,您可以拥有不同的重量。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">创建具有五个节点的无向图的邻接矩阵。该矩阵应该是多维数组。这五个节点在第一和第四节点,第一和第三节点,第三和第五节点以及第四和第五节点之间具有关系。所有边缘权重都是一个。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>undirectedAdjList</code>应该只包含五个节点。
|
||||
testString: assert((adjMatUndirected.length === 5) && adjMatUndirected.map(function(x) { return x.length === 5 }).reduce(function(a, b) { return a && b }) );
|
||||
- text: 第一个和第四个节点之间应该有一条边。
|
||||
testString: assert((adjMatUndirected[0][3] === 1) && (adjMatUndirected[3][0] === 1));
|
||||
- text: 第一个和第三个节点之间应该有一条边。
|
||||
testString: assert((adjMatUndirected[0][2] === 1) && (adjMatUndirected[2][0] === 1));
|
||||
- text: 第三个和第五个节点之间应该有一条边。
|
||||
testString: assert((adjMatUndirected[2][4] === 1) && (adjMatUndirected[4][2] === 1));
|
||||
- text: 第四个和第五个节点之间应该有一条边。
|
||||
testString: assert((adjMatUndirected[3][4] === 1) && (adjMatUndirected[4][3] === 1));
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
var adjMatUndirected = [
|
||||
];
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,76 @@
|
||||
---
|
||||
id: 587d825c367417b2b2512c90
|
||||
title: Breadth-First Search
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 广度优先搜索
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">到目前为止,我们已经学会了创建图表表示的不同方法。现在怎么办?一个自然的问题是图中任何两个节点之间的距离是多少?输入<dfn>图遍历算法</dfn> 。 <dfn>遍历算法</dfn>是遍历或访问图中节点的算法。一种遍历算法是广度优先搜索算法。该算法从一个节点开始,首先访问一个边缘的所有邻居,然后继续访问它们的每个邻居。在视觉上,这就是算法正在做的事情。 <img class="img-responsive" src="https://camo.githubusercontent.com/2f57e6239884a1a03402912f13c49555dec76d06/68747470733a2f2f75706c6f61642e77696b696d656469612e6f72672f77696b6970656469612f636f6d6d6f6e732f342f34362f416e696d617465645f4246532e676966">要实现此算法,您需要输入图形结构和要启动的节点。首先,您需要了解距起始节点的距离。这个你想要开始你所有的距离最初一些大的数字,如<code>Infinity</code> 。这为从起始节点无法访问节点的情况提供了参考。接下来,您将要从开始节点转到其邻居。这些邻居是一个边缘,此时你应该添加一个距离单位到你要跟踪的距离。最后,有助于实现广度优先搜索算法的重要数据结构是队列。这是一个数组,您可以在其中添加元素到一端并从另一端删除元素。这也称为<dfn>FIFO</dfn>或<dfn>先进先出</dfn>数据结构。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">编写一个函数<code>bfs()</code> ,它将邻接矩阵图(二维数组)和节点标签根作为参数。节点标签只是<code>0</code>到<code>n - 1</code>之间节点的整数值,其中<code>n</code>是图中节点的总数。您的函数将输出JavaScript对象键值对与节点及其与根的距离。如果无法到达节点,则其距离应为<code>Infinity</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: '输入图<code>[[0, 1, 0, 0], [1, 0, 1, 0], [0, 1, 0, 1], [0, 0, 1, 0]]</code> ,起始节点为<code>1</code>应该返回<code>{0: 1, 1: 0, 2: 1, 3: 2}</code>'
|
||||
testString: 'assert((function() { var graph = [[0, 1, 0, 0], [1, 0, 1, 0], [0, 1, 0, 1], [0, 0, 1, 0]]; var results = bfs(graph, 1); return isEquivalent(results, {0: 1, 1: 0, 2: 1, 3: 2})})());'
|
||||
- text: '输入图<code>[[0, 1, 0, 0], [1, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 0]]</code> ,起始节点为<code>1</code>应该返回<code>{0: 1, 1: 0, 2: 1, 3: Infinity}</code>'
|
||||
testString: 'assert((function() { var graph = [[0, 1, 0, 0], [1, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 0]]; var results = bfs(graph, 1); return isEquivalent(results, {0: 1, 1: 0, 2: 1, 3: Infinity})})());'
|
||||
- text: '输入图<code>[[0, 1, 0, 0], [1, 0, 1, 0], [0, 1, 0, 1], [0, 0, 1, 0]]</code> ,起始节点为<code>0</code>应该返回<code>{0: 0, 1: 1, 2: 2, 3: 3}</code>'
|
||||
testString: 'assert((function() { var graph = [[0, 1, 0, 0], [1, 0, 1, 0], [0, 1, 0, 1], [0, 0, 1, 0]]; var results = bfs(graph, 0); return isEquivalent(results, {0: 0, 1: 1, 2: 2, 3: 3})})());'
|
||||
- text: '起始节点为<code>0</code>的输入图<code>[[0, 1], [1, 0]]</code>应返回<code>{0: 0, 1: 1}</code>'
|
||||
testString: 'assert((function() { var graph = [[0, 1], [1, 0]]; var results = bfs(graph, 0); return isEquivalent(results, {0: 0, 1: 1})})());'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function bfs(graph, root) {
|
||||
// Distance object returned
|
||||
var nodesLen = {};
|
||||
|
||||
return nodesLen;
|
||||
};
|
||||
|
||||
var exBFSGraph = [
|
||||
[0, 1, 0, 0],
|
||||
[1, 0, 1, 0],
|
||||
[0, 1, 0, 1],
|
||||
[0, 0, 1, 0]
|
||||
];
|
||||
console.log(bfs(exBFSGraph, 3));
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,74 @@
|
||||
---
|
||||
id: 587d8257367417b2b2512c7c
|
||||
title: Check if an Element is Present in a Binary Search Tree
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 检查二进制搜索树中是否存在元素
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">现在我们对二进制搜索树有了一般意义,让我们更详细地讨论它。二进制搜索树为平均情况下的查找,插入和删除的常见操作提供对数时间,并且在最坏情况下提供线性时间。为什么是这样?这些基本操作中的每一个都要求我们在树中找到一个项目(或者在插入的情况下找到它应该去的地方),并且由于每个父节点处的树结构,我们向左或向右分支并且有效地排除了一半的大小剩下的树。这使得搜索与树中节点数的对数成比例,这在平均情况下为这些操作创建对数时间。好的,但最坏的情况呢?那么,可考虑从以下值建构一棵树,将它们从左至右: <code>10</code> , <code>12</code> , <code>17</code> , <code>25</code> 。根据我们的规则二叉搜索树,我们将增加<code>12</code>到右侧<code>10</code> , <code>17</code> ,以这样的权利,以及<code>25</code>到这一权利。现在我们的树类似于一个链表,并且遍历它以找到<code>25</code>将要求我们以线性方式遍历所有项目。因此,在最坏的情况下,线性时间。这里的问题是树是不平衡的。我们将更多地了解这在以下挑战中意味着什么。说明:在此挑战中,我们将为树创建一个实用程序。编写一个方法<code>isPresent</code> ,它接受一个整数值作为输入,并在二叉搜索树中返回该值是否存在的布尔值。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 存在<code>BinarySearchTree</code>数据结构。
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() }; return (typeof test == 'object')})());
|
||||
- text: 二叉搜索树有一个名为<code>isPresent</code>的方法。
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; return (typeof test.isPresent == 'function')})());
|
||||
- text: <code>isPresent</code>方法正确检查添加到树中的元素是否存在。
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; if (typeof test.isPresent !== 'function') { return false; }; test.add(4); test.add(7); test.add(411); test.add(452); return ( test.isPresent(452) && test.isPresent(411) && test.isPresent(7) && !test.isPresent(100) ); })());
|
||||
- text: <code>isPresent</code>处理树为空的情况。
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; if (typeof test.isPresent !== 'function') { return false; }; return test.isPresent(5) == false; })());
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
var displayTree = (tree) => console.log(JSON.stringify(tree, null, 2));
|
||||
function Node(value) {
|
||||
this.value = value;
|
||||
this.left = null;
|
||||
this.right = null;
|
||||
}
|
||||
function BinarySearchTree() {
|
||||
this.root = null;
|
||||
// change code below this line
|
||||
// change code above this line
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,89 @@
|
||||
---
|
||||
id: 587d8255367417b2b2512c75
|
||||
title: Create a Circular Queue
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 创建循环队列
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">在此挑战中,您将创建一个循环队列。循环队列基本上是一个写入集合末尾的队列,然后开始在集合开头写自己。这种类型的数据结构在某些情况下具有一些有用的应用。例如,循环队列可用于流媒体。队列填满后,新媒体数据就会开始覆盖旧数据。说明这个概念的一个好方法是使用数组: <blockquote> [1,2,3,4,5] <br> ^读@ 0 <br> ^写@ 0 </blockquote>这里的读写都在<code>0</code> 。现在队列获得3个新记录<code>a</code> , <code>b</code>和<code>c</code> 。我们的队列现在看起来像: <blockquote> [a,b,c,4,5] <br> ^读@ 0 <br> ^写@ 3 </blockquote>当读头读取时,它可以删除值或保留它们: <blockquote> [null,null,null,4,5] <br> ^阅读@ 3 <br> ^写@ 3 </blockquote>一旦写入到达数组的末尾,它就会循环回到开头: <blockquote> [f,null,null,d,e] <br> ^阅读@ 3 <br> ^写@ 1 </blockquote>这种方法需要恒定的内存量,但允许处理更大尺寸的文件。说明:在此挑战中,我们将实现循环队列。循环队列应提供<code>enqueue</code>和<code>dequeue</code>方法,允许您读取和写入队列。类本身也应该接受一个整数,您可以使用该整数在创建队列时指定队列的大小。我们已经在代码编辑器中为您编写了此类的起始版本。将项目排入队列时,写入指针应向前推进,并在到达队列末尾时循环回到开头。同样,当您使项目出列时,读指针应向前推进。不应允许写指针移过读指针(我们的类不会让你覆盖你还没有读过的数据),并且读指针不能超过你写的数据。此外,如果成功,则<code>enqueue</code>方法应返回您入<code>enqueue</code>的项,否则返回<code>null</code> 。类似地,当你使一个项目出列时,它应该被返回,如果你不能出列,你应该返回<code>null</code> 。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>enqueue</code>方法将项添加到循环队列。
|
||||
testString: assert((function(){ var test = new CircularQueue(3); test.enqueue(17); test.enqueue(32); test.enqueue(591); var print = test.print(); return print[0] === 17 && print[1] === 32 && print[2] === 591; })());
|
||||
- text: 您不能通过读指针将项排入队列。
|
||||
testString: assert((function(){ var test = new CircularQueue(3); test.enqueue(17); test.enqueue(32); test.enqueue(591); test.enqueue(13); test.enqueue(25); test.enqueue(59); var print = test.print(); return print[0] === 17 && print[1] === 32 && print[2] === 591; })());
|
||||
- text: <code>dequeue</code>方法使队列中的项目出列。
|
||||
testString: assert((function(){ var test = new CircularQueue(3); test.enqueue(17); test.enqueue(32); test.enqueue(591); return test.dequeue() === 17 && test.dequeue() === 32 && test.dequeue() === 591; })());
|
||||
- text: 项目出列后,其队列中的位置应重置为<code>null</code> 。
|
||||
testString: assert((function(){ var test = new CircularQueue(3); test.enqueue(17); test.enqueue(32); test.enqueue(672); test.dequeue(); test.dequeue(); var print = test.print(); return print[0] === null && print[1] === null && print[2] === 672; })());
|
||||
- text: 尝试通过写指针出列队列返回<code>null</code>并且不会使写指针前进。
|
||||
testString: assert((function(){ var test = new CircularQueue(3); test.enqueue(17); test.enqueue(32); test.enqueue(591); return test.dequeue() === 17 && test.dequeue() === 32 && test.dequeue() === 591 && test.dequeue() === null && test.dequeue() === null && test.dequeue() === null && test.dequeue() === null && test.enqueue(100) === 100 && test.dequeue() === 100; })());
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
class CircularQueue {
|
||||
constructor(size) {
|
||||
|
||||
this.queue = [];
|
||||
this.read = 0;
|
||||
this.write = 0;
|
||||
this.max = size - 1;
|
||||
|
||||
while (size > 0) {
|
||||
this.queue.push(null);
|
||||
size--;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
print() {
|
||||
return this.queue;
|
||||
}
|
||||
|
||||
|
||||
enqueue(item) {
|
||||
// Only change code below this line
|
||||
|
||||
// Only change code above this line
|
||||
}
|
||||
|
||||
dequeue() {
|
||||
// Only change code below this line
|
||||
|
||||
// Only change code above this line
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,81 @@
|
||||
---
|
||||
id: 587d825a367417b2b2512c87
|
||||
title: Create a Doubly Linked List
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 创建双向链接列表
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">到目前为止,我们创建的所有链接列表都是单链表。在这里,我们将创建一个<dfn>双向链表</dfn> 。顾名思义,双向链表中的节点引用了列表中的下一个和上一个节点。这允许我们在两个方向上遍历列表,但它还需要使用更多内存,因为每个节点必须包含对列表中前一个节点的附加引用。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">我们提供了一个<code>Node</code>对象并启动了我们的<code>DoublyLinkedList</code> 。让我们将两个方法添加到名为<code>add</code> and <code>remove</code>双向链表<code>remove</code> 。 <code>add</code>方法应该将给定元素添加到列表中,而<code>remove</code>方法应该删除列表中所有出现的给定元素。编写这些方法时要小心处理任何可能的边缘情况,例如删除第一个或最后一个元素。此外,删除空列表中的任何项应返回<code>null</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 存在DoublyLinkedList数据结构。
|
||||
testString: assert((function() { var test = false; if (typeof DoublyLinkedList !== 'undefined') { test = new DoublyLinkedList() }; return (typeof test == 'object')})());
|
||||
- text: DoublyLinkedList有一个名为add的方法。
|
||||
testString: assert((function() { var test = false; if (typeof DoublyLinkedList !== 'undefined') { test = new DoublyLinkedList() }; if (test.add == undefined) { return false; }; return (typeof test.add == 'function')})());
|
||||
- text: DoublyLinkedList有一个名为remove的方法。
|
||||
testString: assert((function() { var test = false; if (typeof DoublyLinkedList !== 'undefined') { test = new DoublyLinkedList() }; if (test.remove == undefined) { return false; }; return (typeof test.remove == 'function')})());
|
||||
- text: 从空列表中删除项目将返回null。
|
||||
testString: assert((function() { var test = false; if (typeof DoublyLinkedList !== 'undefined') { test = new DoublyLinkedList() }; return (test.remove(100) == null); })());
|
||||
- text: add方法将项添加到列表中。
|
||||
testString: assert((function() { var test = false; if (typeof DoublyLinkedList !== 'undefined') { test = new DoublyLinkedList() }; test.add(5); test.add(6); test.add(723); return (test.print().join('') == '56723'); })());
|
||||
- text: 每个节点都跟踪前一个节点。
|
||||
testString: assert((function() { var test = false; if (typeof DoublyLinkedList !== 'undefined') { test = new DoublyLinkedList() }; test.add(50); test.add(68); test.add(73); return (test.printReverse().join('') == '736850'); })());
|
||||
- text: 可以从列表中删除第一个项目。
|
||||
testString: assert((function() { var test = false; if (typeof DoublyLinkedList !== 'undefined') { test = new DoublyLinkedList() }; test.add(25); test.add(35); test.add(60); test.remove(25); return ( test.print().join('') == '3560' ) })());
|
||||
- text: 最后一项可以从列表中删除。
|
||||
testString: assert((function() { var test = false; if (typeof DoublyLinkedList !== 'undefined') { test = new DoublyLinkedList() }; test.add(25); test.add(35); test.add(60); test.remove(60); return ( test.print().join('') == '2535' ) })());
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
var Node = function(data, prev) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = null;
|
||||
};
|
||||
var DoublyLinkedList = function() {
|
||||
this.head = null;
|
||||
this.tail = null;
|
||||
// change code below this line
|
||||
// change code above this line
|
||||
};
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,90 @@
|
||||
---
|
||||
id: 587d825b367417b2b2512c8e
|
||||
title: Create a Hash Table
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 创建一个哈希表
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">在这个挑战中,我们将学习哈希表。哈希表用于实现关联数组或键值对的映射,就像我们刚刚研究的对象和地图一样。例如,JavaScript对象可以实现为哈希表(其实际实现将取决于它运行的环境)。哈希表的工作方式是它接受一个键输入并以确定的方式将此键散列到某个数值。然后将该数值用作存储相关值的实际键。然后,如果您尝试再次访问相同的密钥,则散列函数将处理密钥,返回相同的数字结果,然后将用于查找关联的值。这平均提供非常有效的O(1)查找时间。散列表可以实现为具有散列函数的数组,从而生成指定范围内的数组索引。在这种方法中,数组大小的选择很重要,散列函数也是如此。例如,如果散列函数为两个不同的键生成相同的值,该怎么办?这称为碰撞。处理冲突的一种方法是仅将两个键值对存储在该索引处。然后,在查找其中任何一个时,您将不得不遍历项目桶以找到您要查找的密钥。良好的散列函数可最大限度地减少冲突,从而保持有效的搜索时间。在这里,我们不会关注散列或散列表实现的细节,我们将尝试大致了解它们的工作原理。说明:让我们创建哈希表的基本功能。我们已经创建了一个天真的散列函数供您使用。您可以将字符串值传递给函数哈希,它将返回一个哈希值,您可以将其用作存储键。在this.collection对象中根据此散列值存储项目。创建这三种方法:添加,删除和查找。第一个应该接受一个键值对来添加到哈希表。第二个应该在传递密钥时删除键值对。第三个应该接受一个键并返回相关的值,如果该键不存在则返回null。请务必编写代码以解决冲突! </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 存在HashTable数据结构。
|
||||
testString: assert((function() { var test = false; if (typeof HashTable !== 'undefined') { test = new HashTable() }; return (typeof test === 'object')})());
|
||||
- text: HashTable有一个add方法。
|
||||
testString: assert((function() { var test = false; if (typeof HashTable !== 'undefined') { test = new HashTable() }; return ((typeof test.add) === 'function')})());
|
||||
- text: HashTable有一个删除方法。
|
||||
testString: assert((function() { var test = false; if (typeof HashTable !== 'undefined') { test = new HashTable() }; return ((typeof test.remove) === 'function')})());
|
||||
- text: HashTable有一个查找方法。
|
||||
testString: assert((function() { var test = false; if (typeof HashTable !== 'undefined') { test = new HashTable() }; return ((typeof test.lookup) === 'function')})());
|
||||
- text: add方法添加键值对,lookup方法返回与给定键关联的值。
|
||||
testString: assert((function() { var test = false; if (typeof HashTable !== 'undefined') { test = new HashTable() }; test.add('key', 'value'); return (test.lookup('key') === 'value')})());
|
||||
- text: remove方法接受一个键作为输入,并删除关联的键值对。
|
||||
testString: assert((function(){ var test = false; if (typeof HashTable !== 'undefined') { test = new HashTable() }; test.add('key', 'value'); test.remove('key'); test.lookup = function(key){ var theHash = hash(key); if (this.collection.hasOwnProperty(theHash)[key]) { return this.collection[theHash][key]; } return null }; var lookup = test.lookup('key'); test.lookup = null; return (lookup === null)})());
|
||||
- text: 使用哈希函数添加项目。
|
||||
testString: assert((function() { var test = false; if (typeof HashTable !== 'undefined') { test = new HashTable() }; called = 0; test.add('key1','value1'); test.add('key2','value2'); test.add('key3','value3'); return (called >= 3 && called % 3 === 0)})());
|
||||
- text: 哈希表处理冲突。
|
||||
testString: assert((function() { var test = false; if (typeof HashTable !== 'undefined') { test = new HashTable() }; called = 0; test.add('key1','value1'); test.add('1key','value2'); test.add('ke1y','value3'); return (test.lookup('key1') === 'value1' && test.lookup('1key') == 'value2' && test.lookup('ke1y') == 'value3')})());
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
var called = 0;
|
||||
var hash = (string) => {
|
||||
called++;
|
||||
var hash = 0;
|
||||
for (var i = 0; i < string.length; i++) { hash += string.charCodeAt(i); }
|
||||
return hash;
|
||||
};
|
||||
var HashTable = function() {
|
||||
this.collection = {};
|
||||
// change code below this line
|
||||
// change code above this line
|
||||
};
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
### Before Test
|
||||
<div id='js-setup'>
|
||||
|
||||
```js
|
||||
var called = 0;
|
||||
var hash = (string) => {
|
||||
called++;
|
||||
var hash = 0;
|
||||
for (var i = 0; i < string.length; i++) { hash += string.charCodeAt(i); };
|
||||
return hash;
|
||||
};
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,77 @@
|
||||
---
|
||||
id: 587d8251367417b2b2512c62
|
||||
title: Create a Linked List Class
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 创建链接列表类
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">让我们创建一个<code>linked list</code>类。每个链接列表都应该从一些基本属性开始: <code>head</code> (列表中的第一项)和<code>length</code> (列表中的项目数)。有时您会看到链接列表的实现,其中包含列表的最后一个元素的<code>tail</code> ,但是现在我们将坚持使用这两个元素。每当我们向链表添加元素时,我们的<code>length</code>属性应该加1。我们想要一种方法将项添加到链表中,因此我们要创建的第一种方法是<code>add</code>方法。如果我们的列表为空,那么向链表添加一个元素就足够了:我们只是将该元素包装在<code>Node</code>类中,然后将该节点分配给链表的<code>head</code> 。但是如果我们的名单已经有一个或多个成员呢?我们如何在列表中添加元素?回想一下,链表中的每个节点都有一个<code>next</code>属性。要将节点添加到列表,请在列表中找到最后一个节点,并将该节点的最后<code>next</code>属性指向新节点。 (提示:当节点的<code>next</code>属性为<code>null</code>时,您知道已到达链表的末尾。) </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">编写一个add方法,将您推送到链接列表的第一个节点分配给<code>head</code> ;之后,每当添加一个节点时,每个节点都应该由前一个节点的<code>next</code>属性引用。注意每次将元素添加到链接列表时,列表的<code>length</code>应增加1。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 您的<code>LinkedList</code>类应该有一个<code>add</code>方法。
|
||||
testString: assert((function(){var test = new LinkedList(); return (typeof test.add === 'function')}()));
|
||||
- text: 您的<code>LinkedList</code>类应该为添加的第一个节点分配<code>head</code> 。
|
||||
testString: assert((function(){var test = new LinkedList(); test.add('cat'); return test.head().element === 'cat'}()));
|
||||
- text: <code>LinkedList</code>类中的上一个<code>node</code>应该引用创建的最新节点。
|
||||
testString: assert((function(){var test = new LinkedList(); test.add('cat'); test.add('dog'); return test.head().next.element === 'dog'}()));
|
||||
- text: <code>LinkedList</code>类的<code>size</code>应该等于<code>LinkedList</code>的节点数量。
|
||||
testString: assert((function(){var test = new LinkedList(); test.add('cat'); test.add('dog'); return test.size() === 2}()));
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function LinkedList() {
|
||||
var length = 0;
|
||||
var head = null;
|
||||
|
||||
var Node = function(element){
|
||||
this.element = element;
|
||||
this.next = null;
|
||||
};
|
||||
|
||||
this.head = function(){
|
||||
return head;
|
||||
};
|
||||
|
||||
this.size = function(){
|
||||
return length;
|
||||
};
|
||||
|
||||
this.add = function(element){
|
||||
// Only change code below this line
|
||||
|
||||
// Only change code above this line
|
||||
};
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,66 @@
|
||||
---
|
||||
id: 8d5823c8c441eddfaeb5bdef
|
||||
title: Create a Map Data Structure
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 创建地图数据结构
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">接下来的几个挑战将涵盖地图和哈希表。映射是存储键值对的数据结构。在JavaScript中,我们可以将它们作为对象使用。地图可根据键值快速查找存储的项目,是非常常见且有用的数据结构。说明:让我们开始创建自己的地图。因为JavaScript对象提供了比我们在此处编写的任何内容更有效的地图结构,所以这主要是作为学习练习。但是,JavaScript对象仅向我们提供某些操作。如果我们想定义自定义操作怎么办?使用此处提供的<code>Map</code>对象作为JavaScript <code>object</code>的包装器。在Map对象上创建以下方法和操作: <ul><li> <code>add</code>接受要添加到地图的<code>key, value</code>对。 </li><li> <code>remove</code>接受一个键并删除关联的<code>key, value</code>对</li><li> <code>get</code>接受一个<code>key</code>并返回存储的<code>value</code> </li><li>如果密钥存在<code>has</code>接受<code>key</code>并返回<dfn>true,</dfn>否则返回<dfn>false</dfn> 。 </li><li> <code>values</code>返回地图中所有<code>values</code>的数组</li><li> <code>size</code>返回地图中的项目数</li><li> <code>clear</code>清空地图</li></ul></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 存在地图数据结构。
|
||||
testString: assert((function() { var test = false; if (typeof Map !== 'undefined') { test = new Map() }; return (typeof test == 'object')})());
|
||||
- text: Map对象具有以下方法:add,remove,get,has,values,clear和size。
|
||||
testString: "assert((function() { var test = false; if (typeof Map !== 'undefined') { test = new Map() }; return (typeof test.add == 'function' && typeof test.remove == 'function' && typeof test.get == 'function' && typeof test.has == 'function' && typeof test.values == 'function' && typeof test.clear == 'function' && typeof test.size == 'function')})());"
|
||||
- text: add方法将项添加到地图中。
|
||||
testString: assert((function() { var test = false; if (typeof Map !== 'undefined') { test = new Map() }; test.add(5,6); test.add(2,3); test.add(2,5); return (test.size() == 2)})());
|
||||
- text: has方法对于添加的项返回true,对于缺少的项返回false。
|
||||
testString: assert((function() { var test = false; if (typeof Map !== 'undefined') { test = new Map() }; test.add('test','value'); return (test.has('test') && !test.has('false'))})());
|
||||
- text: get方法接受键作为输入并返回关联的值。
|
||||
testString: assert((function() { var test = false; if (typeof Map !== 'undefined') { test = new Map() }; test.add('abc','def'); return (test.get('abc') == 'def')})());
|
||||
- text: values方法将存储在映射中的所有值作为数组中的字符串返回。
|
||||
testString: assert((function() { var test = false; if (typeof Map !== 'undefined') { test = new Map() }; test.add('a','b'); test.add('c','d'); test.add('e','f'); var vals = test.values(); return (vals.indexOf('b') != -1 && vals.indexOf('d') != -1 && vals.indexOf('f') != -1)})());
|
||||
- text: clear方法清空映射,size方法返回映射中存在的项目数。
|
||||
testString: assert((function() { var test = false; if (typeof Map !== 'undefined') { test = new Map() }; test.add('b','b'); test.add('c','d'); test.remove('asdfas'); var init = test.size(); test.clear(); return (init == 2 && test.size() == 0)})());
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
var Map = function() {
|
||||
this.collection = {};
|
||||
// change code below this line
|
||||
// change code above this line
|
||||
};
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,70 @@
|
||||
---
|
||||
id: 587d8255367417b2b2512c74
|
||||
title: Create a Priority Queue Class
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 创建优先级队列类
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">在此挑战中,您将创建一个优先级队列。优先级队列是一种特殊类型的队列,其中项目可能具有指定其优先级的附加信息。这可以简单地用整数表示。项目优先级将覆盖确定序列项目已出列的放置顺序。如果具有较高优先级的项目在具有较低优先级的项目之后排队,则较高优先级项目将在所有其他项目之前出列。例如,让我们假设我们有一个包含三个项目的优先级队列: <code>[['kitten', 2], ['dog', 2], ['rabbit', 2]]</code>这里第二个值(整数)表示项目优先级。如果我们将优先级为<code>1</code> <code>['human', 1]</code>排入队列(假设优先级较低,则优先级较低),那么它将成为第一个出列的项目。该集合将是这样的: <code>[['human', 1], ['kitten', 2], ['dog', 2], ['rabbit', 2]]</code> 。我们已经开始在代码编辑器中编写<code>PriorityQueue</code> 。您需要添加一个<code>enqueue</code>方法来添加具有优先级的项目,一个用于删除项目的<code>dequeue</code>方法,一个用于返回队列中项目数量的<code>size</code>方法,一个用于返回队列<code>front</code>元素的<code>front</code>方法,以及最后一个<code>isEmpty</code>方法,如果队列为空则返回<code>true</code> ,否则返回<code>false</code> 。入<code>enqueue</code>应接受上面显示格式的项目( <code>['human', 1]</code> ),其中<code>1</code>表示优先级。 <code>dequeue</code>应该只返回当前项目,而不是其优先级。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 您的<code>Queue</code>类应该有一个<code>enqueue</code>方法。
|
||||
testString: assert((function(){var test = new PriorityQueue(); return (typeof test.enqueue === 'function')}()));
|
||||
- text: 您的<code>Queue</code>类应该有一个<code>dequeue</code>方法。
|
||||
testString: assert((function(){var test = new PriorityQueue(); return (typeof test.dequeue === 'function')}()));
|
||||
- text: 您的<code>Queue</code>类应该有一个<code>size</code>方法。
|
||||
testString: assert((function(){var test = new PriorityQueue(); return (typeof test.size === 'function')}()));
|
||||
- text: 您的<code>Queue</code>类应该有一个<code>isEmpty</code>方法。
|
||||
testString: assert((function(){var test = new PriorityQueue(); return (typeof test.isEmpty === 'function')}()));
|
||||
- text: 当项目入队和出列时,您的PriorityQueue应使用<code>size</code>方法正确跟踪当前项目数。
|
||||
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: 当队列为空时, <code>isEmpty</code>方法应该返回<code>true</code> 。
|
||||
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: 优先级队列应该在具有较低优先级的项之前返回具有较高优先级的项,否则以先进先出顺序返回项。
|
||||
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';}()));
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function PriorityQueue () {
|
||||
this.collection = [];
|
||||
this.printCollection = function() {
|
||||
console.log(this.collection);
|
||||
};
|
||||
// Only change code below this line
|
||||
|
||||
// Only change code above this line
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,74 @@
|
||||
---
|
||||
id: 587d8250367417b2b2512c60
|
||||
title: Create a Queue Class
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 创建队列类
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">与堆栈一样,队列是元素的集合。但与堆栈不同,队列遵循FIFO(先入先出)原则。添加到队列的元素将被推送到队列的尾部或末尾,并且只允许删除队列前面的元素。我们可以使用数组来表示队列,但就像堆栈一样,我们希望限制我们对队列的控制量。队列类的两个主要方法是enqueue和dequeue方法。 enqueue方法将元素推送到队列的尾部,dequeue方法移除并返回队列前面的元素。其他有用的方法是front,size和isEmpty方法。说明编写一个将元素推送到队列尾部的入队方法,一个删除并返回前面元素的出列方法,一个让我们看到前面元素的前方法,一个显示长度的大小方法,以及一个isEmpty方法检查队列是否为空。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 您的<code>Queue</code>类应该有一个<code>enqueue</code>方法。
|
||||
testString: assert((function(){var test = new Queue(); return (typeof test.enqueue === 'function')}()));
|
||||
- text: 您的<code>Queue</code>类应该有一个<code>dequeue</code>方法。
|
||||
testString: assert((function(){var test = new Queue(); return (typeof test.dequeue === 'function')}()));
|
||||
- text: 您的<code>Queue</code>类应该有一个<code>front</code>方法。
|
||||
testString: assert((function(){var test = new Queue(); return (typeof test.front === 'function')}()));
|
||||
- text: 您的<code>Queue</code>类应该有一个<code>size</code>方法。
|
||||
testString: assert((function(){var test = new Queue(); return (typeof test.size === 'function')}()));
|
||||
- text: 您的<code>Queue</code>类应该有一个<code>isEmpty</code>方法。
|
||||
testString: assert((function(){var test = new Queue(); return (typeof test.isEmpty === 'function')}()));
|
||||
- text: <code>dequeue</code>方法应该删除并返回队列的前端元素
|
||||
testString: assert((function(){var test = new Queue(); test.enqueue('Smith'); test.enqueue('John'); return (test.dequeue() === 'Smith')}()));
|
||||
- text: <code>front</code>方法应该返回队列的front元素的值
|
||||
testString: assert((function(){var test = new Queue(); test.enqueue('Smith'); test.enqueue('John'); return (test.front() === 'Smith')}()));
|
||||
- text: <code>size</code>方法应该返回队列的长度
|
||||
testString: assert((function(){var test = new Queue(); test.enqueue('Smith'); return (test.size() === 1)}()));
|
||||
- text: 如果队列中有元素,则<code>isEmpty</code>方法应返回<code>false</code>
|
||||
testString: assert((function(){var test = new Queue(); test.enqueue('Smith'); return !(test.isEmpty())}()));
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function Queue () {
|
||||
var collection = [];
|
||||
this.print = function() {
|
||||
console.log(collection);
|
||||
};
|
||||
// Only change code below this line
|
||||
|
||||
// Only change code above this line
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,69 @@
|
||||
---
|
||||
id: 8d1323c8c441eddfaeb5bdef
|
||||
title: Create a Set Class
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 创建一个Set类
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">在接下来的几个练习中,我们将创建一个函数来模拟一个名为“Set”的数据结构。 Set类似于数组,但不能包含重复值。 Set的典型用途是简单地检查项目是否存在。这可以用对象实现,例如: <blockquote> var set = new Object(); <br> set.foo = true; <br> //看看我们的集合中是否存在foo: <br> console.log(set.foo)// true </blockquote>在接下来的几个练习中,我们将从头开始构建一个全功能的Set。对于本练习,只要该值中尚不存在该值,就创建一个将值添加到set集合的函数。例如: <blockquote> this.add = function(element){ <br> //一些代码来为集合添加值<br> } </blockquote>如果成功添加该值,则该函数应返回<code>true</code>否则返回<code>false</code> 。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 您的<code>Set</code>类应该有一个<code>add</code>方法。
|
||||
testString: 'assert((function(){var test = new Set(); return (typeof test.add === "function")}()), "Your <code>Set</code> class should have an <code>add</code> method.");'
|
||||
- text: 您的<code>add</code>方法不应添加重复值。
|
||||
testString: 'assert((function(){var test = new Set(); test.add("a"); test.add("b"); test.add("a"); var vals = test.values(); return (vals[0] === "a" && vals[1] === "b" && vals.length === 2)}()), "Your <code>add</code> method should not add duplicate values.");'
|
||||
- text: 成功添加值后, <code>add</code>方法应返回<code>true</code> 。
|
||||
testString: 'assert((function(){var test = new Set(); var result = test.add("a"); return (result != undefined) && (result === true);}()), "Your <code>add</code> method should return <code>true</code> when a value has been successfully added.");'
|
||||
- text: <code>add</code>重复值时, <code>add</code>方法应返回<code>false</code> 。
|
||||
testString: 'assert((function(){var test = new Set(); test.add("a"); var result = test.add("a"); return (result != undefined) && (result === false);}()), "Your <code>add</code> method should return <code>false</code> when a duplicate value is added.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function Set() {
|
||||
// the var collection will hold our set
|
||||
var collection = [];
|
||||
// this method will check for the presence of an element and return true or false
|
||||
this.has = function(element) {
|
||||
return (collection.indexOf(element) !== -1);
|
||||
};
|
||||
// this method will return all the values in the set
|
||||
this.values = function() {
|
||||
return collection;
|
||||
};
|
||||
// change code below this line
|
||||
// change code above this line
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,74 @@
|
||||
---
|
||||
id: 587d8250367417b2b2512c5f
|
||||
title: Create a Stack Class
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 创建一个堆栈类
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">在上一节中,我们讨论了堆栈是什么以及如何使用数组来表示堆栈。在本节中,我们将创建自己的堆栈类。虽然您可以使用数组来创建堆栈,但有时最好限制我们对堆栈的控制量。除了<code>push</code>和<code>pop</code>方法之外,堆栈还有其他有用的方法。让我们为我们的堆栈类添加一个<code>peek</code> , <code>isEmpty</code>和<code>clear</code>方法。说明编写一个<code>push</code>方法,将元素推送到堆栈顶部,一个<code>pop</code>方法删除堆栈顶部的元素,一个<code>peek</code>堆栈中第一个元素的<code>peek</code>方法,一个<code>isEmpty</code>方法,用于检查是否存在stack是空的,是一个<code>clear</code>堆栈中所有元素的方法。通常堆栈没有这个,但我们添加了一个控制台记录集合的<code>print</code>助手方法。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你的<code>Stack</code>类应该有一个<code>push</code>方法。
|
||||
testString: assert((function(){var test = new Stack(); return (typeof test.push === 'function')}()));
|
||||
- text: 你的<code>Stack</code>类应该有一个<code>pop</code>方法。
|
||||
testString: assert((function(){var test = new Stack(); return (typeof test.pop === 'function')}()));
|
||||
- text: 你的<code>Stack</code>类应该有一个<code>peek</code>方法。
|
||||
testString: assert((function(){var test = new Stack(); return (typeof test.peek === 'function')}()));
|
||||
- text: 您的<code>Stack</code>类应该有一个<code>isEmpty</code>方法。
|
||||
testString: assert((function(){var test = new Stack(); return (typeof test.isEmpty === 'function')}()));
|
||||
- text: 你的<code>Stack</code>类应该有一个<code>clear</code>方法。
|
||||
testString: assert((function(){var test = new Stack(); return (typeof test.clear === 'function')}()));
|
||||
- text: <code>peek</code>方法应该返回堆栈的顶部元素
|
||||
testString: assert((function(){var test = new Stack(); test.push('CS50'); return (test.peek() === 'CS50')}()));
|
||||
- text: <code>pop</code>方法应该删除并返回堆栈的顶部元素
|
||||
testString: assert((function(){var test = new Stack(); test.push('CS50'); return (test.pop() === 'CS50');}()));
|
||||
- text: 如果堆栈不包含任何元素,则<code>isEmpty</code>方法应返回true
|
||||
testString: assert((function(){var test = new Stack(); return test.isEmpty()}()));
|
||||
- text: <code>clear</code>方法应该从堆栈中删除所有元素
|
||||
testString: assert((function(){var test = new Stack(); test.push('CS50'); test.clear(); return (test.isEmpty())}()));
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function Stack() {
|
||||
var collection = [];
|
||||
this.print = function() {
|
||||
console.log(collection);
|
||||
};
|
||||
// Only change code below this line
|
||||
|
||||
// Only change code above this line
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,72 @@
|
||||
---
|
||||
id: 587d8259367417b2b2512c84
|
||||
title: Create a Trie Search Tree
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 创建Trie搜索树
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">在这里,我们将继续从二叉搜索树开始,看看另一种称为trie的树结构。 trie是一种常用于保存字符串的有序搜索树,或者更通用的关联数组或其中键是字符串的动态数据集。当许多键具有重叠前缀时,它们非常擅长存储数据集,例如,字典中的所有单词。与二叉树不同,节点不与实际值相关联。相反,节点的路径表示特定的键。例如,如果我们想将字符串代码存储在trie中,我们将有四个节点,每个字母对应一个节点:c - o - d - e。然后,通过所有这些节点的路径将创建代码作为字符串 - 该路径是我们存储的密钥。然后,如果我们想要添加字符串编码,它将在d之后分支之前共享前三个代码节点。通过这种方式,可以非常紧凑地存储大型数据集。此外,搜索可以非常快,因为它实际上限于您存储的字符串的长度。此外,与二叉树不同,节点可以存储任意数量的子节点。正如您可能从上面的示例中猜到的那样,一些元数据通常存储在保存密钥结尾的节点上,以便在以后的遍历中仍可以检索密钥。例如,如果我们在上面的示例中添加了代码,我们需要某种方式来知道代码中的e代表先前输入的密钥的结尾。否则,当我们添加代码时,这些信息将会丢失。说明:让我们创建一个存储单词的trie。它将通过add方法接受单词并将它们存储在trie数据结构中。它还允许我们查询给定字符串是否是带有isWord方法的单词,并使用print方法检索输入到trie中的所有单词。 isWord应该返回一个布尔值,print应该将所有这些单词的数组作为字符串值返回。为了让我们验证这个数据结构是否正确实现,我们为树中的每个节点提供了一个Node结构。每个节点都是一个具有keys属性的对象,该属性是JavaScript Map对象。这将保存作为每个节点的有效密钥的各个字母。我们还在节点上创建了一个end属性,如果节点表示单词的终止,则可以将其设置为true。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: Trie有一个add方法。
|
||||
testString: assert((function testTrie() { var test = false; if (typeof Trie !== 'undefined') { test = new Trie() } else { return false; }; return (typeof test.add == 'function') }()));
|
||||
- text: Trie有一种打印方法。
|
||||
testString: assert((function testTrie() { var test = false; if (typeof Trie !== 'undefined') { test = new Trie() } else { return false; }; return (typeof test.print == 'function') }()));
|
||||
- text: Trie有一个isWord方法。
|
||||
testString: assert((function testTrie() { var test = false; if (typeof Trie !== 'undefined') { test = new Trie() } else { return false; }; return (typeof test.isWord == 'function') }()));
|
||||
- text: print方法将添加到trie的所有项目作为数组中的字符串返回。
|
||||
testString: assert((function testTrie() { var test = false; if (typeof Trie !== 'undefined') { test = new Trie() } else { return false; }; test.add('jump'); test.add('jumps'); test.add('jumped'); test.add('house'); test.add('mouse'); var added = test.print(); return (added.indexOf('jump') != -1 && added.indexOf('jumps') != -1 && added.indexOf('jumped') != -1 && added.indexOf('house') != -1 && added.indexOf('mouse') != -1 && added.length == 5); }()));
|
||||
- text: isWord方法仅对添加到trie的单词返回true,对所有其他单词返回false。
|
||||
testString: assert((function testTrie() { var test = false; if (typeof Trie !== 'undefined') { test = new Trie() } else { return false; }; test.add('hop'); test.add('hops'); test.add('hopped'); test.add('hoppy'); test.add('hope'); return (test.isWord('hop') && !test.isWord('ho') && test.isWord('hopped') && !test.isWord('hopp') && test.isWord('hoppy') && !test.isWord('hoping')); }()));
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
var displayTree = (tree) => console.log(JSON.stringify(tree, null, 2));
|
||||
var Node = function() {
|
||||
this.keys = new Map();
|
||||
this.end = false;
|
||||
this.setEnd = function() {
|
||||
this.end = true;
|
||||
};
|
||||
this.isEnd = function() {
|
||||
return this.end;
|
||||
};
|
||||
};
|
||||
var Trie = function() {
|
||||
// change code below this line
|
||||
// change code above this line
|
||||
};
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,52 @@
|
||||
---
|
||||
id: 587d825b367417b2b2512c8d
|
||||
title: Create an ES6 JavaScript Map
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 创建ES6 JavaScript地图
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">新版本的JavaScript为我们提供了一个内置的Map对象,它提供了我们在上一次挑战中手工编写的大部分功能。此Map对象虽然与常规JavaScript对象类似,但它提供了一些普通对象缺少的有用功能。例如,ES6 Map跟踪添加到其中的项目的插入顺序。以下是其方法的更完整概述: <code>.has(key)</code>基于键的存在返回true或false <code>.get(key)</code>返回与键相关联的值<code>.set(key, value)</code>设置新键,值对<code>.delete(key)</code>删除一个键,值对<code>.clear()</code>删除所有键值对<code>.entries()</code>以插入顺序返回所有键的数组<code>.values()</code>返回插入中所有值的数组order说明:定义一个JavaScript Map对象并为其分配一个名为myMap的变量。添加密钥,值对<code>freeCodeCamp</code> , <code>Awesome!</code>它。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: myMap对象存在。
|
||||
testString: assert(typeof myMap === 'object');
|
||||
- text: myMap包含键值对<code>freeCodeCamp</code> , <code>Awesome!</code> 。
|
||||
testString: assert(myMap.get('freeCodeCamp') === 'Awesome!');
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// change code below this line
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,58 @@
|
||||
---
|
||||
id: 587d8254367417b2b2512c70
|
||||
title: Create and Add to Sets in ES6
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 在ES6中创建和添加集
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">既然您已经完成了ES5,那么您将在ES6中执行类似的操作。这将相当容易。 ES6包含一个内置的数据结构<code>Set</code>现在包含了您手动编写的许多操作。我们来看看:创建一个新的空集: <code>var set = new Set();</code>您可以使用值创建一个集合: <code>var set = new Set(1);</code>您可以使用数组创建一个集合: <code>var set = new Set([1, 2, 3]);</code>创建集合后,可以使用<code>add</code>方法添加所需的值: <blockquote> var set = new Set([1,2,3]); <br> set.add([4,5,6]); </blockquote>提醒一下,集合是一种不能包含重复值的数据结构: <blockquote> var set = new Set([1,2,3,1,2,3]); <br> // set仅包含[1,2,3] </blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">在本练习中,返回一个具有以下值的集合: <code>1, 2, 3, 'Taco', 'Cat', 'Awesome'</code> </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: '您的<code>Set</code>应该只包含值<code>1, 2, 3, Taco, Cat, Awesome</code> 。'
|
||||
testString: 'assert((function(){var test = checkSet(); return (test.size == 6) && test.has(1) && test.has(2) && test.has(3) && test.has("Taco") && test.has("Cat") && test.has("Awesome");})());'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function checkSet() {
|
||||
var set = new Set([1, 2, 3, 3, 2, 1, 2, 3, 1]);
|
||||
// change code below this line
|
||||
|
||||
// change code above this line
|
||||
console.log(set);
|
||||
return set;
|
||||
}
|
||||
|
||||
checkSet();
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,76 @@
|
||||
---
|
||||
id: 587d8258367417b2b2512c80
|
||||
title: Delete a Leaf Node in a Binary Search Tree
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 删除二进制搜索树中的叶节点
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">这是我们将在二叉搜索树中实现更难操作的三个挑战中的第一个:删除。删除很困难,因为删除节点会破坏树中的链接。必须仔细重新建立这些链接以确保维护二叉树结构。对于某些删除,这意味着必须重新排列树。通常,在尝试删除节点时,您将遇到以下三种情况之一:叶节点:要删除的目标没有子节点。一个孩子:要删除的目标只有一个孩子。两个子节点:要删除的目标有两个子节点。删除叶节点很简单,我们只需删除它。删除具有一个子节点的节点也相对容易,我们只需删除它并将其父节点链接到我们删除的节点的子节点。但是,删除具有两个子节点的节点更加困难,因为这会创建两个需要重新连接到父树的子节点。我们将在第三个挑战中看到如何处理这个案例。此外,在处理删除时,您需要注意一些边缘情况。如果树是空的怎么办?如果要删除的节点是根节点怎么办?如果树中只有两个元素怎么办?现在,让我们处理第一种删除叶节点的情况。说明:在我们的二叉树上创建一个名为<code>remove</code> 。我们将在这里为我们的删除操作构建逻辑。首先,您需要在remove中创建一个函数,该函数在当前树中找到我们尝试删除的节点。如果树中不存在该节点,则<code>remove</code>应返回<code>null</code> 。现在,如果目标节点是没有子节点的叶节点,则应将其父节点引用设置为<code>null</code> 。这有效地从树中删除节点。为此,您必须跟踪我们尝试删除的节点的父节点。创建一种跟踪目标节点具有的子节点数的方法也很有用,因为这将确定我们的删除属于哪种情况。我们将在下一次挑战中处理第二和第三个案例。祝你好运! </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 存在<code>BinarySearchTree</code>数据结构。
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() }; return (typeof test == 'object')})());
|
||||
- text: 二叉搜索树有一个名为<code>remove</code>的方法。
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; return (typeof test.remove == 'function')})());
|
||||
- text: 尝试删除不存在的元素将返回<code>null</code> 。
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; if (typeof test.remove !== 'function') { return false; }; return (test.remove(100) == null); })());
|
||||
- text: 如果根节点没有子节点,则删除它会将根节点设置为<code>null</code> 。
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; if (typeof test.remove !== 'function') { return false; }; test.add(500); test.remove(500); return (test.inorder() == null); })());
|
||||
- text: <code>remove</code>方法从树中删除叶节点
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; if (typeof test.remove !== 'function') { return false; }; test.add(5); test.add(3); test.add(7); test.add(6); test.add(10); test.add(12); test.remove(3); test.remove(12); test.remove(10); return (test.inorder().join('') == '567'); })());
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
var displayTree = (tree) => console.log(JSON.stringify(tree, null, 2));
|
||||
function Node(value) {
|
||||
this.value = value;
|
||||
this.left = null;
|
||||
this.right = null;
|
||||
}
|
||||
|
||||
function BinarySearchTree() {
|
||||
this.root = null;
|
||||
// case 1: target has no children, change code below this line
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,121 @@
|
||||
---
|
||||
id: 587d8258367417b2b2512c81
|
||||
title: Delete a Node with One Child in a Binary Search Tree
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 在二叉搜索树中删除具有一个子节点的节点
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">现在我们可以删除叶子节点,让我们继续第二种情况:删除一个子节点。对于这种情况,假设我们有一棵树,其中包含以下节点1 - 2 - 3,其中1是根。要删除2,我们只需要在1到3中做出正确的引用。更一般地说,为了删除只有一个子节点的节点,我们将该节点的父引用作为树中的下一个节点。说明:我们在<code>remove</code>方法中提供了一些代码,用于完成上一次挑战中的任务。我们找到要删除的目标及其父节点,并定义目标节点具有的子节点数。让我们在这里为仅有一个子节点的目标节点添加下一个案例。在这里,我们必须确定单个子节点是树中的左或右分支,然后在父节点中设置正确的引用以指向此节点。另外,让我们考虑目标是根节点的情况(这意味着父节点将为<code>null</code> )。只要通过测试,请随意用自己的代码替换所有入门代码。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 存在<code>BinarySearchTree</code>数据结构。
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() }; return (typeof test == 'object')})());
|
||||
- text: 二叉搜索树有一个名为<code>remove</code>的方法。
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; return (typeof test.remove == 'function')})());
|
||||
- text: 尝试删除不存在的元素将返回<code>null</code> 。
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; if (typeof test.remove !== 'function') { return false; }; return (test.remove(100) == null); })());
|
||||
- text: 如果根节点没有子节点,则删除它会将根节点设置为<code>null</code> 。
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; if (typeof test.remove !== 'function') { return false; }; test.add(500); test.remove(500); return (test.inorder() == null); })());
|
||||
- text: <code>remove</code>方法从树中删除叶节点
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; if (typeof test.remove !== 'function') { return false; }; test.add(5); test.add(3); test.add(7); test.add(6); test.add(10); test.add(12); test.remove(3); test.remove(12); test.remove(10); return (test.inorder().join('') == '567'); })());
|
||||
- text: <code>remove</code>方法删除具有一个子节点的节点。
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; if (typeof test.remove !== 'function') { return false; }; test.add(-1); test.add(3); test.add(7); test.add(16); test.remove(16); test.remove(7); test.remove(3); return (test.inorder().join('') == '-1'); })());
|
||||
- text: 删除具有两个节点的树中的根将第二个节点设置为根。
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; if (typeof test.remove !== 'function') { return false; }; test.add(15); test.add(27); test.remove(15); return (test.inorder().join('') == '27'); })());
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
var displayTree = (tree) => console.log(JSON.stringify(tree, null, 2));
|
||||
function Node(value) {
|
||||
this.value = value;
|
||||
this.left = null;
|
||||
this.right = null;
|
||||
}
|
||||
|
||||
function BinarySearchTree() {
|
||||
this.root = null;
|
||||
this.remove = function(value) {
|
||||
if (this.root === null) {
|
||||
return null;
|
||||
}
|
||||
var target;
|
||||
var parent = null;
|
||||
// find the target value and its parent
|
||||
(function findValue(node = this.root) {
|
||||
if (value == node.value) {
|
||||
target = node;
|
||||
} else if (value < node.value && node.left !== null) {
|
||||
parent = node;
|
||||
return findValue(node.left);
|
||||
} else if (value < node.value && node.left === null) {
|
||||
return null;
|
||||
} else if (value > node.value && node.right !== null) {
|
||||
parent = node;
|
||||
return findValue(node.right);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}).bind(this)();
|
||||
if (target === null) {
|
||||
return null;
|
||||
}
|
||||
// count the children of the target to delete
|
||||
var children = (target.left !== null ? 1 : 0) + (target.right !== null ? 1 : 0);
|
||||
// case 1: target has no children
|
||||
if (children === 0) {
|
||||
if (target == this.root) {
|
||||
this.root = null;
|
||||
}
|
||||
else {
|
||||
if (parent.left == target) {
|
||||
parent.left = null;
|
||||
} else {
|
||||
parent.right = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
// case 2: target has one child, change code below this line
|
||||
};
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,139 @@
|
||||
---
|
||||
id: 587d8258367417b2b2512c82
|
||||
title: Delete a Node with Two Children in a Binary Search Tree
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 在二叉搜索树中删除具有两个子节点的节点
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">删除具有两个子节点的节点是最难实现的。删除这样的节点会生成两个不再连接到原始树结构的子树。我们如何重新连接它们?一种方法是在目标节点的右子树中找到最小值,并用该值替换目标节点。以这种方式选择替换确保它大于左子树中的每个节点,它成为新的父节点,但也小于右子树中的每个节点,它成为新的父节点。完成此替换后,必须从右子树中删除替换节点。即使这个操作也很棘手,因为替换可能是一个叶子,或者它本身可能是一个右子树的父亲。如果是叶子,我们必须删除其父对它的引用。否则,它必须是目标的正确子项。在这种情况下,我们必须用替换值替换目标值,并使目标引用替换的右子。说明:让我们通过处理第三种情况来完成我们的<code>remove</code>方法。我们为前两种情况再次提供了一些代码。现在添加一些代码来处理具有两个子节点的目标节点。任何边缘情况要注意?如果树只有三个节点怎么办?完成后,这将完成二进制搜索树的删除操作。干得好,这是一个非常难的问题! </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 存在<code>BinarySearchTree</code>数据结构。
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() }; return (typeof test == 'object')})());
|
||||
- text: 二叉搜索树有一个名为<code>remove</code>的方法。
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; return (typeof test.remove == 'function')})());
|
||||
- text: 尝试删除不存在的元素将返回<code>null</code> 。
|
||||
testString: "assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; return (typeof test.remove == 'function') ? (test.remove(100) == null) : false})());"
|
||||
- text: 如果根节点没有子节点,则删除它会将根节点设置为<code>null</code> 。
|
||||
testString: "assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; test.add(500); test.remove(500); return (typeof test.remove == 'function') ? (test.inorder() == null) : false})());"
|
||||
- text: <code>remove</code>方法从树中删除叶节点
|
||||
testString: "assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; test.add(5); test.add(3); test.add(7); test.add(6); test.add(10); test.add(12); test.remove(3); test.remove(12); test.remove(10); return (typeof test.remove == 'function') ? (test.inorder().join('') == '567') : false})());"
|
||||
- text: <code>remove</code>方法删除具有一个子节点的节点。
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; if (typeof test.remove !== 'function') { return false; }; test.add(-1); test.add(3); test.add(7); test.add(16); test.remove(16); test.remove(7); test.remove(3); return (test.inorder().join('') == '-1'); })());
|
||||
- text: 删除具有两个节点的树中的根将第二个节点设置为根。
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; if (typeof test.remove !== 'function') { return false; }; test.add(15); test.add(27); test.remove(15); return (test.inorder().join('') == '27'); })());
|
||||
- text: <code>remove</code>方法在保留二叉搜索树结构的同时删除具有两个子节点的节点。
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; if (typeof test.remove !== 'function') { return false; }; test.add(1); test.add(4); test.add(3); test.add(7); test.add(9); test.add(11); test.add(14); test.add(15); test.add(19); test.add(50); test.remove(9); if (!test.isBinarySearchTree()) { return false; }; test.remove(11); if (!test.isBinarySearchTree()) { return false; }; test.remove(14); if (!test.isBinarySearchTree()) { return false; }; test.remove(19); if (!test.isBinarySearchTree()) { return false; }; test.remove(3); if (!test.isBinarySearchTree()) { return false; }; test.remove(50); if (!test.isBinarySearchTree()) { return false; }; test.remove(15); if (!test.isBinarySearchTree()) { return false; }; return (test.inorder().join('') == '147'); })());
|
||||
- text: 可以在三个节点的树上删除根。
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; if (typeof test.remove !== 'function') { return false; }; test.add(100); test.add(50); test.add(300); test.remove(100); return (test.inorder().join('') == 50300); })());
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
var displayTree = (tree) => console.log(JSON.stringify(tree, null, 2));
|
||||
function Node(value) {
|
||||
this.value = value;
|
||||
this.left = null;
|
||||
this.right = null;
|
||||
}
|
||||
|
||||
function BinarySearchTree() {
|
||||
this.root = null;
|
||||
this.remove = function(value) {
|
||||
if (this.root === null) {
|
||||
return null;
|
||||
}
|
||||
var target;
|
||||
var parent = null;
|
||||
// find the target value and its parent
|
||||
(function findValue(node = this.root) {
|
||||
if (value == node.value) {
|
||||
target = node;
|
||||
} else if (value < node.value && node.left !== null) {
|
||||
parent = node;
|
||||
return findValue(node.left);
|
||||
} else if (value < node.value && node.left === null) {
|
||||
return null;
|
||||
} else if (value > node.value && node.right !== null) {
|
||||
parent = node;
|
||||
return findValue(node.right);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}).bind(this)();
|
||||
if (target === null) {
|
||||
return null;
|
||||
}
|
||||
// count the children of the target to delete
|
||||
var children = (target.left !== null ? 1 : 0) + (target.right !== null ? 1 : 0);
|
||||
// case 1: target has no children
|
||||
if (children === 0) {
|
||||
if (target == this.root) {
|
||||
this.root = null;
|
||||
}
|
||||
else {
|
||||
if (parent.left == target) {
|
||||
parent.left = null;
|
||||
} else {
|
||||
parent.right = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
// case 2: target has one child
|
||||
else if (children == 1) {
|
||||
var newChild = (target.left !== null) ? target.left : target.right;
|
||||
if (parent === null) {
|
||||
target.value = newChild.value;
|
||||
target.left = null;
|
||||
target.right = null;
|
||||
} else if (newChild.value < parent.value) {
|
||||
parent.left = newChild;
|
||||
} else {
|
||||
parent.right = newChild;
|
||||
}
|
||||
target = null;
|
||||
}
|
||||
// case 3: target has two children, change code below this line
|
||||
};
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,73 @@
|
||||
---
|
||||
id: 587d825d367417b2b2512c96
|
||||
title: Depth-First Search
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 深度优先搜索
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">与<dfn>广度优先搜索</dfn>类似,这里我们将学习另一种称为<dfn>深度优先搜索的</dfn>图遍历算法。广度优先搜索搜索远离源节点的增量边长度,而<dfn>深度优先搜索</dfn>首先尽可能地沿着边缘路径向下<dfn>搜索</dfn> 。一旦到达路径的一端,搜索将回溯到具有未访问边缘路径的最后一个节点并继续搜索。在视觉上,这就是算法正在做的事情,其中顶部节点是搜索的起始点。 <img class="img-responsive" src="https://camo.githubusercontent.com/aaad9e39961daf34d967c616edeb50abf3bf1235/68747470733a2f2f75706c6f61642e77696b696d656469612e6f72672f77696b6970656469612f636f6d6d6f6e732f372f37662f44657074682d46697273742d5365617263682e676966">该算法的简单输出是可从给定节点到达的节点列表。因此,在实施此算法时,您需要跟踪您访问的节点。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">编写一个函数<code>dfs()</code> ,它将无向,邻接矩阵<code>graph</code>和节点标签<code>root</code>作为参数。节点标签将只是<code>0</code>和<code>n - 1</code>之间节点的数值,其中<code>n</code>是图中节点的总数。您的函数应输出从<code>root</code>可到达的所有节点的数组。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: '输入图<code>[[0, 1, 0, 0], [1, 0, 1, 0], [0, 1, 0, 1], [0, 0, 1, 0]]</code> ,起始节点为<code>1</code>应返回一个数组<code>0</code> , <code>1</code> , <code>2</code> ,和<code>3</code> 。'
|
||||
testString: assert.sameMembers((function() { var graph = [[0, 1, 0, 0], [1, 0, 1, 0], [0, 1, 0, 1], [0, 0, 1, 0]]; return dfs(graph, 1);})(), [0, 1, 2, 3]);
|
||||
- text: '输入图<code>[[0, 1, 0, 0], [1, 0, 1, 0], [0, 1, 0, 1], [0, 0, 1, 0]]</code> ,起始节点为<code>1</code>应该返回一个包含四个元素的数组。'
|
||||
testString: assert((function() { var graph = [[0, 1, 0, 0], [1, 0, 1, 0], [0, 1, 0, 1], [0, 0, 1, 0]]; return dfs(graph, 1);})().length === 4);
|
||||
- text: '输入图<code>[[0, 1, 0, 0], [1, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 0]]</code> ,起始节点为<code>3</code>应该返回一个<code>3</code>的数组。'
|
||||
testString: assert.sameMembers((function() { var graph = [[0, 1, 0, 0], [1, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 0]]; return dfs(graph, 3);})(), [3]);
|
||||
- text: '输入图<code>[[0, 1, 0, 0], [1, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 0]]</code> ,起始节点为<code>3</code>应该返回一个包含一个元素的数组。'
|
||||
testString: assert((function() { var graph = [[0, 1, 0, 0], [1, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 0]]; return dfs(graph, 3);})().length === 1);
|
||||
- text: '输入图<code>[[0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]]</code> ,起始节点为<code>3</code>应该返回一个<code>2</code>和<code>3</code>的数组。'
|
||||
testString: assert.sameMembers((function() { var graph = [[0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]]; return dfs(graph, 3);})(), [2, 3]);
|
||||
- text: '输入图<code>[[0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]]</code> ,起始节点为<code>3</code>应该返回一个包含两个元素的数组。'
|
||||
testString: assert((function() { var graph = [[0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]]; return dfs(graph, 3);})().length === 2);
|
||||
- text: '输入图<code>[[0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]]</code> ,起始节点为<code>0</code>应该返回一个<code>0</code>和<code>1</code>的数组。'
|
||||
testString: assert.sameMembers((function() { var graph = [[0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]]; return dfs(graph, 0);})(), [0, 1]);
|
||||
- text: '输入图<code>[[0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]]</code> ,起始节点为<code>0</code>应该返回一个包含两个元素的数组。'
|
||||
testString: assert((function() { var graph = [[0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]]; return dfs(graph, 0);})().length === 2);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function dfs(graph, root) {
|
||||
|
||||
}
|
||||
|
||||
var exDFSGraph = [
|
||||
[0, 1, 0, 0],
|
||||
[1, 0, 1, 0],
|
||||
[0, 1, 0, 1],
|
||||
[0, 0, 1, 0]
|
||||
];
|
||||
console.log(dfs(exDFSGraph, 3));
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,82 @@
|
||||
---
|
||||
id: 587d8257367417b2b2512c7d
|
||||
title: Find the Minimum and Maximum Height of a Binary Search Tree
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 找到二叉搜索树的最小和最大高度
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">在最后一个挑战中,我们描述了树可能变得不平衡的情景。为了理解平衡的概念,让我们看看另一个树属性:高度。树中的高度表示从根节点到任何给定叶节点的距离。高度分支的树结构中的不同路径可以具有不同的高度,但是对于给定的树,将具有最小和最大高度。如果树是平衡的,则这些值最多相差一个。这意味着在平衡树中,所有叶节点都存在于同一级别中,或者如果它们不在同一级别内,则它们最多相隔一个级别。平衡的属性对于树很重要,因为它决定了树操作的效率。正如我们在上一次挑战中所解释的那样,我们面临严重不平衡树木的最坏情况时间复杂性。自平衡树通常用于在具有动态数据集的树中解决此问题。这些的常见例子包括AVL树,红黑树和B树。这些树都包含额外的内部逻辑,当插入或删除创建不平衡状态时,它会重新平衡树。注意:与height相似的属性是depth,它指的是给定节点距根节点的距离。说明:为我们的二叉树编写两种方法: <code>findMinHeight</code>和<code>findMaxHeight</code> 。这些方法应分别返回给定二叉树内最小和最大高度的整数值。如果节点为空,请为其指定高度<code>-1</code> (这是基本情况)。最后,添加第三个方法<code>isBalanced</code> ,它返回<code>true</code>或<code>false</code>具体取决于树是否平衡。您可以使用刚才编写的前两种方法来确定这一点。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 存在<code>BinarySearchTree</code>数据结构。
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() }; return (typeof test == 'object')})());
|
||||
- text: 二叉搜索树有一个名为<code>findMinHeight</code>的方法。
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; return (typeof test.findMinHeight == 'function')})());
|
||||
- text: 二叉搜索树有一个名为<code>findMaxHeight</code>的方法。
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; return (typeof test.findMaxHeight == 'function')})());
|
||||
- text: 二叉搜索树有一个名为<code>isBalanced</code>的方法。
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; return (typeof test.isBalanced == 'function')})());
|
||||
- text: <code>findMinHeight</code>方法返回树的最小高度。
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; if (typeof test.findMinHeight !== 'function') { return false; }; test.add(4); test.add(1); test.add(7); test.add(87); test.add(34); test.add(45); test.add(73); test.add(8); return (test.findMinHeight() == 1); })());
|
||||
- text: <code>findMaxHeight</code>方法返回树的最大高度。
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; if (typeof test.findMaxHeight !== 'function') { return false; }; test.add(4); test.add(1); test.add(7); test.add(87); test.add(34); test.add(45); test.add(73); test.add(8); return (test.findMaxHeight() == 5); })());
|
||||
- text: 空树返回高度<code>-1</code> 。
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; if (typeof test.findMaxHeight !== 'function') { return false; }; return (test.findMaxHeight() == -1); })());
|
||||
- text: 如果树是平衡二叉搜索树,则<code>isBalanced</code>方法返回true。
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; if (typeof test.isBalanced !== 'function') { return false; }; test.add(4); test.add(1); test.add(7); test.add(87); test.add(34); test.add(45); test.add(73); test.add(8); return !test.isBalanced(); })());
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
var displayTree = (tree) => console.log(JSON.stringify(tree, null, 2));
|
||||
function Node(value) {
|
||||
this.value = value;
|
||||
this.left = null;
|
||||
this.right = null;
|
||||
}
|
||||
function BinarySearchTree() {
|
||||
this.root = null;
|
||||
// change code below this line
|
||||
// change code above this line
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,78 @@
|
||||
---
|
||||
id: 587d8256367417b2b2512c7a
|
||||
title: Find the Minimum and Maximum Value in a Binary Search Tree
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 在二叉搜索树中查找最小值和最大值
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">这一系列挑战将介绍树数据结构。树木是计算机科学中重要且通用的数据结构。当然,他们的名字来源于这样一个事实:当他们看到它们时,它们看起来很像我们在自然界中熟悉的树木。树数据结构以一个节点(通常称为根)开始,并从此处分支到其他节点,每个节点可以具有更多子节点,依此类推。数据结构通常以顶部的根节点可视化;你可以把它想象成一棵倒置的天然树。首先,让我们描述一下我们将在树上遇到的一些常用术语。根节点是树的顶部。树中的数据点称为节点。具有通向其他节点的分支的节点被称为分支通向的节点的父节点(子节点)。其他更复杂的家庭术语适用于您所期望的。子树是指特定节点的所有后代,分支可以称为边,而叶节点是树末端没有子节点的节点。最后,请注意树本质上是递归数据结构。也就是说,节点的任何子节点都是其子树的父节点,依此类推。在为常见树操作设计算法时,树的递归性质非常重要。首先,我们将讨论一种特定类型的树,即二叉树。实际上,我们实际上将讨论一个特定的二叉树,一个二叉搜索树。让我们来描述这意味着什么。虽然树数据结构可以在单个节点上具有任意数量的分支,但是二叉树对于每个节点只能具有两个分支。此外,针对子子树排序二叉搜索树,使得左子树中的每个节点的值小于或等于父节点的值,并且右子树中的每个节点的值是大于或等于父节点的值。可视化这种关系以便更好地理解它是非常有帮助的: <div style="width: 100%; display: flex; justify-content: center; align-items: center;"><img style="width: 100%; max-width: 350px;" src="https://user-images.githubusercontent.com/18563015/32136009-1e665d98-bbd6-11e7-9133-63184f9f9182.png"></div>现在这个有序的关系很容易看到。请注意,根节点8左侧的每个值都小于8,右侧的每个值都大于8.还要注意,此关系也适用于每个子树。例如,第一个左子项是子树。 3是父节点,它有两个子节点 - 通过控制二进制搜索树的规则,我们知道甚至没有看到这个节点的左子节点(及其任何子节点)将小于3,右边child(及其任何子级)将大于3(但也小于结构的根值),依此类推。二进制搜索树是非常常见且有用的数据结构,因为它们在几种常见操作(例如查找,插入和删除)的平均情况下提供对数时间。说明:我们将从简单开始。除了为树创建节点的函数之外,我们还在这里定义了二叉搜索树结构的骨架。观察每个节点可能具有左右值。如果它们存在,将为它们分配子子树。在我们的二叉搜索树中,定义两个方法, <code>findMin</code>和<code>findMax</code> 。这些方法应返回二叉搜索树中保存的最小值和最大值(不用担心现在向树中添加值,我们在后台添加了一些值)。如果遇到困难,请反思二进制搜索树必须为true的不变量:每个左子树小于或等于其父树,每个右子树大于或等于其父树。我们还要说我们的树只能存储整数值。如果树为空,则任一方法都应返回<code>null</code> 。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 存在<code>BinarySearchTree</code>数据结构。
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() }; return (typeof test == 'object')})());
|
||||
- text: 二叉搜索树有一个名为<code>findMin</code>的方法。
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; return (typeof test.findMin == 'function')})());
|
||||
- text: 二叉搜索树有一个名为<code>findMax</code>的方法。
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; return (typeof test.findMax == 'function')})());
|
||||
- text: <code>findMin</code>方法返回二叉搜索树中的最小值。
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; if (typeof test.findMin !== 'function') { return false; }; test.add(4); test.add(1); test.add(7); test.add(87); test.add(34); test.add(45); test.add(73); test.add(8); return test.findMin() == 1; })());
|
||||
- text: <code>findMax</code>方法返回二叉搜索树中的最大值。
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; if (typeof test.findMax !== 'function') { return false; }; test.add(4); test.add(1); test.add(7); test.add(87); test.add(34); test.add(45); test.add(73); test.add(8); return test.findMax() == 87; })());
|
||||
- text: <code>findMin</code>和<code>findMax</code>方法为空树返回<code>null</code> 。
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; if (typeof test.findMin !== 'function') { return false; }; if (typeof test.findMax !== 'function') { return false; }; return (test.findMin() == null && test.findMax() == null) })());
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
var displayTree = (tree) => console.log(JSON.stringify(tree, null, 2));
|
||||
function Node(value) {
|
||||
this.value = value;
|
||||
this.left = null;
|
||||
this.right = null;
|
||||
}
|
||||
function BinarySearchTree() {
|
||||
this.root = null;
|
||||
// change code below this line
|
||||
// change code above this line
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,75 @@
|
||||
---
|
||||
id: 587d825b367417b2b2512c8c
|
||||
title: Implement Heap Sort with a Min Heap
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 用最小堆实现堆排序
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">现在我们可以添加和删除元素,让我们看看堆可用于的一些应用程序。堆通常用于实现优先级队列,因为它们始终将最大值或最小值的项存储在第一个位置。此外,它们还用于实现称为堆排序的排序算法。我们将在这里看到如何做到这一点。堆排序使用最小堆,与最大堆相反。最小堆始终将最小值的元素存储在根位置。堆排序通过获取未排序的数组,将数组中的每个项目添加到最小堆中,然后将最小堆中的每个项目提取到新数组中。最小堆结构确保新数组将包含至少最大顺序的原始项。这是最有效的排序算法之一,具有O(nlog(n))的平均和最差情况性能。说明:让我们用最小堆实现堆排序。您可以在此处调整最大堆代码。使用insert,remove和sort方法创建一个MinHeap对象。 sort方法应返回最小堆中从最小到最大排序的所有元素的数组。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 存在MinHeap数据结构。
|
||||
testString: assert((function() { var test = false; if (typeof MinHeap !== 'undefined') { test = new MinHeap() }; return (typeof test == 'object')})());
|
||||
- text: MinHeap有一个名为insert的方法。
|
||||
testString: assert((function() { var test = false; if (typeof MinHeap !== 'undefined') { test = new MinHeap() } else { return false; }; return (typeof test.insert == 'function')})());
|
||||
- text: MinHeap有一个名为remove的方法。
|
||||
testString: assert((function() { var test = false; if (typeof MinHeap !== 'undefined') { test = new MinHeap() } else { return false; }; return (typeof test.remove == 'function')})());
|
||||
- text: MinHeap有一个名为sort的方法。
|
||||
testString: assert((function() { var test = false; if (typeof MinHeap !== 'undefined') { test = new MinHeap() } else { return false; }; return (typeof test.sort == 'function')})());
|
||||
- text: sort方法返回一个数组,其中包含按排序顺序添加到最小堆的所有项。
|
||||
testString: assert((function() { var test = false; if (typeof MinHeap !== 'undefined') { test = new MinHeap() } else { return false; }; test.insert(3); test.insert(12); test.insert(5); test.insert(10); test.insert(1); test.insert(27); test.insert(42); test.insert(57); test.insert(5); var result = test.sort(); return (isSorted(result)); })());
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// check if array is sorted
|
||||
function isSorted(a){
|
||||
for(let i = 0; i < a.length - 1; i++)
|
||||
if(a[i] > a[i + 1])
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
// generate a randomly filled array
|
||||
var array = new Array();
|
||||
(function createArray(size = 5) {
|
||||
array.push(+(Math.random() * 100).toFixed(0));
|
||||
return (size > 1) ? createArray(size - 1) : undefined;
|
||||
})(25);
|
||||
var MinHeap = function() {
|
||||
// change code below this line
|
||||
// change code above this line
|
||||
};
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
|
||||
</section>
|
@ -0,0 +1,59 @@
|
||||
---
|
||||
id: 587d8256367417b2b2512c79
|
||||
title: Incidence Matrix
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 发生率矩阵
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">表示图形的另一种方式是将其置于<dfn>关联矩阵中。</dfn> <dfn>入射矩阵</dfn>是二维(2D)阵列。一般而言,关联矩阵在其两个维度之间涉及两个不同类别的对象。这种矩阵类似于邻接矩阵。但是,行和列在这里意味着其他东西。在图表中,我们有边缘和节点。这些将是我们的“两类不同的对象”。该矩阵将使行为节点,列为边。这意味着我们可以拥有不均匀的行数和列数。每列将代表一个独特的边缘。此外,每个边连接两个节点。要显示两个节点之间存在边缘,您将在特定列的两行中放置1。下面是一个3节点图,节点1和节点3之间有一条边。 <blockquote> 1 <br> --- <br> 1 | 1 <br> 2 | 0 <br> 3 | 1 </blockquote>以下是具有4个边和4个节点的<code>incidence matrix</code>的示例。请记住,列是边,行是节点本身。 <blockquote> 1 2 3 4 <br> -------- <br> 1 | 0 1 1 1 <br> 2 | 1 1 0 0 <br> 3 | 1 0 0 1 <br> 4 | 0 0 1 0 </blockquote>下面是同一件事的JavaScript实现。 <blockquote> var incMat = [ <br> [0,1,1,1], <br> [1,1,0,0], <br> [1,0,0,1], <br> [0,0,1,0] <br> ]。 </blockquote>要制作有向图,请使用<code>-1</code>表示离开特定节点的边,使用<code>1</code>作为边进入节点。 <blockquote> var incMatDirected = [ <br> [0,-1,1,-1], <br> [-1,1,0,0], <br> [1,0,0,1], <br> [0,0,-1,0] <br> ]。 </blockquote>图形的边缘也可以有权<dfn>重</dfn> 。到目前为止,我们有<dfn>未加权的</dfn>边缘,只有存在和缺少边是二进制( <code>0</code>或<code>1</code> )。根据您的应用,您可以拥有不同的重量。不同的权重表示为大于1的数字。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">创建具有五个节点和四个边的无向图的关联矩阵。该矩阵应该是多维数组。这五个节点在关系之后具有关系。第一边缘在第一和第二节点之间。第二个边缘位于第二个和第三个节点之间。第三个边缘位于第三个和第五个节点之间。并且四个边缘在第四和第二节点之间。所有边权重均为1,边缘顺序很重要。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>incMatUndirected</code>应该只包含五个节点。
|
||||
testString: assert((incMatUndirected.length === 5) && incMatUndirected.map(function(x) { return x.length === 4 }).reduce(function(a, b) { return a && b }) );
|
||||
- text: 第一个和第二个节点之间应该有第一条边。
|
||||
testString: assert((incMatUndirected[0][0] === 1) && (incMatUndirected[1][0] === 1));
|
||||
- text: 第二个和第三个节点之间应该有第二条边。
|
||||
testString: assert((incMatUndirected[1][1] === 1) && (incMatUndirected[2][1] === 1));
|
||||
- text: 第三个和第五个节点之间应该有第三条边。
|
||||
testString: assert((incMatUndirected[2][2] === 1) && (incMatUndirected[4][2] === 1));
|
||||
- text: 第二个和第四个节点之间应该有第四条边。
|
||||
testString: assert((incMatUndirected[1][3] === 1) && (incMatUndirected[3][3] === 1));
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
var incMatUndirected = [
|
||||
|
||||
];
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,59 @@
|
||||
---
|
||||
id: 587d825a367417b2b2512c8a
|
||||
title: Insert an Element into a Max Heap
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 将元素插入最大堆
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">现在我们将继续讨论另一个树数据结构,即二进制堆。二进制堆是部分有序的二叉树,它满足堆属性。 heap属性指定父节点和子节点之间的关系。您可能有一个最大堆,其中所有父节点都大于或等于其子节点,或者最小堆,其中反向为真。二进制堆也是完整的二叉树。这意味着树的所有级别都被完全填充,如果最后一级被部分填充,则从左到右填充。虽然二进制堆可以实现为具有包含左和右引用的节点的树结构,但是根据堆属性的部分排序允许我们用数组表示堆。父子关系是我们感兴趣的,通过简单的算术,我们可以计算任何父节点的子节点和任何子节点的父节点。例如,考虑二进制最小堆的数组表示: <code>[ 6, 22, 30, 37, 63, 48, 42, 76 ]</code>根节点是第一个元素,6。它的子节点是22和30.如果我们看在这些值的数组索引之间的关系中,对于索引i,子项为2 * i + 1和2 * i + 2.同样,索引0处的元素是索引1和2处的这两个子项的父项。通常,我们可以在任何索引处找到节点的父节点,其中包含以下内容:(i - 1)/ 2.当二叉树增长到任意大小时,这些模式将成立。最后,我们可以稍微调整一下,通过跳过数组中的第一个元素,使这个算法更容易。这样做会为给定索引i处的任何元素创建以下关系:示例数组表示形式: <code>[ null, 6, 22, 30, 37, 63, 48, 42, 76 ]</code>元素的左子项:i * 2元素的右子项:i * 2 + 1一个元素的父元素:i / 2一旦你绕过数学运算,使用数组表示非常有用,因为使用这个算法可以快速确定节点位置,因为你不需要内存使用量减少维护对子节点的引用。说明:这里我们将创建一个最大堆。首先创建一个insert方法,将元素添加到堆中。在插入期间,始终保持堆属性非常重要。对于最大堆,这意味着根元素应始终在树中具有最大值,并且所有父节点应该大于其子节点。对于堆的数组实现,这通常分三步完成:将新元素添加到数组的末尾。如果元素大于其父元素,请切换它们。继续切换,直到新元素小于其父元素或到达树的根。最后,添加一个print方法,该方法返回已添加到堆中的所有项的数组。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 存在MaxHeap数据结构。
|
||||
testString: assert((function() { var test = false; if (typeof MaxHeap !== 'undefined') { test = new MaxHeap() }; return (typeof test == 'object')})());
|
||||
- text: MaxHeap有一个名为insert的方法。
|
||||
testString: assert((function() { var test = false; if (typeof MaxHeap !== 'undefined') { test = new MaxHeap() } else { return false; }; return (typeof test.insert == 'function')})());
|
||||
- text: MaxHeap有一个名为print的方法。
|
||||
testString: assert((function() { var test = false; if (typeof MaxHeap !== 'undefined') { test = new MaxHeap() } else { return false; }; return (typeof test.print == 'function')})());
|
||||
- text: insert方法根据max heap属性添加元素。
|
||||
testString: 'assert((function() { var test = false; if (typeof MaxHeap !== ''undefined'') { test = new MaxHeap() } else { return false; }; test.insert(50); test.insert(100); test.insert(700); test.insert(32); test.insert(51); let result = test.print(); return ((result.length == 5) ? result[0] == 700 : result[1] == 700) })());'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
var MaxHeap = function() {
|
||||
// change code below this line
|
||||
// change code above this line
|
||||
};
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,74 @@
|
||||
---
|
||||
id: 587d8259367417b2b2512c83
|
||||
title: Invert a Binary Tree
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 反转二叉树
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">这里我们将创建一个反转二叉树的函数。给定二叉树,我们希望生成一个新树,它等效于该树的镜像。与原始树的inorder遍历相比,在倒置树上运行inorder遍历将以相反的顺序探索节点。在我们的二叉树上编写一个名为<code>invert</code>的方法。调用此方法应该反转当前树结构。理想情况下,我们希望在线性时间内就地执行此操作。也就是说,我们只访问每个节点一次,我们在不使用任何额外内存的情况下修改现有的树结构。祝你好运! </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 存在<code>BinarySearchTree</code>数据结构。
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() }; return (typeof test == 'object')})());
|
||||
- text: 二叉搜索树有一个名为<code>invert</code>的方法。
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; return (typeof test.invert == 'function')})());
|
||||
- text: <code>invert</code>方法正确地反转树结构。
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; if (typeof test.invert !== 'function') { return false; }; test.add(4); test.add(1); test.add(7); test.add(87); test.add(34); test.add(45); test.add(73); test.add(8); test.invert(); return test.inorder().join('') == '877345348741'; })());
|
||||
- text: 反转空树返回<code>null</code> 。
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; if (typeof test.invert !== 'function') { return false; }; return (test.invert() == null); })());
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
var displayTree = (tree) => console.log(JSON.stringify(tree, null, 2));
|
||||
function Node(value) {
|
||||
this.value = value;
|
||||
this.left = null;
|
||||
this.right = null;
|
||||
}
|
||||
function BinarySearchTree() {
|
||||
this.root = null;
|
||||
// change code below this line
|
||||
// change code above this line
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,56 @@
|
||||
---
|
||||
id: 587d8250367417b2b2512c5e
|
||||
title: Learn how a Stack Works
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 了解堆栈的工作原理
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">你可能熟悉桌子上的一摞书。您可能已使用文本编辑器的撤消功能。您也可能习惯按手机上的后退按钮返回应用中的上一个视图。你知道他们都有什么共同之处吗?它们都以某种方式存储数据,以便您可以向后遍历。堆栈中最顶层的书是最后放在那里的书。如果您从堆栈的顶部删除该书,则会显示在最后一本书之前放置的书籍,依此类推。如果你考虑一下,在上面的所有例子中,你都会获得<dfn>Last-In-First-Out</dfn>服务。我们将尝试使用我们的代码来模仿它。该数据存储方案称为<dfn>堆栈</dfn> 。特别是,我们必须实现将JavaScript对象推送到堆栈顶部的<code>push()</code>方法;和<code>pop()</code>方法,它删除当前位于堆栈顶部的JavaScript对象。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">这里我们有一堆作为数组表示的家庭作业: <code>"BIO12"</code>位于基础, <code>"PSY44"</code>位于堆栈的顶部。修改给定的数组,并使用上面提到的JavaScript方法将其视为<code>stack</code> 。从堆栈中删除顶部元素<code>"PSY44"</code> 。然后添加<code>"CS50"</code>作为堆栈的新顶部元素。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>homeworkStack</code>应该只包含4个元素。
|
||||
testString: assert(homeworkStack.length === 4);
|
||||
- text: <code>homeworkStack</code>的最后一个元素应该是<code>"CS50"</code> 。
|
||||
testString: assert(homeworkStack[3] === 'CS50');
|
||||
- text: <code>homeworkStack</code>不应包含<code>"PSY44"</code> 。
|
||||
testString: assert(homeworkStack.indexOf('PSY44') === -1);
|
||||
- text: 不应更改<code>homeworkStack</code>的初始声明。
|
||||
testString: assert(code.match(/=/g).length === 1 && /homeworkStack\s*=\s*\["BIO12"\s*,\s*"HIS80"\s*,\s*"MAT122"\s*,\s*"PSY44"\]/.test(code));
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
var homeworkStack = ["BIO12","HIS80","MAT122","PSY44"];
|
||||
// Only change code below this line
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,110 @@
|
||||
---
|
||||
id: 587d8254367417b2b2512c6e
|
||||
title: Perform a Difference on Two Sets of Data
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 对两组数据执行差异
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">在本练习中,我们将对两组数据进行区分。我们将在我们的<code>Set</code>数据结构上创建一个名为<code>difference</code> 。集合的差异应比较两组并返回第一组中不存在的项目。此方法应将另一个<code>Set</code>作为参数,并返回两个集的<code>difference</code> 。例如,如果<code>setA = ['a','b','c']</code>和<code>setB = ['a','b','d','e']</code> ,则setA和setB的差异为: <code>setA.difference(setB) = ['c']</code> 。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你的<code>Set</code>类应该有一个<code>difference</code>方法。
|
||||
testString: assert((function(){var test = new Set(); return (typeof test.difference === 'function')})());
|
||||
- text: 收回了适当的收藏
|
||||
testString: assert((function(){var setA = new Set(); var setB = new Set(); setA.add('a'); setA.add('b'); setA.add('c'); setB.add('c'); setB.add('d'); var differenceSetAB = setA.difference(setB); return (differenceSetAB.size() === 2) && DeepEqual(differenceSetAB.values(), [ 'a', 'b' ])})());
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function Set() {
|
||||
// the var collection will hold the set
|
||||
var collection = [];
|
||||
// this method will check for the presence of an element and return true or false
|
||||
this.has = function(element) {
|
||||
return (collection.indexOf(element) !== -1);
|
||||
};
|
||||
// this method will return all the values in the set
|
||||
this.values = function() {
|
||||
return collection;
|
||||
};
|
||||
// this method will add an element to the set
|
||||
this.add = function(element) {
|
||||
if(!this.has(element)){
|
||||
collection.push(element);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
// this method will remove an element from a set
|
||||
this.remove = function(element) {
|
||||
if(this.has(element)){
|
||||
var index = collection.indexOf(element);
|
||||
collection.splice(index,1);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
// this method will return the size of the collection
|
||||
this.size = function() {
|
||||
return collection.length;
|
||||
};
|
||||
// this method will return the union of two sets
|
||||
this.union = function(otherSet) {
|
||||
var unionSet = new Set();
|
||||
var firstSet = this.values();
|
||||
var secondSet = otherSet.values();
|
||||
firstSet.forEach(function(e){
|
||||
unionSet.add(e);
|
||||
});
|
||||
secondSet.forEach(function(e){
|
||||
unionSet.add(e);
|
||||
});
|
||||
return unionSet;
|
||||
};
|
||||
// this method will return the intersection of two sets as a new set
|
||||
this.intersection = function(otherSet) {
|
||||
var intersectionSet = new Set();
|
||||
var firstSet = this.values();
|
||||
firstSet.forEach(function(e){
|
||||
if(otherSet.has(e)){
|
||||
intersectionSet.add(e);
|
||||
}
|
||||
});
|
||||
return intersectionSet;
|
||||
};
|
||||
// change code below this line
|
||||
// change code above this line
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,129 @@
|
||||
---
|
||||
id: 587d8254367417b2b2512c6f
|
||||
title: Perform a Subset Check on Two Sets of Data
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 对两组数据执行子集检查
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">在本练习中,我们将对2组数据执行子集测试。我们将在我们的<code>Set</code>数据结构上创建一个名为<code>subset</code> 。这将比较第一组与第二组,如果第一组完全包含在第二组中,则它将返回true。例如,如果<code>setA = ['a','b']</code>和<code>setB = ['a','b','c','d']</code> ,则setA和setB的子集为: <code>setA.subset(setB)</code>应该是<code>true</code> 。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你的<code>Set</code>类应该有一个<code>union</code>方法。
|
||||
testString: assert((function(){var test = new Set(); return (typeof test.subset === 'function')})());
|
||||
- text: 第一个Set()包含在第二个Set中
|
||||
testString: assert((function(){var setA = new Set(); var setB = new Set(); setA.add('a'); setB.add('b'); setB.add('c'); setB.add('a'); setB.add('d'); var subsetSetAB = setA.subset(setB);return (subsetSetAB === true)})());
|
||||
- text: '<code>["a", "b"].subset(["a", "b", "c", "d"])</code>应该返回<code>true</code> “)'
|
||||
testString: assert((function(){var setA = new Set(); var setB = new Set(); setA.add('a'); setA.add('b'); setB.add('a'); setB.add('b'); setB.add('c'); setB.add('d'); var subsetSetAB = setA.subset(setB); return (subsetSetAB === true)})());
|
||||
- text: '<code>["a", "b", "c"].subset(["a", "b"])</code>应返回<code>false</code> “)'
|
||||
testString: assert((function(){var setA = new Set(); var setB = new Set(); setA.add('a'); setA.add('b'); setA.add('c'); setB.add('a'); setB.add('b'); var subsetSetAB = setA.subset(setB); return (subsetSetAB === false)})());
|
||||
- text: '<code>[].subset([])</code>应该返回<code>true</code>'
|
||||
testString: assert((function(){var setA = new Set(); var setB = new Set(); var subsetSetAB = setA.subset(setB); return (subsetSetAB === true)})());
|
||||
- text: '<code>["a", "b"].subset(["c", "d"])</code>应返回<code>false</code> “)'
|
||||
testString: assert((function(){var setA = new Set(); var setB = new Set(); setA.add('a'); setA.add('b'); setB.add('c'); setB.add('d'); var subsetSetAB = setA.subset(setB); return (subsetSetAB === false)})());
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function Set() {
|
||||
// the var collection will hold the set
|
||||
var collection = [];
|
||||
// this method will check for the presence of an element and return true or false
|
||||
this.has = function(element) {
|
||||
return (collection.indexOf(element) !== -1);
|
||||
};
|
||||
// this method will return all the values in the set
|
||||
this.values = function() {
|
||||
return collection;
|
||||
};
|
||||
// this method will add an element to the set
|
||||
this.add = function(element) {
|
||||
if(!this.has(element)){
|
||||
collection.push(element);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
// this method will remove an element from a set
|
||||
this.remove = function(element) {
|
||||
if(this.has(element)){
|
||||
var index = collection.indexOf(element);
|
||||
collection.splice(index,1);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
// this method will return the size of the collection
|
||||
this.size = function() {
|
||||
return collection.length;
|
||||
};
|
||||
// this method will return the union of two sets
|
||||
this.union = function(otherSet) {
|
||||
var unionSet = new Set();
|
||||
var firstSet = this.values();
|
||||
var secondSet = otherSet.values();
|
||||
firstSet.forEach(function(e){
|
||||
unionSet.add(e);
|
||||
});
|
||||
secondSet.forEach(function(e){
|
||||
unionSet.add(e);
|
||||
});
|
||||
return unionSet;
|
||||
};
|
||||
// this method will return the intersection of two sets as a new set
|
||||
this.intersection = function(otherSet) {
|
||||
var intersectionSet = new Set();
|
||||
var firstSet = this.values();
|
||||
firstSet.forEach(function(e){
|
||||
if(otherSet.has(e)){
|
||||
intersectionSet.add(e);
|
||||
}
|
||||
});
|
||||
return intersectionSet;
|
||||
};
|
||||
// this method will return the difference of two sets as a new set
|
||||
this.difference = function(otherSet) {
|
||||
var differenceSet = new Set();
|
||||
var firstSet = this.values();
|
||||
firstSet.forEach(function(e){
|
||||
if(!otherSet.has(e)){
|
||||
differenceSet.add(e);
|
||||
}
|
||||
});
|
||||
return differenceSet;
|
||||
};
|
||||
// change code below this line
|
||||
// change code above this line
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,87 @@
|
||||
---
|
||||
id: 587d8253367417b2b2512c6c
|
||||
title: Perform a Union on Two Sets
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 在两个集上执行联合
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">在本练习中,我们将对两组数据执行联合。我们将在我们的<code>Set</code>数据结构上创建一个名为<code>union</code> 。此方法应将另一个<code>Set</code>作为参数,并返回两个集合的<code>union</code>集,不包括任何重复值。例如,如果<code>setA = ['a','b','c']</code>和<code>setB = ['a','b','d','e']</code> ,则setA和setB的并集为: <code>setA.union(setB) = ['a', 'b', 'c', 'd', 'e']</code> 。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你的<code>Set</code>类应该有一个<code>union</code>方法。
|
||||
testString: assert((function(){var test = new Set(); return (typeof test.union === 'function')})());
|
||||
- text: 收回了适当的收藏
|
||||
testString: assert((function(){var setA = new Set(); var setB = new Set(); setA.add('a'); setA.add('b'); setA.add('c'); setB.add('c'); setB.add('d'); var unionSetAB = setA.union(setB); var final = unionSetAB.values(); return (final.indexOf('a') !== -1 && final.indexOf('b') !== -1 && final.indexOf('c') !== -1 && final.indexOf('d') !== -1 && final.length === 4)})());
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function Set() {
|
||||
// the var collection will hold the set
|
||||
var collection = [];
|
||||
// this method will check for the presence of an element and return true or false
|
||||
this.has = function(element) {
|
||||
return (collection.indexOf(element) !== -1);
|
||||
};
|
||||
// this method will return all the values in the set
|
||||
this.values = function() {
|
||||
return collection;
|
||||
};
|
||||
// this method will add an element to the set
|
||||
this.add = function(element) {
|
||||
if(!this.has(element)){
|
||||
collection.push(element);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
// this method will remove an element from a set
|
||||
this.remove = function(element) {
|
||||
if(this.has(element)){
|
||||
var index = collection.indexOf(element);
|
||||
collection.splice(index,1);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
// this method will return the size of the set
|
||||
this.size = function() {
|
||||
return collection.length;
|
||||
};
|
||||
// change code below this line
|
||||
|
||||
// change code above this line
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,99 @@
|
||||
---
|
||||
id: 587d8253367417b2b2512c6d
|
||||
title: Perform an Intersection on Two Sets of Data
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 在两组数据上执行交集
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">在本练习中,我们将对两组数据执行交集。我们将在我们的<code>Set</code>数据结构上创建一个名为<code>intersection</code> 。集合的交集表示两个或更多集合共有的所有值。此方法应将另一个<code>Set</code>作为参数,并返回两个集合的<code>intersection</code> 。例如,如果<code>setA = ['a','b','c']</code>和<code>setB = ['a','b','d','e']</code> ,则setA和setB的交集为: <code>setA.intersection(setB) = ['a', 'b']</code> 。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 您的<code>Set</code>类应该有一个<code>intersection</code>方法。
|
||||
testString: assert((function(){var test = new Set(); return (typeof test.intersection === 'function')})());
|
||||
- text: 收回了适当的收藏
|
||||
testString: assert((function(){ var setA = new Set(); var setB = new Set(); setA.add('a'); setA.add('b'); setA.add('c'); setB.add('c'); setB.add('d'); var intersectionSetAB = setA.intersection(setB); return (intersectionSetAB.size() === 1 && intersectionSetAB.values()[0] === 'c')})());
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function Set() {
|
||||
// the var collection will hold the set
|
||||
var collection = [];
|
||||
// this method will check for the presence of an element and return true or false
|
||||
this.has = function(element) {
|
||||
return (collection.indexOf(element) !== -1);
|
||||
};
|
||||
// this method will return all the values in the set
|
||||
this.values = function() {
|
||||
return collection;
|
||||
};
|
||||
// this method will add an element to the set
|
||||
this.add = function(element) {
|
||||
if(!this.has(element)){
|
||||
collection.push(element);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
// this method will remove an element from a set
|
||||
this.remove = function(element) {
|
||||
if(this.has(element)){
|
||||
var index = collection.indexOf(element);
|
||||
collection.splice(index,1);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
// this method will return the size of the collection
|
||||
this.size = function() {
|
||||
return collection.length;
|
||||
};
|
||||
// this method will return the union of two sets
|
||||
this.union = function(otherSet) {
|
||||
var unionSet = new Set();
|
||||
var firstSet = this.values();
|
||||
var secondSet = otherSet.values();
|
||||
firstSet.forEach(function(e){
|
||||
unionSet.add(e);
|
||||
});
|
||||
secondSet.forEach(function(e){
|
||||
unionSet.add(e);
|
||||
});
|
||||
return unionSet;
|
||||
};
|
||||
// change code below this line
|
||||
// change code above this line
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,61 @@
|
||||
---
|
||||
id: 587d825b367417b2b2512c8b
|
||||
title: Remove an Element from a Max Heap
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 从最大堆中删除元素
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">现在我们可以向堆中添加元素,让我们看看如何删除元素。删除和插入元素都需要类似的逻辑。在最大堆中,您通常需要删除最大值,因此这只需要从树的根中提取它。这将破坏我们树的堆属性,因此我们必须以某种方式重新建立它。通常,对于最大堆,这可以通过以下方式完成:将堆中的最后一个元素移动到根位置。如果root的子节点大于它,则将root与较大值的子节点交换。继续交换,直到父级大于两个子级,或者到达树中的最后一级。说明:向我们的最大堆添加一个名为remove的方法。此方法应返回已添加到最大堆的最大值,并将其从堆中删除。它还应该重新排序堆,以便保持堆属性。删除元素后,堆中剩余的下一个最大元素应该成为根。此处再次添加插入方法。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 存在MaxHeap数据结构。
|
||||
testString: assert((function() { var test = false; if (typeof MaxHeap !== 'undefined') { test = new MaxHeap() }; return (typeof test == 'object')})());
|
||||
- text: MaxHeap有一个名为print的方法。
|
||||
testString: assert((function() { var test = false; if (typeof MaxHeap !== 'undefined') { test = new MaxHeap() } else { return false; }; return (typeof test.print == 'function')})());
|
||||
- text: MaxHeap有一个名为insert的方法。
|
||||
testString: assert((function() { var test = false; if (typeof MaxHeap !== 'undefined') { test = new MaxHeap() } else { return false; }; return (typeof test.insert == 'function')})());
|
||||
- text: MaxHeap有一个名为remove的方法。
|
||||
testString: assert((function() { var test = false; if (typeof MaxHeap !== 'undefined') { test = new MaxHeap() } else { return false; }; return (typeof test.remove == 'function')})());
|
||||
- text: remove方法从最大堆中删除最大元素,同时保持最大堆属性。
|
||||
testString: assert((function() { var test = false; if (typeof MaxHeap !== 'undefined') { test = new MaxHeap() } else { return false; }; test.insert(30); test.insert(300); test.insert(500); test.insert(10); let result = []; result.push(test.remove()); result.push(test.remove()); result.push(test.remove()); result.push(test.remove()); return (result.join('') == '5003003010') })());
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
var MaxHeap = function() {
|
||||
// change code below this line
|
||||
// change code above this line
|
||||
};
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,111 @@
|
||||
---
|
||||
id: 587d8251367417b2b2512c65
|
||||
title: Remove Elements from a Linked List by Index
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 按索引从链接列表中删除元素
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">在我们继续讨论另一个数据结构之前,让我们先了解链接列表的最后几点练习。让我们编写一个<code>removeAt</code>方法,删除给定<code>index</code>处的<code>element</code> 。该方法应该称为<code>removeAt(index)</code> 。要删除某个<code>index</code>处的<code>element</code> ,我们需要在沿着链表移动时保持每个节点的运行计数。用于遍历链表的元素的常用技术涉及<dfn>“转轮”</dfn>或“哨兵”,它们“指向”代码所比较的节点。在我们的情况下,开始于<code>head</code>我们的名单中,我们先从一个<code>currentIndex</code>始于变量<code>0</code> 。对于我们传递的每个节点, <code>currentIndex</code>应该增加1。就像我们的<code>remove(element)</code>方法一样,当我们在removeAt(index)方法中删除节点时,我们需要注意不要孤立列表的其余部分。我们通过确保引用已删除节点的节点具有对下一节点的引用来保持节点连续。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">编写<code>removeAt(index)</code>方法,删除并返回给定<code>index</code>处的节点。如果给定<code>index</code>为负数,或者大于或等于链表<code>length</code> ,则该方法应返回<code>null</code> 。注意请记住保持<code>currentIndex</code>计数。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 您的<code>LinkedList</code>类应该有一个<code>removeAt</code>方法。
|
||||
testString: 'assert((function(){var test = new LinkedList(); return (typeof test.removeAt === "function")}()), "Your <code>LinkedList</code> class should have a <code>removeAt</code> method.");'
|
||||
- text: 您的<code>removeAt</code>方法应该减少链表的<code>length</code>
|
||||
testString: 'assert((function(){var test = new LinkedList(); test.add("cat"); test.add("dog"); test.add("kitten"); test.removeAt(1); return test.size() === 2}()), "Your <code>removeAt</code> method should reduce the <code>length</code> of the linked list");'
|
||||
- text: 您的<code>removeAt</code>方法还应该返回已删除节点的元素。
|
||||
testString: 'assert((function(){var test = new LinkedList(); test.add("cat"); test.add("dog"); test.add("kitten"); return test.removeAt(1) === "dog"}()), "Your <code>removeAt</code> method should also return the element of the removed node.");'
|
||||
- text: 如果给定索引小于<code>0</code>则<code>removeAt</code>方法也应返回<code>null</code>
|
||||
testString: 'assert((function(){var test = new LinkedList(); test.add("cat"); test.add("dog"); test.add("kitten"); return (test.removeAt(-1) === null)}()), "Your <code>removeAt</code> method should also return <code>null</code> if the given index is less than <code>0</code>");'
|
||||
- text: 如果给定索引等于或大于链表的<code>length</code> ,则<code>removeAt</code>方法也应返回<code>null</code> 。
|
||||
testString: 'assert((function(){var test = new LinkedList(); test.add("cat"); test.add("dog"); test.add("kitten"); return (test.removeAt(3) === null)}()), "Your <code>removeAt</code> method should also return <code>null</code> if the given index is equal or more than the <code>length</code> of the linked list.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function LinkedList() {
|
||||
var length = 0;
|
||||
var head = null;
|
||||
|
||||
var Node = function(element){ // {1}
|
||||
this.element = element;
|
||||
this.next = null;
|
||||
};
|
||||
|
||||
this.size = function(){
|
||||
return length;
|
||||
};
|
||||
|
||||
this.head = function(){
|
||||
return head;
|
||||
};
|
||||
|
||||
this.add = function(element){
|
||||
var node = new Node(element);
|
||||
if(head === null){
|
||||
head = node;
|
||||
} else {
|
||||
var currentNode = head;
|
||||
|
||||
while(currentNode.next){
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
|
||||
currentNode.next = node;
|
||||
}
|
||||
|
||||
length++;
|
||||
};
|
||||
|
||||
this.remove = function(element){
|
||||
var currentNode = head;
|
||||
var previousNode;
|
||||
if(currentNode.element === element){
|
||||
head = currentNode.next;
|
||||
} else {
|
||||
while(currentNode.element !== element) {
|
||||
previousNode = currentNode;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
|
||||
previousNode.next = currentNode.next;
|
||||
}
|
||||
|
||||
length --;
|
||||
};
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
// Only change code above this line
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,94 @@
|
||||
---
|
||||
id: 587d8251367417b2b2512c63
|
||||
title: Remove Elements from a Linked List
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 从链接列表中删除元素
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">链接列表的任何实现所需的下一个重要方法是<code>remove</code>方法。此方法应将要删除的元素作为参数,然后搜索列表以查找并删除包含该元素的节点。每当我们从链表中删除一个节点时,重要的是我们不要意外地孤立列表的其余部分。回想一下,每个节点的<code>next</code>属性都指向列表中跟随它的节点。如果我们删除中间元素,比如说,我们要确保我们从该元素的前一个节点的<code>next</code>属性到中间元素的<code>next</code>属性(这是列表中的下一个节点)的连接!这可能听起来真的很混乱,所以让我们回到康加线的例子,这样我们就有了一个很好的概念模型。想象自己在康加舞线上,直接在你面前的人离开了这条线。刚离开生产线的人不再将手放在任何人身上 - 而且你不再把手放在离开的人身上。你向前走,把你的手放在你看到的下一个人身上。如果我们要删除的元素是<code>head</code>元素,我们将<code>head</code>重新分配给链表的第二个节点。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">编写一个<code>remove</code>方法,该方法接受一个元素并将其从链表中删除。注意每次从链接列表中删除元素时,列表的<code>length</code>应减少一。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 您的<code>LinkedList</code>类应该有一个<code>remove</code>方法。
|
||||
testString: 'assert((function(){var test = new LinkedList(); return (typeof test.remove === "function")}()), "Your <code>LinkedList</code> class should have a <code>remove</code> method.");'
|
||||
- text: <code>remove</code>第一个节点时, <code>remove</code>方法应重新分配<code>head</code>到第二个节点。
|
||||
testString: 'assert((function(){var test = new LinkedList(); test.add("cat"); test.add("dog"); test.remove("cat"); return test.head().element === "dog"}()), "Your <code>remove</code> method should reassign <code>head</code> to the second node when the first node is removed.");'
|
||||
- text: 对于每个删除的节点,您的<code>remove</code>方法应该将链表的<code>length</code>减少一个。
|
||||
testString: 'assert((function(){var test = new LinkedList(); test.add("cat"); test.add("dog"); test.remove("cat"); return test.size() === 1})(), "Your <code>remove</code> method should decrease the <code>length</code> of the linked list by one for every node removed.");'
|
||||
- text: 您的<code>remove</code>方法应该将已删除节点的上<code>next</code>节点的引用重新分配给已删除节点的<code>next</code>引用。
|
||||
testString: 'assert((function(){var test = new LinkedList(); test.add("cat"); test.add("dog");test.add("kitten"); test.remove("dog"); return test.head().next.element === "kitten"})(), "Your <code>remove</code> method should reassign the reference of the previous node of the removed node to the removed node's <code>next</code> reference.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function LinkedList() {
|
||||
var length = 0;
|
||||
var head = null;
|
||||
|
||||
var Node = function(element){
|
||||
this.element = element;
|
||||
this.next = null;
|
||||
};
|
||||
|
||||
this.size = function(){
|
||||
return length;
|
||||
};
|
||||
|
||||
this.head = function(){
|
||||
return head;
|
||||
};
|
||||
|
||||
this.add = function(element){
|
||||
var node = new Node(element);
|
||||
if(head === null){
|
||||
head = node;
|
||||
} else {
|
||||
var currentNode = head;
|
||||
|
||||
while(currentNode.next){
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
|
||||
currentNode.next = node;
|
||||
}
|
||||
|
||||
length++;
|
||||
};
|
||||
|
||||
this.remove = function(element){
|
||||
// Only change code below this line
|
||||
|
||||
// Only change code above this line
|
||||
};
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,55 @@
|
||||
---
|
||||
id: 587d8254367417b2b2512c71
|
||||
title: Remove items from a set in ES6
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 从ES6中的集中删除项目
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">让我们使用<code>delete</code>方法练习从ES6集中<code>delete</code> 。首先,创建一个ES6 Set <code>var set = new Set([1,2,3]);</code>现在使用<code>delete</code>方法从Set中删除一个项目。 <blockquote> set.delete(1); <br> console.log([... set])//应该返回[2,3] <blockquote></blockquote></blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">现在,创建一个整数为1,2,3,4和5的集合。删除值2和5,然后返回集合。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: '您的集应包含值1,3和4'
|
||||
testString: assert((function(){var test = checkSet(); return test.has(1) && test.has(3) && test.has(4) && test.size === 3;})());
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function checkSet(){
|
||||
var set = //Create a set with values 1, 2, 3, 4, & 5
|
||||
//Remove the value 2
|
||||
//Remove the value 5
|
||||
//Return the set
|
||||
return set;
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,78 @@
|
||||
---
|
||||
id: 587d825a367417b2b2512c88
|
||||
title: Reverse a Doubly Linked List
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 反转双重链接列表
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">让我们为我们的双向链表创建一个名为reverse的方法,它可以反转列表。一旦执行该方法,头部应指向前一个尾部,尾部应指向前一个头部。现在,如果我们从头到尾遍历列表,我们应该以与原始列表相反的顺序来满足节点。尝试反转空列表应返回null。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 存在DoublyLinkedList数据结构。
|
||||
testString: assert((function() { var test = false; if (typeof DoublyLinkedList !== 'undefined') { test = new DoublyLinkedList() }; return (typeof test == 'object')})());
|
||||
- text: DoublyLinkedList有一个名为add的方法。
|
||||
testString: assert((function() { var test = false; if (typeof DoublyLinkedList !== 'undefined') { test = new DoublyLinkedList() }; if (test.add == undefined) { return false; }; return (typeof test.add == 'function')})());
|
||||
- text: DoublyLinkedList有一个名为reverse的方法。
|
||||
testString: assert((function() { var test = false; if (typeof DoublyLinkedList !== 'undefined') { test = new DoublyLinkedList() }; if (test.reverse == undefined) { return false; }; return (typeof test.reverse == 'function')})());
|
||||
- text: 反转空列表将返回null。
|
||||
testString: assert((function() { var test = false; if (typeof DoublyLinkedList !== 'undefined') { test = new DoublyLinkedList() }; return (test.reverse() == null); })());
|
||||
- text: 反向方法反转列表。
|
||||
testString: assert((function() { var test = false; if (typeof DoublyLinkedList !== 'undefined') { test = new DoublyLinkedList() }; test.add(58); test.add(61); test.add(32); test.reverse(); return (test.print().join('') == '326158'); })());
|
||||
- text: 当列表反转时,正确维护下一个和上一个引用。
|
||||
testString: assert((function() { var test = false; if (typeof DoublyLinkedList !== 'undefined') { test = new DoublyLinkedList() }; test.add(11); test.add(22); test.add(33); test.reverse(); return (test.printReverse().join('') == '112233'); })());
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
var Node = function(data, prev) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = null;
|
||||
};
|
||||
var DoublyLinkedList = function() {
|
||||
this.head = null;
|
||||
this.tail = null;
|
||||
// change code below this line
|
||||
// change code above this line
|
||||
};
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,111 @@
|
||||
---
|
||||
id: 587d8251367417b2b2512c64
|
||||
title: Search within a Linked List
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 在链接列表中搜索
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">让我们为链表类添加一些更有用的方法。如果我们可以判断我们的列表是否为空,那么它是否有用,就像我们的<code>Stack</code>和<code>Queue</code>类一样?我们还应该能够在链表中找到特定元素。遍历数据结构是你想要进行大量练习的东西!让我们创建一个<code>indexOf</code>方法,该方法将<code>element</code>作为参数,并在链表中返回该元素的<code>index</code> 。如果在链接列表中找不到该元素,则返回<code>-1</code> 。让我们实现一个相反的方法:一个<code>elementAt</code>方法,它将<code>index</code>作为参数并返回给定<code>index</code>处的<code>element</code> 。如果未找到任何<code>element</code> ,则返回<code>undefined</code> 。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">编写一个检查链表是否为空的<code>isEmpty</code>方法,返回给定元素<code>index</code>的<code>indexOf</code>方法,以及返回给定<code>index.</code>处<code>element</code>的<code>elementAt</code> <code>index.</code> </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 您的<code>LinkedList</code>类应该有一个<code>indexOf</code>方法。
|
||||
testString: assert((function(){var test = new LinkedList(); return (typeof test.indexOf === 'function')}()));
|
||||
- text: 您的<code>LinkedList</code>类应该有一个<code>elementAt</code>方法。
|
||||
testString: assert((function(){var test = new LinkedList(); return (typeof test.elementAt === 'function')}()));
|
||||
- text: 您的<code>size</code>方法应返回链表的长度
|
||||
testString: assert((function(){var test = new LinkedList(); test.add('cat'); test.add('dog'); test.add('kitten'); return test.size() === 3}()));
|
||||
- text: <code>indexOf</code>方法应该返回给定元素的索引。
|
||||
testString: assert((function(){var test = new LinkedList(); test.add('cat'); test.add('dog'); test.add('kitten'); return test.indexOf('kitten') === 2}()));
|
||||
- text: 您的<code>elementAt</code>方法应该返回给定索引处的元素。
|
||||
testString: assert((function(){var test = new LinkedList(); test.add('cat'); test.add('dog'); test.add('kitten'); return test.elementAt(1) === 'dog'}()));
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function LinkedList() {
|
||||
var length = 0;
|
||||
var head = null;
|
||||
|
||||
var Node = function(element){ // {1}
|
||||
this.element = element;
|
||||
this.next = null;
|
||||
};
|
||||
|
||||
this.size = function() {
|
||||
return length;
|
||||
};
|
||||
|
||||
this.head = function(){
|
||||
return head;
|
||||
};
|
||||
|
||||
this.add = function(element){
|
||||
var node = new Node(element);
|
||||
if(head === null){
|
||||
head = node;
|
||||
} else {
|
||||
var currentNode = head;
|
||||
|
||||
while(currentNode.next){
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
|
||||
currentNode.next = node;
|
||||
}
|
||||
|
||||
length++;
|
||||
};
|
||||
|
||||
this.remove = function(element){
|
||||
var currentNode = head;
|
||||
var previousNode;
|
||||
if(currentNode.element === element){
|
||||
head = currentNode.next;
|
||||
} else {
|
||||
while(currentNode.element !== element) {
|
||||
previousNode = currentNode;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
|
||||
previousNode.next = currentNode.next;
|
||||
}
|
||||
|
||||
length --;
|
||||
};
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
// Only change code above this line
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,54 @@
|
||||
---
|
||||
id: 587d8253367417b2b2512c6a
|
||||
title: Typed Arrays
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 键入的数组
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">数组是可以容纳许多不同元素的JavaScript对象。 <code>var complexArr = [1, 5, "2", "Word", {"name": "James"}];</code>基本上后台发生的事情是您的浏览器会自动为该阵列提供适当的内存空间。如果添加或删除数据,它也会根据需要进行更改。但是,在高性能和不同元素类型的世界中,有时您需要更具体地了解为阵列提供多少内存。 <dfn>类型化数组</dfn>是这个问题的答案。您现在可以说要为阵列提供多少内存。下面是可用的不同类型数组的基本概述,以及该数组中每个元素的大小(以字节为单位)。 <table class="table table-striped"><tbody><tr><th>类型</th><th>每个元素大小以字节为单位</th></tr><tr><td> <code>Int8Array</code> </td> <td> 1 </td></tr><tr><td> <code>Uint8Array</code> </td> <td> 1 </td></tr><tr><td> <code>Uint8ClampedArray</code> </td> <td> 1 </td></tr><tr><td> <code>Int16Array</code> </td> <td> 2 </td></tr><tr><td> <code>Uint16Array</code> </td> <td> 2 </td></tr><tr><td> <code>Int32Array</code> </td> <td> 4 </td></tr><tr><td> <code>Uint32Array</code> </td> <td> 4 </td></tr><tr><td> <code>Float32Array</code> </td> <td> 4 </td></tr><tr><td> <code>Float64Array</code> </td> <td> 8 </td></tr></tbody></table>创建这种类型的数组有两种方法。一种方法是直接创建它。下面是如何创建一个3长度的<code>Int16Array</code> 。 <blockquote> var i8 = new Int16Array(3); <br>的console.log(I8); <br> //返回[0,0,0] </blockquote>您还可以创建一个<dfn>缓冲区</dfn>来分配您希望数组占用多少数据(以字节为单位)。 <strong>注意</strong> <br>要使用缓冲区创建类型化数组,需要将字节数分配为上面列出的字节的倍数。 <blockquote> //以不同方式创建相同的Int16Array数组<br> var byteSize = 6; //需要是2的倍数<br> var buffer = new ArrayBuffer(byteSize); <br> var i8View = new Int16Array(buffer); <br> buffer.byteLength; //返回6 <br> i8View.byteLength; //返回6 <br>的console.log(i8View); //返回[0,0,0] </blockquote> <dfn>缓冲区</dfn>是仅承载数据的通用对象。您无法正常访问它们。要访问它们,您需要先创建一个<dfn>视图</dfn> 。 <blockquote> i8View [0] = 42; <br>的console.log(i8View); //返回[42,0,0] </blockquote> <strong>注意</strong> <br>类型化数组没有传统数组所具有的某些方法,如<code>.pop()</code>或<code>.push()</code> 。类型化数组也会失败<code>Array.isArray()</code> ,它会检查某些内容是否为数组。虽然更简单,但对于不太复杂的JavaScript引擎来说,这可能是一个优势。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">首先创建一个64字节的<code>buffer</code> 。然后创建一个<code>Int32Array</code>类型数组,其中包含一个名为<code>i32View</code>的视图。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 您的<code>buffer</code>应该是64字节大。
|
||||
testString: assert(buffer.byteLength === 64);
|
||||
- text: 您的缓冲区的<code>i32View</code>视图应该是64字节大。
|
||||
testString: assert(i32View.byteLength === 64);
|
||||
- text: 您的缓冲区的<code>i32View</code>视图应为16个元素长。
|
||||
testString: assert(i32View.length === 16);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
var buffer;
|
||||
var i32View;
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,57 @@
|
||||
---
|
||||
id: 587d8255367417b2b2512c72
|
||||
title: Use .has and .size on an ES6 Set
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 在ES6集上使用.has和.size
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">让我们看一下ES6 Set对象上可用的.has和.size方法。首先,创建一个ES6 Set <code>var set = new Set([1,2,3]);</code> .has方法将检查该值是否包含在集合中。 <code>var hasTwo = set.has(2);</code> .size方法将返回一个表示Set <code>var howBig = set.size;</code>大小的整数<code>var howBig = set.size;</code> </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">在本练习中,我们将数组和值传递给checkSet()函数。您的函数应该从数组参数创建ES6集。查找该集是否包含value参数。找到集合的大小。并在数组中返回这两个值。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: '<code>checkSet([4, 5, 6], 3)</code>应该返回[false,3]'
|
||||
testString: 'assert((function(){var test = checkSet([4,5,6], 3); return DeepEqual(test, [ false, 3 ]);})());'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function checkSet(arrToBeSet, checkValue){
|
||||
|
||||
// change code below this line
|
||||
|
||||
// change code above this line
|
||||
|
||||
}
|
||||
|
||||
checkSet([ 1, 2, 3], 2); // Should return [ true, 3 ]
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,80 @@
|
||||
---
|
||||
id: 587d8258367417b2b2512c7f
|
||||
title: Use Breadth First Search in a Binary Search Tree
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 在二叉搜索树中使用广度优先搜索
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">这里我们将介绍另一种树遍历方法:广度优先搜索。与上一次挑战中的深度优先搜索方法相比,广度优先搜索在继续进入下一级别之前探索树中给定级别中的所有节点。通常,队列在广度优先搜索算法的设计中用作辅助数据结构。在此方法中,我们首先将根节点添加到队列中。然后我们开始一个循环,我们将队列中的第一个项目出列,将其添加到一个新数组,然后检查它们的子子树。如果它的孩子不是空的,他们每个都被排队。此过程将继续,直到队列为空。说明:让我们在树中创建一个名为<code>levelOrder</code>的广度优先搜索方法。此方法应返回一个包含所有树节点值的数组,并以广度优先的方式进行探索。确保返回数组中的值,而不是节点本身。应从左到右遍历一个级别。接下来,让我们编写一个名为<code>reverseLevelOrder</code>的类似方法,它在每个级别执行相同的搜索,但是反向(从右到左)。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 存在<code>BinarySearchTree</code>数据结构。
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() }; return (typeof test == 'object')})());
|
||||
- text: 二叉搜索树有一个名为<code>levelOrder</code>的方法。
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; return (typeof test.levelOrder == 'function')})());
|
||||
- text: 二叉搜索树有一个名为<code>reverseLevelOrder</code>的方法。
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; return (typeof test.reverseLevelOrder == 'function')})());
|
||||
- text: <code>levelOrder</code>方法返回按级别顺序探索的树节点值的数组。
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; if (typeof test.levelOrder !== 'function') { return false; }; test.add(7); test.add(1); test.add(9); test.add(0); test.add(3); test.add(8); test.add(10); test.add(2); test.add(5); test.add(4); test.add(6); return (test.levelOrder().join('') == '719038102546'); })());
|
||||
- text: <code>reverseLevelOrder</code>方法返回以反向级别顺序探索的树节点值的数组。
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; if (typeof test.reverseLevelOrder !== 'function') { return false; }; test.add(7); test.add(1); test.add(9); test.add(0); test.add(3); test.add(8); test.add(10); test.add(2); test.add(5); test.add(4); test.add(6); return (test.reverseLevelOrder().join('') == '791108305264'); })());
|
||||
- text: <code>levelOrder</code>方法为空树返回<code>null</code> 。
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; if (typeof test.levelOrder !== 'function') { return false; }; return (test.levelOrder() == null); })());
|
||||
- text: <code>reverseLevelOrder</code>方法为空树返回<code>null</code> 。
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; if (typeof test.reverseLevelOrder !== 'function') { return false; }; return (test.reverseLevelOrder() == null); })());
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
var displayTree = (tree) => console.log(JSON.stringify(tree, null, 2));
|
||||
function Node(value) {
|
||||
this.value = value;
|
||||
this.left = null;
|
||||
this.right = null;
|
||||
}
|
||||
function BinarySearchTree() {
|
||||
this.root = null;
|
||||
// change code below this line
|
||||
// change code above this line
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,86 @@
|
||||
---
|
||||
id: 587d8257367417b2b2512c7e
|
||||
title: Use Depth First Search in a Binary Search Tree
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 在二叉搜索树中使用深度优先搜索
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">我们知道如何在二叉搜索树中搜索特定值。但是,如果我们只想探索整棵树呢?或者,如果我们没有有序树,我们只需要搜索一个值?这里我们将介绍一些可用于探索树数据结构的树遍历方法。首先是深度优先搜索。在深度优先搜索中,在搜索继续到另一个子树之前,尽可能深地探索给定子树。有三种方法可以完成:按顺序:从最左边的节点开始搜索,到最右边的节点结束。预购:在树叶前探索所有的根。下订单:在根之前探索所有的叶子。您可能会猜到,您可以选择不同的搜索方法,具体取决于树存储的数据类型以及您要查找的内容。对于二叉搜索树,inorder遍历以排序顺序返回节点。说明:这里我们将在二叉搜索树上创建这三种搜索方法。深度优先搜索是一种固有的递归操作,只要子节点存在,它就会继续探索更多的子树。一旦理解了这个基本概念,您就可以简单地重新排列探索节点和子树的顺序,以生成上述三个搜索中的任何一个。例如,在后序搜索中,我们希望在开始返回任何节点本身之前一直递归到叶节点,而在预订搜索中,我们希望首先返回节点,然后继续递归在树下。在我们的树上定义<code>inorder</code> , <code>preorder</code>和<code>postorder</code>方法。这些方法中的每一个都应该返回表示树遍历的项数组。确保返回数组中每个节点的整数值,而不是节点本身。最后,如果树为空,则返回<code>null</code> 。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 存在<code>BinarySearchTree</code>数据结构。
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() }; return (typeof test == 'object')})());
|
||||
- text: 二叉搜索树有一个名为<code>inorder</code>的方法。
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; return (typeof test.inorder == 'function')})());
|
||||
- text: 二叉搜索树有一个名为<code>preorder</code>的方法。
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; return (typeof test.preorder == 'function')})());
|
||||
- text: 二叉搜索树有一个名为<code>postorder</code>的方法。
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; return (typeof test.postorder == 'function')})());
|
||||
- text: <code>inorder</code>方法返回由inorder遍历产生的节点值数组。
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; if (typeof test.inorder !== 'function') { return false; }; test.add(7); test.add(1); test.add(9); test.add(0); test.add(3); test.add(8); test.add(10); test.add(2); test.add(5); test.add(4); test.add(6); return (test.inorder().join('') == '012345678910'); })());
|
||||
- text: <code>preorder</code>方法返回由前序遍历产生的节点值数组。
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; if (typeof test.preorder !== 'function') { return false; }; test.add(7); test.add(1); test.add(9); test.add(0); test.add(3); test.add(8); test.add(10); test.add(2); test.add(5); test.add(4); test.add(6); return (test.preorder().join('') == '710325469810'); })());
|
||||
- text: <code>postorder</code>方法返回由后序遍历产生的节点值数组。
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; if (typeof test.postorder !== 'function') { return false; }; test.add(7); test.add(1); test.add(9); test.add(0); test.add(3); test.add(8); test.add(10); test.add(2); test.add(5); test.add(4); test.add(6); return (test.postorder().join('') == '024653181097'); })());
|
||||
- text: <code>inorder</code>方法为空树返回<code>null</code> 。
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; if (typeof test.inorder !== 'function') { return false; }; return (test.inorder() == null); })());
|
||||
- text: <code>preorder</code>方法为空树返回<code>null</code> 。
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; if (typeof test.preorder !== 'function') { return false; }; return (test.preorder() == null); })());
|
||||
- text: <code>postorder</code>方法为空树返回<code>null</code> 。
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; if (typeof test.postorder !== 'function') { return false; }; return (test.postorder() == null); })());
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
var displayTree = (tree) => console.log(JSON.stringify(tree, null, 2));
|
||||
function Node(value) {
|
||||
this.value = value;
|
||||
this.left = null;
|
||||
this.right = null;
|
||||
}
|
||||
function BinarySearchTree() {
|
||||
this.root = null;
|
||||
// change code below this line
|
||||
// change code above this line
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,53 @@
|
||||
---
|
||||
id: 587d8255367417b2b2512c73
|
||||
title: Use Spread and Notes for ES5 Set() Integration
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 使用Spread和Notes进行ES5 Set()集成
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">你还记得ES6传播运营商<code>...</code> ? <code>...</code>可以在ES6中获取可迭代对象并将它们转换为数组。让我们创建一个Set,并检查传播函数。 <blockquote> var set = new Set([1,2,3]); <br> var setToArr = [... set] <br> console.log(setToArr)//返回[1,2,3] </blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">在本练习中,我们将set对象传递给<code>checkSet</code>函数。它应该返回一个包含Set值的数组。现在你已经成功学会了如何使用ES6 <code>Set()</code>对象,干得好! </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 您的套装已正确退回!
|
||||
testString: 'assert((function(){var test = checkSet(new Set([1,2,3,4,5,6,7])); return DeepEqual(test, [ 1, 2, 3, 4, 5, 6, 7 ]);})());'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function checkSet(set){
|
||||
// change code below this line
|
||||
|
||||
// change code above this line
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,62 @@
|
||||
---
|
||||
id: 587d8251367417b2b2512c61
|
||||
title: Work with Nodes in a Linked List
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 使用链接列表中的节点
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">您将在计算机科学中遇到的另一个常见数据结构是<dfn>链表</dfn> 。链表是数据元素的线性集合,称为“节点”,每个数据元素指向下一个。链表中的每个<dfn>节点都</dfn>包含两个关键信息: <code>element</code>本身和对下一个<code>node</code>的引用。想象一下你在康加舞线上。你的手掌握在线下的下一个人身上,你身后的人就会抓住你。你可以直接看到这个人,但是他们阻挡了前方其他人的视线。一个节点就像一个康加舞线上的人:他们知道自己是谁,他们只能看到下一个人,但他们并不知道前方或后方的其他人。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">在我们的代码编辑器中,我们创建了两个节点, <code>Kitten</code>和<code>Puppy</code> ,我们手动将<code>Kitten</code>节点连接到<code>Puppy</code>节点。创建<code>Cat</code>和<code>Dog</code>节点并手动将它们添加到该行。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 您的<code>Puppy</code>节点应该具有对<code>Cat</code>节点的引用。
|
||||
testString: assert(Puppy.next.element === "Cat");
|
||||
- text: 您的<code>Cat</code>节点应该具有对<code>Dog</code>节点的引用。
|
||||
testString: assert(Cat.next.element === "Dog");
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
var Node = function(element){
|
||||
this.element = element;
|
||||
this.next = null;
|
||||
};
|
||||
var Kitten = new Node("Kitten");
|
||||
var Puppy = new Node("Puppy");
|
||||
|
||||
Kitten.next = Puppy;
|
||||
// only add code below this line
|
||||
|
||||
// test your code
|
||||
console.log(Kitten.next);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,64 @@
|
||||
---
|
||||
id: 5900f36e1000cf542c50fe80
|
||||
challengeType: 5
|
||||
title: 'Problem 1: Multiples of 3 and 5'
|
||||
videoUrl: ''
|
||||
localeTitle: 问题1:3和5的倍数
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">
|
||||
如果我们列出所有10以下是3或5的倍数的自然数,我们会得到3,5,6和9。这些倍数的总和是23。
|
||||
求出所有在<code>number</code>以下的3或5的倍数的总和。
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>multiplesOf3and5(1000)</code>应该返回233168。
|
||||
testString: assert.strictEqual(multiplesOf3and5(1000), 233168);
|
||||
- text: <code>multiplesOf3and5(49)</code>应该返回543。
|
||||
testString: assert.strictEqual(multiplesOf3and5(49), 543);
|
||||
- text: <code>multiplesOf3and5(19564)</code>应该返回89301183。
|
||||
testString: assert.strictEqual(multiplesOf3and5(19564), 89301183);
|
||||
- text: 您的函数未使用我们的测试值返回正确的结果。
|
||||
testString: assert.strictEqual(multiplesOf3and5(8456), 16687353);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function multiplesOf3and5(number) {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
multiplesOf3and5(1000);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,61 @@
|
||||
---
|
||||
id: 5900f3761000cf542c50fe89
|
||||
challengeType: 5
|
||||
title: 'Problem 10: Summation of primes'
|
||||
videoUrl: ''
|
||||
localeTitle: 问题10:素数的总和
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">低于10的素数之和为2 + 3 + 5 + 7 = 17.求出n以下所有素数的总和。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>primeSummation(17)</code>应该返回41。
|
||||
testString: assert.strictEqual(primeSummation(17), 41);
|
||||
- text: <code>primeSummation(2001)</code>应该返回277050。
|
||||
testString: assert.strictEqual(primeSummation(2001), 277050);
|
||||
- text: <code>primeSummation(140759)</code>应该返回873608362。
|
||||
testString: assert.strictEqual(primeSummation(140759), 873608362);
|
||||
- text: <code>primeSummation(2000000)</code>应返回142913828922。
|
||||
testString: assert.strictEqual(primeSummation(2000000), 142913828922);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function primeSummation(n) {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
primeSummation(2000000);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,55 @@
|
||||
---
|
||||
id: 5900f3d01000cf542c50fee3
|
||||
challengeType: 5
|
||||
title: 'Problem 100: Arranged probability'
|
||||
videoUrl: ''
|
||||
localeTitle: 问题100:安排概率
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">如果一个盒子包含21个彩色光盘,由15个蓝色光盘和6个红色光盘组成,随机拍摄两张光盘,可以看出拍摄两张蓝色光盘的概率,P(BB)=(15/21) )×(14/20)= 1/2。下一个这样的安排,其中有50%的机会随机拍摄两张蓝色光盘,是一个包含八十五个蓝色光盘和三十五个红色光盘的盒子。通过找到第一个包含总共超过1012 = 1,000,000,000,000个光盘的布置,确定该盒子将包含的蓝色光盘的数量。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>euler100()</code>应该返回756872327473。
|
||||
testString: assert.strictEqual(euler100(), 756872327473);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function euler100() {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
euler100();
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,55 @@
|
||||
---
|
||||
id: 5900f3d21000cf542c50fee4
|
||||
challengeType: 5
|
||||
title: 'Problem 101: Optimum polynomial'
|
||||
videoUrl: ''
|
||||
localeTitle: 问题101:最佳多项式
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">如果我们被给出序列的前k个项,则不可能肯定地说下一个项的值,因为存在无限多个可以对序列建模的多项式函数。举个例子,让我们考虑一下立方体数字的顺序。这由生成函数定义,un = n3:1,8,27,64,125,216 ......假设我们只给出了该序列的前两个项。根据“简单就是最好”的原则,我们应该假设一个线性关系,并预测下一个项为15(公共差异7)。即使我们被提出前三个术语,按照相同的简单原则,也应假设二次关系。我们将OP(k,n)定义为序列的前k个项的最佳多项式生成函数的第n项。应该清楚的是,OP(k,n)将准确地生成n≤k的序列项,并且可能第一个不正确的项(FIT)将是OP(k,k + 1);在这种情况下,我们将其称为坏OP(BOP)。作为一个基础,如果我们只给出第一个序列项,那么假设恒定是最明智的;也就是说,对于n≥2,OP(1,n)= u1。因此,我们获得了立方序列的以下OP: <p> OP(1,n)= 11 1,1,1,1 ...... OP(2,n)= 7n-6 1,8,15,...... OP(3,n)= 6n2-11n + 6 1,8,27,58,... OP(4,n)= n3 1,8,27,64,125,...... </p><p>显然,对于k≥4,不存在BOP。通过考虑BOP产生的FIT之和(以红色表示),我们得到1 + 15 + 58 = 74.考虑下面的十度多项式生成函数:un = 1 - n + n2 - n3 + n4 - n5 + n6 - n7 + n8 - n9 + n10求BOP的FIT之和。 </p></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>euler101()</code>应该返回37076114526。
|
||||
testString: assert.strictEqual(euler101(), 37076114526);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function euler101() {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
euler101();
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,55 @@
|
||||
---
|
||||
id: 5900f3d21000cf542c50fee5
|
||||
challengeType: 5
|
||||
title: 'Problem 102: Triangle containment'
|
||||
videoUrl: ''
|
||||
localeTitle: 问题102:三角形遏制
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">在笛卡尔平面上随机绘制三个不同的点,其中-1000≤x,y≤1000,从而形成三角形。考虑以下两个三角形:A(-340,495),B(-153,-910),C(835,-947)X(-175,41),Y(-421,-714),Z(574, - 645)可以验证三角形ABC包含原点,而三角形XYZ不包含原点。使用triangles.txt(右键单击并将“Save Link / Target As ...”保存),包含一千个“随机”三角形坐标的27K文本文件,找到内部包含原点的三角形数。注意:文件中的前两个示例表示上面给出的示例中的三角形。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>euler102()</code>应该返回228。
|
||||
testString: assert.strictEqual(euler102(), 228);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function euler102() {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
euler102();
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,55 @@
|
||||
---
|
||||
id: 5900f3d61000cf542c50fee7
|
||||
challengeType: 5
|
||||
title: 'Problem 103: Special subset sums: optimum'
|
||||
videoUrl: ''
|
||||
localeTitle: 问题103:特殊子集和:最佳
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">设S(A)表示大小为n的集合A中的元素之和。如果对于任何两个非空的不相交子集B和C,我们将其称为特殊和集合,以下属性为真:S(B)≠S(C);也就是说,子集的总和不能相等。如果B包含的元素多于C,则S(B)> S(C)。如果对于给定的n,S(A)被最小化,我们将其称为最优的特殊和集。下面给出前五个最佳特殊和集。 n = 1:{1} n = 2:{1,2} n = 3:{2,3,4} n = 4:{3,5,6,7} n = 5:{6,9,11 ,12,13}似乎对于给定的最优集合,A = {a1,a2,...,an},下一个最优集合的形式为B = {b,a1 + b,a2 + b,... ..,an + b},其中b是前一行的“中间”元素。通过应用这个“规则”,我们期望n = 6的最优集合为A = {11,17,20,22,23,24},其中S(A)= 117.但是,这不是最佳集合因为我们仅应用算法来提供接近最优的集合。 n = 6的最佳集合是A = {11,18,19,20,22,25},其中S(A)= 115并且对应的集合字符串:111819202225。假设A是n =的最优特殊和集合7,找到它的设置字符串。注意:此问题与问题105和问题106有关。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>euler103()</code>应该返回20313839404245。
|
||||
testString: assert.strictEqual(euler103(), 20313839404245);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function euler103() {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
euler103();
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,55 @@
|
||||
---
|
||||
id: 5900f3d51000cf542c50fee6
|
||||
challengeType: 5
|
||||
title: 'Problem 104: Pandigital Fibonacci ends'
|
||||
videoUrl: ''
|
||||
localeTitle: 问题104:Pandigital Fibonacci结束
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"> Fibonacci序列由递归关系定义:Fn = Fn-1 + Fn-2,其中F1 = 1且F2 = 1.事实证明,包含113位数字的F541是第一个斐波那契数字,其中最后九个数字是1-9 pandigital(包含所有数字1到9,但不一定按顺序)。 F2749包含575个数字,是第一个斐波那契数字,前九个数字是1-9 pandigital。鉴于Fk是第一个斐波纳契数,前九个数字和后九个数字是1-9 pandigital,找到k。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>euler104()</code>应返回329468。
|
||||
testString: assert.strictEqual(euler104(), 329468);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function euler104() {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
euler104();
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,55 @@
|
||||
---
|
||||
id: 5900f3d61000cf542c50fee8
|
||||
challengeType: 5
|
||||
title: 'Problem 105: Special subset sums: testing'
|
||||
videoUrl: ''
|
||||
localeTitle: 问题105:特殊子集总和:测试
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">设S(A)表示大小为n的集合A中的元素之和。如果对于任何两个非空的不相交子集B和C,我们将其称为特殊和集合,以下属性为真:S(B)≠S(C);也就是说,子集的总和不能相等。如果B包含的元素多于C,则S(B)> S(C)。例如,{81,88,75,42,87,84,86,65}不是一个特殊的和集,因为65 + 87 + 88 = 75 + 81 + 84,而{157,150,164,119,79 ,159,161,139,158}满足所有可能的子集对组合的规则,并且S(A)= 1286.使用sets.txt(右键单击并“将链接/目标另存为...”),4K文本文件包含七到十二个元素的一百个集合(上面给出的两个例子是文件中的前两个集合),识别所有特殊的和集,A1,A2,...,Ak,并找到S的值( A1)+ S(A2)+ ... + S(Ak)。注意:此问题与问题103和问题106有关。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>euler105()</code>应返回73702。
|
||||
testString: assert.strictEqual(euler105(), 73702);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function euler105() {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
euler105();
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,55 @@
|
||||
---
|
||||
id: 5900f3d71000cf542c50fee9
|
||||
challengeType: 5
|
||||
title: 'Problem 106: Special subset sums: meta-testing'
|
||||
videoUrl: ''
|
||||
localeTitle: 问题106:特殊子集和:元测试
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">设S(A)表示大小为n的集合A中的元素之和。如果对于任何两个非空的不相交子集B和C,我们将其称为特殊和集合,以下属性为真:S(B)≠S(C);也就是说,子集的总和不能相等。如果B包含的元素多于C,则S(B)> S(C)。对于这个问题,我们假设给定的集合包含n个严格增加的元素,并且它已经满足第二个规则。令人惊讶的是,在可以从n = 4的集合中获得的25个可能的子集对中,仅需要对这些对中的1个进行相等性测试(第一规则)。类似地,当n = 7时,仅需要测试966个子集对中的70个。对于n = 12,可以获得多少261625个子集对需要进行相等性测试?注意:此问题与问题103和问题105有关。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>euler106()</code>应返回21384。
|
||||
testString: assert.strictEqual(euler106(), 21384);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function euler106() {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
euler106();
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,55 @@
|
||||
---
|
||||
id: 5900f3d91000cf542c50feea
|
||||
challengeType: 5
|
||||
title: 'Problem 107: Minimal network'
|
||||
videoUrl: ''
|
||||
localeTitle: 问题107:最小网络
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">以下无向网络由七个顶点和十二个边组成,总权重为243。 <p>相同的网络可以由下面的矩阵表示。 ABCDEFG A-161221 --- B16--1720-- C12--28-31- D211728-181923 E-20-18--11 F-3119--27 G --- 231127-但是,有可能通过删除一些边缘来优化网络,并仍然确保网络上的所有点保持连接。实现最大节省的网络如下所示。它的权重为93,比原始网络节省了243 - 93 = 150。 </p><p>使用network.txt(右键单击并“保存链接/目标为...”),一个6K文本文件包含一个具有四十个顶点的网络,并以矩阵形式给出,找到通过删除冗余边缘可以实现的最大节省确保网络保持连接。 </p></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>euler107()</code>应该返回259679。
|
||||
testString: assert.strictEqual(euler107(), 259679);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function euler107() {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
euler107();
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,55 @@
|
||||
---
|
||||
id: 5900f3d91000cf542c50feeb
|
||||
challengeType: 5
|
||||
title: 'Problem 108: Diophantine Reciprocals I'
|
||||
videoUrl: ''
|
||||
localeTitle: 问题108:丢番图互惠I
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">在下面的等式中,x,y和n是正整数。 1 / <var>x</var> + 1 / <var>y</var> = 1 / <var>n</var>对于<var>n</var> = 4,恰好有三种不同的解:1/5 + 1/20 = 1/4 <br> 1/6 + 1/12 = 1/4 <br> 1/8 + 1/8 = 1/4不同解的数量超过一千的<var>n的</var>最小值是多少? </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: diophantineOne <code>diophantineOne()</code>应返回180180。
|
||||
testString: assert.strictEqual(diophantineOne(), 180180);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function diophantineOne() {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
diophantineOne();
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,55 @@
|
||||
---
|
||||
id: 5900f3db1000cf542c50feec
|
||||
challengeType: 5
|
||||
title: 'Problem 109: Darts'
|
||||
videoUrl: ''
|
||||
localeTitle: 问题109:飞镖
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">在飞镖游戏中,玩家在目标板上投掷三个飞镖,该目标板被分成二十个相等大小的部分,编号为一到二十。 <p>飞镖的得分由飞镖着陆区域的数量决定。红色/绿色外圈外的飞镖着陆得分为零。该戒指内的黑色和奶油色区域代表单个分数。然而,红色/绿色外圈和中圈分别得分为双倍和高音。在董事会的中心是两个同心圆,称为公牛区或公牛眼。外面的公牛价值25点,内部公牛是双倍,价值50点。规则有很多变化,但在最流行的游戏中,玩家将以301或501的分数开始,并且第一个将他们的总跑动总数减少到零的玩家是胜利者。然而,玩“双打”系统是正常的,这意味着玩家必须在他们的最终飞镖上获得双倍(包括在棋盘中央的双靶心)才能获胜;任何其他飞镖都会将他们的跑动总数减少到一个或更低意味着这组三个飞镖的得分是“半身像”。当玩家能够完成他们当前的分数时,它被称为“结账”,最高结账时间是170:T20 T20 D25(两个高音20和双牛)。有6种截然不同的结账方式,分数为6: </p><p> D3 </p><p> D1 D2 </p><p> S2 D2 </p><p> D2 D1 </p><p> S4 D1 </p><p> S1 S1 D2 S1 T1 D1 S1 S3 D1 D1 D1 D1 D1 S2 D1 S2 S2 D1 </p><p>请注意,D1 D2被认为与D2 D1不同,因为它们在不同的双打上完成。但是,组合S1 T1 D1被认为与T1 S1 D1相同。此外,我们不会在考虑组合时包含未命中数;例如,D3与0 D3和0 0 D3相同。令人难以置信的是,共有42336种不同的检查方式。玩家以低于100的分数结账有多少种不同的方式? </p></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>euler109()</code>应返回38182。
|
||||
testString: assert.strictEqual(euler109(), 38182);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function euler109() {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
euler109();
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,89 @@
|
||||
---
|
||||
id: 5900f3781000cf542c50fe8a
|
||||
challengeType: 5
|
||||
title: 'Problem 11: Largest product in a grid'
|
||||
videoUrl: ''
|
||||
localeTitle: 问题11:网格中最大的产品
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">在下面的20×20网格中,沿对角线的四个数字标记为红色。 <div style="text-align: center;"> 08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08 </div><div style="text-align: center;"> 49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00 </div><div style="text-align: center;"> 81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65 </div><div style="text-align: center;"> 52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91 </div><div style="text-align: center;"> 22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80 </div><div style="text-align: center;"> 24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50 </div><div style="text-align: center;"> 32 98 81 28 64 23 67 10 <span style="color: red"><b>26</b></span> 38 40 67 59 54 70 66 18 38 64 70 </div><div style="text-align: center;"> 67 26 20 68 02 62 12 20 95 <span style="color: red"><b>63</b></span> 94 39 63 08 40 91 66 49 94 21 </div><div style="text-align: center;"> 24 55 58 05 66 73 99 26 97 17 <span style="color: red"><b>78</b></span> 78 96 83 14 88 34 89 63 72 </div><div style="text-align: center;"> 21 36 23 09 75 00 76 44 20 45 35 <span style="color: red"><b>14</b></span> 00 61 33 97 34 31 33 95 </div><div style="text-align: center;"> 78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92 </div><div style="text-align: center;"> 16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57 </div><div style="text-align: center;"> 86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58 </div><div style="text-align: center;"> 19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40 </div><div style="text-align: center;"> 04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66 </div><div style="text-align: center;"> 88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69 </div><div style="text-align: center;"> 04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36 </div><div style="text-align: center;"> 20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16 </div><div style="text-align: center;"> 20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54 </div><div style="text-align: center;"> 01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48 </div><p>这些数字的乘积是26×63×78×14 = 1788696.在给定的<code>arr</code>网格中,相同方向(上,下,左,右或对角)的四个相邻数字的最大乘积是多少? </p></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>largestGridProduct(grid)</code>应该返回70600674。
|
||||
testString: assert.strictEqual(largestGridProduct(grid), 70600674);
|
||||
- text: <code>largestGridProduct(testGrid)</code>应该返回14169081。
|
||||
testString: assert.strictEqual(largestGridProduct(testGrid), 14169081);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function largestGridProduct(arr) {
|
||||
// Good luck!
|
||||
return arr;
|
||||
}
|
||||
|
||||
// Only change code above this line
|
||||
const grid = [
|
||||
[8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8],
|
||||
[49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 4, 56, 62, 0],
|
||||
[81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 3, 49, 13, 36, 65],
|
||||
[52, 70, 95, 23, 4, 60, 11, 42, 69, 24, 68, 56, 1, 32, 56, 71, 37, 2, 36, 91],
|
||||
[22, 31, 16, 71, 51, 67, 63, 89, 41, 92, 36, 54, 22, 40, 40, 28, 66, 33, 13, 80],
|
||||
[24, 47, 32, 60, 99, 3, 45, 2, 44, 75, 33, 53, 78, 36, 84, 20, 35, 17, 12, 50],
|
||||
[32, 98, 81, 28, 64, 23, 67, 10, 26, 38, 40, 67, 59, 54, 70, 66, 18, 38, 64, 70],
|
||||
[67, 26, 20, 68, 2, 62, 12, 20, 95, 63, 94, 39, 63, 8, 40, 91, 66, 49, 94, 21],
|
||||
[24, 55, 58, 5, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88, 34, 89, 63, 72],
|
||||
[21, 36, 23, 9, 75, 0, 76, 44, 20, 45, 35, 14, 0, 61, 33, 97, 34, 31, 33, 95],
|
||||
[78, 17, 53, 28, 22, 75, 31, 67, 15, 94, 3, 80, 4, 62, 16, 14, 9, 53, 56, 92],
|
||||
[16, 39, 5, 42, 96, 35, 31, 47, 55, 58, 88, 24, 0, 17, 54, 24, 36, 29, 85, 57],
|
||||
[86, 56, 0, 48, 35, 71, 89, 7, 5, 44, 44, 37, 44, 60, 21, 58, 51, 54, 17, 58],
|
||||
[19, 80, 81, 68, 5, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77, 4, 89, 55, 40],
|
||||
[4, 52, 8, 83, 97, 35, 99, 16, 7, 97, 57, 32, 16, 26, 26, 79, 33, 27, 98, 66],
|
||||
[88, 36, 68, 87, 57, 62, 20, 72, 3, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53, 69],
|
||||
[4, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18, 8, 46, 29, 32, 40, 62, 76, 36],
|
||||
[20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 4, 36, 16],
|
||||
[20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54],
|
||||
[1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48]
|
||||
];
|
||||
|
||||
const testGrid = [
|
||||
[40, 17, 81, 18, 57],
|
||||
[74, 4, 36, 16, 29],
|
||||
[36, 42, 69, 73, 45],
|
||||
[51, 54, 69, 16, 92],
|
||||
[7, 97, 57, 32, 16]
|
||||
];
|
||||
|
||||
largestGridProduct(testGrid);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,55 @@
|
||||
---
|
||||
id: 5900f3db1000cf542c50feed
|
||||
challengeType: 5
|
||||
title: 'Problem 110: Diophantine Reciprocals II'
|
||||
videoUrl: ''
|
||||
localeTitle: 问题110:丢番图互惠II
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">在下面的等式中,x,y和n是正整数。 1 / <var>x</var> + 1 / <var>y</var> = 1 / <var>n</var>可以证实,当<var>n</var> = 1260时,存在113个不同的解,并且这是<var>n的</var>最小值,其中不同解的总数超过100。 <var>n的</var>最小值是多少,不同解决方案的数量超过四百万? </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>diophantineTwo</code>应该返回9350130049860600。
|
||||
testString: assert.strictEqual(diophantineTwo(), 9350130049860600);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function diophantineTwo() {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
diophantineTwo();
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,55 @@
|
||||
---
|
||||
id: 5900f3db1000cf542c50feee
|
||||
challengeType: 5
|
||||
title: 'Problem 111: Primes with runs'
|
||||
videoUrl: ''
|
||||
localeTitle: 问题111:运行的Primes
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">考虑到包含重复数字的4位数素数,很明显它们不能全部相同:1111可被11整除,2222可被22整除,依此类推。但是有九个4位数的素数包含三个素数:1117,1151,1171,1181,1511,1811,2111,4111,8111我们将说M(n,d)表示n-的最大重复数位数。数字素数,其中d是重复数字,N(n,d)表示这些素数的数量,S(n,d)表示这些素数的总和。所以M(4,1)= 3是4位数的素数的最大重复位数,其中一个是重复的数字,有N(4,1)= 9个这样的素数,这些素数的总和是S (4,1)= 22275.事实证明,对于d = 0,只能使M(4,0)= 2个重复数字,但是存在N(4,0)= 13个这样的情况。同样地,我们获得了4位素数的以下结果。 <p>数字,d M(4,d)N(4,d)S(4,d)0 2 13 67061 1 3 9 22275 2 3 1 2221 3 3 12 46214 4 3 2 8888 5 3 1 5557 6 3 1 6661 7 3 9 57863 8 3 1 8887 9 3 7 48073 </p><p>对于d = 0到9,所有S(4,d)的总和是273700.求所有S(10,d)的总和。 </p></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>euler111()</code>应返回612407567715。
|
||||
testString: assert.strictEqual(euler111(), 612407567715);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function euler111() {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
euler111();
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,55 @@
|
||||
---
|
||||
id: 5900f3dd1000cf542c50feef
|
||||
challengeType: 5
|
||||
title: 'Problem 112: Bouncy numbers'
|
||||
videoUrl: ''
|
||||
localeTitle: 问题112:有弹性的数字
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">如果左边的数字没有超过数字,则从左到右工作,称为递增数字;例如,134468。同样,如果右边的数字没有超过数字,则称为递减数字;例如,66420。我们将调用一个既不增加也不减少“有弹性”数的正整数;例如,155349。显然,不会有任何低于一百的弹性数字,但只有超过一千(525)的数字超过一半是有弹性的。事实上,有弹性数字首次达到50%的最小数量是538.令人惊讶的是,有弹性的数字变得越来越普遍,当我们达到21780时,有弹性数字的比例等于90%。找出有弹性数字的比例正好为99%的最小数字。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>euler112()</code>应返回1587000。
|
||||
testString: assert.strictEqual(euler112(), 1587000);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function euler112() {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
euler112();
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,55 @@
|
||||
---
|
||||
id: 5900f3dd1000cf542c50fef0
|
||||
challengeType: 5
|
||||
title: 'Problem 113: Non-bouncy numbers'
|
||||
videoUrl: ''
|
||||
localeTitle: 问题113:非弹性数字
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">如果左边的数字没有超过数字,则从左到右工作,称为递增数字;例如,134468。同样,如果右边的数字没有超过数字,则称为递减数字;例如,66420。我们将调用一个既不增加也不减少“有弹性”数的正整数;例如,随着n的增加,低于n的弹性数的比例增加,使得只有12951个数字低于一百万个而不是有弹性而只有277032个非有弹性的数字低于1010.一个数量低于一个googol(10100) )是不是有弹性? </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>euler113()</code>应该返回51161058134250。
|
||||
testString: assert.strictEqual(euler113(), 51161058134250);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function euler113() {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
euler113();
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,55 @@
|
||||
---
|
||||
id: 5900f3e01000cf542c50fef2
|
||||
challengeType: 5
|
||||
title: 'Problem 114: Counting block combinations I'
|
||||
videoUrl: ''
|
||||
localeTitle: 问题114:计数块组合I
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">测量七个单元长度的行具有红色块,其上放置有最小长度为三个单元,使得任何两个红色块(允许为不同长度)由至少一个黑色方块隔开。有七种方法可以做到这一点。 <p>一行可以测量多达50个长度的行数?注意:虽然上面的示例不适合这种可能性,但通常允许混合块大小。例如,在一个长度为8个单位的行上,您可以使用红色(3),黑色(1)和红色(4)。 </p></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>euler114()</code>应该返回16475640049。
|
||||
testString: assert.strictEqual(euler114(), 16475640049);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function euler114() {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
euler114();
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,55 @@
|
||||
---
|
||||
id: 5900f3df1000cf542c50fef1
|
||||
challengeType: 5
|
||||
title: 'Problem 115: Counting block combinations II'
|
||||
videoUrl: ''
|
||||
localeTitle: 问题115:计数块组合II
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">注意:这是问题114的更难的版本。测量n个单位长度的行具有红色块,其上放置最小长度为m个单位,使得任何两个红色块(允许不同长度)被分开至少有一个黑色方块。让fill-count函数F(m,n)表示可以填充行的方式的数量。例如,F(3,29)= 673135和F(3,30)= 1089155.也就是说,对于m = 3,可以看出n = 30是填充计数函数首次超过的最小值一百万。同样,对于m = 10,可以验证F(10,56)= 880711和F(10,57)= 1148904,因此n = 57是填充计数函数首次超过的最小值一百万。对于m = 50,找到填充计数函数首先超过一百万的n的最小值。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>euler115()</code>应返回168。
|
||||
testString: assert.strictEqual(euler115(), 168);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function euler115() {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
euler115();
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,55 @@
|
||||
---
|
||||
id: 5900f3e01000cf542c50fef3
|
||||
challengeType: 5
|
||||
title: 'Problem 116: Red, green or blue tiles'
|
||||
videoUrl: ''
|
||||
localeTitle: 问题116:红色,绿色或蓝色瓷砖
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">一排五个黑色正方形瓷砖是用彩色椭圆形瓷砖代替的,其中红色(长度为2),绿色(长度为3)或蓝色(长度为4)。如果选择红色瓷砖,则可以通过七种方式完成此操作。 <p>如果选择绿色瓷砖,有三种方法。 </p><p>如果选择蓝色瓷砖,有两种方法。 </p><p>假设颜色不能混合,则有7 + 3 + 2 = 12种方式替换长度为五个单位的行中的黑色瓷砖。如果不能混合颜色并且必须使用至少一个彩色瓷砖,那么连续测量五十个单位的黑色瓷砖有多少种不同的方式可以替换?注意:这与问题117有关。 </p></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>euler116()</code>应该返回20492570929。
|
||||
testString: assert.strictEqual(euler116(), 20492570929);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function euler116() {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
euler116();
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,55 @@
|
||||
---
|
||||
id: 5900f3e21000cf542c50fef4
|
||||
challengeType: 5
|
||||
title: 'Problem 117: Red, green, and blue tiles'
|
||||
videoUrl: ''
|
||||
localeTitle: 问题117:红色,绿色和蓝色瓷砖
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">使用黑色正方形瓷砖和椭圆形瓷砖的组合,选自:测量两个单位的红色瓷砖,测量三个单位的绿色瓷砖和测量四个单位的蓝色瓷砖,可以以十五种不同的方式平铺一个长度为五个单位的行。 <p>一行测量五十个单位长度的平铺方式有多少?注意:这与问题116有关。 </p></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>euler117()</code>应该返回100808458960497。
|
||||
testString: assert.strictEqual(euler117(), 100808458960497);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function euler117() {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
euler117();
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,55 @@
|
||||
---
|
||||
id: 5900f3e21000cf542c50fef5
|
||||
challengeType: 5
|
||||
title: 'Problem 118: Pandigital prime sets'
|
||||
videoUrl: ''
|
||||
localeTitle: 问题118:Pandigital prime set
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">使用所有数字1到9并自由地连接它们以形成十进制整数,可以形成不同的集合。有趣的是,集合{2,5,47,89,631},属于它的所有元素都是素数。包含每个数字1到9的多少个不同的集合只包含主要元素? </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>euler118()</code>应返回44680。
|
||||
testString: assert.strictEqual(euler118(), 44680);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function euler118() {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
euler118();
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,55 @@
|
||||
---
|
||||
id: 5900f3e41000cf542c50fef6
|
||||
challengeType: 5
|
||||
title: 'Problem 119: Digit power sum'
|
||||
videoUrl: ''
|
||||
localeTitle: 问题119:数字功率总和
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">数字512很有意思,因为它等于它的数字加到某个幂的总和:5 + 1 + 2 = 8,而83 = 512.具有这个属性的数字的另一个例子是614656 = 284.我们将定义一个成为这个序列的第n个术语,并坚持一个数字必须包含至少两个数字才能得到一个总和。给出a2 = 512和a10 = 614656。找到a30。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>euler119()</code>应该返回248155780267521。
|
||||
testString: assert.strictEqual(euler119(), 248155780267521);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function euler119() {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
euler119();
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,63 @@
|
||||
---
|
||||
id: 5900f3781000cf542c50fe8b
|
||||
challengeType: 5
|
||||
title: 'Problem 12: Highly divisible triangular number'
|
||||
videoUrl: ''
|
||||
localeTitle: 问题12:高度可分的三角数
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">通过添加自然数生成三角数的序列。所以第7个三角形数字是1 + 2 + 3 + 4 + 5 + 6 + 7 = 28.前十个术语是: <div style="text-align: center;"> 1,3,6,10,15,21,28,36,45,55 ...... </div>让我们列出前七个三角形数字的因子: <div style="padding-left: 4em;"> <b>1:</b> 1 </div><div style="padding-left: 4em;"> <b>3:</b> 1,3 </div><div style="padding-left: 4em;"> <b>6:</b> 1,2,3,6 </div><div style="padding-left: 4em;"> <b>10:</b> 1,2,5,10 </div><div style="padding-left: 4em;"> <b>15:</b> 1,3,5,15 </div><div style="padding-left: 4em;"> <b>21:</b> 1,3,7,21 </div><div style="padding-left: 4em;"> <b>28:</b> 1,2,4,7,14,28 </div>我们可以看到28是第一个超过五个除数的三角形数。超过<code>n</code>除数的第一个三角形数的值是多少? </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>divisibleTriangleNumber(5)</code>应该返回28。
|
||||
testString: assert.strictEqual(divisibleTriangleNumber(5), 28);
|
||||
- text: <code>divisibleTriangleNumber(23)</code>应该返回630。
|
||||
testString: assert.strictEqual(divisibleTriangleNumber(23), 630);
|
||||
- text: divisibleTriangleNumber <code>divisibleTriangleNumber(167)</code>应该返回1385280。
|
||||
testString: assert.strictEqual(divisibleTriangleNumber(167), 1385280);
|
||||
- text: divisibleTriangleNumber <code>divisibleTriangleNumber(374)</code>应该返回17907120。
|
||||
testString: assert.strictEqual(divisibleTriangleNumber(374), 17907120);
|
||||
- text: divisibleTriangleNumber <code>divisibleTriangleNumber(500)</code>应该返回76576500。
|
||||
testString: assert.strictEqual(divisibleTriangleNumber(500), 76576500);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function divisibleTriangleNumber(n) {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
divisibleTriangleNumber(500);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,55 @@
|
||||
---
|
||||
id: 5900f3e41000cf542c50fef7
|
||||
challengeType: 5
|
||||
title: 'Problem 120: Square remainders'
|
||||
videoUrl: ''
|
||||
localeTitle: 问题120:方形剩余部分
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">设r是(a-1)n +(a + 1)n除以a2时的余数。例如,如果a = 7且n = 3,则r = 42:63 + 83 =728≡42mod 49.并且当n变化时,r也将变化,但是对于a = 7,结果是rmax = 42。对于3≤a≤1000,找到Σrmax。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>euler120()</code>应返回333082500。
|
||||
testString: assert.strictEqual(euler120(), 333082500);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function euler120() {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
euler120();
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,55 @@
|
||||
---
|
||||
id: 5900f3e51000cf542c50fef8
|
||||
challengeType: 5
|
||||
title: 'Problem 121: Disc game prize fund'
|
||||
videoUrl: ''
|
||||
localeTitle: 问题121:光盘游戏奖金
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">一个包包含一个红色圆盘和一个蓝色圆盘。在有机会的游戏中,玩家随机拍摄光盘并记录其颜色。每次转动后,光盘返回到包中,添加一个额外的红色光盘,随机拍摄另一张光盘。如果玩家在游戏结束时拍摄的红色光盘多于红色光盘,则玩家可以支付1英镑玩游戏并获胜。如果游戏进行了四轮比赛,那么玩家获胜的概率恰好是11/120,因此银行家在这场比赛中应该分配的最高奖金将是10英镑,然后才会产生损失。请注意,任何支付都将是一个整数磅,并且还包括为玩游戏而支付的原始£1,因此在给出的示例中,玩家实际上赢得了9英镑。找到应该分配给单个游戏的最大奖金,其中玩15个回合。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>euler121()</code>应返回2269。
|
||||
testString: assert.strictEqual(euler121(), 2269);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function euler121() {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
euler121();
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,55 @@
|
||||
---
|
||||
id: 5900f3e61000cf542c50fef9
|
||||
challengeType: 5
|
||||
title: 'Problem 122: Efficient exponentiation'
|
||||
videoUrl: ''
|
||||
localeTitle: 问题122:有效取幂
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">最简单的计算n15的方法需要十四次乘法:n×n×...×n = n15但是使用“二进制”方法可以在六次乘法中计算它:n×n = n2n2×n2 = n4n4×n4 = n8n8 ×n4 = n12n12×n2 = n14n14×n = n15然而,只能在五次乘法中计算它:n×n = n2n2×n = n3n3×n3 = n6n6×n6 = n12n12×n3 = n15我们将定义m (k)是计算nk的最小乘法数;例如m(15)= 5.对于1≤k≤200,找到Σm(k)。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>euler122()</code>应返回1582。
|
||||
testString: assert.strictEqual(euler122(), 1582);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function euler122() {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
euler122();
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,55 @@
|
||||
---
|
||||
id: 5900f3e71000cf542c50fefa
|
||||
challengeType: 5
|
||||
title: 'Problem 123: Prime square remainders'
|
||||
videoUrl: ''
|
||||
localeTitle: 问题123:素数正方形余数
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">令pn为第n个素数:2,3,5,7,11 ......,并且当r(pn-1)n +(pn + 1)n除以pn2时,令r为余数。例如,当n = 3时,p3 = 5,并且43 + 63 =280≡5mod 25.余数首先超过109的n的最小值是7037.求出余数首次超过的n的最小值1010。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>euler123()</code>应该返回21035。
|
||||
testString: assert.strictEqual(euler123(), 21035);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function euler123() {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
euler123();
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,55 @@
|
||||
---
|
||||
id: 5900f3e81000cf542c50fefb
|
||||
challengeType: 5
|
||||
title: 'Problem 124: Ordered radicals'
|
||||
videoUrl: ''
|
||||
localeTitle: 问题124:有序的激进分子
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"> n,rad(n)的基数是n的不同素因子的乘积。例如,504 = 23×32×7,因此rad(504)= 2×3×7 = 42.如果我们计算1≤n≤10的rad(n),则在rad(n)上对它们进行排序,并进行排序如果激进值相等,我们得到:未分类<p>排序n rad(n) </p><p> n rad(n)k 11 </p><p> 111 22 </p><p> 222 33 </p><p> 423 42 </p><p> 824 55 </p><p> 335 66 </p><p> 936 77 </p><p> 557 82 </p><p> 668 93 </p><p> 779 1010 </p><p> 101010令E(k)为有序n列中的第k个元素;例如,E(4)= 8且E(6)= 9.如果rad(n)按1≤n≤100000排序,则找到E(10000)。 </p></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>euler124()</code>应返回21417。
|
||||
testString: assert.strictEqual(euler124(), 21417);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function euler124() {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
euler124();
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,55 @@
|
||||
---
|
||||
id: 5900f3e91000cf542c50fefc
|
||||
challengeType: 5
|
||||
title: 'Problem 125: Palindromic sums'
|
||||
videoUrl: ''
|
||||
localeTitle: 问题125:回文总和
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">回文数595很有意思,因为它可以写成连续正方形的总和:62 + 72 + 82 + 92 + 102 + 112 + 122.正好有11个以下的回文可以写成连续的平方和,并且这些回文的总和是4164.注意,没有包括1 = 02 + 12,因为该问题涉及正整数的平方。找到小于108的所有数字的总和,这些数字都是回文并且可以写为连续正方形的总和。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>euler125()</code>应该返回2906969179。
|
||||
testString: assert.strictEqual(euler125(), 2906969179);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function euler125() {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
euler125();
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,55 @@
|
||||
---
|
||||
id: 5900f3ea1000cf542c50fefd
|
||||
challengeType: 5
|
||||
title: 'Problem 126: Cuboid layers'
|
||||
videoUrl: ''
|
||||
localeTitle: 问题126:长方体层
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">覆盖尺寸为3 x 2 x 1的长方体上每个可见面的最小立方体数量为22。 <p>如果我们在这个固体上添加第二层,则需要四十六个立方体来覆盖每个可见面,第三层需要七十八个立方体,第四层需要一百一十八个立方体来覆盖每个可见面。然而,尺寸为5 x 1 x 1的长方体上的第一层也需要22个立方体;类似地,尺寸为5 x 3 x 1,7 x 2 x 1和11 x 1 x 1的长方体上的第一层都包含四十六个立方体。我们将定义C(n)来表示在其一个层中包含n个立方体的长方体的数量。因此,C(22)= 2,C(46)= 4,C(78)= 5,并且C(118)= 8.结果,154是n的最小值,其中C(n)= 10。找到n的最小值,其中C(n)= 1000。 </p></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>euler126()</code>应返回18522。
|
||||
testString: assert.strictEqual(euler126(), 18522);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function euler126() {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
euler126();
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,55 @@
|
||||
---
|
||||
id: 5900f3ec1000cf542c50fefe
|
||||
challengeType: 5
|
||||
title: 'Problem 127: abc-hits'
|
||||
videoUrl: ''
|
||||
localeTitle: 问题127:abc-hits
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"> n,rad(n)的基数是n的不同素因子的乘积。例如,504 = 23×32×7,因此rad(504)= 2×3×7 = 42.如果出现以下情况,我们将正整数(a,b,c)的三元组定义为abc-hit:GCD( a,b)= GCD(a,c)= GCD(b,c)= 1 a <ba + b = c rad(abc)<c例如,(5,27,32)是abc-hit,因为:GCD(5,27)= GCD(5,32)= GCD(27,32)= 1 5 <27 5 + 27 = 32 rad(4320)= 30 <32事实证明abc-hits是非常罕见的c <1000只有31次abc命中,Σc= 12523。查找Σc表示c <120000。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>euler127()</code>应该返回18407904。
|
||||
testString: assert.strictEqual(euler127(), 18407904);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function euler127() {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
euler127();
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,55 @@
|
||||
---
|
||||
id: 5900f3ec1000cf542c50feff
|
||||
challengeType: 5
|
||||
title: 'Problem 128: Hexagonal tile differences'
|
||||
videoUrl: ''
|
||||
localeTitle: 问题128:六边形瓷砖差异
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">编号为1的六边形瓷砖由六个六边形瓷砖环围绕,从“12点钟”开始,并以逆时针方向对瓷砖2至7进行编号。新环以相同的方式添加,下一个环编号为8至19,20至37,38至61,依此类推。下图显示了前三个环。 <p>通过找到tile n与其六个邻居中的每一个之间的差异,我们将PD(n)定义为那些作为素数的差异的数量。例如,在瓦片8周围顺时针工作,差异是12,29,11,6,1和13.因此PD(8)= 3.以相同的方式,瓦片17周围的差异是1,17,16,1 ,11和10,因此PD(17)= 2.可以证明PD(n)的最大值是3.如果PD(n)= 3的所有瓦片按升序列出以形成一个序列,第10个瓦片将是271.按此顺序找到第2000个瓦片。 </p></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>euler128()</code>应该返回14516824220。
|
||||
testString: assert.strictEqual(euler128(), 14516824220);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function euler128() {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
euler128();
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,55 @@
|
||||
---
|
||||
id: 5900f3ef1000cf542c50ff01
|
||||
challengeType: 5
|
||||
title: 'Problem 129: Repunit divisibility'
|
||||
videoUrl: ''
|
||||
localeTitle: 问题129:重新划分可分性
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">完全由1组成的数字称为repunit。我们将R(k)定义为长度k的重新定位;例如,R(6)= 111111.假设n是正整数且GCD(n,10)= 1,则可以证明总是存在一个值k,其中R(k)可被n整除让A(n)成为k的最小值;例如,A(7)= 6且A(41)= 5.A(n)首先超过10的n的最小值是17.求出A(n)首先超过1的n的最小值 - 百万。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>euler129()</code>应该返回1000023。
|
||||
testString: assert.strictEqual(euler129(), 1000023);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function euler129() {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
euler129();
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
File diff suppressed because one or more lines are too long
@ -0,0 +1,55 @@
|
||||
---
|
||||
id: 5900f3ee1000cf542c50ff00
|
||||
challengeType: 5
|
||||
title: 'Problem 130: Composites with prime repunit property'
|
||||
videoUrl: ''
|
||||
localeTitle: 问题130:具有主要repunit属性的复合材料
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">完全由1组成的数字称为repunit。我们将R(k)定义为长度k的重新定位;例如,R(6)= 111111.假设n是正整数且GCD(n,10)= 1,则可以证明总是存在一个值k,其中R(k)可被n整除让A(n)成为k的最小值;例如,A(7)= 6和A(41)= 5.对于所有素数,p> 5,p-1可以被A(p)整除。例如,当p = 41时,A(41)= 5,并且40可被5整除。但是,也有罕见的复合值,这也是正确的;前五个例子是91,259,451,481和703.找到n的前25个复合值之和,其中GCD(n,10)= 1,n-1可被A(n)整除。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>euler130()</code>应返回149253。
|
||||
testString: assert.strictEqual(euler130(), 149253);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function euler130() {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
euler130();
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,55 @@
|
||||
---
|
||||
id: 5900f3ef1000cf542c50ff02
|
||||
challengeType: 5
|
||||
title: 'Problem 131: Prime cube partnership'
|
||||
videoUrl: ''
|
||||
localeTitle: 问题131:Prime立方体伙伴关系
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">存在一些素数值p,其中存在正整数n,使得表达式n3 + n2p是完美的立方体。例如,当p = 19时,83 + 82×19 = 123.最令人惊讶的是,对于具有此属性的每个素数,n的值是唯一的,并且在100之下只有四个这样的素数。一百万以下的素数有多少这个非凡的财产? </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>euler131()</code>应该返回173。
|
||||
testString: assert.strictEqual(euler131(), 173);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function euler131() {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
euler131();
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,55 @@
|
||||
---
|
||||
id: 5900f3f11000cf542c50ff03
|
||||
challengeType: 5
|
||||
title: 'Problem 132: Large repunit factors'
|
||||
videoUrl: ''
|
||||
localeTitle: 问题132:大的重新安置因素
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">完全由1组成的数字称为repunit。我们将R(k)定义为长度k的重新定位。例如,R(10)= 1111111111 = 11×41×271×9091,并且这些素因子的总和是9414.求出R(109)的前40个素因子的总和。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>euler132()</code>应返回843296。
|
||||
testString: assert.strictEqual(euler132(), 843296);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function euler132() {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
euler132();
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,55 @@
|
||||
---
|
||||
id: 5900f3f21000cf542c50ff04
|
||||
challengeType: 5
|
||||
title: 'Problem 133: Repunit nonfactors'
|
||||
videoUrl: ''
|
||||
localeTitle: 问题133:重新计算非因素
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">完全由1组成的数字称为repunit。我们将R(k)定义为长度k的重新定位;例如,R(6)= 111111.让我们考虑R(10n)形式的重新组合。虽然R(10),R(100)或R(1000)不能被17整除,但R(10000)可被17整除。但是没有n的值,R(10n)将除以19。事实上,值得注意的是,11,17,41和73是低于100的唯一四个素数,可以是R(10n)的因子。求出十万以下所有素数的总和,这些素数永远不会是R(10n)的因子。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>euler133()</code>应返回453647705。
|
||||
testString: assert.strictEqual(euler133(), 453647705);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function euler133() {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
euler133();
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,55 @@
|
||||
---
|
||||
id: 5900f3f21000cf542c50ff05
|
||||
challengeType: 5
|
||||
title: 'Problem 134: Prime pair connection'
|
||||
videoUrl: ''
|
||||
localeTitle: 问题134:素对对连接
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">考虑连续的素数p1 = 19和p2 = 23.可以证实1219是最小的数字,使得最后的数字由p1形成,同时也可以被p2整除。实际上,除了p1 = 3和p2 = 5之外,对于每对连续质数,p2> p1,存在n的值,其中最后的数字由p1形成,n可以被p2整除。设S是n的这些值中最小的。找到每对连续质数的ΣS,其中5≤p1≤1000000。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>euler134()</code>应该返回18613426663617120。
|
||||
testString: assert.strictEqual(euler134(), 18613426663617120);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function euler134() {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
euler134();
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,55 @@
|
||||
---
|
||||
id: 5900f3f31000cf542c50ff06
|
||||
challengeType: 5
|
||||
title: 'Problem 135: Same differences'
|
||||
videoUrl: ''
|
||||
localeTitle: 问题135:同样的差异
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">给定正整数,x,y和z是算术级数的连续项,正整数n的最小值,其中等式x2-y2-z2 = n,恰好有两个解是n = 27:342 - 272 - 202 = 122 - 92 - 62 = 27事实证明,n = 1155是具有正好十个解的最小值。 n不到一百万的多少个值有十个不同的解? </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>euler135()</code>应该返回4989。
|
||||
testString: assert.strictEqual(euler135(), 4989);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function euler135() {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
euler135();
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,55 @@
|
||||
---
|
||||
id: 5900f3f51000cf542c50ff07
|
||||
challengeType: 5
|
||||
title: 'Problem 136: Singleton difference'
|
||||
videoUrl: ''
|
||||
localeTitle: 问题136:单身人士差异
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">正整数x,y和z是算术级数的连续项。假设n是一个正整数,当n = 20时,方程x2 - y2 - z2 = n恰好有一个解:132 - 102 - 72 = 20实际上有二十五个n低于一百的值,其中方程有一个独特的解决方案。 n小于五千万的有多少个值只有一个解? </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>euler136()</code>应返回2544559。
|
||||
testString: assert.strictEqual(euler136(), 2544559);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function euler136() {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
euler136();
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,55 @@
|
||||
---
|
||||
id: 5900f3f51000cf542c50ff08
|
||||
challengeType: 5
|
||||
title: 'Problem 137: Fibonacci golden nuggets'
|
||||
videoUrl: ''
|
||||
localeTitle: 问题137:斐波那契金块
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">考虑无穷多项式系列AF(x)= xF1 + x2F2 + x3F3 + ...,其中Fk是斐波纳契数列中的第k项:1,1,2,3,5,8,...;也就是说,Fk = Fk-1 + Fk-2,F1 = 1且F2 = 1.对于这个问题,我们将对x的值感兴趣,其中AF(x)是正整数。令人惊讶的是AF(1/2)=(1/2).1 +(1/2)2.1 +(1/2)3.2 +(1/2)4.3 +(1/2)5.5 + ...... <p> = 1/2 + 1/4 + 2/8 + 3/16 + 5/32 + ...... </p><p> = 2前五个自然数的x的相应值如下所示。 </p><p> xAF(x)√2-111/ 22(√13-2)/ 33(√89-5)/ 84(√34-3)/ 55 </p><p>如果x是理性的,我们将AF(x)称为金块,因为它们变得越来越稀少;例如,第10个金块是74049690.找到第15个金块。 </p></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>euler137()</code>应该返回1120149658760。
|
||||
testString: assert.strictEqual(euler137(), 1120149658760);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function euler137() {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
euler137();
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,55 @@
|
||||
---
|
||||
id: 5900f3f61000cf542c50ff09
|
||||
challengeType: 5
|
||||
title: 'Problem 138: Special isosceles triangles'
|
||||
videoUrl: ''
|
||||
localeTitle: 问题138:特殊的等腰三角形
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">考虑具有基本长度,b = 16和腿,L = 17的等腰三角形。 <p>通过使用毕达哥拉斯定理,可以看出三角形的高度h =√(172-82)= 15,比基本长度小1。当b = 272且L = 305时,我们得到h = 273,这比基本长度多一个,这是第二个最小的等腰三角形,具有h = b±1的性质。找到12个最小等腰的ΣL h = b±1且b,L为正整数的三角形。 </p></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>euler138()</code>应该返回1118049290473932。
|
||||
testString: assert.strictEqual(euler138(), 1118049290473932);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function euler138() {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
euler138();
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,55 @@
|
||||
---
|
||||
id: 5900f3f71000cf542c50ff0a
|
||||
challengeType: 5
|
||||
title: 'Problem 139: Pythagorean tiles'
|
||||
videoUrl: ''
|
||||
localeTitle: 问题139:毕达哥拉斯瓷砖
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">设(a,b,c)表示具有整数长边的直角三角形的三个边。可以将四个这样的三角形放在一起以形成长度为c的正方形。例如,(3,4,5)三角形可以放在一起形成一个5乘5的正方形,中间有一个1个洞,可以看到5乘5的正方形可以用二十五个平铺1个方格。 <p>但是,如果使用(5,12,13)三角形,则孔将按7乘7测量,并且这些不能用于平铺13乘13平方。鉴于直角三角形的周长小于一亿,有多少毕达哥拉斯三角形允许这样的平铺? </p></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>euler139()</code>应该返回10057761。
|
||||
testString: assert.strictEqual(euler139(), 10057761);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function euler139() {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
euler139();
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,63 @@
|
||||
---
|
||||
id: 5900f37a1000cf542c50fe8d
|
||||
challengeType: 5
|
||||
title: 'Problem 14: Longest Collatz sequence'
|
||||
videoUrl: ''
|
||||
localeTitle: 问题14:最长的Collatz序列
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">为正整数集定义以下迭代序列: <div style="padding-left: 4em;"> <var>n</var> → <var>n</var> / 2( <var>n</var>是偶数) </div><div style="padding-left: 4em;"> <var>n</var> →3 <var>n</var> + 1( <var>n</var>为奇数) </div>使用上面的规则并从13开始,我们生成以下序列: <div style="text-align: center;"> 13→40→20→10→5→16→8→4→2→1 </div>可以看出,该序列(从13开始并在1结束)包含10个项。虽然尚未证实(Collatz问题),但是认为所有起始数字都在1处结束。在给定<code>limit</code>下,哪个起始数产生最长链?注意:一旦链条启动,条款允许超过一百万。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>longestCollatzSequence(14)</code>应该返回9。
|
||||
testString: assert.strictEqual(longestCollatzSequence(14), 9);
|
||||
- text: <code>longestCollatzSequence(5847)</code>应返回3711。
|
||||
testString: assert.strictEqual(longestCollatzSequence(5847), 3711);
|
||||
- text: <code>longestCollatzSequence(46500)</code>应返回35655。
|
||||
testString: assert.strictEqual(longestCollatzSequence(46500), 35655);
|
||||
- text: <code>longestCollatzSequence(54512)</code>应返回52527。
|
||||
testString: assert.strictEqual(longestCollatzSequence(54512), 52527);
|
||||
- text: <code>longestCollatzSequence(1000000)</code>应返回837799。
|
||||
testString: assert.strictEqual(longestCollatzSequence(100000), 77031);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function longestCollatzSequence(limit) {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
longestCollatzSequence(14);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user