chore(learn): Applied MDX format to Chinese curriculum files (#40462)
This commit is contained in:
@ -1,64 +1,33 @@
|
||||
---
|
||||
id: 594810f028c0303b75339acb
|
||||
title: 100门
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 100门
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"><p>连续100个门都是最初关闭的。你可以在门口进行100次通行证。第一次通过,访问每扇门并“切换”门(如果门关闭,打开它;如果它打开,关闭它)。第二次,只访问每个第二个门(即门#2,#4,#6,......)并切换它。第三次,访问每个第3门(即3号门,#6号,#9号,......)等,直到您只访问第100个门。 </p><p>实现一个功能,以确定最后一次通过后门的状态。将最终结果返回到数组中,如果数组打开,则只包含数字中包含的门号。 </p></section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
<p>连续100个门都是最初关闭的。你可以在门口进行100次通行证。第一次通过,访问每扇门并“切换”门(如果门关闭,打开它;如果它打开,关闭它)。第二次,只访问每个第二个门(即门#2,#4,#6,......)并切换它。第三次,访问每个第3门(即3号门,#6号,#9号,......)等,直到您只访问第100个门。 </p><p>实现一个功能,以确定最后一次通过后门的状态。将最终结果返回到数组中,如果数组打开,则只包含数字中包含的门号。 </p>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>getFinalOpenedDoors</code>是一个函数。
|
||||
testString: assert(typeof getFinalOpenedDoors === 'function');
|
||||
- text: <code>getFinalOpenedDoors</code>应该返回一个数组。
|
||||
testString: assert(Array.isArray(getFinalOpenedDoors(100)));
|
||||
- text: <code>getFinalOpenedDoors</code>没有产生正确的结果。
|
||||
testString: assert.deepEqual(getFinalOpenedDoors(100), solution);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`getFinalOpenedDoors`是一个函数。
|
||||
|
||||
```js
|
||||
function getFinalOpenedDoors (numDoors) {
|
||||
// Good luck!
|
||||
}
|
||||
|
||||
assert(typeof getFinalOpenedDoors === 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`getFinalOpenedDoors`应该返回一个数组。
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
assert(Array.isArray(getFinalOpenedDoors(100)));
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`getFinalOpenedDoors`没有产生正确的结果。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert.deepEqual(getFinalOpenedDoors(100), solution);
|
||||
```
|
||||
|
||||
/section>
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,69 +1,45 @@
|
||||
---
|
||||
id: 5951e88f64ebf159166a1176
|
||||
title: 24场比赛
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 24场比赛
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"><p>实现一个以四位数字串为参数的函数,每个数字从1──►9(含),允许重复,并返回一个算术表达式,其值为24。如果不存在这样的解,则返回“没有解决方案。“ </p><p>规则: </p>只允许以下运算符/函数:乘法,除法,加法,减法除法应使用浮点或有理算术等来保留余数。不允许从提供的数字中形成多位数字。 (所以当给出1,2,2和1时,12 + 12的答案是错误的)。给定的数字顺序不必保留。 <p>示例输入: </p> <code>solve24("4878");</code> <code>solve24("1234");</code> <code>solve24("6789");</code> <code>solve24("1127");</code> <p>示例输出(字符串): </p> <code>(7-8/8)*4</code> <code>3*1*4*2</code> <code>(6*8)/(9-7)</code> <code>(1+7)*(2+1)</code> </section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
<p>实现一个以四位数字串为参数的函数,每个数字从1──►9(含),允许重复,并返回一个算术表达式,其值为24。如果不存在这样的解,则返回“没有解决方案。“ </p><p>规则: </p>只允许以下运算符/函数:乘法,除法,加法,减法除法应使用浮点或有理算术等来保留余数。不允许从提供的数字中形成多位数字。 (所以当给出1,2,2和1时,12 + 12的答案是错误的)。给定的数字顺序不必保留。 <p>示例输入: </p> <code>solve24("4878");</code> <code>solve24("1234");</code> <code>solve24("6789");</code> <code>solve24("1127");</code> <p>示例输出(字符串): </p> <code>(7-8/8)\*4</code> <code>3\*1\*4\*2</code> <code>(6\*8)/(9-7)</code> <code>(1+7)\*(2+1)</code>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>solve24</code>是一个函数。
|
||||
testString: assert(typeof solve24 === 'function');
|
||||
- text: <code>solve24("4878")</code>应返回<code>(7-8/8)*4</code>或<code>4*(7-8/8)</code>
|
||||
testString: assert(include(answers[0], solve24(testCases[0])));
|
||||
- text: <code>solve24("1234")</code>应返回<code>1*2*3*4</code>任何排列
|
||||
testString: assert(include(answers[1], solve24(testCases[1])));
|
||||
- text: <code>solve24("6789")</code>应返回<code>(6*8)/(9-7)</code>或<code>(8*6)/(9-7)</code>
|
||||
testString: assert(include(answers[2], solve24(testCases[2])));
|
||||
- text: <code>solve24("1127")</code>应该返回<code>(1+7)*(1*2)</code>的排列
|
||||
testString: assert(include(answers[3], solve24(testCases[3])));
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`solve24`是一个函数。
|
||||
|
||||
```js
|
||||
function solve24 (numStr) {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
assert(typeof solve24 === 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`solve24("4878")`应返回`(7-8/8)*4`或`4*(7-8/8)`
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
assert(include(answers[0], solve24(testCases[0])));
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`solve24("1234")`应返回`1*2*3*4`任何排列
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert(include(answers[1], solve24(testCases[1])));
|
||||
```
|
||||
|
||||
/section>
|
||||
`solve24("6789")`应返回`(6*8)/(9-7)`或`(8*6)/(9-7)`
|
||||
|
||||
```js
|
||||
assert(include(answers[2], solve24(testCases[2])));
|
||||
```
|
||||
|
||||
`solve24("1127")`应该返回`(1+7)*(1*2)`的排列
|
||||
|
||||
```js
|
||||
assert(include(answers[3], solve24(testCases[3])));
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,71 +1,63 @@
|
||||
---
|
||||
id: 5949b579404977fbaefcd736
|
||||
title: 90亿上帝的名字整数
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 90亿上帝的名字整数
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"><p>这项任务是<a href="https://en.wikipedia.org/wiki/The Nine Billion Names of God#Plot_summary" title="wp:上帝的九十亿名字#Plot_summary">Arthur C. Clarke</a>的<a href="https://en.wikipedia.org/wiki/The Nine Billion Names of God#Plot_summary" title="wp:上帝的九十亿名字#Plot_summary">短篇小说改编</a> 。 </p><p> (求解者应该意识到完成这项任务的后果。) </p><p>详细说明,指定“名称”的含义: </p><p>整数1有1个名称“1”。 </p><p>整数2有2个名称“1 + 1”和“2”。 </p><p>整数3具有3个名称“1 + 1 + 1”,“2 + 1”和“3”。 </p><p>整数4具有5个名称“1 + 1 + 1 + 1”,“2 + 1 + 1”,“2 + 2”,“3 + 1”,“4”。 </p><p>整数5有7个名称“1 + 1 + 1 + 1 + 1”,“2 + 1 + 1 + 1”,“2 + 2 + 1”,“3 + 1 + 1”,“3 + 2”, “4 + 1”,“5”。 </p><p>这可以通过以下形式显示: </p><pre> 1
|
||||
# --description--
|
||||
|
||||
<p>这项任务是<a href='https://en.wikipedia.org/wiki/The Nine Billion Names of God#Plot_summary' title='wp:上帝的九十亿名字#Plot_summary'>Arthur C. Clarke</a>的<a href='https://en.wikipedia.org/wiki/The Nine Billion Names of God#Plot_summary' title='wp:上帝的九十亿名字#Plot_summary'>短篇小说改编</a> 。 </p><p> (求解者应该意识到完成这项任务的后果。) </p><p>详细说明,指定“名称”的含义: </p><p>整数1有1个名称“1”。 </p><p>整数2有2个名称“1 + 1”和“2”。 </p><p>整数3具有3个名称“1 + 1 + 1”,“2 + 1”和“3”。 </p><p>整数4具有5个名称“1 + 1 + 1 + 1”,“2 + 1 + 1”,“2 + 2”,“3 + 1”,“4”。 </p><p>整数5有7个名称“1 + 1 + 1 + 1 + 1”,“2 + 1 + 1 + 1”,“2 + 2 + 1”,“3 + 1 + 1”,“3 + 2”, “4 + 1”,“5”。 </p><p>这可以通过以下形式显示: </p><pre> 1
|
||||
1 1
|
||||
1 1 1
|
||||
1 2 1 1
|
||||
1 2 2 1 1
|
||||
1 3 3 2 1 1
|
||||
</pre><p>其中row $ n $对应于整数$ n $,而行$ m $中从左到右的每列$ C $对应于以$ C $开头的名称数。 </p><p> (可选)请注意$ n $ -th行$ P(n)$的总和是<a href="http://mathworld.wolfram.com/PartitionFunctionP.html" title="链接:http://mathworld.wolfram.com/PartitionFunctionP.html">整数分区函数</a> 。 </p>任务<p>实现一个返回$ n $ -th行之和的函数。 </p></section>
|
||||
</pre><p>其中row $ n $对应于整数$ n $,而行$ m $中从左到右的每列$ C $对应于以$ C $开头的名称数。 </p><p> (可选)请注意$ n $ -th行$ P(n)$的总和是<a href='http://mathworld.wolfram.com/PartitionFunctionP.html' title='链接:http://mathworld.wolfram.com/PartitionFunctionP.html'>整数分区函数</a> 。 </p>任务<p>实现一个返回$ n $ -th行之和的函数。 </p>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
# --hints--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>numberOfNames</code>是一个函数。
|
||||
testString: assert(typeof numberOfNames === 'function');
|
||||
- text: <code>numberOfNames(5)</code>应该等于7。
|
||||
testString: assert.equal(numberOfNames(5), 7);
|
||||
- text: <code>numberOfNames(12)</code>应该等于77。
|
||||
testString: assert.equal(numberOfNames(12), 77);
|
||||
- text: <code>numberOfNames(18)</code>应该等于385。
|
||||
testString: assert.equal(numberOfNames(18), 385);
|
||||
- text: <code>numberOfNames(23)</code>应该等于1255。
|
||||
testString: assert.equal(numberOfNames(23), 1255);
|
||||
- text: <code>numberOfNames(42)</code>应该等于53174。
|
||||
testString: assert.equal(numberOfNames(42), 53174);
|
||||
- text: <code>numberOfNames(123)</code>应该等于2552338241。
|
||||
testString: assert.equal(numberOfNames(123), 2552338241);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`numberOfNames`是一个函数。
|
||||
|
||||
```js
|
||||
function numberOfNames (num) {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
assert(typeof numberOfNames === 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`numberOfNames(5)`应该等于7。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert.equal(numberOfNames(5), 7);
|
||||
```
|
||||
|
||||
/section>
|
||||
`numberOfNames(12)`应该等于77。
|
||||
|
||||
```js
|
||||
assert.equal(numberOfNames(12), 77);
|
||||
```
|
||||
|
||||
`numberOfNames(18)`应该等于385。
|
||||
|
||||
```js
|
||||
assert.equal(numberOfNames(18), 385);
|
||||
```
|
||||
|
||||
`numberOfNames(23)`应该等于1255。
|
||||
|
||||
```js
|
||||
assert.equal(numberOfNames(23), 1255);
|
||||
```
|
||||
|
||||
`numberOfNames(42)`应该等于53174。
|
||||
|
||||
```js
|
||||
assert.equal(numberOfNames(42), 53174);
|
||||
```
|
||||
|
||||
`numberOfNames(123)`应该等于2552338241。
|
||||
|
||||
```js
|
||||
assert.equal(numberOfNames(123), 2552338241);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,74 +1,63 @@
|
||||
---
|
||||
id: 594810f028c0303b75339acc
|
||||
title: ABC问题
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: ABC问题
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"><p>您将获得ABC块的集合(例如,童年字母块)。每个街区有20个街区,两个字母。块的所有侧面都保证有完整的字母表。块的样本集合: </p><p> (BO) </p><p> (XK) </p><p> (DQ) </p><p> (CP) </p><p> (NA) </p><p> (GT) </p><p> (回覆) </p><p> (TG) </p><p> (QD) </p><p> (FS) </p><p> (JW) </p><p> (HU) </p><p> (VI) </p><p> (一个) </p><p> (OB) </p><p> (ER) </p><p> (FS) </p><p> (LY) </p><p> (PC) </p><p> (ZM) </p><p>要记住一些规则: </p>一旦使用了块上的字母,就不能再使用该块。该函数应该不区分大小写。 <p>实现一个带字符串(单词)的函数,并确定该单词是否可以与给定的块集合拼写。 </p></section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
<p>您将获得ABC块的集合(例如,童年字母块)。每个街区有20个街区,两个字母。块的所有侧面都保证有完整的字母表。块的样本集合: </p><p> (BO) </p><p> (XK) </p><p> (DQ) </p><p> (CP) </p><p> (NA) </p><p> (GT) </p><p> (回覆) </p><p> (TG) </p><p> (QD) </p><p> (FS) </p><p> (JW) </p><p> (HU) </p><p> (VI) </p><p> (一个) </p><p> (OB) </p><p> (ER) </p><p> (FS) </p><p> (LY) </p><p> (PC) </p><p> (ZM) </p><p>要记住一些规则: </p>一旦使用了块上的字母,就不能再使用该块。该函数应该不区分大小写。 <p>实现一个带字符串(单词)的函数,并确定该单词是否可以与给定的块集合拼写。 </p>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>canMakeWord</code>是一个功能。
|
||||
testString: assert(typeof canMakeWord === 'function');
|
||||
- text: <code>canMakeWord</code>应该返回一个布尔值。
|
||||
testString: assert(typeof canMakeWord('hi') === 'boolean');
|
||||
- text: <code>canMakeWord("bark")</code>应该返回true。
|
||||
testString: assert(canMakeWord(words[0]));
|
||||
- text: <code>canMakeWord("BooK")</code>应该返回false。
|
||||
testString: assert(!canMakeWord(words[1]));
|
||||
- text: <code>canMakeWord("TReAT")</code>应该返回true。
|
||||
testString: assert(canMakeWord(words[2]));
|
||||
- text: <code>canMakeWord("COMMON")</code>应返回false。
|
||||
testString: assert(!canMakeWord(words[3]));
|
||||
- text: <code>canMakeWord("squAD")</code>应该返回true。
|
||||
testString: assert(canMakeWord(words[4]));
|
||||
- text: <code>canMakeWord("conFUSE")</code>应该返回true。
|
||||
testString: assert(canMakeWord(words[5]));
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`canMakeWord`是一个功能。
|
||||
|
||||
```js
|
||||
function canMakeWord (word) {
|
||||
// Good luck!
|
||||
}
|
||||
|
||||
assert(typeof canMakeWord === 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`canMakeWord`应该返回一个布尔值。
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
assert(typeof canMakeWord('hi') === 'boolean');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`canMakeWord("bark")`应该返回true。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert(canMakeWord(words[0]));
|
||||
```
|
||||
|
||||
/section>
|
||||
`canMakeWord("BooK")`应该返回false。
|
||||
|
||||
```js
|
||||
assert(!canMakeWord(words[1]));
|
||||
```
|
||||
|
||||
`canMakeWord("TReAT")`应该返回true。
|
||||
|
||||
```js
|
||||
assert(canMakeWord(words[2]));
|
||||
```
|
||||
|
||||
`canMakeWord("COMMON")`应返回false。
|
||||
|
||||
```js
|
||||
assert(!canMakeWord(words[3]));
|
||||
```
|
||||
|
||||
`canMakeWord("squAD")`应该返回true。
|
||||
|
||||
```js
|
||||
assert(canMakeWord(words[4]));
|
||||
```
|
||||
|
||||
`canMakeWord("conFUSE")`应该返回true。
|
||||
|
||||
```js
|
||||
assert(canMakeWord(words[5]));
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,66 +1,39 @@
|
||||
---
|
||||
id: 594810f028c0303b75339acd
|
||||
title: 丰富,不足和完善的数字分类
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 丰富,不足和完善的数字分类
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"><p>它们根据<a href="http://rosettacode.org/wiki/Proper divisors" title="适当的除数">适当的除数</a>定义了三个正整数分类。 </p><p>设$ P(n)$是n的适当除数的总和,其中适当的除数都是n本身以外的正整数。 </p><p>如果<code>P(n) < n</code>那么n被归类为“缺陷” </p><p>如果<code>P(n) === n</code>那么n被归类为“完美” </p><p>如果<code>P(n) > n</code>则n被归类为“丰富” </p><p>例: </p><p> 6具有1,2和3的适当除数。 </p><p> 1 + 2 + 3 = 6,因此6被归类为完美数字。 </p><p>实现一个函数,计算三个类中每个类中1到20,000(包括)的整数。以下列格式将结果输出为数组<code>[deficient, perfect, abundant]</code> 。 </p></section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
<p>它们根据<a href='http://rosettacode.org/wiki/Proper divisors' title='适当的除数'>适当的除数</a>定义了三个正整数分类。 </p><p>设$ P(n)$是n的适当除数的总和,其中适当的除数都是n本身以外的正整数。 </p><p>如果<code>P(n) < n</code>那么n被归类为“缺陷” </p><p>如果<code>P(n) === n</code>那么n被归类为“完美” </p><p>如果<code>P(n) > n</code>则n被归类为“丰富” </p><p>例: </p><p> 6具有1,2和3的适当除数。 </p><p> 1 + 2 + 3 = 6,因此6被归类为完美数字。 </p><p>实现一个函数,计算三个类中每个类中1到20,000(包括)的整数。以下列格式将结果输出为数组<code>[deficient, perfect, abundant]</code> 。 </p>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>getDPA</code>是一个功能。
|
||||
testString: assert(typeof getDPA === 'function');
|
||||
- text: <code>getDPA</code>应该返回一个数组。
|
||||
testString: assert(Array.isArray(getDPA(100)));
|
||||
- text: <code>getDPA</code>返回值的长度应为3。
|
||||
testString: assert(getDPA(100).length === 3);
|
||||
- text: '<code>getDPA(20000)</code>应该等于[15043,4,4953]'
|
||||
testString: assert.deepEqual(getDPA(20000), solution);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`getDPA`是一个功能。
|
||||
|
||||
```js
|
||||
function getDPA (num) {
|
||||
// Good luck!
|
||||
}
|
||||
|
||||
assert(typeof getDPA === 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`getDPA`应该返回一个数组。
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
assert(Array.isArray(getDPA(100)));
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`getDPA`返回值的长度应为3。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert(getDPA(100).length === 3);
|
||||
```
|
||||
|
||||
/section>
|
||||
`getDPA(20000)`应该等于[15043,4,4953]
|
||||
|
||||
```js
|
||||
assert.deepEqual(getDPA(20000), solution);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,66 +1,39 @@
|
||||
---
|
||||
id: 594810f028c0303b75339ace
|
||||
title: 蓄能器工厂
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 蓄能器工厂
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"><p>创建一个带有单个(数字)参数的函数,并返回另一个作为累加器的函数。返回的累加器函数又接受一个数字参数,并返回到目前为止传递给该累加器的所有数值的总和(包括创建累加器时传递的初始值)。 </p><p>规则: </p><p>不要使用全局变量。 </p><p>暗示: </p><p>闭包可以保存外部状态。 </p></section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
<p>创建一个带有单个(数字)参数的函数,并返回另一个作为累加器的函数。返回的累加器函数又接受一个数字参数,并返回到目前为止传递给该累加器的所有数值的总和(包括创建累加器时传递的初始值)。 </p><p>规则: </p><p>不要使用全局变量。 </p><p>暗示: </p><p>闭包可以保存外部状态。 </p>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>accumulator</code>是一个功能。
|
||||
testString: assert(typeof accumulator === 'function');
|
||||
- text: <code>accumulator(0)</code>应该返回一个函数。
|
||||
testString: assert(typeof accumulator(0) === 'function');
|
||||
- text: <code>accumulator(0)(2)</code>应该返回一个数字。
|
||||
testString: assert(typeof accumulator(0)(2) === 'number');
|
||||
- text: '传递值3,-4,1.5和5应返回5.5。'
|
||||
testString: assert(testFn(5) === 5.5);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`accumulator`是一个功能。
|
||||
|
||||
```js
|
||||
function accumulator (sum) {
|
||||
// Good luck!
|
||||
}
|
||||
|
||||
assert(typeof accumulator === 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`accumulator(0)`应该返回一个函数。
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
assert(typeof accumulator(0) === 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`accumulator(0)(2)`应该返回一个数字。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert(typeof accumulator(0)(2) === 'number');
|
||||
```
|
||||
|
||||
/section>
|
||||
传递值3,-4,1.5和5应返回5.5。
|
||||
|
||||
```js
|
||||
assert(testFn(5) === 5.5);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,60 +1,45 @@
|
||||
---
|
||||
id: 594810f028c0303b75339acf
|
||||
title: 阿克曼功能
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 阿克曼功能
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"><p> Ackermann函数是递归函数的典型示例,尤其值得注意的是它不是原始递归函数。它的值增长非常快,其调用树的大小也是如此。 </p><p> Ackermann函数通常定义如下: </p> $$ A(m,n)= \ begin {cases} n + 1&\ mbox {if} m = 0 \\ A(m-1,1)&\ mbox {if} m> 0 \ mbox {和} n = 0 \\ A(m-1,A(m,n-1))&\ mbox {if} m> 0 \ mbox {和} n> 0. \ end {cases} $$ <p>它的论点永远不会消极,它总是终止。编写一个返回$ A(m,n)$的值的函数。任意精度是首选(因为函数增长如此之快),但不是必需的。 </p></section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
<p> Ackermann函数是递归函数的典型示例,尤其值得注意的是它不是原始递归函数。它的值增长非常快,其调用树的大小也是如此。 </p><p> Ackermann函数通常定义如下: </p> $$ A(m,n)= \\ begin {cases} n + 1&\\ mbox {if} m = 0 \\\\ A(m-1,1)&\\ mbox {if} m> 0 \\ mbox {和} n = 0 \\\\ A(m-1,A(m,n-1))&\\ mbox {if} m> 0 \\ mbox {和} n> 0. \\ end {cases} $$ <p>它的论点永远不会消极,它总是终止。编写一个返回$ A(m,n)$的值的函数。任意精度是首选(因为函数增长如此之快),但不是必需的。 </p>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>ack</code>是一个功能。
|
||||
testString: assert(typeof ack === 'function');
|
||||
- text: '<code>ack(0, 0)</code>应该返回1。'
|
||||
testString: assert(ack(0, 0) === 1);
|
||||
- text: '<code>ack(1, 1)</code>应该返回3。'
|
||||
testString: assert(ack(1, 1) === 3);
|
||||
- text: '<code>ack(2, 5)</code>应该返回13。'
|
||||
testString: assert(ack(2, 5) === 13);
|
||||
- text: '<code>ack(3, 3)</code>应该返回61。'
|
||||
testString: assert(ack(3, 3) === 61);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`ack`是一个功能。
|
||||
|
||||
```js
|
||||
function ack (m, n) {
|
||||
// Good luck!
|
||||
}
|
||||
|
||||
assert(typeof ack === 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`ack(0, 0)`应该返回1。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert(ack(0, 0) === 1);
|
||||
```
|
||||
|
||||
/section>
|
||||
`ack(1, 1)`应该返回3。
|
||||
|
||||
```js
|
||||
assert(ack(1, 1) === 3);
|
||||
```
|
||||
|
||||
`ack(2, 5)`应该返回13。
|
||||
|
||||
```js
|
||||
assert(ack(2, 5) === 13);
|
||||
```
|
||||
|
||||
`ack(3, 3)`应该返回61。
|
||||
|
||||
```js
|
||||
assert(ack(3, 3) === 61);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,14 +1,15 @@
|
||||
---
|
||||
id: 594810f028c0303b75339ad0
|
||||
title: 对齐列
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 对齐列
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"><p>给定一个包含许多行的文本文件,其中一行中的字段由单个<code>$</code>字符描述,编写一个程序,通过确保每列中的单词至少由一个空格分隔来对齐每列字段。此外,允许列中的每个单词在其列中左对齐,右对齐或居中对齐。 </p><p>使用以下文本测试您的程序: </p><pre>的$ $多行给出$ A $文本$文件$
|
||||
# --description--
|
||||
|
||||
<p>给定一个包含许多行的文本文件,其中一行中的字段由单个<code>$</code>字符描述,编写一个程序,通过确保每列中的单词至少由一个空格分隔来对齐每列字段。此外,允许列中的每个单词在其列中左对齐,右对齐或居中对齐。 </p><p>使用以下文本测试您的程序: </p><pre>的$ $多行给出$ A $文本$文件$
|
||||
其中$领域内$ A $线$ $
|
||||
是$由$ A $单个$ '美元' $字符划定$
|
||||
是$由$ A $单个$ '美元' $字符划定$
|
||||
写$ A $程序
|
||||
即$对齐$ $场的每个$列$
|
||||
由$ $确保该$字美元$ $每
|
||||
@ -16,74 +17,33 @@ title: 对齐列
|
||||
此外,$允许为$ $ $中的每个字$ $ A $ $列到$为$要么离开$ $
|
||||
对齐,右$ $有道理
|
||||
或$ $中心内$有道理$ $其列。
|
||||
</pre><p>注意: </p>示例输入文本行可以或可以不具有尾随美元字符。所有列应共享相同的对齐方式。为了完成任务,在行尾附近产生的连续空格字符是无关紧要的。输出文本将以纯文本编辑器或基本终端上的单行间隔字体查看。列之间的最小间距应根据文本计算,而不是硬编码。不需要在列之间或列周围添加分隔字符。 </section>
|
||||
</pre><p>注意: </p>示例输入文本行可以或可以不具有尾随美元字符。所有列应共享相同的对齐方式。为了完成任务,在行尾附近产生的连续空格字符是无关紧要的。输出文本将以纯文本编辑器或基本终端上的单行间隔字体查看。列之间的最小间距应根据文本计算,而不是硬编码。不需要在列之间或列周围添加分隔字符。
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
# --hints--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>formatText</code>是一个函数。
|
||||
testString: assert(typeof formatText === 'function');
|
||||
- text: 具有上述输入和“右”对齐的<code>formatText</code>应产生以下内容:
|
||||
testString: 'assert.strictEqual(formatText(testInput, ''right''), rightAligned);'
|
||||
- text: 具有上述输入和“左”对齐的<code>formatText</code>应产生以下内容:
|
||||
testString: 'assert.strictEqual(formatText(testInput, ''left''), leftAligned);'
|
||||
- text: 具有上述输入和“居中”对齐的<code>formatText</code>应产生以下内容:
|
||||
testString: 'assert.strictEqual(formatText(testInput, ''center''), centerAligned);'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`formatText`是一个函数。
|
||||
|
||||
```js
|
||||
const testArr = [
|
||||
'Given$a$text$file$of$many$lines',
|
||||
'where$fields$within$a$line$',
|
||||
'are$delineated$by$a$single$"dollar"$character',
|
||||
'write$a$program',
|
||||
'that$aligns$each$column$of$fields$',
|
||||
'by$ensuring$that$words$in$each$',
|
||||
'column$are$separated$by$at$least$one$space.',
|
||||
'Further,$allow$for$each$word$in$a$column$to$be$either$left$',
|
||||
'justified,$right$justified',
|
||||
'or$center$justified$within$its$column.'
|
||||
];
|
||||
|
||||
function formatText (input, justification) {
|
||||
// Good luck!
|
||||
}
|
||||
|
||||
assert(typeof formatText === 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
具有上述输入和“右”对齐的`formatText`应产生以下内容:
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
assert.strictEqual(formatText(testInput, 'right'), rightAligned);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
具有上述输入和“左”对齐的`formatText`应产生以下内容:
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert.strictEqual(formatText(testInput, 'left'), leftAligned);
|
||||
```
|
||||
|
||||
/section>
|
||||
具有上述输入和“居中”对齐的`formatText`应产生以下内容:
|
||||
|
||||
```js
|
||||
assert.strictEqual(formatText(testInput, 'center'), centerAligned);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,67 +1,39 @@
|
||||
---
|
||||
id: 5949b579404977fbaefcd737
|
||||
title: 友好的对
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 友好的对
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">如果$ N \ neq M $和N $ $的<a href="http://rosettacode.org/wiki/Proper divisors" title="适当的除数">适当除数之</a>和($ \ mathrm {sum}(\ mathrm {propDivs}(N)),两个整数$ N $和$ M $被认为是<a href="https://en.wikipedia.org/wiki/Amicable numbers" title="wp:友善号码">友好对</a> $)$ = M $以及$ \ mathrm {sum}(\ mathrm {propDivs}(M))= N $。示例:1184和1210是友好对,具有适当的除数:1,2,4,8,16,32,37,74,148,296,592和1,2,5,10,11,25,55,分别为110,121,242,605。任务:计算并显示低于20,000的Amicable对(有八个)。相关任务<a href="http://rosettacode.org/wiki/Proper divisors" title="适当的除数">适当的除数</a> <a href="http://rosettacode.org/wiki/Abundant, deficient and perfect number classifications" title="丰富,不足和完善的数字分类">丰富,缺陷和完善的数字分类</a> <a href="http://rosettacode.org/wiki/Aliquot sequence classifications" title="等分序列分类">等分序列分类</a>及其友好分类。 </section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
如果$ N \\ neq M $和N $ $的[适当除数之](<http://rosettacode.org/wiki/Proper divisors> "适当的除数")和($ \\ mathrm {sum}(\\ mathrm {propDivs}(N)),两个整数$ N $和$ M $被认为是[友好对](<https://en.wikipedia.org/wiki/Amicable numbers> "wp:友善号码") $)$ = M $以及$ \\ mathrm {sum}(\\ mathrm {propDivs}(M))= N $。示例:1184和1210是友好对,具有适当的除数:1,2,4,8,16,32,37,74,148,296,592和1,2,5,10,11,25,55,分别为110,121,242,605。任务:计算并显示低于20,000的Amicable对(有八个)。相关任务[适当的除数](<http://rosettacode.org/wiki/Proper divisors> "适当的除数") [丰富,缺陷和完善的数字分类](<http://rosettacode.org/wiki/Abundant, deficient and perfect number classifications> "丰富,不足和完善的数字分类") [等分序列分类](<http://rosettacode.org/wiki/Aliquot sequence classifications> "等分序列分类")及其友好分类。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>amicablePairsUpTo</code>是一个函数。
|
||||
testString: assert(typeof amicablePairsUpTo === 'function');
|
||||
- text: '<code>[[220,284]]</code> <code>amicablePairsUpTo(300)</code>应返回<code>[[220,284]]</code> 。'
|
||||
testString: assert.deepEqual(amicablePairsUpTo(300), answer300);
|
||||
- text: '<code>[[220,284],[1184,1210],[2620,2924]]</code> <code>amicablePairsUpTo(3000)</code>应返回<code>[[220,284],[1184,1210],[2620,2924]]</code> 。'
|
||||
testString: assert.deepEqual(amicablePairsUpTo(3000), answer3000);
|
||||
- text: '<code>[[220,284],[1184,1210],[2620,2924],[5020,5564],[6232,6368],[10744,10856],[12285,14595],[17296,18416]]</code> <code>amicablePairsUpTo(20000)</code>应返回<code>[[220,284],[1184,1210],[2620,2924],[5020,5564],[6232,6368],[10744,10856],[12285,14595],[17296,18416]]</code> 。'
|
||||
testString: assert.deepEqual(amicablePairsUpTo(20000), answer20000);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`amicablePairsUpTo`是一个函数。
|
||||
|
||||
```js
|
||||
function amicablePairsUpTo (maxNum) {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
assert(typeof amicablePairsUpTo === 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`[[220,284]]` `amicablePairsUpTo(300)`应返回`[[220,284]]` 。
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
assert.deepEqual(amicablePairsUpTo(300), answer300);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`[[220,284],[1184,1210],[2620,2924]]` `amicablePairsUpTo(3000)`应返回`[[220,284],[1184,1210],[2620,2924]]` 。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert.deepEqual(amicablePairsUpTo(3000), answer3000);
|
||||
```
|
||||
|
||||
/section>
|
||||
`[[220,284],[1184,1210],[2620,2924],[5020,5564],[6232,6368],[10744,10856],[12285,14595],[17296,18416]]` `amicablePairsUpTo(20000)`应返回`[[220,284],[1184,1210],[2620,2924],[5020,5564],[6232,6368],[10744,10856],[12285,14595],[17296,18416]]` 。
|
||||
|
||||
```js
|
||||
assert.deepEqual(amicablePairsUpTo(20000), answer20000);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,65 +1,33 @@
|
||||
---
|
||||
id: 594d8d0ab97724821379b1e6
|
||||
title: 平均值模式
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 平均值模式
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"><p>编写程序以查找集合的<a href="https://en.wikipedia.org/wiki/Mode (statistics)" title="wp:模式(统计)">模式</a>值。 </p><p>可以忽略集合为空的情况。必须小心处理模式不唯一的情况。 </p><p>如果不适合或不可能支持常规集合,请尽可能使用向量(数组)。如果不适合或不可能支持未指定的值类型,请使用整数。 </p></section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
<p>编写程序以查找集合的<a href='https://en.wikipedia.org/wiki/Mode (statistics)' title='wp:模式(统计)'>模式</a>值。 </p><p>可以忽略集合为空的情况。必须小心处理模式不唯一的情况。 </p><p>如果不适合或不可能支持常规集合,请尽可能使用向量(数组)。如果不适合或不可能支持未指定的值类型,请使用整数。 </p>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>mode</code>是一种功能。
|
||||
testString: assert(typeof mode === 'function');
|
||||
- text: '<code>mode([1, 3, 6, 6, 6, 6, 7, 7, 12, 12, 17])</code>应该相等<code>[6]</code>'
|
||||
testString: assert.deepEqual(mode(arr1), [6]);
|
||||
- text: '<code>mode([1, 2, 4, 4, 1])</code>应该等于<code>[1, 4]</code> 。'
|
||||
testString: assert.deepEqual(mode(arr2).sort(), [1, 4]);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`mode`是一种功能。
|
||||
|
||||
```js
|
||||
function mode (arr) {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
assert(typeof mode === 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`mode([1, 3, 6, 6, 6, 6, 7, 7, 12, 12, 17])`应该相等`[6]`
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
assert.deepEqual(mode(arr1), [6]);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`mode([1, 2, 4, 4, 1])`应该等于`[1, 4]` 。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert.deepEqual(mode(arr2).sort(), [1, 4]);
|
||||
```
|
||||
|
||||
/section>
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,70 +1,35 @@
|
||||
---
|
||||
id: 594d966a1467eb84194f0086
|
||||
title: 平均值 - 毕达哥拉斯指的是
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 平均值 - 毕达哥拉斯指的是
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"><p class="rosetta__paragraph">计算整数<big>1</big>到<big>10</big> (包括)的所有三个<a class="rosetta__link--wiki" href="https://en.wikipedia.org/wiki/Pythagorean means" title="wp:毕达哥拉斯意味着">毕达哥拉斯方法</a> 。 </p><p class="rosetta__paragraph">为这组正整数显示<big>$ A(x_1,\ ldots,x_n)\ geq G(x_1,\ ldots,x_n)\ geq H(x_1,\ ldots,x_n)$</big> 。 </p>这三种方法中最常见的<a class="rosetta__link--rosetta" href="http://rosettacode.org/wiki/Averages/Arithmetic mean" title="平均值/算术平均值">算术平均值</a>是列表的总和除以其长度: <big>$ A(x_1,\ ldots,x_n)= \ frac {x_1 + \ cdots + x_n} {n} $</big> <a class="rosetta__link--wiki" href="https://en.wikipedia.org/wiki/Geometric mean" title="wp:几何平均值">几何mean</a>是列表产品的$ n $ th根: <big>$ G(x_1,\ ldots,x_n)= \ sqrt [n] {x_1 \ cdots x_n} $</big> <a class="rosetta__link--wiki" href="https://en.wikipedia.org/wiki/Harmonic mean" title="wp:谐波意味着">调和平均值</a>是$ n $除以总和列表中每个项目的倒数: <big>$ H(x_1,\ ldots,x_n)= \ frac {n} {\ frac {1} {x_1} + \ cdots + \ frac {1} {x_n}} $</big> <p class="rosetta__paragraph">假设输入是包含所有数字的有序数组。 </p><p class="rosetta__paragraph">要获得答案,请按以下格式输出对象: </p><pre class="rosetta__pre"> {
|
||||
# --description--
|
||||
|
||||
<p class='rosetta__paragraph'>计算整数<big>1</big>到<big>10</big> (包括)的所有三个<a class='rosetta__link--wiki' href='https://en.wikipedia.org/wiki/Pythagorean means' title='wp:毕达哥拉斯意味着'>毕达哥拉斯方法</a> 。 </p><p class='rosetta__paragraph'>为这组正整数显示<big>$ A(x_1,\ ldots,x_n)\ geq G(x_1,\ ldots,x_n)\ geq H(x_1,\ ldots,x_n)$</big> 。 </p>这三种方法中最常见的<a class='rosetta__link--rosetta' href='http://rosettacode.org/wiki/Averages/Arithmetic mean' title='平均值/算术平均值'>算术平均值</a>是列表的总和除以其长度: <big>$ A(x_1,\\ ldots,x_n)= \\ frac {x_1 + \\ cdots + x_n} {n} $</big> <a class='rosetta__link--wiki' href='https://en.wikipedia.org/wiki/Geometric mean' title='wp:几何平均值'>几何mean</a>是列表产品的$ n $ th根: <big>$ G(x_1,\\ ldots,x_n)= \\ sqrt \[n] {x_1 \\ cdots x_n} $</big> <a class='rosetta__link--wiki' href='https://en.wikipedia.org/wiki/Harmonic mean' title='wp:谐波意味着'>调和平均值</a>是$ n $除以总和列表中每个项目的倒数: <big>$ H(x_1,\\ ldots,x_n)= \\ frac {n} {\\ frac {1} {x_1} + \\ cdots + \\ frac {1} {x_n}} $</big> <p class='rosetta__paragraph'>假设输入是包含所有数字的有序数组。 </p><p class='rosetta__paragraph'>要获得答案,请按以下格式输出对象: </p><pre class='rosetta__pre'> {
|
||||
值:{
|
||||
算术:5.5,
|
||||
几何:4.528728688116765,
|
||||
谐波:3.414171521474055
|
||||
},
|
||||
测试:'是A> = G> = H?是'
|
||||
测试:'是A> = G> = H?是'
|
||||
}
|
||||
</pre></section>
|
||||
</pre>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
# --hints--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>pythagoreanMeans</code>是一种功能。
|
||||
testString: assert(typeof pythagoreanMeans === 'function');
|
||||
- text: '<code>pythagoreanMeans([1, 2, ..., 10])</code>应该等于上面相同的输出。'
|
||||
testString: assert.deepEqual(pythagoreanMeans(range1), answer1);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`pythagoreanMeans`是一种功能。
|
||||
|
||||
```js
|
||||
function pythagoreanMeans (rangeArr) {
|
||||
// Good luck!
|
||||
}
|
||||
|
||||
assert(typeof pythagoreanMeans === 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`pythagoreanMeans([1, 2, ..., 10])`应该等于上面相同的输出。
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
assert.deepEqual(pythagoreanMeans(range1), answer1);
|
||||
```
|
||||
|
||||
</div>
|
||||
# --solutions--
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
|
||||
/section>
|
||||
|
@ -1,62 +1,27 @@
|
||||
---
|
||||
id: 594da033de4190850b893874
|
||||
title: 平均值 - 均方根
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 平均值 - 均方根
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"><p>计算数字1到10(包括1和10)的<a href="https://en.wikipedia.org/wiki/Root mean square" title="wp:均方根">均方根</a> 。 </p><p>均方根也以其首字母RMS(或rms)和二次均值来表示。 </p><p> RMS计算为数字平方的平均值,平方根: </p><p> <big>$$ x _ {\ mathrm {rms}} = \ sqrt {{{x_1} ^ 2 + {x_2} ^ 2 + \ cdots + {x_n} ^ 2} \ over n}。 $$</big> </p></section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
<p>计算数字1到10(包括1和10)的<a href='https://en.wikipedia.org/wiki/Root mean square' title='wp:均方根'>均方根</a> 。 </p><p>均方根也以其首字母RMS(或rms)和二次均值来表示。 </p><p> RMS计算为数字平方的平均值,平方根: </p><p> <big>$$ x _ {\ mathrm {rms}} = \ sqrt {{{x_1} ^ 2 + {x_2} ^ 2 + \ cdots + {x_n} ^ 2} \ over n}。 $$</big> </p>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>rms</code>是一个功能。
|
||||
testString: assert(typeof rms === 'function');
|
||||
- text: '<code>rms([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])</code>应等于<code>6.2048368229954285</code> 。'
|
||||
testString: assert.equal(rms(arr1), answer1);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`rms`是一个功能。
|
||||
|
||||
```js
|
||||
function rms (arr) {
|
||||
// Good luck!
|
||||
}
|
||||
|
||||
assert(typeof rms === 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`rms([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])`应等于`6.2048368229954285` 。
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
assert.equal(rms(arr1), answer1);
|
||||
```
|
||||
|
||||
</div>
|
||||
# --solutions--
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
|
||||
/section>
|
||||
|
@ -1,63 +1,27 @@
|
||||
---
|
||||
id: 594db4d0dedb4c06a2a4cefd
|
||||
title: 巴贝奇问题
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 巴贝奇问题
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"><p> <a href="https://en.wikipedia.org/wiki/Charles_Babbage" title="wp:Charles_Babbage">Charles Babbage</a> ,展望他的分析引擎能解决的各种问题,给出了这个例子: </p><blockquote>什么是正方形以数字269,696结尾的最小正整数? </blockquote><p> - 巴贝奇,致鲍登勋爵的信,1837年;见Hollingdale和Tootill, <i>电子计算机</i> ,第二版,1970年,p。 125。 </p><p>他认为答案可能是99,736,其平方是9,947,269,696;但他无法确定。 </p><p>任务是找出巴贝奇是否有正确的答案。 </p><p>实现一个函数来返回满足Babbage问题的最小整数。如果巴贝奇是对的,返回巴贝奇的号码。 </p></section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
<p> <a href='https://en.wikipedia.org/wiki/Charles_Babbage' title='wp:Charles_Babbage'>Charles Babbage</a> ,展望他的分析引擎能解决的各种问题,给出了这个例子: </p><blockquote>什么是正方形以数字269,696结尾的最小正整数? </blockquote><p> - 巴贝奇,致鲍登勋爵的信,1837年;见Hollingdale和Tootill, <i>电子计算机</i> ,第二版,1970年,p。 125。 </p><p>他认为答案可能是99,736,其平方是9,947,269,696;但他无法确定。 </p><p>任务是找出巴贝奇是否有正确的答案。 </p><p>实现一个函数来返回满足Babbage问题的最小整数。如果巴贝奇是对的,返回巴贝奇的号码。 </p>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>babbage</code>是一种功能。
|
||||
testString: assert(typeof babbage === 'function');
|
||||
- text: '<code>babbage(99736, 269696)</code>不应该返回99736(答案较小)。'
|
||||
testString: assert.equal(babbage(babbageAns, endDigits), answer);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`babbage`是一种功能。
|
||||
|
||||
```js
|
||||
function babbage (babbageNum, endDigits) {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
assert(typeof babbage === 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`babbage(99736, 269696)`不应该返回99736(答案较小)。
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
assert.equal(babbage(babbageAns, endDigits), answer);
|
||||
```
|
||||
|
||||
</div>
|
||||
# --solutions--
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
|
||||
/section>
|
||||
|
@ -1,97 +1,129 @@
|
||||
---
|
||||
id: 594dc6c729e5700999302b45
|
||||
title: 平衡括号
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 平衡括号
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"><p>确定生成的括号字符串是否平衡;也就是说,它是否完全由成对的开/关括号(按此顺序)组成,其中没有一个错误嵌套。 </p>例子: <p class="rosetta__paragraph"> (空)是的</p><p class="rosetta__paragraph"> <code>[]</code>是的</p><p class="rosetta__paragraph"> <code>][</code>假</p><p class="rosetta__paragraph"> <code>[][]</code>是的</p><p class="rosetta__paragraph"> <code>][][</code>假</p><p class="rosetta__paragraph"> <code>[]][[]</code> false </p><p class="rosetta__paragraph"> <code>[[[[]]]]</code>是的</p></section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
<p>确定生成的括号字符串是否平衡;也就是说,它是否完全由成对的开/关括号(按此顺序)组成,其中没有一个错误嵌套。 </p>例子: <p class='rosetta__paragraph'> (空)是的</p><p class='rosetta__paragraph'> <code>[]</code>是的</p><p class='rosetta__paragraph'> <code>][</code>假</p><p class='rosetta__paragraph'> <code>[][]</code>是的</p><p class='rosetta__paragraph'> <code>][][</code>假</p><p class='rosetta__paragraph'> <code>[]][[]</code> false </p><p class='rosetta__paragraph'> <code>[[[[]]]]</code>是的</p>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>isBalanced</code>是一个函数。
|
||||
testString: assert(typeof isBalanced === 'function');
|
||||
- text: <code>isBalanced("[]")</code>应该返回true。
|
||||
testString: assert(isBalanced(testCases[0]));
|
||||
- text: <code>isBalanced("]][[[][][][]][")</code>应该返回false。
|
||||
testString: assert(!isBalanced(testCases[1]));
|
||||
- text: <code>isBalanced("[][[[[][][[[]]]]]]")</code>应该返回true。
|
||||
testString: assert(isBalanced(testCases[2]));
|
||||
- text: <code>isBalanced("][")</code>应该返回true。
|
||||
testString: assert(!isBalanced(testCases[3]));
|
||||
- text: <code>isBalanced("[[[]]]][[]")</code>应该返回true。
|
||||
testString: assert(!isBalanced(testCases[4]));
|
||||
- text: <code>isBalanced("][[]")</code>应该返回true。
|
||||
testString: assert(!isBalanced(testCases[5]));
|
||||
- text: <code>isBalanced("][[][]][[[]]")</code>应该返回true。
|
||||
testString: assert(!isBalanced(testCases[6]));
|
||||
- text: <code>isBalanced("[[][]]][")</code>应该返回true。
|
||||
testString: assert(!isBalanced(testCases[7]));
|
||||
- text: <code>isBalanced("[[[]]][[]]]][][[")</code>应该返回true。
|
||||
testString: assert(!isBalanced(testCases[8]));
|
||||
- text: <code>isBalanced("[]][[]]][[[[][]]")</code>应该返回true。
|
||||
testString: assert(!isBalanced(testCases[9]));
|
||||
- text: <code>isBalanced("][]][[][")</code>应该返回true。
|
||||
testString: assert(!isBalanced(testCases[10]));
|
||||
- text: <code>isBalanced("[[]][[][]]")</code>应该返回true。
|
||||
testString: assert(isBalanced(testCases[11]));
|
||||
- text: <code>isBalanced("[[]]")</code>应该返回true。
|
||||
testString: assert(isBalanced(testCases[12]));
|
||||
- text: <code>isBalanced("]][]][[]][[[")</code>应该返回true。
|
||||
testString: assert(!isBalanced(testCases[13]));
|
||||
- text: <code>isBalanced("][]][][[")</code>应该返回true。
|
||||
testString: assert(!isBalanced(testCases[14]));
|
||||
- text: <code>isBalanced("][][")</code>应该返回true。
|
||||
testString: assert(!isBalanced(testCases[15]));
|
||||
- text: <code>isBalanced("[[]]][][][[]][")</code>应该返回true。
|
||||
testString: assert(!isBalanced(testCases[16]));
|
||||
- text: <code>isBalanced("")</code>应该返回true。
|
||||
testString: assert(isBalanced(testCases[17]));
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`isBalanced`是一个函数。
|
||||
|
||||
```js
|
||||
function isBalanced (str) {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
assert(typeof isBalanced === 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`isBalanced("[]")`应该返回true。
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
assert(isBalanced(testCases[0]));
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`isBalanced("]][[[][][][]][")`应该返回false。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert(!isBalanced(testCases[1]));
|
||||
```
|
||||
|
||||
/section>
|
||||
`isBalanced("[][[[[][][[[]]]]]]")`应该返回true。
|
||||
|
||||
```js
|
||||
assert(isBalanced(testCases[2]));
|
||||
```
|
||||
|
||||
`isBalanced("][")`应该返回true。
|
||||
|
||||
```js
|
||||
assert(!isBalanced(testCases[3]));
|
||||
```
|
||||
|
||||
`isBalanced("[[[]]]][[]")`应该返回true。
|
||||
|
||||
```js
|
||||
assert(!isBalanced(testCases[4]));
|
||||
```
|
||||
|
||||
`isBalanced("][[]")`应该返回true。
|
||||
|
||||
```js
|
||||
assert(!isBalanced(testCases[5]));
|
||||
```
|
||||
|
||||
`isBalanced("][[][]][[[]]")`应该返回true。
|
||||
|
||||
```js
|
||||
assert(!isBalanced(testCases[6]));
|
||||
```
|
||||
|
||||
`isBalanced("[[][]]][")`应该返回true。
|
||||
|
||||
```js
|
||||
assert(!isBalanced(testCases[7]));
|
||||
```
|
||||
|
||||
`isBalanced("[[[]]][[]]]][][[")`应该返回true。
|
||||
|
||||
```js
|
||||
assert(!isBalanced(testCases[8]));
|
||||
```
|
||||
|
||||
`isBalanced("[]][[]]][[[[][]]")`应该返回true。
|
||||
|
||||
```js
|
||||
assert(!isBalanced(testCases[9]));
|
||||
```
|
||||
|
||||
`isBalanced("][]][[][")`应该返回true。
|
||||
|
||||
```js
|
||||
assert(!isBalanced(testCases[10]));
|
||||
```
|
||||
|
||||
`isBalanced("[[]][[][]]")`应该返回true。
|
||||
|
||||
```js
|
||||
assert(isBalanced(testCases[11]));
|
||||
```
|
||||
|
||||
`isBalanced("[[]]")`应该返回true。
|
||||
|
||||
```js
|
||||
assert(isBalanced(testCases[12]));
|
||||
```
|
||||
|
||||
`isBalanced("]][]][[]][[[")`应该返回true。
|
||||
|
||||
```js
|
||||
assert(!isBalanced(testCases[13]));
|
||||
```
|
||||
|
||||
`isBalanced("][]][][[")`应该返回true。
|
||||
|
||||
```js
|
||||
assert(!isBalanced(testCases[14]));
|
||||
```
|
||||
|
||||
`isBalanced("][][")`应该返回true。
|
||||
|
||||
```js
|
||||
assert(!isBalanced(testCases[15]));
|
||||
```
|
||||
|
||||
`isBalanced("[[]]][][][[]][")`应该返回true。
|
||||
|
||||
```js
|
||||
assert(!isBalanced(testCases[16]));
|
||||
```
|
||||
|
||||
`isBalanced("")`应该返回true。
|
||||
|
||||
```js
|
||||
assert(isBalanced(testCases[17]));
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,77 +1,57 @@
|
||||
---
|
||||
id: 5951815dd895584b06884620
|
||||
title: 给定半径的圆圈通过两个点
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 给定半径的圆圈通过两个点
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"><p>给定平面上的两个点和半径,通常可以通过这些点绘制给定半径的两个圆。 </p>例外:零半径应视为从不描述圆(除非点是重合的)。如果这些点是重合的,则可以绘制无限数量的圆,其圆周上的点可以被绘制,除非半径也等于零,然后将圆圈折叠到一个点。如果点形成直径,则返回单个圆。如果这些点相距太远则无法绘制圆圈。任务:实现一个取两个点和一个半径的函数,并通过这些点返回两个圆。对于每个结果圆,提供每个圆的中心的坐标,四舍五入到四个十进制数字。将每个坐标作为数组返回,并作为数组数组进行坐标。对于边缘情况,请返回以下内容:如果点在直径上,则返回一个点。如果半径也为零,则返回<code>"Radius Zero"</code> 。如果点重合,则返回<code>"Coincident point. Infinite solutions"</code> 。如果点与直径相距更远,则返回<code>"No intersection. Points further apart than circle diameter"</code>更远的<code>"No intersection. Points further apart than circle diameter"</code> 。样本输入: <pre> p1 p2 r
|
||||
# --description--
|
||||
|
||||
<p>给定平面上的两个点和半径,通常可以通过这些点绘制给定半径的两个圆。 </p>例外:零半径应视为从不描述圆(除非点是重合的)。如果这些点是重合的,则可以绘制无限数量的圆,其圆周上的点可以被绘制,除非半径也等于零,然后将圆圈折叠到一个点。如果点形成直径,则返回单个圆。如果这些点相距太远则无法绘制圆圈。任务:实现一个取两个点和一个半径的函数,并通过这些点返回两个圆。对于每个结果圆,提供每个圆的中心的坐标,四舍五入到四个十进制数字。将每个坐标作为数组返回,并作为数组数组进行坐标。对于边缘情况,请返回以下内容:如果点在直径上,则返回一个点。如果半径也为零,则返回<code>"Radius Zero"</code> 。如果点重合,则返回<code>"Coincident point. Infinite solutions"</code> 。如果点与直径相距更远,则返回<code>"No intersection. Points further apart than circle diameter"</code>更远的<code>"No intersection. Points further apart than circle diameter"</code> 。样本输入: <pre> p1 p2 r
|
||||
0.1234,0.9876 0.8765,0.2345 2.0
|
||||
0.0000,2.000000 0.0000,0.0000 1.0
|
||||
0.1234,0.9876 0.1234,0.9876 2.0
|
||||
0.1234,0.9876 0.8765,0.2345 0.5
|
||||
0.1234,0.9876 0.1234,0.9876 0.0
|
||||
</pre>参考:从数学论坛@Drexel的<a href="http://mathforum.org/library/drmath/view/53027.html" title="链接:http://mathforum.org/library/drmath/view/53027.html">2点和半径中找到一个</a>圆心</section>
|
||||
</pre>参考:从数学论坛@Drexel的<a href='http://mathforum.org/library/drmath/view/53027.html' title='链接:http://mathforum.org/library/drmath/view/53027.html'>2点和半径中找到一个</a>圆心
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
# --hints--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>getCircles</code>是一个函数。
|
||||
testString: assert(typeof getCircles === 'function');
|
||||
- text: '<code>getCircles([0.1234, 0.9876], [0.8765, 0.2345], 2.0)</code>应该返回<code>[[1.8631, 1.9742], [-0.8632, -0.7521]]</code> 。'
|
||||
testString: assert.deepEqual(getCircles(...testCases[0]), answers[0]);
|
||||
- text: '<code>getCircles([0.0000, 2.0000], [0.0000, 0.0000], 1.0)</code>应该返回<code>[0, 1]</code>'
|
||||
testString: assert.deepEqual(getCircles(...testCases[1]), answers[1]);
|
||||
- text: '<code>getCircles([0.1234, 0.9876], [0.1234, 0.9876], 2.0)</code>应返回<code>Coincident point. Infinite solutions</code>'
|
||||
testString: assert.deepEqual(getCircles(...testCases[2]), answers[2]);
|
||||
- text: '<code>getCircles([0.1234, 0.9876], [0.8765, 0.2345], 0.5)</code>应返回<code>No intersection. Points further apart than circle diameter</code>'
|
||||
testString: assert.deepEqual(getCircles(...testCases[3]), answers[3]);
|
||||
- text: '<code>getCircles([0.1234, 0.9876], [0.1234, 0.9876], 0.0)</code>应返回<code>Radius Zero</code>'
|
||||
testString: assert.deepEqual(getCircles(...testCases[4]), answers[4]);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`getCircles`是一个函数。
|
||||
|
||||
```js
|
||||
function getCircles (...args) {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
assert(typeof getCircles === 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`getCircles([0.1234, 0.9876], [0.8765, 0.2345], 2.0)`应该返回`[[1.8631, 1.9742], [-0.8632, -0.7521]]` 。
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
assert.deepEqual(getCircles(...testCases[0]), answers[0]);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`getCircles([0.0000, 2.0000], [0.0000, 0.0000], 1.0)`应该返回`[0, 1]`
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert.deepEqual(getCircles(...testCases[1]), answers[1]);
|
||||
```
|
||||
|
||||
/section>
|
||||
`getCircles([0.1234, 0.9876], [0.1234, 0.9876], 2.0)`应返回`Coincident point. Infinite solutions`
|
||||
|
||||
```js
|
||||
assert.deepEqual(getCircles(...testCases[2]), answers[2]);
|
||||
```
|
||||
|
||||
`getCircles([0.1234, 0.9876], [0.8765, 0.2345], 0.5)`应返回`No intersection. Points further apart than circle diameter`
|
||||
|
||||
```js
|
||||
assert.deepEqual(getCircles(...testCases[3]), answers[3]);
|
||||
```
|
||||
|
||||
`getCircles([0.1234, 0.9876], [0.1234, 0.9876], 0.0)`应返回`Radius Zero`
|
||||
|
||||
```js
|
||||
assert.deepEqual(getCircles(...testCases[4]), answers[4]);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,28 +1,38 @@
|
||||
---
|
||||
id: 5951a53863c8a34f02bf1bdc
|
||||
title: 最近对的问题
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 最近对的问题
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">任务: <p>提供一个函数来在二维中找到一组给定点中最接近的两个点,即求解平面情况下的<a href="https://en.wikipedia.org/wiki/Closest pair of points problem" title="wp:最近点的问题">最近点对问题</a> 。 </p><p>直接的解决方案是O(n <sup>2</sup> )算法(我们可以称之为强力算法);伪代码(使用索引)可以简单地: </p><pre> bruteForceClosestPair of P(1),P(2),... P(N)
|
||||
如果N <2那么
|
||||
返回∞
|
||||
# --description--
|
||||
|
||||
任务:
|
||||
|
||||
提供一个函数来在二维中找到一组给定点中最接近的两个点,即求解平面情况下的[最近点对问题](<https://en.wikipedia.org/wiki/Closest pair of points problem> "wp:最近点的问题") 。
|
||||
|
||||
直接的解决方案是O(n <sup>2</sup> )算法(我们可以称之为强力算法);伪代码(使用索引)可以简单地:
|
||||
|
||||
```
|
||||
bruteForceClosestPair of P(1),P(2),... P(N)
|
||||
如果N <2那么
|
||||
返回∞
|
||||
其他
|
||||
minDistance←| P(1) - P(2)|
|
||||
minPoints←{P(1),P(2)}
|
||||
foreachi∈[1,N-1]
|
||||
foreachj∈[i + 1,N]
|
||||
if | P(i) - P(j)| <minDistance然后
|
||||
minDistance←| P(i) - P(j)|
|
||||
minPoints←{P(i),P(j)}
|
||||
万一
|
||||
ENDFOR
|
||||
ENDFOR
|
||||
return minDistance,minPoints
|
||||
minDistance←| P(1) - P(2)|
|
||||
minPoints←{P(1),P(2)}
|
||||
foreachi∈[1,N-1]
|
||||
foreachj∈[i + 1,N]
|
||||
if | P(i) - P(j)| <minDistance然后
|
||||
minDistance←| P(i) - P(j)|
|
||||
minPoints←{P(i),P(j)}
|
||||
万一
|
||||
</pre><p>一个更好的算法是基于递归分而治之的方法,正如<a href="https://en.wikipedia.org/wiki/Closest pair of points problem#Planar_case" title="wp:最近点的问题#Planar_case">维基百科最近的一对点问题</a>所解释的那样,即O(n log n);伪代码可以是: </p><pre>最近的(xP,yP)
|
||||
ENDFOR
|
||||
ENDFOR
|
||||
return minDistance,minPoints
|
||||
万一
|
||||
```
|
||||
|
||||
</pre><p>一个更好的算法是基于递归分而治之的方法,正如<a href='https://en.wikipedia.org/wiki/Closest pair of points problem#Planar_case' title='wp:最近点的问题#Planar_case'>维基百科最近的一对点问题</a>所解释的那样,即O(n log n);伪代码可以是: </p><pre>最近的(xP,yP)
|
||||
其中xP是P(1).. P(N)按x坐标排序,和
|
||||
yP是P(1).. P(N)按y坐标排序(升序)
|
||||
如果N≤3那么
|
||||
@ -32,20 +42,20 @@ title: 最近对的问题
|
||||
xR←xP点从⌈N/2⌉+ 1到N.
|
||||
xm←xP(⌈N/2⌉) <sub>x</sub>
|
||||
基←{P∈YP:P <sub>X≤XM}</sub>
|
||||
yR←{p∈yP:p <sub>x</sub> > xm}
|
||||
yR←{p∈yP:p <sub>x</sub> > xm}
|
||||
(dL,pairL)←nearestPair(xL,yL)
|
||||
(dR,pairR)←nearestRair(xR,yR)
|
||||
(dmin,pairMin)←(dR,pairR)
|
||||
如果dL <dR则
|
||||
如果dL <dR则
|
||||
(dmin,pairMin)←(dL,pairL)
|
||||
万一
|
||||
yS←{p∈yP:| xm - p <sub>x</sub> | <dmin}
|
||||
yS←{p∈yP:| xm - p <sub>x</sub> | <dmin}
|
||||
nS←yS中的点数
|
||||
(最近,最近的公里)←(dmin,pairMin)
|
||||
我从1到nS - 1
|
||||
k←i + 1
|
||||
而k≤nS和yS(k) <sub>y</sub> -yS(i) <sub>y</sub> <dmin
|
||||
if | yS(k) - yS(i)| <最接近的
|
||||
而k≤nS和yS(k) <sub>y</sub> -yS(i) <sub>y</sub> <dmin
|
||||
if | yS(k) - yS(i)| <最接近的
|
||||
(最近,最近的公里)←(| yS(k) - yS(i)|,{yS(k),yS(i)})
|
||||
万一
|
||||
k←k + 1
|
||||
@ -53,75 +63,45 @@ title: 最近对的问题
|
||||
ENDFOR
|
||||
返回最近,最近的
|
||||
万一
|
||||
</pre>参考和进一步阅读: <a href="https://en.wikipedia.org/wiki/Closest pair of points problem" title="wp:最近点的问题">最近的一对点问题</a> <a href="http://www.cs.mcgill.ca/~cs251/ClosestPair/ClosestPairDQ.html" title="链接:http://www.cs.mcgill.ca/~cs251/ClosestPair/ClosestPairDQ.html">最近的一对(麦吉尔)</a> <a href="http://www.cs.ucsb.edu/~suri/cs235/ClosestPair.pdf" title="链接:http://www.cs.ucsb.edu/~suri/cs235/ClosestPair.pdf">最近的一对(UCSB)</a> <a href="http://classes.cec.wustl.edu/~cse241/handouts/closestpair.pdf" title="链接:http://classes.cec.wustl.edu/~cse241/handouts/closestpair.pdf">最近的一对(WUStL)</a> <a href="http://www.cs.iupui.edu/~xkzou/teaching/CS580/Divide-and-conquer-closestPair.ppt" title="链接:http://www.cs.iupui.edu/~xkzou/teaching/CS580/Divide-and-conquer-closestPair.ppt">最近的一对(IUPUI)</a> <p>对于输入,期望参数是一个对象(点)数组,其中<code>x</code>和<code>y</code>成员设置为数字。对于输出,返回一个包含键的对象: <code>distance</code>和<code>pair</code>值对(即两对最近点的对)。 </p></section>
|
||||
</pre>参考和进一步阅读: <a href='https://en.wikipedia.org/wiki/Closest pair of points problem' title='wp:最近点的问题'>最近的一对点问题</a> <a href='http://www.cs.mcgill.ca/~cs251/ClosestPair/ClosestPairDQ.html' title='链接:http://www.cs.mcgill.ca/~cs251/ClosestPair/ClosestPairDQ.html'>最近的一对(麦吉尔)</a> <a href='http://www.cs.ucsb.edu/~suri/cs235/ClosestPair.pdf' title='链接:http://www.cs.ucsb.edu/~suri/cs235/ClosestPair.pdf'>最近的一对(UCSB)</a> <a href='http://classes.cec.wustl.edu/~cse241/handouts/closestpair.pdf' title='链接:http://classes.cec.wustl.edu/~cse241/handouts/closestpair.pdf'>最近的一对(WUStL)</a> <a href='http://www.cs.iupui.edu/~xkzou/teaching/CS580/Divide-and-conquer-closestPair.ppt' title='链接:http://www.cs.iupui.edu/~xkzou/teaching/CS580/Divide-and-conquer-closestPair.ppt'>最近的一对(IUPUI)</a> <p>对于输入,期望参数是一个对象(点)数组,其中<code>x</code>和<code>y</code>成员设置为数字。对于输出,返回一个包含键的对象: <code>distance</code>和<code>pair</code>值对(即两对最近点的对)。 </p>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
# --hints--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>getClosestPair</code>是一个函数。
|
||||
testString: assert(typeof getClosestPair === 'function');
|
||||
- text: 距离应如下。
|
||||
testString: assert.equal(getClosestPair(points1).distance, answer1.distance);
|
||||
- text: 要点应如下。
|
||||
testString: assert.deepEqual(JSON.parse(JSON.stringify(getClosestPair(points1))).pair, answer1.pair);
|
||||
- text: 距离应如下。
|
||||
testString: assert.equal(getClosestPair(points2).distance, answer2.distance);
|
||||
- text: 要点应如下。
|
||||
testString: assert.deepEqual(JSON.parse(JSON.stringify(getClosestPair(points2))).pair, answer2.pair);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`getClosestPair`是一个函数。
|
||||
|
||||
```js
|
||||
const Point = function (x, y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
};
|
||||
Point.prototype.getX = function () {
|
||||
return this.x;
|
||||
};
|
||||
Point.prototype.getY = function () {
|
||||
return this.y;
|
||||
};
|
||||
|
||||
function getClosestPair (pointsArr) {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
assert(typeof getClosestPair === 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
距离应如下。
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
assert.equal(getClosestPair(points1).distance, answer1.distance);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
要点应如下。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert.deepEqual(
|
||||
JSON.parse(JSON.stringify(getClosestPair(points1))).pair,
|
||||
answer1.pair
|
||||
);
|
||||
```
|
||||
|
||||
/section>
|
||||
距离应如下。
|
||||
|
||||
```js
|
||||
assert.equal(getClosestPair(points2).distance, answer2.distance);
|
||||
```
|
||||
|
||||
要点应如下。
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
JSON.parse(JSON.stringify(getClosestPair(points2))).pair,
|
||||
answer2.pair
|
||||
);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,12 +1,22 @@
|
||||
---
|
||||
id: 5958469238c0d8d2632f46db
|
||||
title: 组合
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 组合
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">任务: <p>给定非负整数<big>m</big>和<big>n</big> ,以排序顺序生成从<big>0</big> (零)到<big>n-1</big>的整数的所有大小<big>m个</big> <a href="http://mathworld.wolfram.com/Combination.html" title="链接:http://mathworld.wolfram.com/Combination.html">组合</a> (每个组合被排序并且整个表被排序)。 </p>例: <p> <big>3</big>梳子<big>5</big>是: </p><pre> 0 1 2
|
||||
# --description--
|
||||
|
||||
任务:
|
||||
|
||||
给定非负整数m和n ,以排序顺序生成从0 (零)到n-1的整数的所有大小m个 [组合](http://mathworld.wolfram.com/Combination.html "链接:http://mathworld.wolfram.com/Combination.html") (每个组合被排序并且整个表被排序)。
|
||||
|
||||
例:
|
||||
|
||||
3梳子5是:
|
||||
|
||||
```
|
||||
0 1 2
|
||||
0 1 3
|
||||
0 1 4
|
||||
0 2 3
|
||||
@ -16,60 +26,29 @@ title: 组合
|
||||
1 2 4
|
||||
1 3 4
|
||||
2 3 4
|
||||
</pre></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>combinations</code>是一种功能。
|
||||
testString: assert(typeof combinations === 'function');
|
||||
- text: '<code>combinations(3, 5)</code>应返回<code>[[0, 1, 2], [0, 1, 3], [0, 1, 4], [0, 2, 3], [0, 2, 4], [0, 3, 4], [1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4]]</code> 。'
|
||||
testString: assert.deepEqual(combinations(testInput1[0], testInput1[1]), testOutput1);
|
||||
- text: '<code>combinations(4, 6)</code>应返回<code>[[0,1,2,3], [0,1,2,4], [0,1,2,5], [0,1,3,4], [0,1,3,5], [0,1,4,5], [0,2,3,4], [0,2,3,5], [0,2,4,5], [0,3,4,5], [1,2,3,4], [1,2,3,5], [1,2,4,5], [1,3,4,5], [2,3,4,5]]</code>'
|
||||
testString: assert.deepEqual(combinations(testInput2[0], testInput2[1]), testOutput2);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
</pre>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
# --hints--
|
||||
|
||||
<div id='js-seed'>
|
||||
`combinations`是一种功能。
|
||||
|
||||
```js
|
||||
function combinations (m, n) {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
assert(typeof combinations === 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`combinations(3, 5)`应返回`[[0, 1, 2], [0, 1, 3], [0, 1, 4], [0, 2, 3], [0, 2, 4], [0, 3, 4], [1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4]]` 。
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
assert.deepEqual(combinations(testInput1[0], testInput1[1]), testOutput1);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`combinations(4, 6)`应返回`[[0,1,2,3], [0,1,2,4], [0,1,2,5], [0,1,3,4], [0,1,3,5], [0,1,4,5], [0,2,3,4], [0,2,3,5], [0,2,4,5], [0,3,4,5], [1,2,3,4], [1,2,3,5], [1,2,4,5], [1,3,4,5], [2,3,4,5]]`
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert.deepEqual(combinations(testInput2[0], testInput2[1]), testOutput2);
|
||||
```
|
||||
|
||||
/section>
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,71 +1,51 @@
|
||||
---
|
||||
id: 596e414344c3b2872167f0fe
|
||||
title: 逗号狡猾
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 逗号狡猾
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"><p> Comma quibbling是Eric Lippert在他的<a href="http://blogs.msdn.com/b/ericlippert/archive/2009/04/15/comma-quibbling.aspx" title="链接:http://blogs.msdn.com/b/ericlippert/archive/2009/04/15/comma-quibbling.aspx">博客中</a>最初设定的任务。 </p>任务: <p>编写一个函数来生成一个字符串输出,它是列表/序列中输入字的串联,其中: </p>没有单词的输入产生仅两个大括号字符“{}”的输出字符串。只有一个单词的输入,例如[“ABC”],会在两个大括号内产生单词的输出字符串,例如“{ABC}”。两个单词的输入,例如[“ABC”,“DEF”],产生两个大括号内的两个单词的输出字符串,其中单词由字符串“和”分隔,例如“{ABC和DEF}”。三个或更多单词的输入,例如[“ABC”,“DEF”,“G”,“H”],产生除了最后一个单词之外的所有输出字符串,用“,”分隔,最后一个单词用“和”分隔。 “并且都在括号内;例如“{ABC,DEF,G和H}”。 <p>在此页面上显示输出的以下一系列输入测试您的功能: </p> []#(无输入字)。 [“ABC”] [“ABC”,“DEF”] [“ABC”,“DEF”,“G”,“H”] <p>注意:假设此单词是此任务的非空字符串大写字符。 </p></section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
<p> Comma quibbling是Eric Lippert在他的<a href='http://blogs.msdn.com/b/ericlippert/archive/2009/04/15/comma-quibbling.aspx' title='链接:http://blogs.msdn.com/b/ericlippert/archive/2009/04/15/comma-quibbling.aspx'>博客中</a>最初设定的任务。 </p>任务: <p>编写一个函数来生成一个字符串输出,它是列表/序列中输入字的串联,其中: </p>没有单词的输入产生仅两个大括号字符“{}”的输出字符串。只有一个单词的输入,例如\[“ABC”],会在两个大括号内产生单词的输出字符串,例如“{ABC}”。两个单词的输入,例如\[“ABC”,“DEF”],产生两个大括号内的两个单词的输出字符串,其中单词由字符串“和”分隔,例如“{ABC和DEF}”。三个或更多单词的输入,例如\[“ABC”,“DEF”,“G”,“H”],产生除了最后一个单词之外的所有输出字符串,用“,”分隔,最后一个单词用“和”分隔。 “并且都在括号内;例如“{ABC,DEF,G和H}”。 <p>在此页面上显示输出的以下一系列输入测试您的功能: </p> \[]#(无输入字)。 \[“ABC”] \[“ABC”,“DEF”] \[“ABC”,“DEF”,“G”,“H”] <p>注意:假设此单词是此任务的非空字符串大写字符。 </p>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>quibble</code>是一种功能。
|
||||
testString: assert(typeof quibble === 'function');
|
||||
- text: <code>quibble(["ABC"])</code>应该返回一个字符串。
|
||||
testString: assert(typeof quibble(["ABC"]) === 'string');
|
||||
- text: <code>quibble([])</code>应返回“{}”。
|
||||
testString: assert.equal(quibble(testCases[0]), results[0]);
|
||||
- text: <code>quibble(["ABC"])</code>应该返回“{ABC}”。
|
||||
testString: assert.equal(quibble(testCases[1]), results[1]);
|
||||
- text: <code>quibble(["ABC", "DEF"])</code>应返回“{ABC和DEF}”。
|
||||
testString: assert.equal(quibble(testCases[2]), results[2]);
|
||||
- text: <code>quibble(["ABC", "DEF", "G", "H"])</code>应返回“{ABC,DEF,G和H}”。
|
||||
testString: assert.equal(quibble(testCases[3]), results[3]);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`quibble`是一种功能。
|
||||
|
||||
```js
|
||||
function quibble (words) {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
assert(typeof quibble === 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`quibble(["ABC"])`应该返回一个字符串。
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
assert(typeof quibble(['ABC']) === 'string');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`quibble([])`应返回“{}”。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert.equal(quibble(testCases[0]), results[0]);
|
||||
```
|
||||
|
||||
/section>
|
||||
`quibble(["ABC"])`应该返回“{ABC}”。
|
||||
|
||||
```js
|
||||
assert.equal(quibble(testCases[1]), results[1]);
|
||||
```
|
||||
|
||||
`quibble(["ABC", "DEF"])`应返回“{ABC和DEF}”。
|
||||
|
||||
```js
|
||||
assert.equal(quibble(testCases[2]), results[2]);
|
||||
```
|
||||
|
||||
`quibble(["ABC", "DEF", "G", "H"])`应返回“{ABC,DEF,G和H}”。
|
||||
|
||||
```js
|
||||
assert.equal(quibble(testCases[3]), results[3]);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,88 +1,87 @@
|
||||
---
|
||||
id: 596e457071c35c882915b3e4
|
||||
title: 比较字符串列表
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 比较字符串列表
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"><p>给定一个任意多个字符串的<a href="https://en.wikipedia.org/wiki/List_(abstract_data_type)" title="wp:List_(abstract_data_type)">列表</a> ,为以下每个条件实现一个函数: </p>测试它们是否都是词法上相等的测试,如果每个字符串在词法上小于它之后的字符串(即列表是否按严格的升序排列) </section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
<p>给定一个任意多个字符串的<a href='https://en.wikipedia.org/wiki/List_(abstract_data_type)' title='wp:List_(abstract_data_type)'>列表</a> ,为以下每个条件实现一个函数: </p>测试它们是否都是词法上相等的测试,如果每个字符串在词法上小于它之后的字符串(即列表是否按严格的升序排列)
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>allEqual</code>是一个函数。
|
||||
testString: assert(typeof allEqual === 'function');
|
||||
- text: <code>azSorted</code>是一个函数。
|
||||
testString: assert(typeof azSorted === 'function');
|
||||
- text: <code>allEqual(["AA", "AA", "AA", "AA"])</code>返回true。
|
||||
testString: assert(allEqual(testCases[0]));
|
||||
- text: <code>azSorted(["AA", "AA", "AA", "AA"])</code>返回false。
|
||||
testString: assert(!azSorted(testCases[0]));
|
||||
- text: <code>allEqual(["AA", "ACB", "BB", "CC"])</code>返回false。
|
||||
testString: assert(!allEqual(testCases[1]));
|
||||
- text: <code>azSorted(["AA", "ACB", "BB", "CC"])</code>返回true。
|
||||
testString: assert(azSorted(testCases[1]));
|
||||
- text: <code>allEqual([])</code>返回true。
|
||||
testString: assert(allEqual(testCases[2]));
|
||||
- text: <code>azSorted([])</code>返回true。
|
||||
testString: assert(azSorted(testCases[2]));
|
||||
- text: <code>allEqual(["AA"])</code>返回true。
|
||||
testString: assert(allEqual(testCases[3]));
|
||||
- text: <code>azSorted(["AA"])</code>返回true。
|
||||
testString: assert(azSorted(testCases[3]));
|
||||
- text: <code>allEqual(["BB", "AA"])</code>返回false。
|
||||
testString: assert(!allEqual(testCases[4]));
|
||||
- text: <code>azSorted(["BB", "AA"])</code>返回false。
|
||||
testString: assert(!azSorted(testCases[4]));
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`allEqual`是一个函数。
|
||||
|
||||
```js
|
||||
function allEqual (arr) {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
function azSorted (arr) {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
assert(typeof allEqual === 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`azSorted`是一个函数。
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
assert(typeof azSorted === 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`allEqual(["AA", "AA", "AA", "AA"])`返回true。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert(allEqual(testCases[0]));
|
||||
```
|
||||
|
||||
/section>
|
||||
`azSorted(["AA", "AA", "AA", "AA"])`返回false。
|
||||
|
||||
```js
|
||||
assert(!azSorted(testCases[0]));
|
||||
```
|
||||
|
||||
`allEqual(["AA", "ACB", "BB", "CC"])`返回false。
|
||||
|
||||
```js
|
||||
assert(!allEqual(testCases[1]));
|
||||
```
|
||||
|
||||
`azSorted(["AA", "ACB", "BB", "CC"])`返回true。
|
||||
|
||||
```js
|
||||
assert(azSorted(testCases[1]));
|
||||
```
|
||||
|
||||
`allEqual([])`返回true。
|
||||
|
||||
```js
|
||||
assert(allEqual(testCases[2]));
|
||||
```
|
||||
|
||||
`azSorted([])`返回true。
|
||||
|
||||
```js
|
||||
assert(azSorted(testCases[2]));
|
||||
```
|
||||
|
||||
`allEqual(["AA"])`返回true。
|
||||
|
||||
```js
|
||||
assert(allEqual(testCases[3]));
|
||||
```
|
||||
|
||||
`azSorted(["AA"])`返回true。
|
||||
|
||||
```js
|
||||
assert(azSorted(testCases[3]));
|
||||
```
|
||||
|
||||
`allEqual(["BB", "AA"])`返回false。
|
||||
|
||||
```js
|
||||
assert(!allEqual(testCases[4]));
|
||||
```
|
||||
|
||||
`azSorted(["BB", "AA"])`返回false。
|
||||
|
||||
```js
|
||||
assert(!azSorted(testCases[4]));
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,67 +1,69 @@
|
||||
---
|
||||
id: 596fd036dc1ab896c5db98b1
|
||||
title: 将秒转换为复合持续时间
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 将秒转换为复合持续时间
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">任务: <p>实现一个功能: </p>取一个表示以秒为单位的持续时间的正整数作为输入(例如, <code>100</code> ),并返回一个字符串,该字符串显示分解为周,日,小时,分钟和秒的相同持续时间,如下所述(例如,“ <code>1 min, 40 sec</code> “)。 <p>证明它通过以下三个测试用例: </p><p style="font-size:115%; margin:1em 0 0 0">测试用例</p><table><tbody><tr><th>输入号码</th><th>输出数量</th></tr><tr><td> 7259 </td><td> <code>2 hr, 59 sec</code> </td> </tr><tr><td> 86400 </td><td> <code>1 d</code> </td> </tr><tr><td> 6000000 </td><td> <code>9 wk, 6 d, 10 hr, 40 min</code> </td> </tr></tbody></table><p style="font-size:115%; margin:1em 0 0 0">细节</p>应使用以下五个单位: <table><tbody><tr><th>单元</th><th>输出中使用的后缀</th><th>转变</th></tr><tr><td>周</td><td> <code>wk</code> </td> <td> 1周= 7天</td></tr><tr><td>天</td><td> <code>d</code> </td> <td> 1天= 24小时</td></tr><tr><td>小时</td><td> <code>hr</code> </td> <td> 1小时= 60分钟</td></tr><tr><td>分钟</td><td> <code>min</code> </td> <td> 1分钟= 60秒</td></tr><tr><td>第二</td><td> <code>sec</code> </td> <td></td></tr></tbody></table>但是,仅包括输出中具有非零值的数量(例如,返回“ <code>1 d</code> ”而不是“ <code>0 wk, 1 d, 0 hr, 0 min, 0 sec</code> ”)。更大的单位优先于较小的单位尽可能(例如,返回<code>2 min, 10 sec</code>而不是<code>1 min, 70 sec</code>或<code>130 sec</code> )模拟测试用例中显示的格式(从最大单位到最小单位的数量,以逗号+空格分隔;数值和单位每个数量用空格分隔)。 <p></p><hr style="margin:1em 0;"><p></p></section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
任务:
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
实现一个功能:
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>convertSeconds</code>是一个函数。
|
||||
testString: assert(typeof convertSeconds === 'function');
|
||||
- text: '<code>convertSeconds(7259)</code>应该返回<code>2 hr, 59 sec</code> 。'
|
||||
testString: assert.equal(convertSeconds(testCases[0]), results[0]);
|
||||
- text: <code>convertSeconds(86400)</code>应返回<code>1 d</code> 。
|
||||
testString: assert.equal(convertSeconds(testCases[1]), results[1]);
|
||||
- text: '<code>convertSeconds(6000000)</code>应该返回<code>9 wk, 6 d, 10 hr, 40 min</code> 。'
|
||||
testString: assert.equal(convertSeconds(testCases[2]), results[2]);
|
||||
取一个表示以秒为单位的持续时间的正整数作为输入(例如, `100` ),并返回一个字符串,该字符串显示分解为周,日,小时,分钟和秒的相同持续时间,如下所述(例如,“ `1 min, 40 sec` “)。
|
||||
|
||||
```
|
||||
证明它通过以下三个测试用例:
|
||||
|
||||
</section>
|
||||
测试用例
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
| 输入号码 | 输出数量 |
|
||||
| ------- | -------------------------- |
|
||||
| 7259 | `2 hr, 59 sec` |
|
||||
| 86400 | `1 d` |
|
||||
| 6000000 | `9 wk, 6 d, 10 hr, 40 min` |
|
||||
|
||||
<div id='js-seed'>
|
||||
细节
|
||||
|
||||
应使用以下五个单位:
|
||||
|
||||
| 单元 | 输出中使用的后缀 | 转变 |
|
||||
| -- | -------- | --------- |
|
||||
| 周 | `wk` | 1周= 7天 |
|
||||
| 天 | `d` | 1天= 24小时 |
|
||||
| 小时 | `hr` | 1小时= 60分钟 |
|
||||
| 分钟 | `min` | 1分钟= 60秒 |
|
||||
| 第二 | `sec` | |
|
||||
|
||||
但是,仅包括输出中具有非零值的数量(例如,返回“ `1 d` ”而不是“ `0 wk, 1 d, 0 hr, 0 min, 0 sec` ”)。更大的单位优先于较小的单位尽可能(例如,返回`2 min, 10 sec`而不是`1 min, 70 sec`或`130 sec` )模拟测试用例中显示的格式(从最大单位到最小单位的数量,以逗号+空格分隔;数值和单位每个数量用空格分隔)。
|
||||
|
||||
* * *
|
||||
|
||||
# --hints--
|
||||
|
||||
`convertSeconds`是一个函数。
|
||||
|
||||
```js
|
||||
function convertSeconds (sec) {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
assert(typeof convertSeconds === 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`convertSeconds(7259)`应该返回`2 hr, 59 sec` 。
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
assert.equal(convertSeconds(testCases[0]), results[0]);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`convertSeconds(86400)`应返回`1 d` 。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert.equal(convertSeconds(testCases[1]), results[1]);
|
||||
```
|
||||
|
||||
/section>
|
||||
`convertSeconds(6000000)`应该返回`9 wk, 6 d, 10 hr, 40 min` 。
|
||||
|
||||
```js
|
||||
assert.equal(convertSeconds(testCases[2]), results[2]);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,67 +1,51 @@
|
||||
---
|
||||
id: 596fda99c69f779975a1b67d
|
||||
title: 计算子字符串的出现次数
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 计算子字符串的出现次数
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">任务: <p>创建函数或显示内置函数,以计算字符串中子字符串的非重叠出现次数。 </p><p>该函数应该有两个参数: </p>第一个参数是要搜索的字符串,第二个参数是要搜索的子字符串。 <p>它应该返回一个整数计数。 </p><p>匹配应产生最多数量的非重叠匹配。 </p><p>通常,这实质上意味着从左到右或从右到左匹配。 </p></section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
任务:
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
创建函数或显示内置函数,以计算字符串中子字符串的非重叠出现次数。
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>countSubstring</code>是一个函数。
|
||||
testString: assert(typeof countSubstring === 'function');
|
||||
- text: <code>countSubstring("the three truths", "th")</code>应该返回<code>3</code> 。
|
||||
testString: assert.equal(countSubstring(testCases[0], searchString[0]), results[0]);
|
||||
- text: <code>countSubstring("ababababab", "abab")</code>应返回<code>2</code> 。
|
||||
testString: assert.equal(countSubstring(testCases[1], searchString[1]), results[1]);
|
||||
- text: <code>countSubstring("abaabba*bbaba*bbab", "a*b")</code>应返回<code>2</code> 。
|
||||
testString: assert.equal(countSubstring(testCases[2], searchString[2]), results[2]);
|
||||
该函数应该有两个参数:
|
||||
|
||||
```
|
||||
第一个参数是要搜索的字符串,第二个参数是要搜索的子字符串。
|
||||
|
||||
</section>
|
||||
它应该返回一个整数计数。
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
匹配应产生最多数量的非重叠匹配。
|
||||
|
||||
<div id='js-seed'>
|
||||
通常,这实质上意味着从左到右或从右到左匹配。
|
||||
|
||||
# --hints--
|
||||
|
||||
`countSubstring`是一个函数。
|
||||
|
||||
```js
|
||||
function countSubstring (str, subStr) {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
assert(typeof countSubstring === 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`countSubstring("the three truths", "th")`应该返回`3` 。
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
assert.equal(countSubstring(testCases[0], searchString[0]), results[0]);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`countSubstring("ababababab", "abab")`应返回`2` 。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert.equal(countSubstring(testCases[1], searchString[1]), results[1]);
|
||||
```
|
||||
|
||||
/section>
|
||||
`countSubstring("abaabba*bbaba*bbab", "a*b")`应返回`2` 。
|
||||
|
||||
```js
|
||||
assert.equal(countSubstring(testCases[2], searchString[2]), results[2]);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,55 +1,27 @@
|
||||
---
|
||||
id: 59713bd26bdeb8a594fb9413
|
||||
title: 计算硬币
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 计算硬币
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"><p> <a href="https://en.wikipedia.org/wiki/United_States" title="链接:https://en.wikipedia.org/wiki/United_States">美国</a>货币有四种常见硬币: </p>季度(25美分)硬币(10美分)镍(5美分)和便士(1美分) <p>有六种方法可以换15美分: </p>一角钱和一角钱一角钱和5便士3镍2镍和5便士一镍和10便士15便士任务: <p>实现一个功能,以确定使用这些普通硬币改变一美元的方式有多少? (1美元= 100美分)。 </p>参考: <a href="http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-11.html#%_sec_Temp_52" title="链接:http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-11.html#%_sec_Temp_52">麻省理工学院出版社的算法</a> 。 </section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
<p> <a href='https://en.wikipedia.org/wiki/United_States' title='链接:https://en.wikipedia.org/wiki/United_States'>美国</a>货币有四种常见硬币: </p>季度(25美分)硬币(10美分)镍(5美分)和便士(1美分) <p>有六种方法可以换15美分: </p>一角钱和一角钱一角钱和5便士3镍2镍和5便士一镍和10便士15便士任务: <p>实现一个功能,以确定使用这些普通硬币改变一美元的方式有多少? (1美元= 100美分)。 </p>参考: <a href='http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-11.html#%_sec_Temp_52' title='链接:http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-11.html#%_sec_Temp_52'>麻省理工学院出版社的算法</a> 。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>countCoins</code>是一个函数。
|
||||
testString: assert(typeof countCoins === 'function');
|
||||
- text: <code>countCoints()</code>应该返回242。
|
||||
testString: assert.equal(countCoins(), 242);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`countCoins`是一个函数。
|
||||
|
||||
```js
|
||||
function countCoins () {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
assert(typeof countCoins === 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`countCoints()`应该返回242。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert.equal(countCoins(), 242);
|
||||
```
|
||||
|
||||
/section>
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,65 +1,33 @@
|
||||
---
|
||||
id: 59713da0a428c1a62d7db430
|
||||
title: 克莱默的统治
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 克莱默的统治
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"><p> <a href="https://en.wikipedia.org/wiki/linear algebra" title="wp:线性代数">在线性代数中</a> , <a href="https://en.wikipedia.org/wiki/Cramer's rule" title="wp:Cramer的规则">Cramer规则</a>是一个<a href="https://en.wikipedia.org/wiki/system of linear equations" title="wp:线性方程组">线性方程组</a>解的显式公式,其中包含与未知数一样多的方程,只要系统具有唯一解,就有效。它通过用方程右边的矢量替换一列来表示(方形)系数矩阵的决定因素和从它获得的矩阵的解决方案。 </p><p>特定</p><p><big></big></p><p> <big>$ \ left \ {\ begin {matrix} a_1x + b_1y + c_1z&= {\ color {red} d_1} \\ a_2x + b_2y + c_2z&= {\ color {red} d_2} \\ a_3x + b_3y + c_3z&= {\颜色{红} D_3} \ {结束矩阵} \权。$</big> </p><p>以矩阵格式表示</p><p><big></big></p><p> <big>$ \ begin {bmatrix} a_1&b_1&c_1 \\ a_2&b_2&c_2 \\ a_3&b_3&c_3 \ end {bmatrix} \ begin {bmatrix} x \\ y \\ z \ end {bmatrix} = \ begin {bmatrix} {\ color {red} d_1} \\ {\ color {red} d_2} \\ {\ color {red} d_3} \ end {bmatrix}。$</big> </p><p>然后可以找到$ x,y $和$ z $的值,如下所示: </p><p><big></big></p><p> <big>$ x = \ frac {\ begin {vmatrix} {\ color {red} d_1}&b_1&c_1 \\ {\ color {red} d_2}&b_2&c_2 \\ {\ color {red} d_3}&b_3& c_3 \ end {vmatrix}} {\ begin {vmatrix} a_1&b_1&c_1 \\ a_2&b_2&c_2 \\ a_3&b_3&c_3 \ end {vmatrix}},\ quad y = \ frac {\ begin {vmatrix } a_1&{\ color {red} d_1}&c_1 \\ a_2&{\ color {red} d_2}&c_2 \\ a_3&{\ color {red} d_3}&c_3 \ end {vmatrix}} {\ begin {vmatrix} a_1&b_1&c_1 \\ a_2&b_2&c_2 \\ a_3&b_3&c_3 \ end {vmatrix}},\ text {和} z = \ frac {\ begin {vmatrix} a_1&b_1&{\ color {red} d_1} \\ a_2&b_2&{\ color {red} d_2} \\ a_3&b_3&{\ color {red} d_3} \ end {vmatrix}} {\ begin {vmatrix} a_1&b_1& c_1 \\ a_2&b_2&c_2 \\ a_3&b_3&c_3 \ end {vmatrix}}。$</big> </p>任务<p>给定以下方程组: </p><p> <big>$ \ begin {例} 2w-x + 5y + z = -3 \\ 3w + 2x + 2y-6z = -32 \\ w + 3x + 3y-z = -47 \\ 5w-2x-3y + 3z = 49 \\ \ end {cases} $</big> </p><p>使用Cramer的规则解决<big>$ w $,$ x $,$ y $</big>和<big>$ z $</big> 。 </p></section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
<p> <a href='https://en.wikipedia.org/wiki/linear algebra' title='wp:线性代数'>在线性代数中</a> , <a href='https://en.wikipedia.org/wiki/Cramer's rule' title='wp:Cramer的规则'>Cramer规则</a>是一个<a href='https://en.wikipedia.org/wiki/system of linear equations' title='wp:线性方程组'>线性方程组</a>解的显式公式,其中包含与未知数一样多的方程,只要系统具有唯一解,就有效。它通过用方程右边的矢量替换一列来表示(方形)系数矩阵的决定因素和从它获得的矩阵的解决方案。 </p><p>特定</p><p><big></big></p><p> <big>$ \ left \ {\ begin {matrix} a_1x + b_1y + c_1z&= {\ color {red} d_1} \\ a_2x + b_2y + c_2z&= {\ color {red} d_2} \\ a_3x + b_3y + c_3z&= {\颜色{红} D_3} \ {结束矩阵} \权。$</big> </p><p>以矩阵格式表示</p><p><big></big></p><p> <big>$ \ begin {bmatrix} a_1&b_1&c_1 \\ a_2&b_2&c_2 \\ a_3&b_3&c_3 \ end {bmatrix} \ begin {bmatrix} x \\ y \\ z \ end {bmatrix} = \ begin {bmatrix} {\ color {red} d_1} \\ {\ color {red} d_2} \\ {\ color {red} d_3} \ end {bmatrix}。$</big> </p><p>然后可以找到$ x,y $和$ z $的值,如下所示: </p><p><big></big></p><p> <big>$ x = \ frac {\ begin {vmatrix} {\ color {red} d_1}&b_1&c_1 \\ {\ color {red} d_2}&b_2&c_2 \\ {\ color {red} d_3}&b_3& c_3 \ end {vmatrix}} {\ begin {vmatrix} a_1&b_1&c_1 \\ a_2&b_2&c_2 \\ a_3&b_3&c_3 \ end {vmatrix}},\ quad y = \ frac {\ begin {vmatrix } a_1&{\ color {red} d_1}&c_1 \\ a_2&{\ color {red} d_2}&c_2 \\ a_3&{\ color {red} d_3}&c_3 \ end {vmatrix}} {\ begin {vmatrix} a_1&b_1&c_1 \\ a_2&b_2&c_2 \\ a_3&b_3&c_3 \ end {vmatrix}},\ text {和} z = \ frac {\ begin {vmatrix} a_1&b_1&{\ color {red} d_1} \\ a_2&b_2&{\ color {red} d_2} \\ a_3&b_3&{\ color {red} d_3} \ end {vmatrix}} {\ begin {vmatrix} a_1&b_1& c_1 \\ a_2&b_2&c_2 \\ a_3&b_3&c_3 \ end {vmatrix}}。$</big> </p>任务<p>给定以下方程组: </p><p> <big>$ \ begin {例} 2w-x + 5y + z = -3 \\ 3w + 2x + 2y-6z = -32 \\ w + 3x + 3y-z = -47 \\ 5w-2x-3y + 3z = 49 \\ \ end {cases} $</big> </p><p>使用Cramer的规则解决<big>$ w $,$ x $,$ y $</big>和<big>$ z $</big> 。 </p>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>cramersRule</code>是一个函数。
|
||||
testString: assert(typeof cramersRule === 'function');
|
||||
- text: '<code>cramersRule([[2, -1, 5, 1], [3, 2, 2, -6], [1, 3, 3, -1], [5, -2, -3, 3]], [-3, -32, -47, 49])</code>应返回<code>[2, -12, -4, 1]</code> 。'
|
||||
testString: assert.deepEqual(cramersRule(matrices[0], freeTerms[0]), answers[0]);
|
||||
- text: '<code>cramersRule([[3, 1, 1], [2, 2, 5], [1, -3, -4]], [3, -1, 2])</code>应返回<code>[1, 1, -1]</code> 。'
|
||||
testString: assert.deepEqual(cramersRule(matrices[1], freeTerms[1]), answers[1]);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`cramersRule`是一个函数。
|
||||
|
||||
```js
|
||||
function cramersRule (matrix, freeTerms) {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
assert(typeof cramersRule === 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`cramersRule([[2, -1, 5, 1], [3, 2, 2, -6], [1, 3, 3, -1], [5, -2, -3, 3]], [-3, -32, -47, 49])`应返回`[2, -12, -4, 1]` 。
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
assert.deepEqual(cramersRule(matrices[0], freeTerms[0]), answers[0]);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`cramersRule([[3, 1, 1], [2, 2, 5], [1, -3, -4]], [3, -1, 2])`应返回`[1, 1, -1]` 。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert.deepEqual(cramersRule(matrices[1], freeTerms[1]), answers[1]);
|
||||
```
|
||||
|
||||
/section>
|
||||
# --solutions--
|
||||
|
||||
|
@ -0,0 +1,100 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc7e03
|
||||
title: Cumulative standard deviation
|
||||
challengeType: 5
|
||||
forumTopicId: 302240
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Write a function that takes an array of numbers as parameter and returns the [standard deviation](<https://en.wikipedia.org/wiki/Standard Deviation>) of the series.
|
||||
|
||||
# --hints--
|
||||
|
||||
`standardDeviation` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof standardDeviation == 'function');
|
||||
```
|
||||
|
||||
`standardDeviation([2, 4, 4, 4, 5, 5, 7, 9])` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof standardDeviation([2, 4, 4, 4, 5, 5, 7, 9]) == 'number');
|
||||
```
|
||||
|
||||
`standardDeviation([2, 4, 4, 4, 5, 5, 7, 9])` should return `2`.
|
||||
|
||||
```js
|
||||
assert.equal(standardDeviation([2, 4, 4, 4, 5, 5, 7, 9]), 2);
|
||||
```
|
||||
|
||||
`standardDeviation([600, 470, 170, 430, 300])` should return `147.323`.
|
||||
|
||||
```js
|
||||
assert.equal(standardDeviation([600, 470, 170, 430, 300]), 147.323);
|
||||
```
|
||||
|
||||
`standardDeviation([75, 83, 96, 100, 121, 125])` should return `18.239`.
|
||||
|
||||
```js
|
||||
assert.equal(standardDeviation([75, 83, 96, 100, 121, 125]), 18.239);
|
||||
```
|
||||
|
||||
`standardDeviation([23, 37, 45, 49, 56, 63, 63, 70, 72, 82])` should return `16.87`.
|
||||
|
||||
```js
|
||||
assert.equal(
|
||||
standardDeviation([23, 37, 45, 49, 56, 63, 63, 70, 72, 82]),
|
||||
16.87
|
||||
);
|
||||
```
|
||||
|
||||
`standardDeviation([271, 354, 296, 301, 333, 326, 285, 298, 327, 316, 287, 314])` should return `22.631`.
|
||||
|
||||
```js
|
||||
assert.equal(
|
||||
standardDeviation([
|
||||
271,
|
||||
354,
|
||||
296,
|
||||
301,
|
||||
333,
|
||||
326,
|
||||
285,
|
||||
298,
|
||||
327,
|
||||
316,
|
||||
287,
|
||||
314
|
||||
]),
|
||||
22.631
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function standardDeviation(arr) {
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function standardDeviation(arr) {
|
||||
var sum = 0,
|
||||
sum_sq = 0,
|
||||
n = arr.length;
|
||||
arr.forEach(function(e) {
|
||||
sum += e;
|
||||
sum_sq += e * e;
|
||||
});
|
||||
|
||||
var std_dev = Math.sqrt(sum_sq / n - Math.pow(sum / n, 2));
|
||||
return Math.round(std_dev * 1000) / 1000;
|
||||
}
|
||||
```
|
@ -0,0 +1,117 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc7e05
|
||||
title: CUSIP
|
||||
challengeType: 5
|
||||
forumTopicId: 302241
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
A **CUSIP** is a nine-character alphanumeric code that identifies a North American financial security for the purposes of facilitating clearing and settlement of trades. The CUSIP was adopted as an American National Standard under Accredited Standards X9.6.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Write a function that takes a string as a parameter and checks if the string is valid CUSIP.
|
||||
|
||||
# --hints--
|
||||
|
||||
`isCusip` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof isCusip == 'function');
|
||||
```
|
||||
|
||||
`isCusip("037833100")` should return a boolean.
|
||||
|
||||
```js
|
||||
assert(typeof isCusip('037833100') == 'boolean');
|
||||
```
|
||||
|
||||
`isCusip("037833100")` should return `true`.
|
||||
|
||||
```js
|
||||
assert.equal(isCusip('037833100'), true);
|
||||
```
|
||||
|
||||
`isCusip("17275R102")` should return `true`.
|
||||
|
||||
```js
|
||||
assert.equal(isCusip('17275R102'), true);
|
||||
```
|
||||
|
||||
`isCusip("38259P50a")` should return `false`.
|
||||
|
||||
```js
|
||||
assert.equal(isCusip('38259P50a'), false);
|
||||
```
|
||||
|
||||
`isCusip("38259P508")` should return `true`.
|
||||
|
||||
```js
|
||||
assert.equal(isCusip('38259P508'), true);
|
||||
```
|
||||
|
||||
`isCusip("38259P50#")` should return `false`.
|
||||
|
||||
```js
|
||||
assert.equal(isCusip('38259P50#'), false);
|
||||
```
|
||||
|
||||
`isCusip("68389X105")` should return `true`.
|
||||
|
||||
```js
|
||||
assert.equal(isCusip('68389X105'), true);
|
||||
```
|
||||
|
||||
`isCusip("68389X106")` should return `false`.
|
||||
|
||||
```js
|
||||
assert.equal(isCusip('68389X106'), false);
|
||||
```
|
||||
|
||||
`isCusip("5949181")` should return `false`.
|
||||
|
||||
```js
|
||||
assert.equal(isCusip('5949181'), false);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function isCusip(s) {
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function isCusip(s) {
|
||||
if (s.length != 9) return false;
|
||||
var sum = 0;
|
||||
var ASCII = x => x.charCodeAt(0);
|
||||
for (var i = 0; i < 7; i++) {
|
||||
var c = s.charCodeAt(i);
|
||||
|
||||
var v;
|
||||
if (c >= ASCII('0') && c <= ASCII('9')) {
|
||||
v = c - 48;
|
||||
} else if (c >= ASCII('A') && c <= ASCII('Z')) {
|
||||
v = c - 64; // lower case letters apparently invalid
|
||||
} else if (c == ASCII('*')) {
|
||||
v = 36;
|
||||
} else if (c == ASCII('@')) {
|
||||
v = 37;
|
||||
} else if (c == ASCII('#')) {
|
||||
v = 38;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
if (i % 2 == 1) v *= 2; // check if odd as using 0-based indexing
|
||||
sum += Math.floor(v / 10) + (v % 10);
|
||||
}
|
||||
return s.charCodeAt(8) - 48 == (10 - (sum % 10)) % 10;
|
||||
}
|
||||
```
|
@ -0,0 +1,171 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc7e06
|
||||
title: Cut a rectangle
|
||||
challengeType: 5
|
||||
forumTopicId: 302242
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
A given rectangle is made from *m* × *n* squares. If *m* and *n* are not both odd, then it is possible to cut a path through the rectangle along the square edges such that the rectangle splits into two connected pieces with the same shape (after rotating one of the pieces by 180°). All such paths for 2 × 2 and 4 × 3 rectangles are shown below.
|
||||
|
||||
<div style="width: 100%; text-align: center;">
|
||||
<svg xmlns="https://www.w3.org/2000/svg" xmlns:xlink="https://www.w3.org/1999/xlink" width="520" height="170" aria-hidden="true" alt="Diagram showing the possible paths for 2 by 2 and 4 by 3 rectangles">
|
||||
<style>
|
||||
.g { fill: none; stroke: #ccc }
|
||||
.s, .s2 { fill: #bff; stroke: black; fill-opacity: .4 }
|
||||
.s2 { fill: #fbf }
|
||||
.d { stroke:black; fill:none}
|
||||
</style>
|
||||
<defs> <g id="m">
|
||||
<g id="h4"><g id="h2">
|
||||
<path id="h" d="m0 10h 640" class="g"/>
|
||||
<use xlink:href="#h" transform="translate(0,20)"/></g>
|
||||
<use xlink:href="#h2" transform="translate(0, 40)"/></g>
|
||||
<use xlink:href="#h4" transform="translate(0,80)"/>
|
||||
<g id="v8"><g id="v4"><g id="v2">
|
||||
<path id="v" d="m10 0v160 m 20 0 v-160" class="g"/>
|
||||
<use xlink:href="#v" transform="translate(40,0)"/></g>
|
||||
<use xlink:href="#v2" transform="translate(80,0)"/></g>
|
||||
<use xlink:href="#v4" transform="translate(160,0)"/></g>
|
||||
<use xlink:href="#v8" transform="translate(320,0)"/></g>
|
||||
<path id="b" d="m0 0h80v60h-80z" class="s"/>
|
||||
</defs>
|
||||
<g transform="translate(.5,.5)">
|
||||
<use xlink:href="#m"/>
|
||||
<g transform="translate(10,10)">
|
||||
<path d="m0 0v40h40v-40z" class="s2"/><path d="m20 0v40" class="d"/>
|
||||
<path d="m60 0v40h40v-40z" class="s2"/><path d="m60 20h40" class="d"/>
|
||||
<g transform="translate(120, 0)">
|
||||
<use xlink:href="#b"/><path d="m0 20h40v20h40" class="d"/></g>
|
||||
<g transform="translate(220, 0)">
|
||||
<use xlink:href="#b"/><path d="m0 40h40v-20h40" class="d"/></g>
|
||||
<g transform="translate(320, 0)">
|
||||
<use xlink:href="#b"/><path d="m20 0v40h20v-20h20v40" class="d"/></g>
|
||||
<g transform="translate(420, 0)">
|
||||
<use xlink:href="#b"/><path d="m60 0v40h-20v-20h-20v40" class="d"/></g>
|
||||
<g transform="translate(20, 80)">
|
||||
<use xlink:href="#b"/><path d="m40 0v60" class="d"/></g>
|
||||
<g transform="translate(120, 80)">
|
||||
<use xlink:href="#b"/><path d="m60 0v20h-20v20h-20v20" class="d"/></g>
|
||||
<g transform="translate(220, 80)">
|
||||
<use xlink:href="#b"/><path d="m20 0v20h20v20h20v20" class="d"/></g>
|
||||
<g transform="translate(320, 80)">
|
||||
<use xlink:href="#b"/><path d="m0 20h20v20h20v-20h20v20h20" class="d"/></g>
|
||||
<g transform="translate(420, 80)">
|
||||
<use xlink:href="#b"/><path d="m0 40h20v-20h20v20h20v-20h20" class="d"/></g>
|
||||
</g></g>
|
||||
</svg>
|
||||
</div>
|
||||
|
||||
# --instructions--
|
||||
|
||||
Write a function that calculates the number of different ways to cut an *m* × *n* rectangle.
|
||||
|
||||
# --hints--
|
||||
|
||||
`cutRectangle` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof cutRectangle == 'function');
|
||||
```
|
||||
|
||||
`cutRectangle(2, 2)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof cutRectangle(2, 2) == 'number');
|
||||
```
|
||||
|
||||
`cutRectangle(2, 2)` should return `2`.
|
||||
|
||||
```js
|
||||
assert.equal(cutRectangle(2, 2), 2);
|
||||
```
|
||||
|
||||
`cutRectangle(4, 3)` should return `9`.
|
||||
|
||||
```js
|
||||
assert.equal(cutRectangle(4, 3), 9);
|
||||
```
|
||||
|
||||
`cutRectangle(4, 4)` should return `22`.
|
||||
|
||||
```js
|
||||
assert.equal(cutRectangle(4, 4), 22);
|
||||
```
|
||||
|
||||
`cutRectangle(8, 3)` should return `53`.
|
||||
|
||||
```js
|
||||
assert.equal(cutRectangle(8, 3), 53);
|
||||
```
|
||||
|
||||
`cutRectangle(7, 4)` should return `151`.
|
||||
|
||||
```js
|
||||
assert.equal(cutRectangle(7, 4), 151);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function cutRectangle(w, h) {
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function cutRectangle(w, h) {
|
||||
if (w % 2 == 1 && h % 2 == 1) return;
|
||||
|
||||
var dirs = [[0, -1], [-1, 0], [0, 1], [1, 0]];
|
||||
|
||||
var grid = new Array(h);
|
||||
for (var i = 0; i < grid.length; i++) grid[i] = new Array(w);
|
||||
var stack = [];
|
||||
|
||||
var half = Math.floor((w * h) / 2);
|
||||
var bits = Math.pow(2, half) - 1;
|
||||
var result = 0;
|
||||
for (; bits > 0; bits -= 2) {
|
||||
for (var i = 0; i < half; i++) {
|
||||
var r = Math.floor(i / w);
|
||||
var c = i % w;
|
||||
grid[r][c] = (bits & (1 << i)) != 0 ? 1 : 0;
|
||||
grid[h - r - 1][w - c - 1] = 1 - grid[r][c];
|
||||
}
|
||||
|
||||
stack.push(0);
|
||||
grid[0][0] = 2;
|
||||
var count = 1;
|
||||
while (stack.length != 0) {
|
||||
var pos = stack.pop();
|
||||
var r = Math.floor(pos / w);
|
||||
var c = pos % w;
|
||||
|
||||
for (var dir of dirs) {
|
||||
var nextR = r + dir[0];
|
||||
var nextC = c + dir[1];
|
||||
|
||||
if (nextR >= 0 && nextR < h && nextC >= 0 && nextC < w) {
|
||||
if (grid[nextR][nextC] == 1) {
|
||||
stack.push(nextR * w + nextC);
|
||||
grid[nextR][nextC] = 2;
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (count == half) {
|
||||
result++;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
```
|
@ -1,67 +1,47 @@
|
||||
---
|
||||
id: 59669d08d75b60482359409f
|
||||
title: 日期格式
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 日期格式
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">任务: <p>返回包含以下格式的当前日期的数组: </p><p> - 2007-11-23和</p><p> - 2007年11月23日星期日</p><p>示例输出: <code>['2007-11-23', 'Sunday, November 23, 2007']</code> </p></section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
任务:
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
返回包含以下格式的当前日期的数组:
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>getDateFormats</code>是一个函数。
|
||||
testString: assert(typeof getDateFormats === 'function');
|
||||
- text: 应该返回一个对象。
|
||||
testString: assert(typeof getDateFormats() === 'object');
|
||||
- text: 应该返回一个包含2个元素的数组。
|
||||
testString: assert(getDateFormats().length === 2);
|
||||
- text: 应以正确的格式返回正确的日期
|
||||
testString: assert.deepEqual(getDateFormats(), dates, equalsMessage);
|
||||
\- 2007-11-23和
|
||||
|
||||
```
|
||||
\- 2007年11月23日星期日
|
||||
|
||||
</section>
|
||||
示例输出: `['2007-11-23', 'Sunday, November 23, 2007']`
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
# --hints--
|
||||
|
||||
<div id='js-seed'>
|
||||
`getDateFormats`是一个函数。
|
||||
|
||||
```js
|
||||
function getDateFormats () {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
assert(typeof getDateFormats === 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
应该返回一个对象。
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
assert(typeof getDateFormats() === 'object');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
应该返回一个包含2个元素的数组。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert(getDateFormats().length === 2);
|
||||
```
|
||||
|
||||
/section>
|
||||
应以正确的格式返回正确的日期
|
||||
|
||||
```js
|
||||
assert.deepEqual(getDateFormats(), dates, equalsMessage);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,73 +1,73 @@
|
||||
---
|
||||
id: 5966c21cf732a95f1b67dd28
|
||||
title: 日期操纵
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 日期操纵
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">任务: <p>给定EST中的日期字符串,将给定日期输出为字符串,并添加12小时。 </p><p>时区应该保留。 </p><p>示例输入: </p><p> <code>"March 7 2009 7:30pm EST"</code> </p> <p>输出示例: </p><p> <code>"March 8 2009 7:30am EST"</code> </p> </section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
任务:
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
给定EST中的日期字符串,将给定日期输出为字符串,并添加12小时。
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>add12Hours</code>是一个功能。
|
||||
testString: assert(typeof add12Hours === 'function');
|
||||
- text: <code>add12Hours(dateString)</code>应该返回一个字符串。
|
||||
testString: assert(typeof add12Hours('January 17 2017 11:43am EST') === 'string');
|
||||
- text: <code>add12Hours("" + tests[0] + "")</code>应该返回<code>"" + answers[0] + ""</code>
|
||||
testString: assert(add12Hours('January 17 2017 11:43am EST') === 'January 17 2017 11:43pm EST');
|
||||
- text: 汉德尔应该改变一天。 <code>add12Hours("" + tests[1] + "")</code>应返回<code>"" + answers[1] + ""</code>
|
||||
testString: assert(add12Hours('March 7 2009 7:30pm EST') === 'March 8 2009 7:30am EST');
|
||||
- text: 汉德尔月份应该在闰年中发生变化。 <code>add12Hours("" + tests[2] + "")</code>应返回<code>"" + answers[2] + ""</code>
|
||||
testString: assert(add12Hours('February 29 2004 9:15pm EST') === 'March 1 2004 9:15am EST');
|
||||
- text: 应该在一个共同的年份改变汉德尔月份。 <code>add12Hours("" + tests[3] + "")</code>应该返回<code>"" + answers[3] + ""</code>
|
||||
testString: assert(add12Hours('February 28 1999 3:15pm EST') === 'March 1 1999 3:15am EST');
|
||||
- text: 汉德尔应该改变一年。 <code>add12Hours("" + tests[4] + "")</code>应该返回<code>"" + answers[4] + ""</code>
|
||||
testString: assert(add12Hours('December 31 2020 1:45pm EST') === 'January 1 2021 1:45am EST');
|
||||
时区应该保留。
|
||||
|
||||
```
|
||||
示例输入:
|
||||
|
||||
</section>
|
||||
`"March 7 2009 7:30pm EST"`
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
输出示例:
|
||||
|
||||
<div id='js-seed'>
|
||||
`"March 8 2009 7:30am EST"`
|
||||
|
||||
# --hints--
|
||||
|
||||
`add12Hours`是一个功能。
|
||||
|
||||
```js
|
||||
function add12Hours (dateString) {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
assert(typeof add12Hours === 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`add12Hours(dateString)`应该返回一个字符串。
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
assert(typeof add12Hours('January 17 2017 11:43am EST') === 'string');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`add12Hours("" + tests[0] + "")`应该返回`"" + answers[0] + ""`
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert(
|
||||
add12Hours('January 17 2017 11:43am EST') === 'January 17 2017 11:43pm EST'
|
||||
);
|
||||
```
|
||||
|
||||
/section>
|
||||
汉德尔应该改变一天。 `add12Hours("" + tests[1] + "")`应返回`"" + answers[1] + ""`
|
||||
|
||||
```js
|
||||
assert(add12Hours('March 7 2009 7:30pm EST') === 'March 8 2009 7:30am EST');
|
||||
```
|
||||
|
||||
汉德尔月份应该在闰年中发生变化。 `add12Hours("" + tests[2] + "")`应返回`"" + answers[2] + ""`
|
||||
|
||||
```js
|
||||
assert(add12Hours('February 29 2004 9:15pm EST') === 'March 1 2004 9:15am EST');
|
||||
```
|
||||
|
||||
应该在一个共同的年份改变汉德尔月份。 `add12Hours("" + tests[3] + "")`应该返回`"" + answers[3] + ""`
|
||||
|
||||
```js
|
||||
assert(add12Hours('February 28 1999 3:15pm EST') === 'March 1 1999 3:15am EST');
|
||||
```
|
||||
|
||||
汉德尔应该改变一年。 `add12Hours("" + tests[4] + "")`应该返回`"" + answers[4] + ""`
|
||||
|
||||
```js
|
||||
assert(
|
||||
add12Hours('December 31 2020 1:45pm EST') === 'January 1 2021 1:45am EST'
|
||||
);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,67 +1,39 @@
|
||||
---
|
||||
id: 5966f99c45e8976909a85575
|
||||
title: 一周中的天
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 一周中的天
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"><p>一家公司决定,每当圣诞节落在星期天,他们将给予他们的工人所有额外带薪假期,这样,在任何公共假期,工人将不必在下一周(12月25日到1月1日之间)工作。 </p><p>任务: </p><p>编写一个开始年份和结束年份的函数,并返回12月25日为星期日的所有年份的数组。 </p></section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
<p>一家公司决定,每当圣诞节落在星期天,他们将给予他们的工人所有额外带薪假期,这样,在任何公共假期,工人将不必在下一周(12月25日到1月1日之间)工作。 </p><p>任务: </p><p>编写一个开始年份和结束年份的函数,并返回12月25日为星期日的所有年份的数组。 </p>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>findXmasSunday</code>是一个函数。
|
||||
testString: assert(typeof findXmasSunday === 'function');
|
||||
- text: '<code>findChristmasSunday(2000, 2100)</code>应该返回一个数组。'
|
||||
testString: assert(typeof findXmasSunday(2000, 2100) === 'object');
|
||||
- text: '<code>findChristmasSunday(2008, 2121</code>应该回归[1977,1983,1988,1994,2005,2011,2016]'
|
||||
testString: assert.deepEqual(findXmasSunday(1970, 2017), firstSolution);
|
||||
- text: '<code>findChristmasSunday(2008, 2121</code>应该返回[2011,2016,2022,2033,2039,2044,2050,2061,2067,2072,2078,2089,2095,2101,2107,2112,2118]'
|
||||
testString: assert.deepEqual(findXmasSunday(2008, 2121), secondSolution);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`findXmasSunday`是一个函数。
|
||||
|
||||
```js
|
||||
function findXmasSunday (start, end) {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
assert(typeof findXmasSunday === 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`findChristmasSunday(2000, 2100)`应该返回一个数组。
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
assert(typeof findXmasSunday(2000, 2100) === 'object');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`findChristmasSunday(2008, 2121`应该回归[1977,1983,1988,1994,2005,2011,2016]
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert.deepEqual(findXmasSunday(1970, 2017), firstSolution);
|
||||
```
|
||||
|
||||
/section>
|
||||
`findChristmasSunday(2008, 2121`应该返回[2011,2016,2022,2033,2039,2044,2050,2061,2067,2072,2078,2089,2095,2101,2107,2112,2118]
|
||||
|
||||
```js
|
||||
assert.deepEqual(findXmasSunday(2008, 2121), secondSolution);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,91 +1,67 @@
|
||||
---
|
||||
id: 59694356a6e7011f7f1c5f4e
|
||||
title: FreeCell的交易卡
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: FreeCell的交易卡
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"><p> Free Cell是Paul Alfille在1978年向PLATO系统推出的单人纸牌游戏。微软的Jim Horne将名称更改为FreeCell并重新实现了<a href="http://rosettacode.org/wiki/DOS" title="DOS">DOS</a>游戏,然后是<a href="http://rosettacode.org/wiki/Windows" title="视窗">Windows</a>游戏。 </p><p>这个版本引入了32000个编号的交易。 ( <a href="http://www.solitairelaboratory.com/fcfaq.html" title="链接:http://www.solitairelaboratory.com/fcfaq.html">FreeCell FAQ</a>讲述了这段历史。) </p><p>随着游戏的流行,Jim Horne披露<a href="http://www.solitairelaboratory.com/mshuffle.txt" title="链接:http://www.solitairelaboratory.com/mshuffle.txt">了这种算法</a> ,FreeCell的其他实现开始重现微软的交易。 </p><p>这些交易编号从1到32000。 </p><p>微软的新版本有100万笔交易,编号从1到1000000;某些实现允许该范围之外的数字。 </p><p>该算法使用Microsoft C的这种<a href="http://rosettacode.org/wiki/linear congruential generator" title="线性同余发生器">线性同余生成器</a> : </p> $ state_ {n + 1} \ equiv 214013 \ times state_n + 2531011 \ pmod {2 ^ {31}} $ $ rand_n = state_n \ div 2 ^ {16} $ $ rand_n $的范围是0到32767。 <p>该算法如下: </p>使用交易编号为RNG播种。创建一个由52张牌组成的<a href="http://rosettacode.org/wiki/array" title="排列">阵列</a> :俱乐部的王牌,钻石王牌,心中的王牌,黑桃王牌,2个俱乐部,2个钻石等等等等:Ace,2,3,4,5,6, 7,8,9,10,杰克,女王,国王。数组索引为0到51,其中俱乐部的Ace为0,黑桃之王为51.直到数组为空:在索引≡下一个随机数(mod数组长度)中选择随机卡。将此随机卡与阵列的最后一张卡交换。从阵列中删除此随机卡。 (数组长度减少1.)处理此随机卡。交易所有52张牌,面朝上,横跨8列。前8张牌分为8列,后8张牌分为8张,依此类推。例: <p>订购交易卡</p><p></p><pre> 1 2 3 4 5 6 7 8
|
||||
# --description--
|
||||
|
||||
<p> Free Cell是Paul Alfille在1978年向PLATO系统推出的单人纸牌游戏。微软的Jim Horne将名称更改为FreeCell并重新实现了<a href='http://rosettacode.org/wiki/DOS' title='DOS'>DOS</a>游戏,然后是<a href='http://rosettacode.org/wiki/Windows' title='视窗'>Windows</a>游戏。 </p><p>这个版本引入了32000个编号的交易。 ( <a href='http://www.solitairelaboratory.com/fcfaq.html' title='链接:http://www.solitairelaboratory.com/fcfaq.html'>FreeCell FAQ</a>讲述了这段历史。) </p><p>随着游戏的流行,Jim Horne披露<a href='http://www.solitairelaboratory.com/mshuffle.txt' title='链接:http://www.solitairelaboratory.com/mshuffle.txt'>了这种算法</a> ,FreeCell的其他实现开始重现微软的交易。 </p><p>这些交易编号从1到32000。 </p><p>微软的新版本有100万笔交易,编号从1到1000000;某些实现允许该范围之外的数字。 </p><p>该算法使用Microsoft C的这种<a href='http://rosettacode.org/wiki/linear congruential generator' title='线性同余发生器'>线性同余生成器</a> : </p> $ state\_ {n + 1} \\ equiv 214013 \\ times state_n + 2531011 \\ pmod {2 ^ {31}} $ $ rand_n = state_n \\ div 2 ^ {16} $ $ rand_n $的范围是0到32767。 <p>该算法如下: </p>使用交易编号为RNG播种。创建一个由52张牌组成的<a href='http://rosettacode.org/wiki/array' title='排列'>阵列</a> :俱乐部的王牌,钻石王牌,心中的王牌,黑桃王牌,2个俱乐部,2个钻石等等等等:Ace,2,3,4,5,6, 7,8,9,10,杰克,女王,国王。数组索引为0到51,其中俱乐部的Ace为0,黑桃之王为51.直到数组为空:在索引≡下一个随机数(mod数组长度)中选择随机卡。将此随机卡与阵列的最后一张卡交换。从阵列中删除此随机卡。 (数组长度减少1.)处理此随机卡。交易所有52张牌,面朝上,横跨8列。前8张牌分为8列,后8张牌分为8张,依此类推。例: <p>订购交易卡</p><p></p><pre> 1 2 3 4 5 6 7 8
|
||||
9 10 11 12 13 14 15 16
|
||||
17 18 19 20 21 22 23 24
|
||||
25 26 27 28 29 30 31 32
|
||||
33 34 35 36 37 38 39 40
|
||||
41 42 43 44 45 46 47 48
|
||||
49 50 51 52 </pre><p></p><p>游戏#1 </p><p></p><pre> [
|
||||
['JD','2D','9H','JC','5D','7H','7C','5H'],
|
||||
['KD','KC','9S','5S','AD','QC','KH','3H'],
|
||||
['2S','KS','9D','QD','JS','AS','AH','3C'],
|
||||
['4C','5C','TS','QH','4H','AC','4D','7S'],
|
||||
['3S','TD','4S','TH','8H','2C','JH','7D'],
|
||||
['6D','8S','8D','QS','6C','3D','8C','TC'],
|
||||
['6S','9C','2H','6H']
|
||||
['JD','2D','9H','JC','5D','7H','7C','5H'],
|
||||
['KD','KC','9S','5S','AD','QC','KH','3H'],
|
||||
['2S','KS','9D','QD','JS','AS','AH','3C'],
|
||||
['4C','5C','TS','QH','4H','AC','4D','7S'],
|
||||
['3S','TD','4S','TH','8H','2C','JH','7D'],
|
||||
['6D','8S','8D','QS','6C','3D','8C','TC'],
|
||||
['6S','9C','2H','6H']
|
||||
] </pre><p></p><p>游戏#617 </p><p></p><pre> [
|
||||
['7D','AD','5C','3S','5S','8C','2D','AH'],
|
||||
['TD','7S','QD','AC','6D','8H','AS','KH'],
|
||||
['TH','QC','3H','9D','6S','8D','3D','TC'],
|
||||
['KD','5H','9S','3C','8S','7H','4D','JS'],
|
||||
['4C','QS','9C','9H','7C','6H','2C','2S'],
|
||||
['4S','TS','2H','5D','JC','6C','JH','QH'],
|
||||
['JD','KS','KC','4H']
|
||||
] </pre><p></p>任务: <p>编写一个函数来获取交易编号并按照与此算法相同的顺序处理卡。 </p><p>该函数必须返回表示FreeCell板的二维数组。 </p><p>还可以针对<a href="http://freecellgamesolutions.com/" title="链接:http://freecellgamesolutions.com/">1000000游戏的FreeCell解决方案</a>检查交易。 </p><p> (召唤一个视频解决方案,它会显示初始交易。) </p></section>
|
||||
['7D','AD','5C','3S','5S','8C','2D','AH'],
|
||||
['TD','7S','QD','AC','6D','8H','AS','KH'],
|
||||
['TH','QC','3H','9D','6S','8D','3D','TC'],
|
||||
['KD','5H','9S','3C','8S','7H','4D','JS'],
|
||||
['4C','QS','9C','9H','7C','6H','2C','2S'],
|
||||
['4S','TS','2H','5D','JC','6C','JH','QH'],
|
||||
['JD','KS','KC','4H']
|
||||
] </pre><p></p>任务: <p>编写一个函数来获取交易编号并按照与此算法相同的顺序处理卡。 </p><p>该函数必须返回表示FreeCell板的二维数组。 </p><p>还可以针对<a href='http://freecellgamesolutions.com/' title='链接:http://freecellgamesolutions.com/'>1000000游戏的FreeCell解决方案</a>检查交易。 </p><p> (召唤一个视频解决方案,它会显示初始交易。) </p>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
# --hints--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>dealFreeCell</code>是一个功能。
|
||||
testString: assert(typeof dealFreeCell === 'function');
|
||||
- text: <code>dealFreeCell(seed)</code>应该返回一个对象。
|
||||
testString: assert(typeof dealFreeCell(1) === 'object');
|
||||
- text: <code>dealFreeCell(seed)</code>应该返回一个长度为7的数组。
|
||||
testString: assert(dealFreeCell(1).length === 7);
|
||||
- text: <code>dealFreeCell(1)</code>应该返回一个与示例“Game#1”相同的数组
|
||||
testString: "assert.deepEqual(dealFreeCell(1), game1);"
|
||||
- text: <code>dealFreeCell(617)</code>应该返回一个与示例“Game#617”相同的数组
|
||||
testString: "assert.deepEqual(dealFreeCell(617), game617);"
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`dealFreeCell`是一个功能。
|
||||
|
||||
```js
|
||||
function dealFreeCell (seed) {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
assert(typeof dealFreeCell === 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`dealFreeCell(seed)`应该返回一个对象。
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
assert(typeof dealFreeCell(1) === 'object');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`dealFreeCell(seed)`应该返回一个长度为7的数组。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert(dealFreeCell(1).length === 7);
|
||||
```
|
||||
|
||||
/section>
|
||||
`dealFreeCell(1)`应该返回一个与示例“Game#1”相同的数组
|
||||
|
||||
```js
|
||||
assert.deepEqual(dealFreeCell(1), game1);
|
||||
```
|
||||
|
||||
`dealFreeCell(617)`应该返回一个与示例“Game#617”相同的数组
|
||||
|
||||
```js
|
||||
assert.deepEqual(dealFreeCell(617), game617);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,69 +1,53 @@
|
||||
---
|
||||
id: 596a8888ab7c01048de257d5
|
||||
title: deepcopy的
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: deepcopy的
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">任务: <p>编写一个返回给定对象的深层副本的函数。 </p><p>副本不得与给定的对象相同。 </p><p>此任务不会测试: </p>具有属性属性的对象Date对象或具有Date对象属性的对象RegEx或具有RegEx对象属性的对象原型复制</section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
任务:
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
编写一个返回给定对象的深层副本的函数。
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>deepcopy</code>应该是一个功能。
|
||||
testString: assert(typeof deepcopy === 'function');
|
||||
- text: '<code>deepcopy({test: "test"})</code>应返回一个对象。'
|
||||
testString: 'assert(typeof deepcopy(obj1) === ''object'');'
|
||||
- text: 不应该返回提供的相同对象。
|
||||
testString: assert(deepcopy(obj2) != obj2);
|
||||
- text: 传递包含数组的对象时,应返回该对象的深层副本。
|
||||
testString: assert.deepEqual(deepcopy(obj2), obj2);
|
||||
- text: 传递包含另一个对象的对象时,应返回该对象的深层副本。
|
||||
testString: assert.deepEqual(deepcopy(obj3), obj3);
|
||||
副本不得与给定的对象相同。
|
||||
|
||||
```
|
||||
此任务不会测试:
|
||||
|
||||
</section>
|
||||
具有属性属性的对象Date对象或具有Date对象属性的对象RegEx或具有RegEx对象属性的对象原型复制
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
# --hints--
|
||||
|
||||
<div id='js-seed'>
|
||||
`deepcopy`应该是一个功能。
|
||||
|
||||
```js
|
||||
function deepcopy (obj) {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
assert(typeof deepcopy === 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`deepcopy({test: "test"})`应返回一个对象。
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
assert(typeof deepcopy(obj1) === 'object');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
不应该返回提供的相同对象。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert(deepcopy(obj2) != obj2);
|
||||
```
|
||||
|
||||
/section>
|
||||
传递包含数组的对象时,应返回该对象的深层副本。
|
||||
|
||||
```js
|
||||
assert.deepEqual(deepcopy(obj2), obj2);
|
||||
```
|
||||
|
||||
传递包含另一个对象的对象时,应返回该对象的深层副本。
|
||||
|
||||
```js
|
||||
assert.deepEqual(deepcopy(obj3), obj3);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,79 +1,103 @@
|
||||
---
|
||||
id: 597089c87eec450c68aa1643
|
||||
title: 定义原始数据类型
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 定义原始数据类型
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">任务: <p>定义一个行为类似于整数但最低有效值为1且最高有效值为10的类型。 </p>错误:如果您尝试实例化一个值超出1 - 10的<code>Num</code> ,它应该抛出一个错误消息为<code>'Out of range'</code>的<code>TypeError</code> 。如果您尝试使用不是<code>Num</code>的值实例化<code>Num</code> ,则应抛出<code>TypeError</code>并显示错误消息<code>'Not a Number'</code> 。 </section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
任务:
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
定义一个行为类似于整数但最低有效值为1且最高有效值为10的类型。
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>Num</code>应该是一个功能。
|
||||
testString: assert(typeof Num === 'function');
|
||||
- text: <code>new Num(4)</code>应返回一个对象。
|
||||
testString: assert(typeof (new Num(4)) === 'object');
|
||||
- text: '<code>new Num(\'test\')</code>应抛出一个带有消息“非数字”的TypeError。'
|
||||
testString: assert.throws(() => new Num('test'), TypeError);
|
||||
- text: <code>new Num(0)</code>应该抛出一个带有消息“超出范围”的TypeError。
|
||||
testString: assert.throws(() => new Num(0), TypeError);
|
||||
- text: <code>new Num(-5)</code>应该抛出一个带有消息“超出范围”的TypeError。
|
||||
testString: assert.throws(() => new Num(-5), TypeError);
|
||||
- text: <code>new Num(10)</code>应抛出一个带有消息“超出范围”的TypeError。
|
||||
testString: assert.throws(() => new Num(11), TypeError);
|
||||
- text: <code>new Num(20)</code>应抛出一个带有消息“超出范围”的TypeError。
|
||||
testString: assert.throws(() => new Num(20), TypeError);
|
||||
- text: <code>new Num(3) + new Num(4)</code>应该等于7。
|
||||
testString: assert.equal(new Num(3) + new Num(4), 7);
|
||||
- text: <code>new Num(3) - new Num(4)</code>应该等于-1。
|
||||
testString: assert.equal(new Num(3) - new Num(4), -1);
|
||||
- text: <code>new Num(3) * new Num(4)</code>应该等于12。
|
||||
testString: assert.equal(new Num(3) * new Num(4), 12);
|
||||
- text: <code>new Num(3) / new Num(4)</code>应该等于0.75。
|
||||
testString: assert.equal(new Num(3) / new Num(4), 0.75);
|
||||
- text: <code>new Num(3) < new Num(4)</code>应该是真的。
|
||||
testString: assert(new Num(3) < new Num(4));
|
||||
- text: <code>new Num(3) > new Num(4)</code>应该是假的。
|
||||
testString: assert(!(new Num(3) > new Num(4)));
|
||||
- text: '<code>(new Num(5)).toString()</code>应返回\'5 \''
|
||||
testString: assert.equal((new Num(5)).toString(), '5');
|
||||
错误:如果您尝试实例化一个值超出1 - 10的`Num` ,它应该抛出一个错误消息为`'Out of range'`的`TypeError` 。如果您尝试使用不是`Num`的值实例化`Num` ,则应抛出`TypeError`并显示错误消息`'Not a Number'` 。
|
||||
|
||||
```
|
||||
# --hints--
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`Num`应该是一个功能。
|
||||
|
||||
```js
|
||||
function Num (n) {
|
||||
// Good luck!
|
||||
return n;
|
||||
}
|
||||
|
||||
assert(typeof Num === 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`new Num(4)`应返回一个对象。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert(typeof new Num(4) === 'object');
|
||||
```
|
||||
|
||||
/section>
|
||||
`new Num(\'test\')`应抛出一个带有消息“非数字”的TypeError。
|
||||
|
||||
```js
|
||||
assert.throws(() => new Num('test'), TypeError);
|
||||
```
|
||||
|
||||
`new Num(0)`应该抛出一个带有消息“超出范围”的TypeError。
|
||||
|
||||
```js
|
||||
assert.throws(() => new Num(0), TypeError);
|
||||
```
|
||||
|
||||
`new Num(-5)`应该抛出一个带有消息“超出范围”的TypeError。
|
||||
|
||||
```js
|
||||
assert.throws(() => new Num(-5), TypeError);
|
||||
```
|
||||
|
||||
`new Num(10)`应抛出一个带有消息“超出范围”的TypeError。
|
||||
|
||||
```js
|
||||
assert.throws(() => new Num(11), TypeError);
|
||||
```
|
||||
|
||||
`new Num(20)`应抛出一个带有消息“超出范围”的TypeError。
|
||||
|
||||
```js
|
||||
assert.throws(() => new Num(20), TypeError);
|
||||
```
|
||||
|
||||
`new Num(3) + new Num(4)`应该等于7。
|
||||
|
||||
```js
|
||||
assert.equal(new Num(3) + new Num(4), 7);
|
||||
```
|
||||
|
||||
`new Num(3) - new Num(4)`应该等于-1。
|
||||
|
||||
```js
|
||||
assert.equal(new Num(3) - new Num(4), -1);
|
||||
```
|
||||
|
||||
`new Num(3) * new Num(4)`应该等于12。
|
||||
|
||||
```js
|
||||
assert.equal(new Num(3) * new Num(4), 12);
|
||||
```
|
||||
|
||||
`new Num(3) / new Num(4)`应该等于0.75。
|
||||
|
||||
```js
|
||||
assert.equal(new Num(3) / new Num(4), 0.75);
|
||||
```
|
||||
|
||||
`new Num(3) < new Num(4)`应该是真的。
|
||||
|
||||
```js
|
||||
assert(new Num(3) < new Num(4));
|
||||
```
|
||||
|
||||
`new Num(3) > new Num(4)`应该是假的。
|
||||
|
||||
```js
|
||||
assert(!(new Num(3) > new Num(4)));
|
||||
```
|
||||
|
||||
`(new Num(5)).toString()`应返回\\'5 \\'
|
||||
|
||||
```js
|
||||
assert.equal(new Num(5).toString(), '5');
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,67 +1,39 @@
|
||||
---
|
||||
id: 59f40b17e79dbf1ab720ed7a
|
||||
title: 部门编号
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 部门编号
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"><p>有一个高度组织化的城市决定为每个部门分配一个号码: </p>警察局环卫部门消防部门<p>每个部门的数字可以在1到7之间(含)。 </p><p>这三个部门编号应该是唯一的(彼此不同),并且必须加起来为12。 </p><p>警察局长不喜欢奇怪的号码,并希望他的部门有一个偶数。 </p>任务: <p>编写一个输出所有有效组合的程序: </p><p> [2,3,7] </p><p> [2,4,6] </p><p> [2,6,4] </p><p> [2,7,3] </p><p> [4,1,7] </p><p> [4,2,6] </p><p> [4,3,5] </p><p> [4,5,3] </p><p> [4,6,2] </p><p> [4,7,1] </p><p> [6,1,5] </p><p> [6,2,4] </p><p> [6,4,2] </p><p> [6,5,1] </p></section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
<p>有一个高度组织化的城市决定为每个部门分配一个号码: </p>警察局环卫部门消防部门<p>每个部门的数字可以在1到7之间(含)。 </p><p>这三个部门编号应该是唯一的(彼此不同),并且必须加起来为12。 </p><p>警察局长不喜欢奇怪的号码,并希望他的部门有一个偶数。 </p>任务: <p>编写一个输出所有有效组合的程序: </p><p> [2,3,7] </p><p> [2,4,6] </p><p> [2,6,4] </p><p> [2,7,3] </p><p> [4,1,7] </p><p> [4,2,6] </p><p> [4,3,5] </p><p> [4,5,3] </p><p> [4,6,2] </p><p> [4,7,1] </p><p> [6,1,5] </p><p> [6,2,4] </p><p> [6,4,2] </p><p> [6,5,1] </p>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>combinations</code>应该是一个功能。
|
||||
testString: assert(typeof combinations === 'function');
|
||||
- text: '<code>combinations([1, 2, 3], 6)</code>应该返回一个数组。'
|
||||
testString: assert(Array.isArray(combinations([1, 2, 3], 6)));
|
||||
- text: '<code>combinations([1, 2, 3, 4, 5, 6, 7], 12)</code>应返回长度为14的数组。'
|
||||
testString: assert(combinations(nums, total).length === len);
|
||||
- text: '<code>combinations([1, 2, 3, 4, 5, 6, 7], 12)</code>应返回所有有效组合。'
|
||||
testString: assert.deepEqual(combinations(nums, total), result);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`combinations`应该是一个功能。
|
||||
|
||||
```js
|
||||
function combinations (possibleNumbers, total) {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
assert(typeof combinations === 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`combinations([1, 2, 3], 6)`应该返回一个数组。
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
assert(Array.isArray(combinations([1, 2, 3], 6)));
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`combinations([1, 2, 3, 4, 5, 6, 7], 12)`应返回长度为14的数组。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert(combinations(nums, total).length === len);
|
||||
```
|
||||
|
||||
/section>
|
||||
`combinations([1, 2, 3, 4, 5, 6, 7], 12)`应返回所有有效组合。
|
||||
|
||||
```js
|
||||
assert.deepEqual(combinations(nums, total), result);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,67 +1,86 @@
|
||||
---
|
||||
id: 59f4eafba0343628bb682785
|
||||
title: Discordian日期
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: Discordian日期
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">任务: <p>将给定日期从<a href="https://en.wikipedia.org/wiki/Gregorian calendar" title="wp:阳历">公历</a>转换为<a href="https://en.wikipedia.org/wiki/Discordian calendar" title="wp:Discordian日历">Discordian日历</a> 。 </p></section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
任务:
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
将给定日期从[公历](<https://en.wikipedia.org/wiki/Gregorian calendar> "wp:阳历")转换为[Discordian日历](<https://en.wikipedia.org/wiki/Discordian calendar> "wp:Discordian日历") 。
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>discordianDate</code>是一个函数。
|
||||
testString: assert(typeof discordianDate === 'function');
|
||||
- text: <code>discordianDate(new Date(2010, 6, 22))</code> <code>"Pungenday, the 57th day of Confusion in the YOLD 3176"</code> <code>discordianDate(new Date(2010, 6, 22))</code>应该返回<code>"Pungenday, the 57th day of Confusion in the YOLD 3176"</code> 。
|
||||
testString: assert(discordianDate(new Date(2010, 6, 22)) === 'Pungenday, the 57th day of Confusion in the YOLD 3176');
|
||||
- text: <code>discordianDate(new Date(2012, 1, 28))</code>应该返回<code>"Prickle-Prickle, the 59th day of Chaos in the YOLD 3178"</code> 。
|
||||
testString: assert(discordianDate(new Date(2012, 1, 28)) === 'Prickle-Prickle, the 59th day of Chaos in the YOLD 3178');
|
||||
- text: <code>discordianDate(new Date(2012, 1, 29))</code> <code>"Setting Orange, the 60th day of Chaos in the YOLD 3178. Celebrate St. Tib's Day!"</code> <code>discordianDate(new Date(2012, 1, 29))</code>应该返回<code>"Setting Orange, the 60th day of Chaos in the YOLD 3178. Celebrate St. Tib's Day!"</code> 。
|
||||
testString: assert(discordianDate(new Date(2012, 1, 29)) === 'Setting Orange, the 60th day of Chaos in the YOLD 3178. Celebrate St. Tib\'s Day!');
|
||||
- text: <code>discordianDate(new Date(2012, 2, 1))</code>应该返回<code>"Setting Orange, the 60th day of Chaos in the YOLD 3178"</code> 。
|
||||
testString: assert(discordianDate(new Date(2012, 2, 1)) === 'Setting Orange, the 60th day of Chaos in the YOLD 3178');
|
||||
- text: <code>discordianDate(new Date(2010, 0, 5))</code> <code>"Setting Orange, the 5th day of Chaos in the YOLD 3176. Celebrate Mungday!"</code> <code>discordianDate(new Date(2010, 0, 5))</code>应该返回<code>"Setting Orange, the 5th day of Chaos in the YOLD 3176. Celebrate Mungday!"</code> 。
|
||||
testString: assert(discordianDate(new Date(2010, 0, 5)) === 'Setting Orange, the 5th day of Chaos in the YOLD 3176. Celebrate Mungday!');
|
||||
- text: <code>discordianDate(new Date(2011, 4, 3))</code>应该返回<code>"Pungenday, the 50th day of Discord in the YOLD 3177. Celebrate Discoflux!"</code> 。
|
||||
testString: assert(discordianDate(new Date(2011, 4, 3)) === 'Pungenday, the 50th day of Discord in the YOLD 3177. Celebrate Discoflux!');
|
||||
- text: <code>discordianDate(new Date(2015, 9, 19))</code> <code>"Boomtime, the 73rd day of Bureaucracy in the YOLD 3181"</code> <code>discordianDate(new Date(2015, 9, 19))</code>应该返回<code>"Boomtime, the 73rd day of Bureaucracy in the YOLD 3181"</code> 。
|
||||
testString: assert(discordianDate(new Date(2015, 9, 19)) === 'Boomtime, the 73rd day of Bureaucracy in the YOLD 3181');
|
||||
# --hints--
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`discordianDate`是一个函数。
|
||||
|
||||
```js
|
||||
function discordianDate (date) {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
assert(typeof discordianDate === 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`discordianDate(new Date(2010, 6, 22))` `"Pungenday, the 57th day of Confusion in the YOLD 3176"` `discordianDate(new Date(2010, 6, 22))`应该返回`"Pungenday, the 57th day of Confusion in the YOLD 3176"` 。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert(
|
||||
discordianDate(new Date(2010, 6, 22)) ===
|
||||
'Pungenday, the 57th day of Confusion in the YOLD 3176'
|
||||
);
|
||||
```
|
||||
|
||||
/section>
|
||||
`discordianDate(new Date(2012, 1, 28))`应该返回`"Prickle-Prickle, the 59th day of Chaos in the YOLD 3178"` 。
|
||||
|
||||
```js
|
||||
assert(
|
||||
discordianDate(new Date(2012, 1, 28)) ===
|
||||
'Prickle-Prickle, the 59th day of Chaos in the YOLD 3178'
|
||||
);
|
||||
```
|
||||
|
||||
`discordianDate(new Date(2012, 1, 29))` `"Setting Orange, the 60th day of Chaos in the YOLD 3178. Celebrate St. Tib's Day!"` `discordianDate(new Date(2012, 1, 29))`应该返回`"Setting Orange, the 60th day of Chaos in the YOLD 3178. Celebrate St. Tib's Day!"` 。
|
||||
|
||||
```js
|
||||
assert(
|
||||
discordianDate(new Date(2012, 1, 29)) ===
|
||||
"Setting Orange, the 60th day of Chaos in the YOLD 3178. Celebrate St. Tib's Day!"
|
||||
);
|
||||
```
|
||||
|
||||
`discordianDate(new Date(2012, 2, 1))`应该返回`"Setting Orange, the 60th day of Chaos in the YOLD 3178"` 。
|
||||
|
||||
```js
|
||||
assert(
|
||||
discordianDate(new Date(2012, 2, 1)) ===
|
||||
'Setting Orange, the 60th day of Chaos in the YOLD 3178'
|
||||
);
|
||||
```
|
||||
|
||||
`discordianDate(new Date(2010, 0, 5))` `"Setting Orange, the 5th day of Chaos in the YOLD 3176. Celebrate Mungday!"` `discordianDate(new Date(2010, 0, 5))`应该返回`"Setting Orange, the 5th day of Chaos in the YOLD 3176. Celebrate Mungday!"` 。
|
||||
|
||||
```js
|
||||
assert(
|
||||
discordianDate(new Date(2010, 0, 5)) ===
|
||||
'Setting Orange, the 5th day of Chaos in the YOLD 3176. Celebrate Mungday!'
|
||||
);
|
||||
```
|
||||
|
||||
`discordianDate(new Date(2011, 4, 3))`应该返回`"Pungenday, the 50th day of Discord in the YOLD 3177. Celebrate Discoflux!"` 。
|
||||
|
||||
```js
|
||||
assert(
|
||||
discordianDate(new Date(2011, 4, 3)) ===
|
||||
'Pungenday, the 50th day of Discord in the YOLD 3177. Celebrate Discoflux!'
|
||||
);
|
||||
```
|
||||
|
||||
`discordianDate(new Date(2015, 9, 19))` `"Boomtime, the 73rd day of Bureaucracy in the YOLD 3181"` `discordianDate(new Date(2015, 9, 19))`应该返回`"Boomtime, the 73rd day of Bureaucracy in the YOLD 3181"` 。
|
||||
|
||||
```js
|
||||
assert(
|
||||
discordianDate(new Date(2015, 9, 19)) ===
|
||||
'Boomtime, the 73rd day of Bureaucracy in the YOLD 3181'
|
||||
);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -0,0 +1,74 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc7e1e
|
||||
title: Dot product
|
||||
challengeType: 5
|
||||
forumTopicId: 302251
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Create a function, to compute the **[dot product](<https://en.wikipedia.org/wiki/Dot product>)**, also known as the **scalar product** of two vectors.
|
||||
|
||||
# --hints--
|
||||
|
||||
`dotProduct` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof dotProduct == 'function');
|
||||
```
|
||||
|
||||
`dotProduct([1, 3, -5], [4, -2, -1])` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof dotProduct([1, 3, -5], [4, -2, -1]) == 'number');
|
||||
```
|
||||
|
||||
`dotProduct([1, 3, -5], [4, -2, -1])` should return `3`.
|
||||
|
||||
```js
|
||||
assert.equal(dotProduct([1, 3, -5], [4, -2, -1]), 3);
|
||||
```
|
||||
|
||||
`dotProduct([1, 2, 3, 4, 5], [6, 7, 8, 9, 10])` should return `130`.
|
||||
|
||||
```js
|
||||
assert.equal(dotProduct([1, 2, 3, 4, 5], [6, 7, 8, 9, 10]), 130);
|
||||
```
|
||||
|
||||
`dotProduct([5, 4, 3, 2], [7, 8, 9, 6])` should return `106`.
|
||||
|
||||
```js
|
||||
assert.equal(dotProduct([5, 4, 3, 2], [7, 8, 9, 6]), 106);
|
||||
```
|
||||
|
||||
`dotProduct([-5, 4, -3, 2], [-7, -8, 9, -6])` should return `-36`.
|
||||
|
||||
```js
|
||||
assert.equal(dotProduct([-5, 4, -3, 2], [-7, -8, 9, -6]), -36);
|
||||
```
|
||||
|
||||
`dotProduct([17, 27, 34, 43, 15], [62, 73, 48, 95, 110])` should return `10392`.
|
||||
|
||||
```js
|
||||
assert.equal(dotProduct([17, 27, 34, 43, 15], [62, 73, 48, 95, 110]), 10392);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function dotProduct(ary1, ary2) {
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function dotProduct(ary1, ary2) {
|
||||
var dotprod = 0;
|
||||
for (var i = 0; i < ary1.length; i++) dotprod += ary1[i] * ary2[i];
|
||||
return dotprod;
|
||||
}
|
||||
```
|
@ -1,66 +1,172 @@
|
||||
---
|
||||
id: 599c333915e0ea32d04d4bec
|
||||
title: 元素操作
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 元素操作
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"><p>实现基本的元素矩阵 - 矩阵和标量矩阵运算。 </p><p>实行: </p><p> :: *另外</p><p> :: *减法</p><p> :: *乘法</p><p> :: *分裂</p><p> :: *取幂</p><p>第一个参数是要执行的操作,例如:用于矩阵加法的“m_add”和用于标量加法的“s_add”。第二和第三参数将是要在其上执行操作的矩阵。 </p></section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
<p>实现基本的元素矩阵 - 矩阵和标量矩阵运算。 </p><p>实行: </p><p> :: *另外</p><p> :: *减法</p><p> :: *乘法</p><p> :: *分裂</p><p> :: *取幂</p><p>第一个参数是要执行的操作,例如:用于矩阵加法的“m_add”和用于标量加法的“s_add”。第二和第三参数将是要在其上执行操作的矩阵。 </p>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>operation</code>是一种功能。
|
||||
testString: assert(typeof operation === 'function');
|
||||
- text: <code>operation("m_add",[[1,2],[3,4]],[[1,2],[3,4]])</code>应返回<code>[[2,4],[6,8]]</code> 。
|
||||
testString: assert.deepEqual(operation('m_add', [[1, 2], [3, 4]], [[1, 2], [3, 4]]), [[2, 4], [6, 8]]);
|
||||
- text: <code>operation("s_add",[[1,2],[3,4]],[[1,2],[3,4]])</code>应返回<code>[[3,4],[5,6]]</code> 。
|
||||
testString: assert.deepEqual(operation('s_add', [[1, 2], [3, 4]], 2), [[3, 4], [5, 6]]);
|
||||
- text: <code>operation("m_sub",[[1,2],[3,4]],[[1,2],[3,4]])</code>应返回<code>[[0,0],[0,0]]</code> 。
|
||||
testString: assert.deepEqual(operation('m_sub', [[1, 2], [3, 4]], [[1, 2], [3, 4]]), [[0, 0], [0, 0]]);
|
||||
- text: <code>operation("m_mult",[[1,2],[3,4]],[[1,2],[3,4]])</code>应该返回<code>[[1,4],[9,16]]</code> 。
|
||||
testString: assert.deepEqual(operation('m_mult', [[1, 2], [3, 4]], [[1, 2], [3, 4]]), [[1, 4], [9, 16]]);
|
||||
- text: <code>operation("m_div",[[1,2],[3,4]],[[1,2],[3,4]])</code>应返回<code>[[1,1],[1,1]]</code> 。
|
||||
testString: assert.deepEqual(operation('m_div', [[1, 2], [3, 4]], [[1, 2], [3, 4]]), [[1, 1], [1, 1]]);
|
||||
- text: <code>operation("m_exp",[[1,2],[3,4]],[[1,2],[3,4]])</code>应返回<code>[[1,4],[27,256]]</code> 。
|
||||
testString: assert.deepEqual(operation('m_exp', [[1, 2], [3, 4]], [[1, 2], [3, 4]]), [[1, 4], [27, 256]]);
|
||||
- text: <code>operation("m_add",[[1,2,3,4],[5,6,7,8]],[[9,10,11,12],[13,14,15,16]])</code>应该返回<code>[[10,12,14,16],[18,20,22,24]]</code> 。
|
||||
testString: assert.deepEqual(operation('m_add', [[1, 2, 3, 4], [5, 6, 7, 8]], [[9, 10, 11, 12], [13, 14, 15, 16]]), [[10, 12, 14, 16], [18, 20, 22, 24]]);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`operation`是一种功能。
|
||||
|
||||
```js
|
||||
function operation (op, arr1, arr2) {
|
||||
// Good luck!
|
||||
}
|
||||
|
||||
assert(typeof operation === 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`operation("m_add",[[1,2],[3,4]],[[1,2],[3,4]])`应返回`[[2,4],[6,8]]` 。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert.deepEqual(
|
||||
operation(
|
||||
'm_add',
|
||||
[
|
||||
[1, 2],
|
||||
[3, 4]
|
||||
],
|
||||
[
|
||||
[1, 2],
|
||||
[3, 4]
|
||||
]
|
||||
),
|
||||
[
|
||||
[2, 4],
|
||||
[6, 8]
|
||||
]
|
||||
);
|
||||
```
|
||||
|
||||
/section>
|
||||
`operation("s_add",[[1,2],[3,4]],[[1,2],[3,4]])`应返回`[[3,4],[5,6]]` 。
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
operation(
|
||||
's_add',
|
||||
[
|
||||
[1, 2],
|
||||
[3, 4]
|
||||
],
|
||||
2
|
||||
),
|
||||
[
|
||||
[3, 4],
|
||||
[5, 6]
|
||||
]
|
||||
);
|
||||
```
|
||||
|
||||
`operation("m_sub",[[1,2],[3,4]],[[1,2],[3,4]])`应返回`[[0,0],[0,0]]` 。
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
operation(
|
||||
'm_sub',
|
||||
[
|
||||
[1, 2],
|
||||
[3, 4]
|
||||
],
|
||||
[
|
||||
[1, 2],
|
||||
[3, 4]
|
||||
]
|
||||
),
|
||||
[
|
||||
[0, 0],
|
||||
[0, 0]
|
||||
]
|
||||
);
|
||||
```
|
||||
|
||||
`operation("m_mult",[[1,2],[3,4]],[[1,2],[3,4]])`应该返回`[[1,4],[9,16]]` 。
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
operation(
|
||||
'm_mult',
|
||||
[
|
||||
[1, 2],
|
||||
[3, 4]
|
||||
],
|
||||
[
|
||||
[1, 2],
|
||||
[3, 4]
|
||||
]
|
||||
),
|
||||
[
|
||||
[1, 4],
|
||||
[9, 16]
|
||||
]
|
||||
);
|
||||
```
|
||||
|
||||
`operation("m_div",[[1,2],[3,4]],[[1,2],[3,4]])`应返回`[[1,1],[1,1]]` 。
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
operation(
|
||||
'm_div',
|
||||
[
|
||||
[1, 2],
|
||||
[3, 4]
|
||||
],
|
||||
[
|
||||
[1, 2],
|
||||
[3, 4]
|
||||
]
|
||||
),
|
||||
[
|
||||
[1, 1],
|
||||
[1, 1]
|
||||
]
|
||||
);
|
||||
```
|
||||
|
||||
`operation("m_exp",[[1,2],[3,4]],[[1,2],[3,4]])`应返回`[[1,4],[27,256]]` 。
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
operation(
|
||||
'm_exp',
|
||||
[
|
||||
[1, 2],
|
||||
[3, 4]
|
||||
],
|
||||
[
|
||||
[1, 2],
|
||||
[3, 4]
|
||||
]
|
||||
),
|
||||
[
|
||||
[1, 4],
|
||||
[27, 256]
|
||||
]
|
||||
);
|
||||
```
|
||||
|
||||
`operation("m_add",[[1,2,3,4],[5,6,7,8]],[[9,10,11,12],[13,14,15,16]])`应该返回`[[10,12,14,16],[18,20,22,24]]` 。
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
operation(
|
||||
'm_add',
|
||||
[
|
||||
[1, 2, 3, 4],
|
||||
[5, 6, 7, 8]
|
||||
],
|
||||
[
|
||||
[9, 10, 11, 12],
|
||||
[13, 14, 15, 16]
|
||||
]
|
||||
),
|
||||
[
|
||||
[10, 12, 14, 16],
|
||||
[18, 20, 22, 24]
|
||||
]
|
||||
);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,60 +1,78 @@
|
||||
---
|
||||
id: 599d0ba974141b0f508b37d5
|
||||
title: Emirp奖金
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: Emirp奖金
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"><p>一个emirp(向后拼写的主要拼写)是素数,当它们被反转时(在它们的十进制表示中)是一个不同的素数。 </p><p>编写应能功能:显示前<b>n</b> eprimes numbers.Show在range.Show的eprimes数字eprimes的在range.Show <b><sup>第</sup> n <sup>个</sup></b> eprimes数目。 </p><p>该函数应该有两个参数。第一个将接收<b>n</b>或作为数组的范围。第二个将接收一个布尔值,它指定函数是否将eprimes作为数组或单个数字(范围中的素数或第<b>n <sup>个</sup></b>素数)返回。根据参数,函数应返回数组或数字。 </p></section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
<p>一个emirp(向后拼写的主要拼写)是素数,当它们被反转时(在它们的十进制表示中)是一个不同的素数。 </p><p>编写应能功能:显示前<b>n</b> eprimes numbers.Show在range.Show的eprimes数字eprimes的在range.Show <b><sup>第</sup> n <sup>个</sup></b> eprimes数目。 </p><p>该函数应该有两个参数。第一个将接收<b>n</b>或作为数组的范围。第二个将接收一个布尔值,它指定函数是否将eprimes作为数组或单个数字(范围中的素数或第<b>n <sup>个</sup></b>素数)返回。根据参数,函数应返回数组或数字。 </p>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>emirps</code>是一个功能。
|
||||
testString: assert(typeof emirps === 'function');
|
||||
- text: '<code>emirps(20,true)</code>应该返回<code>[13,17,31,37,71,73,79,97,107,113,149,157,167,179,199,311,337,347,359,389]</code>'
|
||||
testString: assert.deepEqual(emirps(20, true), [13, 17, 31, 37, 71, 73, 79, 97, 107, 113, 149, 157, 167, 179, 199, 311, 337, 347, 359, 389]);
|
||||
- text: <code>emirps(10000)</code>应该返回<code>948349</code>
|
||||
testString: assert.deepEqual(emirps(1000), 70529);
|
||||
- text: '<code>emirps([7700,8000],true)</code>应该返回<code>[7717,7757,7817,7841,7867,7879,7901,7927,7949,7951,7963]</code>'
|
||||
testString: assert.deepEqual(emirps([7700, 8000], true), [7717, 7757, 7817, 7841, 7867, 7879, 7901, 7927, 7949, 7951, 7963]);
|
||||
- text: '<code>emirps([7700,8000],true)</code>应该返回<code>11</code>'
|
||||
testString: assert.deepEqual(emirps([7700, 8000], false), 11);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`emirps`是一个功能。
|
||||
|
||||
```js
|
||||
function emirps(n) {
|
||||
// Good luck!
|
||||
}
|
||||
|
||||
assert(typeof emirps === 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`emirps(20,true)`应该返回`[13,17,31,37,71,73,79,97,107,113,149,157,167,179,199,311,337,347,359,389]`
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert.deepEqual(emirps(20, true), [
|
||||
13,
|
||||
17,
|
||||
31,
|
||||
37,
|
||||
71,
|
||||
73,
|
||||
79,
|
||||
97,
|
||||
107,
|
||||
113,
|
||||
149,
|
||||
157,
|
||||
167,
|
||||
179,
|
||||
199,
|
||||
311,
|
||||
337,
|
||||
347,
|
||||
359,
|
||||
389
|
||||
]);
|
||||
```
|
||||
|
||||
/section>
|
||||
`emirps(10000)`应该返回`948349`
|
||||
|
||||
```js
|
||||
assert.deepEqual(emirps(1000), 70529);
|
||||
```
|
||||
|
||||
`emirps([7700,8000],true)`应该返回`[7717,7757,7817,7841,7867,7879,7901,7927,7949,7951,7963]`
|
||||
|
||||
```js
|
||||
assert.deepEqual(emirps([7700, 8000], true), [
|
||||
7717,
|
||||
7757,
|
||||
7817,
|
||||
7841,
|
||||
7867,
|
||||
7879,
|
||||
7901,
|
||||
7927,
|
||||
7949,
|
||||
7951,
|
||||
7963
|
||||
]);
|
||||
```
|
||||
|
||||
`emirps([7700,8000],true)`应该返回`11`
|
||||
|
||||
```js
|
||||
assert.deepEqual(emirps([7700, 8000], false), 11);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,64 +1,65 @@
|
||||
---
|
||||
id: 599d15309e88c813a40baf58
|
||||
title: 熵
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 熵
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">任务: <p>计算给定输入字符串的香农熵H. </p><p>给定谨慎的随机变量$ X $,它是$ N $“符号”(总字符)的字符串,由$ n $个不同的字符组成(对于二进制,n = 2),位/符号中X的香农熵是: </p><p> $ H_2(X)= - \ sum_ {i = 1} ^ n \ frac {count_i} {N} \ log_2 \ left(\ frac {count_i} {N} \ right)$ </p><p>其中$ count_i $是字符$ n_i $的计数。 </p></section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
任务:
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
计算给定输入字符串的香农熵H.
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>entropy</code>是一种功能。
|
||||
testString: assert(typeof entropy === 'function');
|
||||
- text: <code>entropy("0")</code>应该返回<code>0</code>
|
||||
testString: assert.equal(entropy('0'), 0);
|
||||
- text: <code>entropy("01")</code>应该返回<code>1</code>
|
||||
testString: assert.equal(entropy('01'), 1);
|
||||
- text: <code>entropy("0123")</code>应该返回<code>2</code>
|
||||
testString: assert.equal(entropy('0123'), 2);
|
||||
- text: <code>entropy("01234567")</code>应该返回<code>3</code>
|
||||
testString: assert.equal(entropy('01234567'), 3);
|
||||
- text: <code>entropy("0123456789abcdef")</code>应返回<code>4</code>
|
||||
testString: assert.equal(entropy('0123456789abcdef'), 4);
|
||||
- text: <code>entropy("1223334444")</code>应返回<code>1.8464393446710154</code>
|
||||
testString: assert.equal(entropy('1223334444'), 1.8464393446710154);
|
||||
给定谨慎的随机变量$ X $,它是$ N $“符号”(总字符)的字符串,由$ n $个不同的字符组成(对于二进制,n = 2),位/符号中X的香农熵是:
|
||||
|
||||
```
|
||||
$ H*2(X)= - \\ sum* {i = 1} ^ n \\ frac {count_i} {N} \\ log_2 \\ left(\\ frac {count_i} {N} \\ right)$
|
||||
|
||||
</section>
|
||||
其中$ count_i $是字符$ n_i $的计数。
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
# --hints--
|
||||
|
||||
<div id='js-seed'>
|
||||
`entropy`是一种功能。
|
||||
|
||||
```js
|
||||
function entropy (s) {
|
||||
// Good luck!
|
||||
}
|
||||
|
||||
assert(typeof entropy === 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`entropy("0")`应该返回`0`
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert.equal(entropy('0'), 0);
|
||||
```
|
||||
|
||||
/section>
|
||||
`entropy("01")`应该返回`1`
|
||||
|
||||
```js
|
||||
assert.equal(entropy('01'), 1);
|
||||
```
|
||||
|
||||
`entropy("0123")`应该返回`2`
|
||||
|
||||
```js
|
||||
assert.equal(entropy('0123'), 2);
|
||||
```
|
||||
|
||||
`entropy("01234567")`应该返回`3`
|
||||
|
||||
```js
|
||||
assert.equal(entropy('01234567'), 3);
|
||||
```
|
||||
|
||||
`entropy("0123456789abcdef")`应返回`4`
|
||||
|
||||
```js
|
||||
assert.equal(entropy('0123456789abcdef'), 4);
|
||||
```
|
||||
|
||||
`entropy("1223334444")`应返回`1.8464393446710154`
|
||||
|
||||
```js
|
||||
assert.equal(entropy('1223334444'), 1.8464393446710154);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,72 +1,57 @@
|
||||
---
|
||||
id: 5987fd532b954e0f21b5d3f6
|
||||
title: 均衡指数
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 均衡指数
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"><p>序列的平衡指数是序列的索引,使得较低指数处的元素之和等于较高指数处的元素之和。 </p><p>例如,在序列<big>$ A $中</big> : </p><p> :::: <big>$ A_0 = -7 $</big> </p><p> :::: <big>$ A_1 = 1 $</big> </p><p> :::: <big>$ A_2 = 5 $</big> </p><p> :::: <big>$ A_3 = 2 $</big> </p><p> :::: <big>$ A_4 = -4 $</big> </p><p> :::: <big>$ A_5 = 3 $</big> </p><p> :::: <big>$ A_6 = 0 $</big> </p><p> 3是均衡指数,因为: </p><p> :::: <big>$ A_0 + A_1 + A_2 = A_4 + A_5 + A_6 $</big> </p><p> 6也是均衡指数,因为: </p><p> :::: <big>$ A_0 + A_1 + A_2 + A_3 + A_4 + A_5 = 0 $</big> </p><p> (零元素之和为零) </p><p> 7不是均衡指数,因为它不是序列<big>$ A $</big>的有效索引。 </p><p>编写一个函数,给定一个序列,返回其平衡指数(如果有的话)。 </p><p>假设序列可能很长。 </p></section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
<p>序列的平衡指数是序列的索引,使得较低指数处的元素之和等于较高指数处的元素之和。 </p><p>例如,在序列<big>$ A $中</big> : </p><p> :::: <big>$ A_0 = -7 $</big> </p><p> :::: <big>$ A_1 = 1 $</big> </p><p> :::: <big>$ A_2 = 5 $</big> </p><p> :::: <big>$ A_3 = 2 $</big> </p><p> :::: <big>$ A_4 = -4 $</big> </p><p> :::: <big>$ A_5 = 3 $</big> </p><p> :::: <big>$ A_6 = 0 $</big> </p><p> 3是均衡指数,因为: </p><p> :::: <big>$ A_0 + A_1 + A_2 = A_4 + A_5 + A_6 $</big> </p><p> 6也是均衡指数,因为: </p><p> :::: <big>$ A_0 + A_1 + A_2 + A_3 + A_4 + A_5 = 0 $</big> </p><p> (零元素之和为零) </p><p> 7不是均衡指数,因为它不是序列<big>$ A $</big>的有效索引。 </p><p>编写一个函数,给定一个序列,返回其平衡指数(如果有的话)。 </p><p>假设序列可能很长。 </p>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>equilibrium</code>是一种功能。
|
||||
testString: assert(typeof equilibrium === 'function');
|
||||
- text: '<code>equilibrium([-7, 1, 5, 2, -4, 3, 0])</code> <code>[3,6]</code> <code>equilibrium([-7, 1, 5, 2, -4, 3, 0])</code>应该返回<code>[3,6]</code> 。'
|
||||
testString: assert.deepEqual(equilibrium(equilibriumTests[0]), ans[0]);
|
||||
- text: '<code>equilibrium([2, 4, 6])</code>应该返回<code>[]</code> 。'
|
||||
testString: assert.deepEqual(equilibrium(equilibriumTests[1]), ans[1]);
|
||||
- text: '<code>equilibrium([2, 9, 2])</code>应该返回<code>[1]</code> 。'
|
||||
testString: assert.deepEqual(equilibrium(equilibriumTests[2]), ans[2]);
|
||||
- text: '<code>equilibrium([1, -1, 1, -1, 1, -1, 1])</code>应该返回<code>[0,1,2,3,4,5,6]</code> 。'
|
||||
testString: assert.deepEqual(equilibrium(equilibriumTests[3]), ans[3]);
|
||||
- text: '<code>equilibrium([1])</code>应该返回<code>[0]</code> 。'
|
||||
testString: assert.deepEqual(equilibrium(equilibriumTests[4]), ans[4]);
|
||||
- text: '<code>equilibrium([])</code>应该返回<code>[]</code> 。'
|
||||
testString: assert.deepEqual(equilibrium(equilibriumTests[5]), ans[5]);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`equilibrium`是一种功能。
|
||||
|
||||
```js
|
||||
function equilibrium (a) {
|
||||
// Good luck!
|
||||
}
|
||||
|
||||
assert(typeof equilibrium === 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`equilibrium([-7, 1, 5, 2, -4, 3, 0])` `[3,6]` `equilibrium([-7, 1, 5, 2, -4, 3, 0])`应该返回`[3,6]` 。
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
assert.deepEqual(equilibrium(equilibriumTests[0]), ans[0]);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`equilibrium([2, 4, 6])`应该返回`[]` 。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert.deepEqual(equilibrium(equilibriumTests[1]), ans[1]);
|
||||
```
|
||||
|
||||
/section>
|
||||
`equilibrium([2, 9, 2])`应该返回`[1]` 。
|
||||
|
||||
```js
|
||||
assert.deepEqual(equilibrium(equilibriumTests[2]), ans[2]);
|
||||
```
|
||||
|
||||
`equilibrium([1, -1, 1, -1, 1, -1, 1])`应该返回`[0,1,2,3,4,5,6]` 。
|
||||
|
||||
```js
|
||||
assert.deepEqual(equilibrium(equilibriumTests[3]), ans[3]);
|
||||
```
|
||||
|
||||
`equilibrium([1])`应该返回`[0]` 。
|
||||
|
||||
```js
|
||||
assert.deepEqual(equilibrium(equilibriumTests[4]), ans[4]);
|
||||
```
|
||||
|
||||
`equilibrium([])`应该返回`[]` 。
|
||||
|
||||
```js
|
||||
assert.deepEqual(equilibrium(equilibriumTests[5]), ans[5]);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,62 +1,51 @@
|
||||
---
|
||||
id: 599d1566a02b571412643b84
|
||||
title: 埃塞俄比亚的乘法
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 埃塞俄比亚的乘法
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"><p>埃塞俄比亚乘法是一种仅使用加法,加倍和减半来乘以整数的方法。 </p><p>方法: </p>取两个数字相乘,然后将它们写在两列的顶部。在左侧列中反复将最后一个数字减半,丢弃任何余数,并将结果写入同一列中的最后一个,直到您写入值1.在右侧列中重复加倍最后一个数字并写入结果如下。在左侧列显示的同一行中添加结果时停止1.检查生成的表并丢弃左列中的值为偶数的任何行。将右侧列中的值相加,以产生将原始两个数相乘的结果<p>例如:17×34 </p><p> 17 34 </p><p>将第一列减半: </p><p> 17 34 </p><p> 8 </p><p> 4 </p><p> 2 </p><p> 1 </p><p>加倍第二列: </p><p> 17 34 </p><p> 8 68 </p><p> 4 136 </p><p> 2 272 </p><p> 1 544 </p><p>第一个单元格为偶数的删除行: </p><p> 17 34 </p><p> 8 <strike>68</strike> </p><p> 4 <strike>136</strike> </p><p> 2 <strike>272</strike> </p><p> 1 544 </p><p>将右侧列中的剩余数字相加: </p><p> 17 34 </p><p> 8 - </p><p> 4 --- </p><p> 2 --- </p><p> 1 544 </p><p> ==== </p><p> 578 </p><p>所以17乘以34,埃塞俄比亚方法是578。 </p>任务: <p>任务是定义三个命名函数/方法/过程/子例程: </p>一个将一个整数减半,一个减半整数,一个整数是偶数。 <p>使用这些函数创建一个执行埃塞俄比亚乘法的函数。 </p></section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
<p>埃塞俄比亚乘法是一种仅使用加法,加倍和减半来乘以整数的方法。 </p><p>方法: </p>取两个数字相乘,然后将它们写在两列的顶部。在左侧列中反复将最后一个数字减半,丢弃任何余数,并将结果写入同一列中的最后一个,直到您写入值1.在右侧列中重复加倍最后一个数字并写入结果如下。在左侧列显示的同一行中添加结果时停止1.检查生成的表并丢弃左列中的值为偶数的任何行。将右侧列中的值相加,以产生将原始两个数相乘的结果<p>例如:17×34 </p><p> 17 34 </p><p>将第一列减半: </p><p> 17 34 </p><p> 8 </p><p> 4 </p><p> 2 </p><p> 1 </p><p>加倍第二列: </p><p> 17 34 </p><p> 8 68 </p><p> 4 136 </p><p> 2 272 </p><p> 1 544 </p><p>第一个单元格为偶数的删除行: </p><p> 17 34 </p><p> 8 <strike>68</strike> </p><p> 4 <strike>136</strike> </p><p> 2 <strike>272</strike> </p><p> 1 544 </p><p>将右侧列中的剩余数字相加: </p><p> 17 34 </p><p> 8 - </p><p> 4 --- </p><p> 2 --- </p><p> 1 544 </p><p> ==== </p><p> 578 </p><p>所以17乘以34,埃塞俄比亚方法是578。 </p>任务: <p>任务是定义三个命名函数/方法/过程/子例程: </p>一个将一个整数减半,一个减半整数,一个整数是偶数。 <p>使用这些函数创建一个执行埃塞俄比亚乘法的函数。 </p>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>eth_mult</code>是一个功能。
|
||||
testString: assert(typeof eth_mult === 'function');
|
||||
- text: '<code>eth_mult(17,34)</code>应该返回<code>578</code> 。'
|
||||
testString: assert.equal(eth_mult(17, 34), 578);
|
||||
- text: '<code>eth_mult(23,46)</code>应该返回<code>1058</code> 。'
|
||||
testString: assert.equal(eth_mult(23, 46), 1058);
|
||||
- text: '<code>eth_mult(12,27)</code>应该返回<code>324</code> 。'
|
||||
testString: assert.equal(eth_mult(12, 27), 324);
|
||||
- text: '<code>eth_mult(56,98)</code>应该返回<code>5488</code> 。'
|
||||
testString: assert.equal(eth_mult(56, 98), 5488);
|
||||
- text: '<code>eth_mult(63,74)</code>应该返回<code>4662</code> 。'
|
||||
testString: assert.equal(eth_mult(63, 74), 4662);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`eth_mult`是一个功能。
|
||||
|
||||
```js
|
||||
function eth_mult (a, b) {
|
||||
// Good luck!
|
||||
}
|
||||
|
||||
assert(typeof eth_mult === 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`eth_mult(17,34)`应该返回`578` 。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert.equal(eth_mult(17, 34), 578);
|
||||
```
|
||||
|
||||
/section>
|
||||
`eth_mult(23,46)`应该返回`1058` 。
|
||||
|
||||
```js
|
||||
assert.equal(eth_mult(23, 46), 1058);
|
||||
```
|
||||
|
||||
`eth_mult(12,27)`应该返回`324` 。
|
||||
|
||||
```js
|
||||
assert.equal(eth_mult(12, 27), 324);
|
||||
```
|
||||
|
||||
`eth_mult(56,98)`应该返回`5488` 。
|
||||
|
||||
```js
|
||||
assert.equal(eth_mult(56, 98), 5488);
|
||||
```
|
||||
|
||||
`eth_mult(63,74)`应该返回`4662` 。
|
||||
|
||||
```js
|
||||
assert.equal(eth_mult(63, 74), 4662);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,60 +1,45 @@
|
||||
---
|
||||
id: 59880443fb36441083c6c20e
|
||||
title: 欧拉方法
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 欧拉方法
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"><p>欧拉方法在数值上近似具有给定初始值的一阶常微分方程(ODE)的解。它是解决初始值问题(IVP)的一种显式方法,如<a href="https://en.wikipedia.org/wiki/Euler method" title="wp:欧拉方法">维基百科页面中所述</a> 。 </p><p> ODE必须以下列形式提供: </p><p> :: <big>$ \ frac {dy(t)} {dt} = f(t,y(t))$</big> </p><p>具有初始值</p><p> :: <big>$ y(t_0)= y_0 $</big> </p><p>为了得到数值解,我们用有限差分近似替换LHS上的导数: </p><p> :: <big>$ \ frac {dy(t)} {dt} \ approx \ frac {y(t + h)-y(t)} {h} $</big> </p><p>然后解决$ y(t + h)$: </p><p> :: <big>$ y(t + h)\ about y(t)+ h \,\ frac {dy(t)} {dt} $</big> </p><p>这是一样的</p><p> :: <big>$ y(t + h)\ about y(t)+ h \,f(t,y(t))$</big> </p><p>然后迭代解决方案规则是: </p><p> :: <big>$ y_ {n + 1} = y_n + h \,f(t_n,y_n)$</big> </p><p>其中<big>$ h $</big>是步长,是解决方案准确性最相关的参数。较小的步长会提高精度,但也会增加计算成本,因此必须根据手头的问题手工挑选。 </p><p>示例:牛顿冷却法</p><p> Newton的冷却定律描述了在温度<big>$ T_R $</big>的环境中初始温度<big>$ T(t_0)= T_0 $</big>的对象如何冷却: </p><p> :: <big>$ \ frac {dT(t)} {dt} = -k \,\ Delta T $</big> </p><p>要么</p><p> :: <big>$ \ frac {dT(t)} {dt} = -k \,(T(t) - T_R)$</big> </p><p>它表示物体的冷却速率<big>$ \ frac {dT(t)} {dt} $</big>与周围环境的当前温差<big>$ \ Delta T =(T(t) - T_R)$成正比</big> 。 </p><p>我们将与数值近似进行比较的解析解是</p><p> :: <big>$ T(t)= T_R +(T_0 - T_R)\; Ë^ { -克拉} $</big> </p>任务: <p>实现欧拉方法的一个例程,然后用它来解决牛顿冷却定律的给定例子,它有三种不同的步长: </p><p> :: * 2秒</p><p> :: * 5秒和</p><p> :: * 10秒</p><p>并与分析解决方案进行比较。 </p>初始值: <p> :: *初始温度<big>$ T_0 $</big>应为100°C </p><p> :: *室温<big>$ T_R $</big>应为20°C </p><p> :: *冷却常数<big>$ k $</big>应为0.07 </p><p> :: *计算的时间间隔应为0s──►100s </p></section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
<p>欧拉方法在数值上近似具有给定初始值的一阶常微分方程(ODE)的解。它是解决初始值问题(IVP)的一种显式方法,如<a href='https://en.wikipedia.org/wiki/Euler method' title='wp:欧拉方法'>维基百科页面中所述</a> 。 </p><p> ODE必须以下列形式提供: </p><p> :: <big>$ \ frac {dy(t)} {dt} = f(t,y(t))$</big> </p><p>具有初始值</p><p> :: <big>$ y(t_0)= y_0 $</big> </p><p>为了得到数值解,我们用有限差分近似替换LHS上的导数: </p><p> :: <big>$ \ frac {dy(t)} {dt} \ approx \ frac {y(t + h)-y(t)} {h} $</big> </p><p>然后解决$ y(t + h)$: </p><p> :: <big>$ y(t + h)\ about y(t)+ h \,\ frac {dy(t)} {dt} $</big> </p><p>这是一样的</p><p> :: <big>$ y(t + h)\ about y(t)+ h \,f(t,y(t))$</big> </p><p>然后迭代解决方案规则是: </p><p> :: <big>$ y_ {n + 1} = y_n + h \,f(t_n,y_n)$</big> </p><p>其中<big>$ h $</big>是步长,是解决方案准确性最相关的参数。较小的步长会提高精度,但也会增加计算成本,因此必须根据手头的问题手工挑选。 </p><p>示例:牛顿冷却法</p><p> Newton的冷却定律描述了在温度<big>$ T_R $</big>的环境中初始温度<big>$ T(t_0)= T_0 $</big>的对象如何冷却: </p><p> :: <big>$ \ frac {dT(t)} {dt} = -k \,\ Delta T $</big> </p><p>要么</p><p> :: <big>$ \ frac {dT(t)} {dt} = -k \,(T(t) - T_R)$</big> </p><p>它表示物体的冷却速率<big>$ \ frac {dT(t)} {dt} $</big>与周围环境的当前温差<big>$ \ Delta T =(T(t) - T_R)$成正比</big> 。 </p><p>我们将与数值近似进行比较的解析解是</p><p> :: <big>$ T(t)= T_R +(T_0 - T_R)\; Ë^ { -克拉} $</big> </p>任务: <p>实现欧拉方法的一个例程,然后用它来解决牛顿冷却定律的给定例子,它有三种不同的步长: </p><p> :: * 2秒</p><p> :: * 5秒和</p><p> :: * 10秒</p><p>并与分析解决方案进行比较。 </p>初始值: <p> :: *初始温度<big>$ T_0 $</big>应为100°C </p><p> :: *室温<big>$ T_R $</big>应为20°C </p><p> :: *冷却常数<big>$ k $</big>应为0.07 </p><p> :: *计算的时间间隔应为0s──►100s </p>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>eulersMethod</code>是一个函数。
|
||||
testString: assert(typeof eulersMethod === 'function');
|
||||
- text: '<code>eulersMethod(0, 100, 100, 10)</code>应该返回一个数字。'
|
||||
testString: assert(typeof eulersMethod(0, 100, 100, 10) === 'number');
|
||||
- text: '<code>eulersMethod(0, 100, 100, 10)</code>应返回20.0424631833732。'
|
||||
testString: assert.equal(eulersMethod(0, 100, 100, 2), 20.0424631833732);
|
||||
- text: '<code>eulersMethod(0, 100, 100, 10)</code>应返回20.01449963666907。'
|
||||
testString: assert.equal(eulersMethod(0, 100, 100, 5), 20.01449963666907);
|
||||
- text: '<code>eulersMethod(0, 100, 100, 10)</code>应返回20.000472392。'
|
||||
testString: assert.equal(eulersMethod(0, 100, 100, 10), 20.000472392);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`eulersMethod`是一个函数。
|
||||
|
||||
```js
|
||||
function eulersMethod (x1, y1, x2, h) {
|
||||
// Good luck!
|
||||
}
|
||||
|
||||
assert(typeof eulersMethod === 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`eulersMethod(0, 100, 100, 10)`应该返回一个数字。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert(typeof eulersMethod(0, 100, 100, 10) === 'number');
|
||||
```
|
||||
|
||||
/section>
|
||||
`eulersMethod(0, 100, 100, 10)`应返回20.0424631833732。
|
||||
|
||||
```js
|
||||
assert.equal(eulersMethod(0, 100, 100, 2), 20.0424631833732);
|
||||
```
|
||||
|
||||
`eulersMethod(0, 100, 100, 10)`应返回20.01449963666907。
|
||||
|
||||
```js
|
||||
assert.equal(eulersMethod(0, 100, 100, 5), 20.01449963666907);
|
||||
```
|
||||
|
||||
`eulersMethod(0, 100, 100, 10)`应返回20.000472392。
|
||||
|
||||
```js
|
||||
assert.equal(eulersMethod(0, 100, 100, 10), 20.000472392);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,62 +1,51 @@
|
||||
---
|
||||
id: 598de241872ef8353c58a7a2
|
||||
title: 评估二项式系数
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 评估二项式系数
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"><p>写一个函数来计算给定n和k值的二项式系数。 </p><p>推荐这个公式: </p> $ \ binom {n} {k} = \ frac {n!} {(nk)!k!} = \ frac {n(n-1)(n-2)\ ldots(n-k + 1)} { k(k-1)(k-2)\ ldots 1} $ </section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
<p>写一个函数来计算给定n和k值的二项式系数。 </p><p>推荐这个公式: </p> $ \\ binom {n} {k} = \\ frac {n!} {(nk)!k!} = \\ frac {n(n-1)(n-2)\\ ldots(n-k + 1)} { k(k-1)(k-2)\\ ldots 1} $
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>binom</code>是一个功能。
|
||||
testString: assert(typeof binom === 'function');
|
||||
- text: '<code>binom(5,3)</code>应该返回10。'
|
||||
testString: assert.equal(binom(5, 3), 10);
|
||||
- text: '<code>binom(7,2)</code>应该返回21。'
|
||||
testString: assert.equal(binom(7, 2), 21);
|
||||
- text: '<code>binom(10,4)</code>应该返回210。'
|
||||
testString: assert.equal(binom(10, 4), 210);
|
||||
- text: '<code>binom(6,1)</code>应该返回6。'
|
||||
testString: assert.equal(binom(6, 1), 6);
|
||||
- text: '<code>binom(12,8)</code>应该返回495。'
|
||||
testString: assert.equal(binom(12, 8), 495);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`binom`是一个功能。
|
||||
|
||||
```js
|
||||
function binom (n, k) {
|
||||
// Good luck!
|
||||
}
|
||||
|
||||
assert(typeof binom === 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`binom(5,3)`应该返回10。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert.equal(binom(5, 3), 10);
|
||||
```
|
||||
|
||||
/section>
|
||||
`binom(7,2)`应该返回21。
|
||||
|
||||
```js
|
||||
assert.equal(binom(7, 2), 21);
|
||||
```
|
||||
|
||||
`binom(10,4)`应该返回210。
|
||||
|
||||
```js
|
||||
assert.equal(binom(10, 4), 210);
|
||||
```
|
||||
|
||||
`binom(6,1)`应该返回6。
|
||||
|
||||
```js
|
||||
assert.equal(binom(6, 1), 6);
|
||||
```
|
||||
|
||||
`binom(12,8)`应该返回495。
|
||||
|
||||
```js
|
||||
assert.equal(binom(12, 8), 495);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,34 +1,71 @@
|
||||
---
|
||||
id: 59e09e6d412c5939baa02d16
|
||||
title: 执行马尔可夫算法
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 执行马尔可夫算法
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">任务: <p>为<a href="https://en.wikipedia.org/wiki/Markov algorithm" title="wp:马尔可夫算法">马尔可夫算法</a>创建解释器。 </p><p>规则具有以下语法: </p><p><ruleset> :: =(( <comment> | <rule> ) <newline> +)* </newline></rule></comment></ruleset></p><p><comment> :: =#{ <any character=""> } </any></comment></p><p><rule> :: = <pattern><whitespace> - > <whitespace> [。] <replacement></replacement></whitespace></whitespace></pattern></rule></p><p><whitespace> :: =( <tab> | <space> )[ <whitespace> ] </whitespace></space></tab></whitespace></p><p>每行有一条规则。 </p><p>如果有的话<b>。</b> (期间)出席之前<replacement> ,那么这是一个终止规则,在这种情况下,解释器必须停止执行。 </replacement></p><p>规则集由一系列规则和可选注释组成。 </p><p> <big><big>规则集</big></big> </p><p>对条目使用以下测试: </p>规则集1: <pre>此规则文件是从Wikipedia中提取的:
|
||||
http://en.wikipedia.org/wiki/Markov_AlgorithmA - > apple
|
||||
B - >包
|
||||
S - >店
|
||||
T - >
|
||||
商店 - >我的兄弟
|
||||
一个从未使用过的 - >。终止规则
|
||||
</pre><p>示例文本: </p><p> <code>I bought a B of As from T S.</code> </p> <p>应该生成输出: </p><p> <code>I bought a bag of apples from my brother.</code> </p>规则集2: <p>终止规则的测试</p><pre>从维基百科的规则略有修改 - >苹果
|
||||
B - >包
|
||||
S - > .shop
|
||||
T - >
|
||||
商店 - >我的兄弟
|
||||
一个从未使用过的 - >。终止规则</pre><p>示例文本: </p><p> <code>I bought a B of As from T S.</code> </p> <p>应该生成: </p><p> <code>I bought a bag of apples from T shop.</code> </p>规则集3: <p>如果没有转义特殊的正则表达式字符,这将测试正确的替换顺序并可能捕获简单的基于正则表达式的替换例程。 </p><pre> BNF语法测试规则A - > apple
|
||||
WWWW - >用
|
||||
行李 - > - >。*
|
||||
B - >包
|
||||
- >。* - >钱
|
||||
W - > WW
|
||||
S - > .shop
|
||||
T - >
|
||||
商店 - >我的兄弟
|
||||
一个从未使用过的 - >。终止规则
|
||||
</pre><p>示例文本: </p><p> <code>I bought a B of As W my Bgage from T S.</code> </p> <p>应该生成: </p><p> <code>I bought a bag of apples with my money from T shop.</code> </p>规则集4: <p>这将测试规则扫描的正确顺序,并可能捕获以错误顺序扫描的替换例程。它实现了一般的一元乘法引擎。 (请注意,在此实现中,输入表达式必须放在下划线中。) </p><pre> ##一元乘法引擎,用于测试马尔可夫算法实现
|
||||
# --description--
|
||||
|
||||
任务:
|
||||
|
||||
为[马尔可夫算法](<https://en.wikipedia.org/wiki/Markov algorithm> "wp:马尔可夫算法")创建解释器。
|
||||
|
||||
规则具有以下语法:
|
||||
|
||||
:: =((<!--undefined-->
|
||||
|
||||
<!--undefined-->
|
||||
|
||||
:: =- >\[。]
|
||||
|
||||
:: =(|)\[]
|
||||
|
||||
每行有一条规则。
|
||||
|
||||
如果有的话**。** (期间)出席之前,那么这是一个终止规则,在这种情况下,解释器必须停止执行。
|
||||
|
||||
规则集由一系列规则和可选注释组成。
|
||||
|
||||
规则集
|
||||
|
||||
对条目使用以下测试:
|
||||
|
||||
规则集1:
|
||||
|
||||
<pre># This rules file is extracted from Wikipedia:
|
||||
# <code>http://en.wikipedia.org/wiki/Markov_Algorithm</code>
|
||||
A -> apple
|
||||
B -> bag
|
||||
S -> shop
|
||||
T -> the
|
||||
the shop -> my brother
|
||||
a never used -> .terminating rule
|
||||
</pre>
|
||||
|
||||
<p>示例文本: </p><p> <code>I bought a B of As from T S.</code> </p> <p>应该生成输出: </p><p> <code>I bought a bag of apples from my brother.</code> </p>规则集2: <p>终止规则的测试</p>
|
||||
<pre># Slightly modified from the rules on Wikipedia
|
||||
A -> apple
|
||||
B -> bag
|
||||
S -> .shop
|
||||
T -> the
|
||||
the shop -> my brother
|
||||
a never used -> .terminating rule
|
||||
</pre>
|
||||
<p>示例文本: </p><p> <code>I bought a B of As from T S.</code> </p> <p>应该生成: </p><p> <code>I bought a bag of apples from T shop.</code> </p>规则集3: <p>如果没有转义特殊的正则表达式字符,这将测试正确的替换顺序并可能捕获简单的基于正则表达式的替换例程。 </p>
|
||||
<pre># BNF Syntax testing rules
|
||||
A -> apple
|
||||
WWWW -> with
|
||||
Bgage -> ->.*
|
||||
B -> bag
|
||||
->.* -> money
|
||||
W -> WW
|
||||
S -> .shop
|
||||
T -> the
|
||||
the shop -> my brother
|
||||
a never used -> .terminating rule
|
||||
</pre>
|
||||
<p>示例文本: </p><p> <code>I bought a B of As W my Bgage from T S.</code> </p> <p>应该生成: </p><p> <code>I bought a bag of apples with my money from T shop.</code> </p>规则集4: <p>这将测试规则扫描的正确顺序,并可能捕获以错误顺序扫描的替换例程。它实现了一般的一元乘法引擎。 (请注意,在此实现中,输入表达式必须放在下划线中。) </p><pre> ##一元乘法引擎,用于测试马尔可夫算法实现
|
||||
## Donal Fellows。
|
||||
一元加法引擎_ + 1 - > _1 +
|
||||
1 + 1 - > 11+
|
||||
@ -51,7 +88,7 @@ apply1 @ 1 - > x,@ y的下一阶段
|
||||
添加终止清除_1 - > 1
|
||||
1 + _ - > 1
|
||||
_ + _ - >
|
||||
</pre><p>示例文本: </p><p> <code>_1111*11111_</code> </p> <p>应该生成输出: </p><p> <code>11111111111111111111</code> </p>规则集5: <p>一台简单的<a href="http://en.wikipedia.org/wiki/Turing_machine" title="链接:http://en.wikipedia.org/wiki/Turing_machine">图灵机</a> , </p><p>实施三态<a href="http://en.wikipedia.org/wiki/Busy_beaver" title="链接:http://en.wikipedia.org/wiki/Busy_beaver">繁忙的海狸</a> 。 </p><p>磁带由0和1组成,状态为A,B,C和H(对于Halt),并且通过在头部所在的字符之前写入状态字母来指示磁头位置。 </p><p>必须在输入中给出机器操作的初始磁带的所有部分。 </p><p>除了证明Markov算法是Turing-complete之外,它还让我抓住了C ++实现中的一个错误,这个错误没有被前四个规则集捕获。 </p><pre>图灵机:三态繁忙的海狸
|
||||
</pre><p>示例文本: </p><p> <code>_1111*11111_</code> </p> <p>应该生成输出: </p><p> <code>11111111111111111111</code> </p>规则集5: <p>一台简单的<a href='http://en.wikipedia.org/wiki/Turing_machine' title='链接:http://en.wikipedia.org/wiki/Turing_machine'>图灵机</a> , </p><p>实施三态<a href='http://en.wikipedia.org/wiki/Busy_beaver' title='链接:http://en.wikipedia.org/wiki/Busy_beaver'>繁忙的海狸</a> 。 </p><p>磁带由0和1组成,状态为A,B,C和H(对于Halt),并且通过在头部所在的字符之前写入状态字母来指示磁头位置。 </p><p>必须在输入中给出机器操作的初始磁带的所有部分。 </p><p>除了证明Markov算法是Turing-complete之外,它还让我抓住了C ++实现中的一个错误,这个错误没有被前四个规则集捕获。 </p><pre>图灵机:三态繁忙的海狸
|
||||
状态A,符号0 =>写1,向右移动,新状态BA0 - > 1B
|
||||
状态A,符号1 =>写1,向左移动,新状态C0A1 - > C01
|
||||
1A1 - > C11
|
||||
@ -62,57 +99,45 @@ _ + _ - >
|
||||
1C0 - > B11
|
||||
状态C,符号1 =>写1,向左移动,停止0C1 - > H01
|
||||
1C1 - > H11
|
||||
</pre><p>这个规则集应该转向</p><p> <code>000000A000000</code> </p> <p>成</p><p> <code>00011H1111000</code> </p> </section>
|
||||
</pre><p>这个规则集应该转向</p><p> <code>000000A000000</code> </p> <p>成</p><p> <code>00011H1111000</code> </p>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
# --hints--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>markov</code>是一个功能。
|
||||
testString: assert(typeof markov === 'function');
|
||||
- text: <code>markov(["A -> apple","B -> bag","S -> shop","T -> the","the shop -> my brother","a never used -> .terminating rule"],"I bought a B of As from T S.")</code>应该回复“我从我兄弟那里买了一袋苹果”。
|
||||
testString: assert.deepEqual(markov(rules[0],tests[0]),outputs[0]);
|
||||
- text: <code>markov(["A -> apple","B -> bag","S -> .shop","T -> the","the shop -> my brother","a never used -> .terminating rule"],"I bought a B of As from T S.")</code>应该回来“我从T商店买了一袋苹果。”
|
||||
testString: assert.deepEqual(markov(rules[1],tests[1]),outputs[1]);
|
||||
- text: <code>markov(["A -> apple","WWWW -> with","Bgage -> ->.*","B -> bag","->.* -> money","W -> WW","S -> .shop","T -> the","the shop -> my brother","a never used -> .terminating rule"],"I bought a B of As W my Bgage from T S.")</code>应该返回”我从T商店用我的钱买了一袋苹果。“
|
||||
testString: assert.deepEqual(markov(rules[2],tests[2]),outputs[2]);
|
||||
- text: <code>markov(["_+1 -> _1+","1+1 -> 11+","1! -> !1",",! -> !+","_! -> _","1*1 -> x,@y","1x -> xX","X, -> 1,1","X1 -> 1X","_x -> _X",",x -> ,X","y1 -> 1y","y_ -> _","1@1 -> x,@y","1@_ -> @_",",@_ -> !_","++ -> +","_1 -> 1","1+_ -> 1","_+_ -> "],"_1111*11111_")</code>应返回”11111111111111111111“。
|
||||
testString: assert.deepEqual(markov(rules[3],tests[3]),outputs[3]);
|
||||
- text: <code>markov(["A0 -> 1B","0A1 -> C01","1A1 -> C11","0B0 -> A01","1B0 -> A11","B1 -> 1B","0C0 -> B01","1C0 -> B11","0C1 -> H01","1C1 -> H11"],"")</code>应返回”00011H1111000“。
|
||||
testString: assert.deepEqual(markov(rules[4],tests[4]),outputs[4]);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`markov`是一个功能。
|
||||
|
||||
```js
|
||||
function markov (rules,test) {
|
||||
// Good luck!
|
||||
}
|
||||
|
||||
assert(typeof markov === 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`markov(["A -> apple","B -> bag","S -> shop","T -> the","the shop -> my brother","a never used -> .terminating rule"],"I bought a B of As from T S.")`应该回复“我从我兄弟那里买了一袋苹果”。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert.deepEqual(markov(rules[0], tests[0]), outputs[0]);
|
||||
```
|
||||
|
||||
/section>
|
||||
`markov(["A -> apple","B -> bag","S -> .shop","T -> the","the shop -> my brother","a never used -> .terminating rule"],"I bought a B of As from T S.")`应该回来“我从T商店买了一袋苹果。”
|
||||
|
||||
```js
|
||||
assert.deepEqual(markov(rules[1], tests[1]), outputs[1]);
|
||||
```
|
||||
|
||||
`markov(["A -> apple","WWWW -> with","Bgage -> ->.*","B -> bag","->.* -> money","W -> WW","S -> .shop","T -> the","the shop -> my brother","a never used -> .terminating rule"],"I bought a B of As W my Bgage from T S.")`应该返回”我从T商店用我的钱买了一袋苹果。“
|
||||
|
||||
```js
|
||||
assert.deepEqual(markov(rules[2], tests[2]), outputs[2]);
|
||||
```
|
||||
|
||||
`markov(["_+1 -> _1+","1+1 -> 11+","1! -> !1",",! -> !+","_! -> _","1*1 -> x,@y","1x -> xX","X, -> 1,1","X1 -> 1X","_x -> _X",",x -> ,X","y1 -> 1y","y_ -> _","1@1 -> x,@y","1@_ -> @_",",@_ -> !_","++ -> +","_1 -> 1","1+_ -> 1","_+_ -> "],"_1111*11111_")`应返回”11111111111111111111“。
|
||||
|
||||
```js
|
||||
assert.deepEqual(markov(rules[3], tests[3]), outputs[3]);
|
||||
```
|
||||
|
||||
`markov(["A0 -> 1B","0A1 -> C01","1A1 -> C11","0B0 -> A01","1B0 -> A11","B1 -> 1B","0C0 -> B01","1C0 -> B11","0C1 -> H01","1C1 -> H11"],"")`应返回”00011H1111000“。
|
||||
|
||||
```js
|
||||
assert.deepEqual(markov(rules[4], tests[4]), outputs[4]);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,133 +1,45 @@
|
||||
---
|
||||
id: 59e0a8df964e4540d5abe599
|
||||
title: 执行大脑****
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 执行大脑****
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"><p>编写一个函数来实现Brain ****解释器。该函数将字符串作为参数,并应返回一个字符串作为输出。更多细节如下: </p><p> <a href="http://rosettacode.org/wiki/Brainf***" title="Brainf ***">RCBF</a>是一套为各种语言的Rosetta Code编写的<a href="http://rosettacode.org/wiki/Brainf***" title="Brainf ***">Brainf ***</a>编译器和解释器。 </p><p>以下是RCBF每个版本的链接。 </p><p>实现只需要正确实现以下指令: </p><p> {| </p><p> !命令</p><p> !描述</p><p> | - </p><p> |风格=“文本对齐:中心” | <code>></code> ||将指针向右移动</p><p> | - </p><p> |风格=“文本对齐:中心” | <code><</code> ||将指针移到左侧</p><p> | - </p><p> |风格=“文本对齐:中心” | <code>+</code> ||增加指针下的内存单元格</p><p> | - </p><p> |风格=“文本对齐:中心” | <code>-</code> ||减少指针下的内存单元格</p><p> | - </p><p> |风格=“文本对齐:中心” | <code>.</code> ||输出指针处单元格表示的字符</p><p> | - </p><p> |风格=“文本对齐:中心” | <code>,</code> ||输入一个字符并将其存储在指针的单元格中</p><p> | - </p><p> |风格=“文本对齐:中心” | <code>[</code> ||如果指针下的单元格为0,则跳过匹配<code>]</code> </p><p> | - </p><p> |风格=“文本对齐:中心” | <code>]</code> ||跳回匹配<code>[</code>如果指针下的单元格非零</p><p> |} </p><p>允许任何单元格大小,EOF( <u>E</u> nd- <u>O</u> - <u>F</u> ile)支持是可选的,无论您是否有有界或无界内存。 </p></section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
<p>编写一个函数来实现Brain ****解释器。该函数将字符串作为参数,并应返回一个字符串作为输出。更多细节如下: </p><p> <a href='http://rosettacode.org/wiki/Brainf***' title='Brainf ***'>RCBF</a>是一套为各种语言的Rosetta Code编写的<a href='http://rosettacode.org/wiki/Brainf***' title='Brainf ***'>Brainf ***</a>编译器和解释器。 </p><p>以下是RCBF每个版本的链接。 </p><p>实现只需要正确实现以下指令: </p><p> {| </p><p> !命令</p><p> !描述</p><p> | - </p><p> |风格=“文本对齐:中心” | <code>></code> ||将指针向右移动</p><p> | - </p><p> |风格=“文本对齐:中心” | <code><</code> ||将指针移到左侧</p><p> | - </p><p> |风格=“文本对齐:中心” | <code>+</code> ||增加指针下的内存单元格</p><p> | - </p><p> |风格=“文本对齐:中心” | <code>-</code> ||减少指针下的内存单元格</p><p> | - </p><p> |风格=“文本对齐:中心” | <code>.</code> ||输出指针处单元格表示的字符</p><p> | - </p><p> |风格=“文本对齐:中心” | <code>,</code> ||输入一个字符并将其存储在指针的单元格中</p><p> | - </p><p> |风格=“文本对齐:中心” | <code>[</code> ||如果指针下的单元格为0,则跳过匹配<code>]</code> </p><p> | - </p><p> |风格=“文本对齐:中心” | <code>]</code> ||跳回匹配<code>[</code>如果指针下的单元格非零</p><p> |} </p><p>允许任何单元格大小,EOF( <u>E</u> nd- <u>O</u> - <u>F</u> ile)支持是可选的,无论您是否有有界或无界内存。 </p>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>brain(bye)</code>应该重新调整一个字符串
|
||||
testString: assert(typeof brain(bye) === 'string');
|
||||
- text: <code>brain("++++++[>++++++++++<-]>+++++.")</code should return "A"
|
||||
testString: assert.equal(brain("++++++[>++++++++++<-]>+++++."),"A");
|
||||
- text: <code>brain(bye)</code>应该回归<code>Goodbye, World!\\r\\n</code>
|
||||
testString: assert.equal(brain(bye), 'Goodbye, World!\r\n');
|
||||
- text: <code>brain(hello)</code>应该回归<code>Hello World!\\n</code>
|
||||
testString: assert.equal(brain(hello), "Hello World!\n");
|
||||
- text: <code>brain(fib)</code>应该返回<code>1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89</code>
|
||||
testString: assert.equal(brain(fib), "1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89");
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`brain(bye)`应该重新调整一个字符串
|
||||
|
||||
```js
|
||||
function brain (prog) {
|
||||
// Good luck!
|
||||
}
|
||||
|
||||
assert(typeof brain(bye) === 'string');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
### Before Test
|
||||
<div id='js-setup'>
|
||||
<code>brain("++++++[>++++++++++<-]>+++++.")</code should return "A"
|
||||
|
||||
```js
|
||||
let fib=`+
|
||||
|
||||
++
|
||||
|
||||
+++
|
||||
|
||||
++++
|
||||
|
||||
+>+>>
|
||||
|
||||
>>++++
|
||||
|
||||
+++++++
|
||||
|
||||
++++++++
|
||||
|
||||
+++++++++
|
||||
|
||||
++++++++++
|
||||
|
||||
++++++>++++
|
||||
|
||||
++++++++++++
|
||||
|
||||
+++++++++++++
|
||||
|
||||
+++<<<<<<[>[>>
|
||||
|
||||
>>>>+>+<<<<<<<-
|
||||
|
||||
]>>>>>>>[<<<<<<<
|
||||
|
||||
+>>>>>>>-]<[>++++
|
||||
|
||||
++++++[-<-[>>+>+<<
|
||||
|
||||
<-]>>>[<<<+>>>-]+<[
|
||||
|
||||
>[-]<[-]]>[<<[>>>+<<
|
||||
|
||||
<-]>>[-]]<<]>>>[>>+>+
|
||||
|
||||
<<<-]>>>[<<<+>>>-]+<[>
|
||||
|
||||
[-]<[-]]>[<<+>>[-]]<<<<
|
||||
|
||||
<<<]>>>>>[++++++++++++++
|
||||
|
||||
+++++++++++++++++++++++++
|
||||
|
||||
+++++++++.[-]]++++++++++<[
|
||||
|
||||
->-<]>+++++++++++++++++++++
|
||||
|
||||
+++++++++++++++++++++++++++.
|
||||
|
||||
[-]<<<<<<<<<<<<[>>>+>+<<<<-]>
|
||||
|
||||
>>>[<<<<+>>>>-]<-[>>.>.<<<[-]]
|
||||
|
||||
<<[>>+>+<<<-]>>>[<<<+>>>-]<<[<+
|
||||
|
||||
>-]>[<+>-]<<<-]`;
|
||||
let hello='++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.'
|
||||
let bye='++++++++++[>+>+++>++++>+++++++>++++++++>+++++++++>++++++++++>+++++++++++>++++++++++++<<<<<<<<<-]>>>>+.>>>>+..<.<++++++++.>>>+.<<+.<<<<++++.<++.>>>+++++++.>>>.+++.<+++++++.--------.<<<<<+.<+++.---.';
|
||||
|
||||
assert.equal(brain('++++++[>++++++++++<-]>+++++.'), 'A');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`brain(bye)`应该回归`Goodbye, World!\\r\\n`
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert.equal(brain(bye), 'Goodbye, World!\r\n');
|
||||
```
|
||||
|
||||
/section>
|
||||
`brain(hello)`应该回归`Hello World!\\n`
|
||||
|
||||
```js
|
||||
assert.equal(brain(hello), 'Hello World!\n');
|
||||
```
|
||||
|
||||
`brain(fib)`应该返回`1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89`
|
||||
|
||||
```js
|
||||
assert.equal(brain(fib), '1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89');
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,60 +1,77 @@
|
||||
---
|
||||
id: 598ee8b91b410510ae82efef
|
||||
title: 可扩展的素发生器
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 可扩展的素发生器
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"><p>按顺序编写素数生成器,它将自动调整以适应任何合理高素数的生成。 </p>发电机应能:显示前<b>N个</b>黄金numbers.Show在range.Show的素数的素数在range.Show数<b><sup>第</sup> n <sup>个</sup></b>素数。 <p>该函数应该有两个参数。第一个将接收<b>n</b>或作为数组的范围。第二个将接收一个布尔值,它指定函数是否将素数作为数组或单个数字(范围中的素数或第<b>n <sup>个</sup></b>素数)返回。根据参数,函数应该返回一个数组。 </p></section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
<p>按顺序编写素数生成器,它将自动调整以适应任何合理高素数的生成。 </p>发电机应能:显示前<b>N个</b>黄金numbers.Show在range.Show的素数的素数在range.Show数<b><sup>第</sup> n <sup>个</sup></b>素数。 <p>该函数应该有两个参数。第一个将接收<b>n</b>或作为数组的范围。第二个将接收一个布尔值,它指定函数是否将素数作为数组或单个数字(范围中的素数或第<b>n <sup>个</sup></b>素数)返回。根据参数,函数应该返回一个数组。 </p>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>primeGenerator</code>是一个函数。
|
||||
testString: assert(typeof primeGenerator === 'function');
|
||||
- text: <code>primeGenerator</code>是一个函数。
|
||||
testString: assert.deepEqual(primeGenerator(20, true), [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71]);
|
||||
- text: <code>primeGenerator</code>是一个函数。
|
||||
testString: assert.deepEqual(primeGenerator([100, 150], true), [101, 103, 107, 109, 113, 127, 131, 137, 139, 149]);
|
||||
- text: <code>primeGenerator</code>是一个函数。
|
||||
testString: assert.equal(primeGenerator([7700, 8000], false), 30);
|
||||
- text: <code>primeGenerator</code>是一个函数。
|
||||
testString: assert.equal(primeGenerator(10000, false), 104729);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`primeGenerator`是一个函数。
|
||||
|
||||
```js
|
||||
function primeGenerator (num, showPrimes) {
|
||||
// Good luck!
|
||||
}
|
||||
|
||||
assert(typeof primeGenerator === 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`primeGenerator`是一个函数。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert.deepEqual(primeGenerator(20, true), [
|
||||
2,
|
||||
3,
|
||||
5,
|
||||
7,
|
||||
11,
|
||||
13,
|
||||
17,
|
||||
19,
|
||||
23,
|
||||
29,
|
||||
31,
|
||||
37,
|
||||
41,
|
||||
43,
|
||||
47,
|
||||
53,
|
||||
59,
|
||||
61,
|
||||
67,
|
||||
71
|
||||
]);
|
||||
```
|
||||
|
||||
/section>
|
||||
`primeGenerator`是一个函数。
|
||||
|
||||
```js
|
||||
assert.deepEqual(primeGenerator([100, 150], true), [
|
||||
101,
|
||||
103,
|
||||
107,
|
||||
109,
|
||||
113,
|
||||
127,
|
||||
131,
|
||||
137,
|
||||
139,
|
||||
149
|
||||
]);
|
||||
```
|
||||
|
||||
`primeGenerator`是一个函数。
|
||||
|
||||
```js
|
||||
assert.equal(primeGenerator([7700, 8000], false), 30);
|
||||
```
|
||||
|
||||
`primeGenerator`是一个函数。
|
||||
|
||||
```js
|
||||
assert.equal(primeGenerator(10000, false), 104729);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,68 +1,45 @@
|
||||
---
|
||||
id: 597b2b2a2702b44414742771
|
||||
title: 阶乘
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 阶乘
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"><p>编写一个函数来返回一个数字的阶乘。 </p><p>一个数字的因子由下式给出: </p> N! = n *(n-1)*(n-2)* ..... * 1 <p>例如:3! = 3 * 2 * 1 = 6 4! = 4 * 3 * 2 * 1 = 24 </p><p>注意:0! = 1 </p></section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
<p>编写一个函数来返回一个数字的阶乘。 </p><p>一个数字的因子由下式给出: </p> N! = n \*(n-1)\*(n-2)\* ..... \* 1 <p>例如:3! = 3 * 2 * 1 = 6 4! = 4 * 3 * 2 * 1 = 24 </p><p>注意:0! = 1 </p>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>factorial</code>是一种功能。
|
||||
testString: assert(typeof factorial === 'function');
|
||||
- text: <code>factorial(2)</code>应该返回一个数字。
|
||||
testString: assert(typeof factorial(2) === 'number');
|
||||
- text: <code>factorial(3)</code>应该返回6.“)
|
||||
testString: assert.equal(factorial(3), 6);
|
||||
- text: <code>factorial(3)</code>应返回120.“)
|
||||
testString: assert.equal(factorial(5), 120);
|
||||
- text: '<code>factorial(3)</code>应返回3,628,800。“)'
|
||||
testString: assert.equal(factorial(10), 3628800);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`factorial`是一种功能。
|
||||
|
||||
```js
|
||||
function factorial (n) {
|
||||
// Good luck!
|
||||
}
|
||||
|
||||
assert(typeof factorial === 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`factorial(2)`应该返回一个数字。
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
assert(typeof factorial(2) === 'number');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`factorial(3)`应该返回6.“)
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert.equal(factorial(3), 6);
|
||||
```
|
||||
|
||||
/section>
|
||||
`factorial(3)`应返回120.“)
|
||||
|
||||
```js
|
||||
assert.equal(factorial(5), 120);
|
||||
```
|
||||
|
||||
`factorial(3)`应返回3,628,800。“)
|
||||
|
||||
```js
|
||||
assert.equal(factorial(10), 3628800);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,60 +1,48 @@
|
||||
---
|
||||
id: 598eea87e5cf4b116c3ff81a
|
||||
title: 梅森数的因素
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 梅森数的因素
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"><p>梅森数是2 <sup>P</sup> -1形式的数字。 </p><p>如果P是素数,那么梅森数可能是梅森素数</p><p> (如果P不是素数,则梅森数也不是素数)。 </p><p>在搜索梅森素数时,通过在开始可能冗长的<a href="http://rosettacode.org/wiki/Lucas-Lehmer test" title="Lucas-Lehmer测试">Lucas-Lehmer检验</a>之前找到一个小因子来消除指数是有利的。 </p><p>有非常有效的算法来确定数字是否除以2 <sup>P</sup> -1(或等效地,如果2 <sup>P</sup> mod(数字)= 1)。 </p><p>有些语言已经有了这个exponent-and-mod操作的内置实现(称为modPow或类似的)。 </p><p>以下是如何自己实现这个modPow: </p><p>例如,让我们计算2 <sup>23</sup> mod 47。 </p><p>将指数23转换为二进制,得到10111.从<tt>square</tt> = 1开始,重复平方。 </p><p>卸下指数的最高位,并且如果它是1 <tt>平方</tt>乘以由所述幂(2)的基础上,然后计算<tt>平方</tt>模47。 </p><p>在下一步中使用最后一步的模数结果作为<tt>square</tt>的初始值: </p><p>删除可选</p><p>方形顶部位乘以2 mod 47 </p><p> ------------ ------- ------------- ------ </p><p> 1 * 1 = 1 1 0111 1 * 2 = 2 2 </p><p> 2 * 2 = 4 0 111否4 </p><p> 4 * 4 = 16 1 11 16 * 2 = 32 32 </p><p> 32 * 32 = 1024 1 1 1024 * 2 = 2048 27 </p><p> 27 * 27 = 729 1 729 * 2 = 1458 1 </p><p>由于2 <sup>23</sup> mod 47 = 1,47是2 <sup>P</sup> -1的因子。 </p><p> (要看到这一点,从两边减去1:2 <sup>23</sup> -1 = 0 mod 47.) </p><p>由于我们已经证明47是一个因子,因此2 <sup>23</sup> -1不是素数。 </p><p> Mersenne数字的其他属性使我们能够进一步完善这一过程。 </p><p>任何因子q为2 <sup>P</sup> -1必须是2kP + 1的形式,k是正整数或零。此外,q必须是1或7 mod 8。 </p><p>最后任何潜在因素q必须是<a href="http://rosettacode.org/wiki/Primality by Trial Division" title="审判分庭的原始性">素数</a> 。 </p><p>与其他试验划分算法一样,算法在2kP + 1> sqrt(N)时停止。 </p><p>这些素性测试仅适用于P为素数的Mersenne数。例如,M <sub>4</sub> = 15不使用这些技术产生因子,而是产生3和5的因子,两者都不符合2kP + 1。 </p>任务: <p>使用上述方法找到因子2 <sup>929</sup> -1(又名M929) </p>相关任务: <a href="http://rosettacode.org/wiki/count in factors" title="算上因素">计数因素</a> <a href="http://rosettacode.org/wiki/prime decomposition" title="主要分解">素数分解</a> <a href="http://rosettacode.org/wiki/factors of an integer" title="整数的因子">的整数的因素</a> <a href="http://rosettacode.org/wiki/Sieve of Eratosthenes" title="Eratosthenes的筛子">埃拉托塞尼的筛</a> <a href="http://rosettacode.org/wiki/primality by trial division" title="审判分裂的素性">通过试验除法素性</a> <a href="http://rosettacode.org/wiki/trial factoring of a Mersenne number" title="试用Mensenne数的因式">梅森数的试验理</a> <a href="http://rosettacode.org/wiki/partition an integer X into N primes" title="将整数X划分为N个素数">分区的整数X为N个素数</a> <a href="http://rosettacode.org/wiki/sequence of primes by Trial Division" title="审判分庭的素数序列">由审判庭素数的序列</a> <a href="https://www.youtube.com/watch?v=SNwvJ7psoow" title="链接:https://www.youtube.com/watch?v = SNwvJ7psoow">在1948年计算机:2¹²⁷-1</a> </section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
<p>梅森数是2 <sup>P</sup> -1形式的数字。 </p><p>如果P是素数,那么梅森数可能是梅森素数</p><p> (如果P不是素数,则梅森数也不是素数)。 </p><p>在搜索梅森素数时,通过在开始可能冗长的<a href='http://rosettacode.org/wiki/Lucas-Lehmer test' title='Lucas-Lehmer测试'>Lucas-Lehmer检验</a>之前找到一个小因子来消除指数是有利的。 </p><p>有非常有效的算法来确定数字是否除以2 <sup>P</sup> -1(或等效地,如果2 <sup>P</sup> mod(数字)= 1)。 </p><p>有些语言已经有了这个exponent-and-mod操作的内置实现(称为modPow或类似的)。 </p><p>以下是如何自己实现这个modPow: </p><p>例如,让我们计算2 <sup>23</sup> mod 47。 </p><p>将指数23转换为二进制,得到10111.从<tt>square</tt> = 1开始,重复平方。 </p><p>卸下指数的最高位,并且如果它是1 <tt>平方</tt>乘以由所述幂(2)的基础上,然后计算<tt>平方</tt>模47。 </p><p>在下一步中使用最后一步的模数结果作为<tt>square</tt>的初始值: </p><p>删除可选</p><p>方形顶部位乘以2 mod 47 </p><p> ------------ ------- ------------- ------ </p><p> 1 * 1 = 1 1 0111 1 * 2 = 2 2 </p><p> 2 * 2 = 4 0 111否4 </p><p> 4 * 4 = 16 1 11 16 * 2 = 32 32 </p><p> 32 * 32 = 1024 1 1 1024 * 2 = 2048 27 </p><p> 27 * 27 = 729 1 729 * 2 = 1458 1 </p><p>由于2 <sup>23</sup> mod 47 = 1,47是2 <sup>P</sup> -1的因子。 </p><p> (要看到这一点,从两边减去1:2 <sup>23</sup> -1 = 0 mod 47.) </p><p>由于我们已经证明47是一个因子,因此2 <sup>23</sup> -1不是素数。 </p><p> Mersenne数字的其他属性使我们能够进一步完善这一过程。 </p><p>任何因子q为2 <sup>P</sup> -1必须是2kP + 1的形式,k是正整数或零。此外,q必须是1或7 mod 8。 </p><p>最后任何潜在因素q必须是<a href='http://rosettacode.org/wiki/Primality by Trial Division' title='审判分庭的原始性'>素数</a> 。 </p><p>与其他试验划分算法一样,算法在2kP + 1> sqrt(N)时停止。 </p><p>这些素性测试仅适用于P为素数的Mersenne数。例如,M <sub>4</sub> = 15不使用这些技术产生因子,而是产生3和5的因子,两者都不符合2kP + 1。 </p>任务: <p>使用上述方法找到因子2 <sup>929</sup> -1(又名M929) </p>相关任务: <a href='http://rosettacode.org/wiki/count in factors' title='算上因素'>计数因素</a> <a href='http://rosettacode.org/wiki/prime decomposition' title='主要分解'>素数分解</a> <a href='http://rosettacode.org/wiki/factors of an integer' title='整数的因子'>的整数的因素</a> <a href='http://rosettacode.org/wiki/Sieve of Eratosthenes' title='Eratosthenes的筛子'>埃拉托塞尼的筛</a> <a href='http://rosettacode.org/wiki/primality by trial division' title='审判分裂的素性'>通过试验除法素性</a> <a href='http://rosettacode.org/wiki/trial factoring of a Mersenne number' title='试用Mensenne数的因式'>梅森数的试验理</a> <a href='http://rosettacode.org/wiki/partition an integer X into N primes' title='将整数X划分为N个素数'>分区的整数X为N个素数</a> <a href='http://rosettacode.org/wiki/sequence of primes by Trial Division' title='审判分庭的素数序列'>由审判庭素数的序列</a> <a href='https://www.youtube.com/watch?v=SNwvJ7psoow' title='链接:https://www.youtube.com/watch?v = SNwvJ7psoow'>在1948年计算机:2¹²⁷-1</a>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>check_mersenne</code>是一个函数。
|
||||
testString: assert(typeof check_mersenne === 'function');
|
||||
- text: <code>check_mersenne(3)</code>应该返回一个字符串。
|
||||
testString: assert(typeof check_mersenne(3) == 'string');
|
||||
- text: <code>check_mersenne(3)</code>应该返回“M3 = 2 ^ 3-1是素数”。
|
||||
testString: assert.equal(check_mersenne(3),"M3 = 2^3-1 is prime");
|
||||
- text: <code>check_mersenne(23)</code>应返回“M23 = 2 ^ 23-1与因子47复合”。
|
||||
testString: assert.equal(check_mersenne(23),"M23 = 2^23-1 is composite with factor 47");
|
||||
- text: <code>check_mersenne(929)</code>应返回“M929 = 2 ^ 929-1与因子13007复合
|
||||
testString: assert.equal(check_mersenne(929),"M929 = 2^929-1 is composite with factor 13007");
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`check_mersenne`是一个函数。
|
||||
|
||||
```js
|
||||
function check_mersenne (p) {
|
||||
// Good luck!
|
||||
}
|
||||
|
||||
assert(typeof check_mersenne === 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`check_mersenne(3)`应该返回一个字符串。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert(typeof check_mersenne(3) == 'string');
|
||||
```
|
||||
|
||||
/section>
|
||||
`check_mersenne(3)`应该返回“M3 = 2 ^ 3-1是素数”。
|
||||
|
||||
```js
|
||||
assert.equal(check_mersenne(3), 'M3 = 2^3-1 is prime');
|
||||
```
|
||||
|
||||
`check_mersenne(23)`应返回“M23 = 2 ^ 23-1与因子47复合”。
|
||||
|
||||
```js
|
||||
assert.equal(check_mersenne(23), 'M23 = 2^23-1 is composite with factor 47');
|
||||
```
|
||||
|
||||
`check_mersenne(929)`应返回“M929 = 2 ^ 929-1与因子13007复合
|
||||
|
||||
```js
|
||||
assert.equal(
|
||||
check_mersenne(929),
|
||||
'M929 = 2^929-1 is composite with factor 13007'
|
||||
);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,66 +1,39 @@
|
||||
---
|
||||
id: 597f1e7fbc206f0e9ba95dc4
|
||||
title: 整数因子
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 整数因子
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"><p>编写一个返回正整数因子的函数。 </p><p>这些因子是正整数,通过该正整数可以将被分解的数量除以产生正整数结果。 </p> /// </section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
<p>编写一个返回正整数因子的函数。 </p><p>这些因子是正整数,通过该正整数可以将被分解的数量除以产生正整数结果。 </p> ///
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>factors</code>是一种功能。
|
||||
testString: assert(typeof factors === 'function');
|
||||
- text: '<code>factors(45)</code>应该返回<code>[1,3,5,9,15,45]</code> 。'
|
||||
testString: assert.deepEqual(factors(45), ans[0]);
|
||||
- text: '<code>factors(53)</code>应该返回<code>[1,53]</code> 。'
|
||||
testString: assert.deepEqual(factors(53), ans[1]);
|
||||
- text: '<code>factors(64)</code>应该返回<code>[1,2,4,8,16,32,64]</code> 。'
|
||||
testString: assert.deepEqual(factors(64), ans[2]);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`factors`是一种功能。
|
||||
|
||||
```js
|
||||
function factors (num) {
|
||||
// Good luck!
|
||||
}
|
||||
|
||||
assert(typeof factors === 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`factors(45)`应该返回`[1,3,5,9,15,45]` 。
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
assert.deepEqual(factors(45), ans[0]);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`factors(53)`应该返回`[1,53]` 。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert.deepEqual(factors(53), ans[1]);
|
||||
```
|
||||
|
||||
/section>
|
||||
`factors(64)`应该返回`[1,2,4,8,16,32,64]` 。
|
||||
|
||||
```js
|
||||
assert.deepEqual(factors(64), ans[2]);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,60 +1,56 @@
|
||||
---
|
||||
id: 59c3ec9f15068017c96eb8a3
|
||||
title: Farey序列
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: Farey序列
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"><p>编写一个返回n阶Farey序列的函数。该函数应该有一个参数n。它应该将序列作为数组返回。阅读以下内容了解更多详情: </p><p>阶数n的<a href="https://en.wikipedia.org/wiki/Farey sequence" title="wp:Farey序列">Farey序列</a> F <sub>n</sub>是在0和1之间的完全减少的分数的序列,当在最低阶段时,具有小于或等于n的分母,按照增大的大小排列。 </p><p> Farey序列有时被错误地称为Farey系列。 </p><p>每个Farey序列: </p><p> :: *以值0开头,由分数$ \ frac {0} {1} $表示</p><p> :: *以值1结尾,由$ \ frac {1} {1} $分数表示。 </p><p>订单1到5的Farey序列是: </p><p> $ {\ bf \ it {F}} _ 1 = \ frac {0} {1},\ frac {1} {1} $ </p><p></p><p> $ {\ bf \ it {F}} _ 2 = \ frac {0} {1},\ frac {1} {2},\ frac {1} {1} $ </p><p></p><p> $ {\ bf \ it {F}} _ 3 = \ frac {0} {1},\ frac {1} {3},\ frac {1} {2},\ frac {2} {3},\ frac {1} {1} $ </p><p></p><p> $ {\ bf \ it {F}} _ 4 = \ frac {0} {1},\ frac {1} {4},\ frac {1} {3},\ frac {1} {2},\ frac {2} {3},\ frac {3} {4},\ frac {1} {1} $ </p><p></p><p> $ {\ bf \ it {F}} _ 5 = \ frac {0} {1},\ frac {1} {5},\ frac {1} {4},\ frac {1} {3},\ frac {2} {5},\ frac {1} {2},\ frac {3} {5},\ frac {2} {3},\ frac {3} {4},\ frac {4} {5 },\ frac {1} {1} $ </p></section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
<p>编写一个返回n阶Farey序列的函数。该函数应该有一个参数n。它应该将序列作为数组返回。阅读以下内容了解更多详情: </p><p>阶数n的<a href='https://en.wikipedia.org/wiki/Farey sequence' title='wp:Farey序列'>Farey序列</a> F <sub>n</sub>是在0和1之间的完全减少的分数的序列,当在最低阶段时,具有小于或等于n的分母,按照增大的大小排列。 </p><p> Farey序列有时被错误地称为Farey系列。 </p><p>每个Farey序列: </p><p> :: *以值0开头,由分数$ \ frac {0} {1} $表示</p><p> :: *以值1结尾,由$ \ frac {1} {1} $分数表示。 </p><p>订单1到5的Farey序列是: </p><p> $ {\ bf \ it {F}} _ 1 = \ frac {0} {1},\ frac {1} {1} $ </p><p></p><p> $ {\ bf \ it {F}} _ 2 = \ frac {0} {1},\ frac {1} {2},\ frac {1} {1} $ </p><p></p><p> $ {\ bf \ it {F}} _ 3 = \ frac {0} {1},\ frac {1} {3},\ frac {1} {2},\ frac {2} {3},\ frac {1} {1} $ </p><p></p><p> $ {\ bf \ it {F}} _ 4 = \ frac {0} {1},\ frac {1} {4},\ frac {1} {3},\ frac {1} {2},\ frac {2} {3},\ frac {3} {4},\ frac {1} {1} $ </p><p></p><p> $ {\ bf \ it {F}} _ 5 = \ frac {0} {1},\ frac {1} {5},\ frac {1} {4},\ frac {1} {3},\ frac {2} {5},\ frac {1} {2},\ frac {3} {5},\ frac {2} {3},\ frac {3} {4},\ frac {4} {5 },\ frac {1} {1} $ </p>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>farey</code>是一种功能。
|
||||
testString: assert(typeof farey === 'function');
|
||||
- text: <code>farey(3)</code>应该返回一个数组
|
||||
testString: assert(Array.isArray(farey(3)));
|
||||
- text: <code>farey(3)</code>应该返回<code>["1/3","1/2","2/3"]</code>
|
||||
testString: assert.deepEqual(farey(3), ["1/3","1/2","2/3"]);
|
||||
- text: <code>farey(4)</code>应该返回<code>["1/4","1/3","1/2","2/4","2/3","3/4"]</code>
|
||||
testString: assert.deepEqual(farey(4), ["1/4","1/3","1/2","2/4","2/3","3/4"]);
|
||||
- text: <code>farey(5)</code>应返回<code>["1/5","1/4","1/3","2/5","1/2","2/4","3/5","2/3","3/4","4/5"]</code>
|
||||
testString: assert.deepEqual(farey(5), ["1/5","1/4","1/3","2/5","1/2","2/4","3/5","2/3","3/4","4/5"]);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`farey`是一种功能。
|
||||
|
||||
```js
|
||||
function farey (n) {
|
||||
// Good luck!
|
||||
}
|
||||
|
||||
assert(typeof farey === 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`farey(3)`应该返回一个数组
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert(Array.isArray(farey(3)));
|
||||
```
|
||||
|
||||
/section>
|
||||
`farey(3)`应该返回`["1/3","1/2","2/3"]`
|
||||
|
||||
```js
|
||||
assert.deepEqual(farey(3), ['1/3', '1/2', '2/3']);
|
||||
```
|
||||
|
||||
`farey(4)`应该返回`["1/4","1/3","1/2","2/4","2/3","3/4"]`
|
||||
|
||||
```js
|
||||
assert.deepEqual(farey(4), ['1/4', '1/3', '1/2', '2/4', '2/3', '3/4']);
|
||||
```
|
||||
|
||||
`farey(5)`应返回`["1/5","1/4","1/3","2/5","1/2","2/4","3/5","2/3","3/4","4/5"]`
|
||||
|
||||
```js
|
||||
assert.deepEqual(farey(5), [
|
||||
'1/5',
|
||||
'1/4',
|
||||
'1/3',
|
||||
'2/5',
|
||||
'1/2',
|
||||
'2/4',
|
||||
'3/5',
|
||||
'2/3',
|
||||
'3/4',
|
||||
'4/5'
|
||||
]);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,74 +1,63 @@
|
||||
---
|
||||
id: 598eef80ba501f1268170e1e
|
||||
title: 斐波那契n步数序列
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 斐波那契n步数序列
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"><p>编写一个函数来生成Fibonacci n步数序列和Lucas序列。第一个参数是n。第二个参数是要返回的元素数。第三个参数将指定是输出Fibonacci序列还是Lucas序列。如果参数为“f”,则返回Fibonacci序列,如果为“l”,则返回Lucas序列。序列必须作为数组返回。更多细节如下: </p><p>这些数字序列是普通<a href="http://rosettacode.org/wiki/Fibonacci sequence" title="斐波那契序列">斐波纳契数列</a>的扩展,其中: </p>对于$ n = 2 $,我们有Fibonacci序列;初始值$ [1,1] $和$ F_k ^ 2 = F_ {k-1} ^ 2 + F_ {k-2} ^ 2 $对于$ n = 3 $,我们有tribonacci序列;初始值$ [1,1,2] $和$ F_k ^ 3 = F_ {k-1} ^ 3 + F_ {k-2} ^ 3 + F_ {k-3} ^ 3 $ $ $ = 4 $我们有tetranacci序列;初始值$ [1,1,2,4] $和$ F_k ^ 4 = F_ {k-1} ^ 4 + F_ {k-2} ^ 4 + F_ {k-3} ^ 4 + F_ {k -4} ^ 4 $ ...对于一般的$ n> 2 $,我们有斐波那契$ n $ -step序列 - $ F_k ^ n $; $(n-1)$'斐波那契$ n $ -step序列$ F_k ^ {n-1} $的前$ n $值的初始值;和$ k $'这个$ n $'序列的值是$ F_k ^ n = \ sum_ {i = 1} ^ {(n)} {F_ {ki} ^ {(n)}} $ <p>对于$ n $的小值, <a href="https://en.wikipedia.org/wiki/Number prefix#Greek_series" title="wp:数字前缀#Greek_series">希腊数字前缀</a>有时用于单独命名每个系列。 </p><p> {| style =“text-align:left;” border =“4”cellpadding =“2”cellspacing =“2” </p><p> | + Fibonacci $ n $ -step序列</p><p> | - style =“background-color:rgb(255,204,255);” </p><p> ! $ n $ !!系列名称!!值</p><p> | - </p><p> | 2 ||斐波那契|| 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 ...... </p><p> | - </p><p> | 3 || tribonacci || 1 1 2 4 7 13 24 44 81 149 274 504 927 1705 3136 ...... </p><p> | - </p><p> | 4 || tetranacci || 1 1 2 4 8 15 29 56 108 208 401 773 1490 2872 5536 ...... </p><p> | - </p><p> | 5 || pentanacci || 1 1 2 4 8 16 31 61 120 236 464 912 1793 3525 6930 ...... </p><p> | - </p><p> | 6 || hexanacci || 1 1 2 4 8 16 32 63 125 248 492 976 1936 3840 7617 ...... </p><p> | - </p><p> | 7 || heptanacci || 1 1 2 4 8 16 32 64 127 253 504 1004 2000 3984 7936 ...... </p><p> | - </p><p> | 8 || octonacci || 1 1 2 4 8 16 32 64 128 255 509 1016 2028 4048 8080 ... </p><p> | - </p><p> | 9 || nonanacci || 1 1 2 4 8 16 32 64 128 256 511 1021 2040 4076 8144 ...... </p><p> | - </p><p> | 10 || decanacci || 1 1 2 4 8 16 32 64 128 256 512 1023 2045 4088 8172 ... </p><p> |} </p><p>可以在更改初始值的位置生成联合序列: </p><p> <a href="https://en.wikipedia.org/wiki/Lucas number" title="wp:卢卡斯号码">Lucas系列</a>将两个前面的值相加,例如$ n = 2 $的斐波那契数列,但使用$ [2,1] $作为其初始值。 </p><p><!-- Lucas numbers, Lucas number, Lucas series [added to make searches easier.] --></p></section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
<p>编写一个函数来生成Fibonacci n步数序列和Lucas序列。第一个参数是n。第二个参数是要返回的元素数。第三个参数将指定是输出Fibonacci序列还是Lucas序列。如果参数为“f”,则返回Fibonacci序列,如果为“l”,则返回Lucas序列。序列必须作为数组返回。更多细节如下: </p><p>这些数字序列是普通<a href='http://rosettacode.org/wiki/Fibonacci sequence' title='斐波那契序列'>斐波纳契数列</a>的扩展,其中: </p>对于$ n = 2 $,我们有Fibonacci序列;初始值$ \[1,1] $和$ F_k ^ 2 = F\_ {k-1} ^ 2 + F\_ {k-2} ^ 2 $对于$ n = 3 $,我们有tribonacci序列;初始值$ \[1,1,2] $和$ F_k ^ 3 = F\_ {k-1} ^ 3 + F\_ {k-2} ^ 3 + F\_ {k-3} ^ 3 $ $ $ = 4 $我们有tetranacci序列;初始值$ \[1,1,2,4] $和$ F_k ^ 4 = F\_ {k-1} ^ 4 + F\_ {k-2} ^ 4 + F\_ {k-3} ^ 4 + F\_ {k -4} ^ 4 $ ...对于一般的$ n> 2 $,我们有斐波那契$ n $ -step序列 - $ F_k ^ n $; $(n-1)$'斐波那契$ n $ -step序列$ F_k ^ {n-1} $的前$ n $值的初始值;和$ k $'这个$ n $'序列的值是$ F_k ^ n = \\ sum\_ {i = 1} ^ {(n)} {F\_ {ki} ^ {(n)}} $ <p>对于$ n $的小值, <a href='https://en.wikipedia.org/wiki/Number prefix#Greek_series' title='wp:数字前缀#Greek_series'>希腊数字前缀</a>有时用于单独命名每个系列。 </p><p> {| style =“text-align:left;” border =“4”cellpadding =“2”cellspacing =“2” </p><p> | + Fibonacci $ n $ -step序列</p><p> | - style =“background-color:rgb(255,204,255);” </p><p> ! $ n $ !!系列名称!!值</p><p> | - </p><p> | 2 ||斐波那契|| 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 ...... </p><p> | - </p><p> | 3 || tribonacci || 1 1 2 4 7 13 24 44 81 149 274 504 927 1705 3136 ...... </p><p> | - </p><p> | 4 || tetranacci || 1 1 2 4 8 15 29 56 108 208 401 773 1490 2872 5536 ...... </p><p> | - </p><p> | 5 || pentanacci || 1 1 2 4 8 16 31 61 120 236 464 912 1793 3525 6930 ...... </p><p> | - </p><p> | 6 || hexanacci || 1 1 2 4 8 16 32 63 125 248 492 976 1936 3840 7617 ...... </p><p> | - </p><p> | 7 || heptanacci || 1 1 2 4 8 16 32 64 127 253 504 1004 2000 3984 7936 ...... </p><p> | - </p><p> | 8 || octonacci || 1 1 2 4 8 16 32 64 128 255 509 1016 2028 4048 8080 ... </p><p> | - </p><p> | 9 || nonanacci || 1 1 2 4 8 16 32 64 128 256 511 1021 2040 4076 8144 ...... </p><p> | - </p><p> | 10 || decanacci || 1 1 2 4 8 16 32 64 128 256 512 1023 2045 4088 8172 ... </p><p> |} </p><p>可以在更改初始值的位置生成联合序列: </p><p> <a href='https://en.wikipedia.org/wiki/Lucas number' title='wp:卢卡斯号码'>Lucas系列</a>将两个前面的值相加,例如$ n = 2 $的斐波那契数列,但使用$ [2,1] $作为其初始值。 </p><p><!-- Lucas numbers, Lucas number, Lucas series [added to make searches easier.] --></p>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>fib_luc</code>是一个功能。
|
||||
testString: assert(typeof fib_luc === 'function');
|
||||
- text: <code>fib_luc(2,10,"f")</code>应返回<code>[1,1,2,3,5,8,13,21,34,55]</code> 。
|
||||
testString: assert.deepEqual(fib_luc(2,10,"f"),ans[0]);
|
||||
- text: <code>fib_luc(3,15,"f")</code>应返回<code>[1,1,2,4,7,13,24,44,81,149,274,504,927,1705,3136]</code> 。
|
||||
testString: assert.deepEqual(fib_luc(3,15,"f"),ans[1]);
|
||||
- text: <code>fib_luc(4,15,"f")</code>应返回<code>[1,1,2,4,8,15,29,56,108,208,401,773,1490,2872,5536]</code> 。
|
||||
testString: assert.deepEqual(fib_luc(4,15,"f"),ans[2]);
|
||||
- text: <code>fib_luc(2,10,"l")</code>应返回<code>[ 2, 1, 3, 4, 7, 11, 18, 29, 47, 76]</code> <code>fib_luc(2,10,"l")</code> <code>[ 2, 1, 3, 4, 7, 11, 18, 29, 47, 76]</code> 。
|
||||
testString: assert.deepEqual(fib_luc(2,10,"l"),ans[3]);
|
||||
- text: <code>fib_luc(3,15,"l")</code>应返回<code>[ 2, 1, 3, 6, 10, 19, 35, 64, 118, 217, 399, 734, 1350, 2483, 4567 ]</code> <code>fib_luc(3,15,"l")</code> <code>[ 2, 1, 3, 6, 10, 19, 35, 64, 118, 217, 399, 734, 1350, 2483, 4567 ]</code> 。
|
||||
testString: assert.deepEqual(fib_luc(3,15,"l"),ans[4]);
|
||||
- text: <code>fib_luc(4,15,"l")</code>应该返回<code>[ 2, 1, 3, 6, 12, 22, 43, 83, 160, 308, 594, 1145, 2207, 4254, 8200 ]</code> <code>fib_luc(4,15,"l")</code> <code>[ 2, 1, 3, 6, 12, 22, 43, 83, 160, 308, 594, 1145, 2207, 4254, 8200 ]</code> 。
|
||||
testString: assert.deepEqual(fib_luc(4,15,"l"),ans[5]);
|
||||
- text: <code>fib_luc(5,15,"l")</code>应该返回<code>[ 2, 1, 3, 6, 12, 24, 46, 91, 179, 352, 692, 1360, 2674, 5257, 10335 ]</code> 。
|
||||
testString: assert.deepEqual(fib_luc(5,15,"l"),ans[6]);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`fib_luc`是一个功能。
|
||||
|
||||
```js
|
||||
function fib_luc (n, len, w) {
|
||||
// Good luck!
|
||||
}
|
||||
|
||||
assert(typeof fib_luc === 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`fib_luc(2,10,"f")`应返回`[1,1,2,3,5,8,13,21,34,55]` 。
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
assert.deepEqual(fib_luc(2, 10, 'f'), ans[0]);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`fib_luc(3,15,"f")`应返回`[1,1,2,4,7,13,24,44,81,149,274,504,927,1705,3136]` 。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert.deepEqual(fib_luc(3, 15, 'f'), ans[1]);
|
||||
```
|
||||
|
||||
/section>
|
||||
`fib_luc(4,15,"f")`应返回`[1,1,2,4,8,15,29,56,108,208,401,773,1490,2872,5536]` 。
|
||||
|
||||
```js
|
||||
assert.deepEqual(fib_luc(4, 15, 'f'), ans[2]);
|
||||
```
|
||||
|
||||
`fib_luc(2,10,"l")`应返回`[ 2, 1, 3, 4, 7, 11, 18, 29, 47, 76]` `fib_luc(2,10,"l")` `[ 2, 1, 3, 4, 7, 11, 18, 29, 47, 76]` 。
|
||||
|
||||
```js
|
||||
assert.deepEqual(fib_luc(2, 10, 'l'), ans[3]);
|
||||
```
|
||||
|
||||
`fib_luc(3,15,"l")`应返回`[ 2, 1, 3, 6, 10, 19, 35, 64, 118, 217, 399, 734, 1350, 2483, 4567 ]` `fib_luc(3,15,"l")` `[ 2, 1, 3, 6, 10, 19, 35, 64, 118, 217, 399, 734, 1350, 2483, 4567 ]` 。
|
||||
|
||||
```js
|
||||
assert.deepEqual(fib_luc(3, 15, 'l'), ans[4]);
|
||||
```
|
||||
|
||||
`fib_luc(4,15,"l")`应该返回`[ 2, 1, 3, 6, 12, 22, 43, 83, 160, 308, 594, 1145, 2207, 4254, 8200 ]` `fib_luc(4,15,"l")` `[ 2, 1, 3, 6, 12, 22, 43, 83, 160, 308, 594, 1145, 2207, 4254, 8200 ]` 。
|
||||
|
||||
```js
|
||||
assert.deepEqual(fib_luc(4, 15, 'l'), ans[5]);
|
||||
```
|
||||
|
||||
`fib_luc(5,15,"l")`应该返回`[ 2, 1, 3, 6, 12, 24, 46, 91, 179, 352, 692, 1360, 2674, 5257, 10335 ]` 。
|
||||
|
||||
```js
|
||||
assert.deepEqual(fib_luc(5, 15, 'l'), ans[6]);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,60 +1,45 @@
|
||||
---
|
||||
id: 597f24c1dda4e70f53c79c81
|
||||
title: 斐波那契序列
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 斐波那契序列
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"><p>编写一个函数来生成第<big>n <sup>个</sup></big> Fibonacci数。 </p> /// <p>第<big>n <sup>个</sup></big> Fibonacci数由下式给出:/// </p><p> F <sub>n</sub> = F <sub>n-1</sub> + F <sub>n-2</sub> </p> /// <p>该系列的前两个术语是0,1。 </p> /// <p>因此,该系列是:0,1,1,2,3,5,8,13 ...... </p> /// </section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
<p>编写一个函数来生成第<big>n <sup>个</sup></big> Fibonacci数。 </p> /// <p>第<big>n <sup>个</sup></big> Fibonacci数由下式给出:/// </p><p> F <sub>n</sub> = F <sub>n-1</sub> + F <sub>n-2</sub> </p> /// <p>该系列的前两个术语是0,1。 </p> /// <p>因此,该系列是:0,1,1,2,3,5,8,13 ...... </p> ///
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>fibonacci</code>是一种功能。
|
||||
testString: assert(typeof fibonacci === 'function');
|
||||
- text: <code>fibonacci(2)</code>应该返回一个数字。
|
||||
testString: assert(typeof fibonacci(2) == 'number');
|
||||
- text: <code>fibonacci(3)</code>应该返回1.“)
|
||||
testString: assert.equal(fibonacci(3),1);
|
||||
- text: <code>fibonacci(5)</code>应该返回3.“)
|
||||
testString: assert.equal(fibonacci(5),3);
|
||||
- text: <code>fibonacci(10)</code>应该返回34.“)
|
||||
testString: assert.equal(fibonacci(10),34);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`fibonacci`是一种功能。
|
||||
|
||||
```js
|
||||
function fibonacci(n) {
|
||||
// Good luck!
|
||||
}
|
||||
|
||||
assert(typeof fibonacci === 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`fibonacci(2)`应该返回一个数字。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert(typeof fibonacci(2) == 'number');
|
||||
```
|
||||
|
||||
/section>
|
||||
`fibonacci(3)`应该返回1.“)
|
||||
|
||||
```js
|
||||
assert.equal(fibonacci(3), 1);
|
||||
```
|
||||
|
||||
`fibonacci(5)`应该返回3.“)
|
||||
|
||||
```js
|
||||
assert.equal(fibonacci(5), 3);
|
||||
```
|
||||
|
||||
`fibonacci(10)`应该返回34.“)
|
||||
|
||||
```js
|
||||
assert.equal(fibonacci(10), 34);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,64 +1,33 @@
|
||||
---
|
||||
id: 5992e222d397f00d21122931
|
||||
title: 斐波那契字
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 斐波那契字
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"><p>编写一个函数将Fibonacci字返回到N.N将作为参数提供给函数。该函数应返回一个对象数组。对象的形式应为:{N:1,长度:1,熵:0,单词:'1'}。更多细节如下: </p><p> Fibonacci Word可以类似于Fibonacci Sequence的方式创建, <a href="http://hal.archives-ouvertes.fr/docs/00/36/79/72/PDF/The_Fibonacci_word_fractal.pdf" title="链接:http://hal.archives-ouvertes.fr/docs/00/36/79/72/PDF/The_Fibonacci_word_fractal.pdf">如下所述</a> : </p><p>将F_Word <sub>1</sub>定义为1 </p><p>将F_Word <sub>2</sub>定义为0 </p><p>将F_Word <sub>3表示</sub>为F_Word <sub>2</sub>与F_Word <sub>1</sub>连接,即:01 </p><p>将F_Word <sub>n表示</sub>为F_Word <sub>n-1</sub>与F_word <sub>n-2连接</sub> </p></section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
<p>编写一个函数将Fibonacci字返回到N.N将作为参数提供给函数。该函数应返回一个对象数组。对象的形式应为:{N:1,长度:1,熵:0,单词:'1'}。更多细节如下: </p><p> Fibonacci Word可以类似于Fibonacci Sequence的方式创建, <a href='http://hal.archives-ouvertes.fr/docs/00/36/79/72/PDF/The_Fibonacci_word_fractal.pdf' title='链接:http://hal.archives-ouvertes.fr/docs/00/36/79/72/PDF/The_Fibonacci_word_fractal.pdf'>如下所述</a> : </p><p>将F_Word <sub>1</sub>定义为1 </p><p>将F_Word <sub>2</sub>定义为0 </p><p>将F_Word <sub>3表示</sub>为F_Word <sub>2</sub>与F_Word <sub>1</sub>连接,即:01 </p><p>将F_Word <sub>n表示</sub>为F_Word <sub>n-1</sub>与F_word <sub>n-2连接</sub> </p>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>fibWord</code>是一个功能。
|
||||
testString: assert(typeof fibWord === 'function');
|
||||
- text: <code>fibWord(5)</code>应该返回一个数组。
|
||||
testString: assert(Array.isArray(fibWord(5)));
|
||||
- text: '<code>fibWord(5)</code>应该返回<code>'+JSON.stringify(ans)+'</code> 。'
|
||||
testString: assert.deepEqual(fibWord(5),ans);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`fibWord`是一个功能。
|
||||
|
||||
```js
|
||||
function fibWord (n) {
|
||||
// Good luck!
|
||||
}
|
||||
|
||||
assert(typeof fibWord === 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`fibWord(5)`应该返回一个数组。
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
assert(Array.isArray(fibWord(5)));
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`fibWord(5)`应该返回`'+JSON.stringify(ans)+'` 。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert.deepEqual(fibWord(5), ans);
|
||||
```
|
||||
|
||||
/section>
|
||||
# --solutions--
|
||||
|
||||
|
@ -0,0 +1,91 @@
|
||||
---
|
||||
id: 5e9ddb06ec35240f39657419
|
||||
title: FizzBuzz
|
||||
challengeType: 5
|
||||
forumTopicId: 385370
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Write a program that generates an array of integers from 1 to 100 (inclusive). But:
|
||||
|
||||
<ul>
|
||||
<li>for multiples of 3, add <code>"Fizz"</code> to the array instead of the number</li>
|
||||
<li>for multiples of 5, add <code>"Buzz"</code> to the array instead of the number</li>
|
||||
<li>for multiples of 3 and 5, add <code>"FizzBuzz"</code> to the array instead of the number</li>
|
||||
</ul>
|
||||
|
||||
# --instructions--
|
||||
|
||||
Your program should return an array containing the results based on the rules above.
|
||||
|
||||
# --hints--
|
||||
|
||||
`fizzBuzz` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof fizzBuzz == 'function');
|
||||
```
|
||||
|
||||
`fizzBuzz()` should return an Array.
|
||||
|
||||
```js
|
||||
assert(Array.isArray(fizzBuzz()) == true);
|
||||
```
|
||||
|
||||
Numbers divisible by only 3 should return `"Fizz"`.
|
||||
|
||||
```js
|
||||
assert.equal(fizzBuzz()[2], 'Fizz');
|
||||
```
|
||||
|
||||
Numbers divisible by only 5 should return `"Buzz"`.
|
||||
|
||||
```js
|
||||
assert.equal(fizzBuzz()[99], 'Buzz');
|
||||
```
|
||||
|
||||
Numbers divisible by both 3 and 5 should return `"FizzBuzz"`.
|
||||
|
||||
```js
|
||||
assert.equal(fizzBuzz()[89], 'FizzBuzz');
|
||||
```
|
||||
|
||||
Numbers not divisible by either 3 or 5 should return the number itself.
|
||||
|
||||
```js
|
||||
assert.equal(fizzBuzz()[12], 13);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function fizzBuzz() {
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function fizzBuzz() {
|
||||
let res=[];
|
||||
for (let i =1; i < 101; i++) {
|
||||
if (i % 3 === 0 && i % 5 === 0) {
|
||||
res.push("FizzBuzz");
|
||||
}
|
||||
else if (i % 3 === 0) {
|
||||
res.push("Fizz");
|
||||
}
|
||||
else if (i % 5 === 0) {
|
||||
res.push("Buzz");
|
||||
}
|
||||
else {
|
||||
res.push(i);
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
```
|
@ -1,72 +1,62 @@
|
||||
---
|
||||
id: 5a7dad05be01840e1778a0d1
|
||||
title: Fractran
|
||||
challengeType: 3
|
||||
videoUrl: ''
|
||||
title: Fractran
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"><div class="rosetta"><p class="rosetta__paragraph"> <span class="rosetta__text--bold"><a class="rosetta__link--wiki" href="https://en.wikipedia.org/wiki/FRACTRAN" title="wp:FRACTRAN">FRACTRAN</a></span>是由数学家<a class="rosetta__link--wiki" href="https://en.wikipedia.org/wiki/John Horton Conway" title="wp:John Horton Conway">John Horton Conway</a>发明的图灵完备的深奥编程语言。 </p><br><p class="rosetta__paragraph"> FRACTRAN程序是正分数$ P =(f_1,f_2,\ ldots,f_m)$的有序列表,以及初始正整数输入$ n $。 </p><br><p class="rosetta__paragraph">该程序通过更新整数$ n $来运行,如下所示: </p><br><ul class="rosetta__unordered-list"><li class="rosetta__list-item--unordered">对于第一个分数$ f_i $,在$ nf_i $为整数的列表中,用$ nf_i $替换$ n $; </li><li class="rosetta__list-item--unordered">重复此规则,直到列表中没有分数乘以$ n $时产生整数,然后停止。 </li></ul><br><p class="rosetta__paragraph">康威为FRACTRAN提供了素数计划: </p><br><p class="rosetta__paragraph"> <span class="rosetta__text--indented">$ 17/91 $,$ 78/85 $,$ 19/51 $,$ 23/38 $,$ 29/33 $,$ 77/29 $,$ 95/23 $,$ 77/19 $,$ 1/17 $,$ 11/13 $, $ 13/11 $ $,$ 15/14 $,$ 15/2 $,$ 55/1 $</span> </p><br><p class="rosetta__paragraph">从$ n = 2 $开始,此FRACTRAN程序将$ n $更改为$ 15 = 2 \ times(15/2)$,然后$ 825 = 15 \ times(55/1)$,生成以下整数序列: </p><br><p class="rosetta__paragraph"> <span class="rosetta__text--indented">$ 2 $,$ 15 $,$ 825 $,$ 725 $,$ 1925 $,$ 2275 $,$ 425 $,$ 390 $,$ 330 $,$ 290 $,$ 770 $,$ \ ldots $</span> </p><br><p class="rosetta__paragraph"> 2之后,此序列包含以下2的幂: </p><br><p class="rosetta__paragraph"> <span class="rosetta__text--indented">$ 2 ^ 2 = 4 $,$ 2 ^ 3 = 8 $,$ 2 ^ 5 = 32 $,$ 2 ^ 7 = 128 $,$ 2 ^ {11} = 2048 $,$ 2 ^ {13} = 8192 $,$ 2 ^ {17 } = 131072 $,$ 2 ^ {19} = 524288 $,$ \ ldots $</span> </p><br><p class="rosetta__paragraph">这是2的主要权力。 </p><br><dl class="rosetta__description-list"><dt class="rosetta__description-title">任务: </dt></dl><p class="rosetta__paragraph">编写一个函数,将fractran程序作为字符串参数,并将程序的前10个数字作为数组返回。如果结果没有10个数字,则按原样返回数字。 </p></div></section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
<div class='rosetta'><p class='rosetta__paragraph'> <span class='rosetta__text--bold'><a class='rosetta__link--wiki' href='https://en.wikipedia.org/wiki/FRACTRAN' title='wp:FRACTRAN'>FRACTRAN</a></span>是由数学家<a class='rosetta__link--wiki' href='https://en.wikipedia.org/wiki/John Horton Conway' title='wp:John Horton Conway'>John Horton Conway</a>发明的图灵完备的深奥编程语言。 </p><br><p class='rosetta__paragraph'> FRACTRAN程序是正分数$ P =(f_1,f_2,\ ldots,f_m)$的有序列表,以及初始正整数输入$ n $。 </p><br><p class='rosetta__paragraph'>该程序通过更新整数$ n $来运行,如下所示: </p><br><ul class='rosetta__unordered-list'><li class='rosetta__list-item--unordered'>对于第一个分数$ f_i $,在$ nf_i $为整数的列表中,用$ nf_i $替换$ n $; </li><li class='rosetta__list-item--unordered'>重复此规则,直到列表中没有分数乘以$ n $时产生整数,然后停止。 </li></ul><br><p class='rosetta__paragraph'>康威为FRACTRAN提供了素数计划: </p><br><p class='rosetta__paragraph'> <span class='rosetta__text--indented'>$ 17/91 $,$ 78/85 $,$ 19/51 $,$ 23/38 $,$ 29/33 $,$ 77/29 $,$ 95/23 $,$ 77/19 $,$ 1/17 $,$ 11/13 $, $ 13/11 $ $,$ 15/14 $,$ 15/2 $,$ 55/1 $</span> </p><br><p class='rosetta__paragraph'>从$ n = 2 $开始,此FRACTRAN程序将$ n $更改为$ 15 = 2 \ times(15/2)$,然后$ 825 = 15 \ times(55/1)$,生成以下整数序列: </p><br><p class='rosetta__paragraph'> <span class='rosetta__text--indented'>$ 2 $,$ 15 $,$ 825 $,$ 725 $,$ 1925 $,$ 2275 $,$ 425 $,$ 390 $,$ 330 $,$ 290 $,$ 770 $,$ \ ldots $</span> </p><br><p class='rosetta__paragraph'> 2之后,此序列包含以下2的幂: </p><br><p class='rosetta__paragraph'> <span class='rosetta__text--indented'>$ 2 ^ 2 = 4 $,$ 2 ^ 3 = 8 $,$ 2 ^ 5 = 32 $,$ 2 ^ 7 = 128 $,$ 2 ^ {11} = 2048 $,$ 2 ^ {13} = 8192 $,$ 2 ^ {17 } = 131072 $,$ 2 ^ {19} = 524288 $,$ \ ldots $</span> </p><br><p class='rosetta__paragraph'>这是2的主要权力。 </p><br><dl class='rosetta__description-list'><dt class='rosetta__description-title'>任务: </dt></dl><p class='rosetta__paragraph'>编写一个函数,将fractran程序作为字符串参数,并将程序的前10个数字作为数组返回。如果结果没有10个数字,则按原样返回数字。 </p></div>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>fractran</code>应该是一个功能。
|
||||
testString: assert(typeof fractran=='function');
|
||||
- text: '<code>fractran(""+tests[0]+"")</code>应该返回一个数组。'
|
||||
testString: assert(Array.isArray(fractran('3/2, 1/3')));
|
||||
- text: '<code>fractran(""+tests[0]+"")</code>应返回<code>"+JSON.stringify(results[0])+"</code> 。'
|
||||
testString: assert.deepEqual(fractran('3/2, 1/3'), [ 2, 3, 1 ]);
|
||||
- text: '<code>fractran(""+tests[1]+"")</code>应返回<code>"+JSON.stringify(results[1])+"</code> 。'
|
||||
testString: assert.deepEqual(fractran('3/2, 5/3, 1/5'), [ 2, 3, 5, 1 ]);
|
||||
- text: '<code>fractran(""+tests[2]+"")</code>应返回<code>"+JSON.stringify(results[2])+"</code> 。'
|
||||
testString: assert.deepEqual(fractran('3/2, 6/3'), [ 2, 3, 6, 9, 18, 27, 54, 81, 162, 243 ]);
|
||||
- text: '<code>fractran(""+tests[3]+"")</code>应返回<code>"+JSON.stringify(results[3])+"</code> 。'
|
||||
testString: assert.deepEqual(fractran('2/7, 7/2'), [ 2, 7, 2, 7, 2, 7, 2, 7, 2, 7 ]);
|
||||
- text: '<code>fractran(""+tests[4]+"")</code>应返回<code>"+JSON.stringify(results[4])+"</code> 。'
|
||||
testString: assert.deepEqual(fractran('17/91, 78/85, 19/51, 23/38, 29/33, 77/29, 95/23, 77/19, 1/17, 11/13, 13/11, 15/14, 15/2, 55/1'), [ 2, 15, 825, 725, 1925, 2275, 425, 390, 330, 290 ]);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`fractran`应该是一个功能。
|
||||
|
||||
```js
|
||||
function fractran (progStr) {
|
||||
// Good luck!
|
||||
}
|
||||
|
||||
assert(typeof fractran == 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`fractran(""+tests[0]+"")`应该返回一个数组。
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
assert(Array.isArray(fractran('3/2, 1/3')));
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`fractran(""+tests[0]+"")`应返回`"+JSON.stringify(results[0])+"` 。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert.deepEqual(fractran('3/2, 1/3'), [2, 3, 1]);
|
||||
```
|
||||
|
||||
/section>
|
||||
`fractran(""+tests[1]+"")`应返回`"+JSON.stringify(results[1])+"` 。
|
||||
|
||||
```js
|
||||
assert.deepEqual(fractran('3/2, 5/3, 1/5'), [2, 3, 5, 1]);
|
||||
```
|
||||
|
||||
`fractran(""+tests[2]+"")`应返回`"+JSON.stringify(results[2])+"` 。
|
||||
|
||||
```js
|
||||
assert.deepEqual(fractran('3/2, 6/3'), [2, 3, 6, 9, 18, 27, 54, 81, 162, 243]);
|
||||
```
|
||||
|
||||
`fractran(""+tests[3]+"")`应返回`"+JSON.stringify(results[3])+"` 。
|
||||
|
||||
```js
|
||||
assert.deepEqual(fractran('2/7, 7/2'), [2, 7, 2, 7, 2, 7, 2, 7, 2, 7]);
|
||||
```
|
||||
|
||||
`fractran(""+tests[4]+"")`应返回`"+JSON.stringify(results[4])+"` 。
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
fractran(
|
||||
'17/91, 78/85, 19/51, 23/38, 29/33, 77/29, 95/23, 77/19, 1/17, 11/13, 13/11, 15/14, 15/2, 55/1'
|
||||
),
|
||||
[2, 15, 825, 725, 1925, 2275, 425, 390, 330, 290]
|
||||
);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,72 +1,59 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc7e76
|
||||
title: 伽玛功能
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 伽玛功能
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">实现一个算法(或更多)来计算<a href="https://en.wikipedia.org/wiki/Gamma function">Gamma</a> ($ \ Gamma $)函数(仅在实际字段中)。 Gamma功能可以定义为: <div style="padding-left: 4em;"> <big><big>$ \ Gamma(x)= \ displaystyle \ int_0 ^ \ infty t ^ {x-1} e ^ { - t} dt $</big></big> </div></section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
实现一个算法(或更多)来计算[Gamma](<https://en.wikipedia.org/wiki/Gamma function>) ($ \\ Gamma $)函数(仅在实际字段中)。 Gamma功能可以定义为:
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
$ \\ Gamma(x)= \\ displaystyle \\ int_0 ^ \\ infty t ^ {x-1} e ^ { - t} dt $
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>gamma</code>应该是一个功能。
|
||||
testString: assert(typeof gamma=='function')
|
||||
- text: '<code>gamma("+tests[0]+")</code>应该返回一个数字。'
|
||||
testString: assert(typeof gamma(.1)=='number')
|
||||
- text: '<code>gamma("+tests[0]+")</code>应该返回<code>"+results[0]+"</code> 。'
|
||||
testString: assert.equal(round(gamma(.1)), round(9.513507698668736))
|
||||
- text: '<code>gamma("+tests[1]+")</code>应该返回<code>"+results[1]+"</code> 。'
|
||||
testString: assert.equal(round(gamma(.2)), round(4.590843711998803))
|
||||
- text: '<code>gamma("+tests[2]+")</code>应该返回<code>"+results[2]+"</code> 。'
|
||||
testString: assert.equal(round(gamma(.3)), round(2.9915689876875904))
|
||||
- text: '<code>gamma("+tests[3]+")</code>应该返回<code>"+results[3]+"</code> 。'
|
||||
testString: assert.equal(round(gamma(.4)), round(2.218159543757687))
|
||||
- text: '<code>gamma("+tests[4]+")</code>应返回<code>"+results[4]+"</code> 。'
|
||||
testString: assert.equal(round(gamma(.5)), round(1.7724538509055159))
|
||||
# --hints--
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`gamma`应该是一个功能。
|
||||
|
||||
```js
|
||||
function gamma (x) {
|
||||
// Good luck!
|
||||
}
|
||||
|
||||
assert(typeof gamma == 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`gamma("+tests[0]+")`应该返回一个数字。
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
assert(typeof gamma(0.1) == 'number');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`gamma("+tests[0]+")`应该返回`"+results[0]+"` 。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert.equal(round(gamma(0.1)), round(9.513507698668736));
|
||||
```
|
||||
|
||||
/section>
|
||||
`gamma("+tests[1]+")`应该返回`"+results[1]+"` 。
|
||||
|
||||
```js
|
||||
assert.equal(round(gamma(0.2)), round(4.590843711998803));
|
||||
```
|
||||
|
||||
`gamma("+tests[2]+")`应该返回`"+results[2]+"` 。
|
||||
|
||||
```js
|
||||
assert.equal(round(gamma(0.3)), round(2.9915689876875904));
|
||||
```
|
||||
|
||||
`gamma("+tests[3]+")`应该返回`"+results[3]+"` 。
|
||||
|
||||
```js
|
||||
assert.equal(round(gamma(0.4)), round(2.218159543757687));
|
||||
```
|
||||
|
||||
`gamma("+tests[4]+")`应返回`"+results[4]+"` 。
|
||||
|
||||
```js
|
||||
assert.equal(round(gamma(0.5)), round(1.7724538509055159));
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,72 +1,113 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc7e77
|
||||
title: 高斯消除
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 高斯消除
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">编写一个函数来解决\(Ax = b \)使用高斯消除然后向后替换。 \(A \)是\(n \次n \)矩阵。此外,\(x \)和\(b \)是\(n \)乘以1个向量。要提高准确性,请使用部分旋转和缩放。 </section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
编写一个函数来解决\\(Ax = b \\)使用高斯消除然后向后替换。 \\(A \\)是\\(n \\次n \\)矩阵。此外,\\(x \\)和\\(b \\)是\\(n \\)乘以1个向量。要提高准确性,请使用部分旋转和缩放。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>gaussianElimination</code>应该是一个函数。
|
||||
testString: assert(typeof gaussianElimination=='function');
|
||||
- text: '<code>gaussianElimination("+JSON.stringify(tests[0][0])+","+JSON.stringify(tests[0][1])+")</code>应该返回一个数组。'
|
||||
testString: assert(Array.isArray(gaussianElimination([[1,1],[1,-1]], [5,1])));
|
||||
- text: '<code>gaussianElimination("+JSON.stringify(tests[0][0])+","+JSON.stringify(tests[0][1])+")</code>应返回<code>"+JSON.stringify(results[0])+"</code> 。'
|
||||
testString: assert.deepEqual(gaussianElimination([[1,1],[1,-1]], [5,1]), [ 3, 2 ]);
|
||||
- text: '<code>gaussianElimination("+JSON.stringify(tests[1][0])+","+JSON.stringify(tests[1][1])+")</code>应返回<code>"+JSON.stringify(results[1])+"</code> 。'
|
||||
testString: assert.deepEqual(gaussianElimination([[2,3],[2,1]] , [8,4]), [ 1, 2 ]);
|
||||
- text: '<code>gaussianElimination("+JSON.stringify(tests[2][0])+","+JSON.stringify(tests[2][1])+")</code>应返回<code>"+JSON.stringify(results[2])+"</code> 。'
|
||||
testString: assert.deepEqual(gaussianElimination([[1,3],[5,-2]], [14,19]), [ 5, 3 ]);
|
||||
- text: '<code>gaussianElimination("+JSON.stringify(tests[3][0])+","+JSON.stringify(tests[3][1])+")</code>应返回<code>"+JSON.stringify(results[3])+"</code> 。'
|
||||
testString: assert.deepEqual(gaussianElimination([[1,1],[5,-1]] , [10,14]), [ 4, 6 ]);
|
||||
- text: '<code>gaussianElimination("+JSON.stringify(tests[4][0])+","+JSON.stringify(tests[4][1])+")</code>应返回<code>"+JSON.stringify(results[4])+"</code> 。'
|
||||
testString: assert.deepEqual(gaussianElimination([[1,2,3],[4,5,6],[7,8,8]] , [6,15,23]), [ 1, 1, 1 ]);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`gaussianElimination`应该是一个函数。
|
||||
|
||||
```js
|
||||
function gaussianElimination (A,b) {
|
||||
// Good luck!
|
||||
}
|
||||
|
||||
assert(typeof gaussianElimination == 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`gaussianElimination("+JSON.stringify(tests[0][0])+","+JSON.stringify(tests[0][1])+")`应该返回一个数组。
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
assert(
|
||||
Array.isArray(
|
||||
gaussianElimination(
|
||||
[
|
||||
[1, 1],
|
||||
[1, -1]
|
||||
],
|
||||
[5, 1]
|
||||
)
|
||||
)
|
||||
);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`gaussianElimination("+JSON.stringify(tests[0][0])+","+JSON.stringify(tests[0][1])+")`应返回`"+JSON.stringify(results[0])+"` 。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert.deepEqual(
|
||||
gaussianElimination(
|
||||
[
|
||||
[1, 1],
|
||||
[1, -1]
|
||||
],
|
||||
[5, 1]
|
||||
),
|
||||
[3, 2]
|
||||
);
|
||||
```
|
||||
|
||||
/section>
|
||||
`gaussianElimination("+JSON.stringify(tests[1][0])+","+JSON.stringify(tests[1][1])+")`应返回`"+JSON.stringify(results[1])+"` 。
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
gaussianElimination(
|
||||
[
|
||||
[2, 3],
|
||||
[2, 1]
|
||||
],
|
||||
[8, 4]
|
||||
),
|
||||
[1, 2]
|
||||
);
|
||||
```
|
||||
|
||||
`gaussianElimination("+JSON.stringify(tests[2][0])+","+JSON.stringify(tests[2][1])+")`应返回`"+JSON.stringify(results[2])+"` 。
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
gaussianElimination(
|
||||
[
|
||||
[1, 3],
|
||||
[5, -2]
|
||||
],
|
||||
[14, 19]
|
||||
),
|
||||
[5, 3]
|
||||
);
|
||||
```
|
||||
|
||||
`gaussianElimination("+JSON.stringify(tests[3][0])+","+JSON.stringify(tests[3][1])+")`应返回`"+JSON.stringify(results[3])+"` 。
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
gaussianElimination(
|
||||
[
|
||||
[1, 1],
|
||||
[5, -1]
|
||||
],
|
||||
[10, 14]
|
||||
),
|
||||
[4, 6]
|
||||
);
|
||||
```
|
||||
|
||||
`gaussianElimination("+JSON.stringify(tests[4][0])+","+JSON.stringify(tests[4][1])+")`应返回`"+JSON.stringify(results[4])+"` 。
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
gaussianElimination(
|
||||
[
|
||||
[1, 2, 3],
|
||||
[4, 5, 6],
|
||||
[7, 8, 8]
|
||||
],
|
||||
[6, 15, 23]
|
||||
),
|
||||
[1, 1, 1]
|
||||
);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,76 +1,141 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc7e78
|
||||
title: 一般的FizzBuzz
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 一般的FizzBuzz
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">编写<a href="http://rosettacode.org/wiki/FizzBuzz">FizzBuzz</a>的通用版本,适用于任何因子列表及其单词。这基本上是一种“fizzbuzz”实现,其中游戏规则被提供给用户。创建一个实现此功能的函数。该函数应该有两个参数。第一个是带有FizzBuzz规则的数组。例如: <code>[ [3,"Fizz"] , [5,"Buzz"] ]</code> 。此indcates该<code>Fizz</code> ,如果数量是3的倍数,并应被打印<code>Buzz</code>如果是5的倍数。如果它是两则字符串应该在阵列中指定的顺序被连结的倍数。在这种情况下,如果数字是3和5的倍数,则为<code>FizzBuzz</code> 。第二个参数是函数应返回如上所述的字符串的数字。 </section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
编写[FizzBuzz](http://rosettacode.org/wiki/FizzBuzz)的通用版本,适用于任何因子列表及其单词。这基本上是一种“fizzbuzz”实现,其中游戏规则被提供给用户。创建一个实现此功能的函数。该函数应该有两个参数。第一个是带有FizzBuzz规则的数组。例如: `[ [3,"Fizz"] , [5,"Buzz"] ]` 。此indcates该`Fizz` ,如果数量是3的倍数,并应被打印`Buzz`如果是5的倍数。如果它是两则字符串应该在阵列中指定的顺序被连结的倍数。在这种情况下,如果数字是3和5的倍数,则为`FizzBuzz` 。第二个参数是函数应返回如上所述的字符串的数字。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>genFizzBuzz</code>应该是一个功能。
|
||||
testString: assert(typeof genFizzBuzz=='function');
|
||||
- text: <code>genFizzBuzz("+JSON.stringify(tests[0][0])+","+tests[0][1]+")</code>应该返回一个类型。
|
||||
testString: assert(typeof genFizzBuzz([[3, "Fizz"],[5, "Buzz"]], 6)=='string');
|
||||
- text: <code>genFizzBuzz("+JSON.stringify(tests[0][0])+","+tests[0][1]+")</code>应返回<code>""+results[0]+""</code> 。
|
||||
testString: assert.equal(genFizzBuzz([[3, "Fizz"],[5, "Buzz"]], 6), "Fizz");
|
||||
- text: <code>genFizzBuzz("+JSON.stringify(tests[1][0])+","+tests[1][1]+")</code>应返回<code>""+results[1]+""</code> 。
|
||||
testString: assert.equal(genFizzBuzz([[3, "Fizz"],[5, "Buzz"]], 10), "Buzz");
|
||||
- text: <code>genFizzBuzz("+JSON.stringify(tests[2][0])+","+tests[2][1]+")</code>应返回<code>""+results[2]+""</code> 。
|
||||
testString: assert.equal(genFizzBuzz([[3, "Buzz"],[5, "Fizz"]], 12), "Buzz");
|
||||
- text: <code>genFizzBuzz("+JSON.stringify(tests[3][0])+","+tests[3][1]+")</code>应返回<code>""+results[3]+""</code> 。
|
||||
testString: assert.equal(genFizzBuzz([[3, "Buzz"],[5, "Fizz"]], 13), '13');
|
||||
- text: <code>genFizzBuzz("+JSON.stringify(tests[4][0])+","+tests[4][1]+")</code>应该返回<code>""+results[4]+""</code> 。
|
||||
testString: assert.equal(genFizzBuzz([[3, "Buzz"],[5, "Fizz"]], 15), "BuzzFizz");
|
||||
- text: <code>genFizzBuzz("+JSON.stringify(tests[5][0])+","+tests[5][1]+")</code>应返回<code>""+results[5]+""</code> 。
|
||||
testString: assert.equal(genFizzBuzz([[3, "Fizz"],[5, "Buzz"]], 15), "FizzBuzz");
|
||||
- text: <code>genFizzBuzz("+JSON.stringify(tests[6][0])+","+tests[6][1]+")</code>应该返回<code>""+results[6]+""</code> 。
|
||||
testString: assert.equal(genFizzBuzz([[3, "Fizz"],[5, "Buzz"],[7, "Baxx"]], 105), "FizzBuzzBaxx");
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`genFizzBuzz`应该是一个功能。
|
||||
|
||||
```js
|
||||
function genFizzBuzz (rules, num) {
|
||||
// Good luck!
|
||||
}
|
||||
|
||||
assert(typeof genFizzBuzz == 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`genFizzBuzz("+JSON.stringify(tests[0][0])+","+tests[0][1]+")`应该返回一个类型。
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
assert(
|
||||
typeof genFizzBuzz(
|
||||
[
|
||||
[3, 'Fizz'],
|
||||
[5, 'Buzz']
|
||||
],
|
||||
6
|
||||
) == 'string'
|
||||
);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`genFizzBuzz("+JSON.stringify(tests[0][0])+","+tests[0][1]+")`应返回`""+results[0]+""` 。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert.equal(
|
||||
genFizzBuzz(
|
||||
[
|
||||
[3, 'Fizz'],
|
||||
[5, 'Buzz']
|
||||
],
|
||||
6
|
||||
),
|
||||
'Fizz'
|
||||
);
|
||||
```
|
||||
|
||||
/section>
|
||||
`genFizzBuzz("+JSON.stringify(tests[1][0])+","+tests[1][1]+")`应返回`""+results[1]+""` 。
|
||||
|
||||
```js
|
||||
assert.equal(
|
||||
genFizzBuzz(
|
||||
[
|
||||
[3, 'Fizz'],
|
||||
[5, 'Buzz']
|
||||
],
|
||||
10
|
||||
),
|
||||
'Buzz'
|
||||
);
|
||||
```
|
||||
|
||||
`genFizzBuzz("+JSON.stringify(tests[2][0])+","+tests[2][1]+")`应返回`""+results[2]+""` 。
|
||||
|
||||
```js
|
||||
assert.equal(
|
||||
genFizzBuzz(
|
||||
[
|
||||
[3, 'Buzz'],
|
||||
[5, 'Fizz']
|
||||
],
|
||||
12
|
||||
),
|
||||
'Buzz'
|
||||
);
|
||||
```
|
||||
|
||||
`genFizzBuzz("+JSON.stringify(tests[3][0])+","+tests[3][1]+")`应返回`""+results[3]+""` 。
|
||||
|
||||
```js
|
||||
assert.equal(
|
||||
genFizzBuzz(
|
||||
[
|
||||
[3, 'Buzz'],
|
||||
[5, 'Fizz']
|
||||
],
|
||||
13
|
||||
),
|
||||
'13'
|
||||
);
|
||||
```
|
||||
|
||||
`genFizzBuzz("+JSON.stringify(tests[4][0])+","+tests[4][1]+")`应该返回`""+results[4]+""` 。
|
||||
|
||||
```js
|
||||
assert.equal(
|
||||
genFizzBuzz(
|
||||
[
|
||||
[3, 'Buzz'],
|
||||
[5, 'Fizz']
|
||||
],
|
||||
15
|
||||
),
|
||||
'BuzzFizz'
|
||||
);
|
||||
```
|
||||
|
||||
`genFizzBuzz("+JSON.stringify(tests[5][0])+","+tests[5][1]+")`应返回`""+results[5]+""` 。
|
||||
|
||||
```js
|
||||
assert.equal(
|
||||
genFizzBuzz(
|
||||
[
|
||||
[3, 'Fizz'],
|
||||
[5, 'Buzz']
|
||||
],
|
||||
15
|
||||
),
|
||||
'FizzBuzz'
|
||||
);
|
||||
```
|
||||
|
||||
`genFizzBuzz("+JSON.stringify(tests[6][0])+","+tests[6][1]+")`应该返回`""+results[6]+""` 。
|
||||
|
||||
```js
|
||||
assert.equal(
|
||||
genFizzBuzz(
|
||||
[
|
||||
[3, 'Fizz'],
|
||||
[5, 'Buzz'],
|
||||
[7, 'Baxx']
|
||||
],
|
||||
105
|
||||
),
|
||||
'FizzBuzzBaxx'
|
||||
);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,72 +1,57 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc7e7a
|
||||
title: 生成小写ASCII字母表
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 生成小写ASCII字母表
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">编写一个函数以生成给定范围的小写ASCII字符数组。例如:对于范围1到4,函数应返回<code>['a','b','c','d']</code> 。 </section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
编写一个函数以生成给定范围的小写ASCII字符数组。例如:对于范围1到4,函数应返回`['a','b','c','d']` 。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>lascii</code>应该是一个功能。
|
||||
testString: assert(typeof lascii=='function');
|
||||
- text: <code>lascii("a","d")</code>应该返回一个数组。
|
||||
testString: assert(Array.isArray(lascii('a','d')));
|
||||
- text: <code>lascii("a","d")</code>应该返回<code>[ "a", "b", "c", "d" ]</code> 。
|
||||
testString: assert.deepEqual(lascii("a","d"),results[0]);
|
||||
- text: <code>lascii("c","i")</code>应该返回<code>[ "c", "d", "e", "f", "g", "h", "i" ]</code> 。
|
||||
testString: assert.deepEqual(lascii("c","i"),results[1]);
|
||||
- text: <code>lascii("m","q")</code>应该返回<code>[ "m", "n", "o", "p", "q" ]</code> 。
|
||||
testString: assert.deepEqual(lascii("m","q"),results[2]);
|
||||
- text: <code>lascii("k","n")</code>应返回<code>[ "k", "l", "m", "n" ]</code> 。
|
||||
testString: assert.deepEqual(lascii("k","n"),results[3]);
|
||||
- text: <code>lascii("t","z")</code>应该返回<code>[ "t", "u", "v", "w", "x", "y", "z" ]</code> 。
|
||||
testString: assert.deepEqual(lascii("t","z"),results[4]);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`lascii`应该是一个功能。
|
||||
|
||||
```js
|
||||
function lascii (cFrom, cTo) {
|
||||
// Good luck!
|
||||
}
|
||||
|
||||
assert(typeof lascii == 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`lascii("a","d")`应该返回一个数组。
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
assert(Array.isArray(lascii('a', 'd')));
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`lascii("a","d")`应该返回`[ "a", "b", "c", "d" ]` 。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert.deepEqual(lascii('a', 'd'), results[0]);
|
||||
```
|
||||
|
||||
/section>
|
||||
`lascii("c","i")`应该返回`[ "c", "d", "e", "f", "g", "h", "i" ]` 。
|
||||
|
||||
```js
|
||||
assert.deepEqual(lascii('c', 'i'), results[1]);
|
||||
```
|
||||
|
||||
`lascii("m","q")`应该返回`[ "m", "n", "o", "p", "q" ]` 。
|
||||
|
||||
```js
|
||||
assert.deepEqual(lascii('m', 'q'), results[2]);
|
||||
```
|
||||
|
||||
`lascii("k","n")`应返回`[ "k", "l", "m", "n" ]` 。
|
||||
|
||||
```js
|
||||
assert.deepEqual(lascii('k', 'n'), results[3]);
|
||||
```
|
||||
|
||||
`lascii("t","z")`应该返回`[ "t", "u", "v", "w", "x", "y", "z" ]` 。
|
||||
|
||||
```js
|
||||
assert.deepEqual(lascii('t', 'z'), results[4]);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -0,0 +1,116 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc7e7b
|
||||
title: Generator/Exponential
|
||||
challengeType: 5
|
||||
forumTopicId: 302275
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
A generator is an executable entity (like a function or procedure) that contains code that yields a sequence of values, one at a time, so that each time you call the generator, the next value in the sequence is provided.
|
||||
|
||||
Generators are often built on top of coroutines or objects so that the internal state of the object is handled "naturally".
|
||||
|
||||
Generators are often used in situations where a sequence is potentially infinite, and where it is possible to construct the next value of the sequence with only minimal state.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Write a function that uses generators to generate squares and cubes. Create a new generator that filters all cubes from the generator of squares.
|
||||
|
||||
The function should return the \\( n^{th} \\) value of the filtered generator.
|
||||
|
||||
For example for \\(n=7\\), the function should return 81 as the sequence would be 4, 9, 16, 25, 36, 49, 81. Here 64 is filtered out, as it is a cube.
|
||||
|
||||
# --hints--
|
||||
|
||||
`exponentialGenerator` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof exponentialGenerator == 'function');
|
||||
```
|
||||
|
||||
`exponentialGenerator()` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof exponentialGenerator(10) == 'number');
|
||||
```
|
||||
|
||||
`exponentialGenerator(10)` should return `144`.
|
||||
|
||||
```js
|
||||
assert.equal(exponentialGenerator(10), 144);
|
||||
```
|
||||
|
||||
`exponentialGenerator(12)` should return `196`.
|
||||
|
||||
```js
|
||||
assert.equal(exponentialGenerator(12), 196);
|
||||
```
|
||||
|
||||
`exponentialGenerator(14)` should return `256`.
|
||||
|
||||
```js
|
||||
assert.equal(exponentialGenerator(14), 256);
|
||||
```
|
||||
|
||||
`exponentialGenerator(20)` should return `484`.
|
||||
|
||||
```js
|
||||
assert.equal(exponentialGenerator(20), 484);
|
||||
```
|
||||
|
||||
`exponentialGenerator(25)` should return `784`.
|
||||
|
||||
```js
|
||||
assert.equal(exponentialGenerator(25), 784);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function exponentialGenerator(n) {
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function exponentialGenerator(n){
|
||||
function* PowersGenerator(m) {
|
||||
var n=0;
|
||||
while(1) {
|
||||
yield Math.pow(n, m);
|
||||
n += 1;
|
||||
}
|
||||
}
|
||||
|
||||
function* FilteredGenerator(g, f){
|
||||
var value = g.next().value;
|
||||
var filter = f.next().value;
|
||||
while(1) {
|
||||
if( value < filter ) {
|
||||
yield value;
|
||||
value = g.next().value;
|
||||
} else if ( value > filter ) {
|
||||
filter = f.next().value;
|
||||
} else {
|
||||
value = g.next().value;
|
||||
filter = f.next().value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var squares = PowersGenerator(2);
|
||||
var cubes = PowersGenerator(3);
|
||||
|
||||
var filtered = FilteredGenerator(squares, cubes);
|
||||
|
||||
var curr=0;
|
||||
for(var i=0;i<n;i++) curr=filtered.next();
|
||||
|
||||
return curr.value;
|
||||
}
|
||||
```
|
@ -1,66 +1,67 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc7e80
|
||||
title: 格雷码
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 格雷码
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"> <a href="https://en.wikipedia.org/wiki/Gray code">格雷码</a>是二进制编码的一种形式,其中连续数字之间的转换仅相差一位。这是一种有用的编码,用于减少硬件数据危险,其值快速变化和/或连接到较慢的硬件作为输入。从左到右或从上到下依次为<a href="https://en.wikipedia.org/wiki/Karnaugh map">卡诺图</a>生成输入也很有用。创建一个函数来编码数字并解码格雷码中的数字。该函数应该有2个参数。第一个是布尔值。该函数应编码为true,解码为false。第二个参数是要编码/解码的数字。显示所有5位二进制数的正常二进制表示,格雷码表示和解码格雷码值(0-31包括0,不需要前导0)。有许多可能的格雷码。以下编码所谓的“二进制反射格雷码”。 <br>编码(MSB为0位,b为二进制,g为格雷码): <code><br> if b[i-1] = 1 <br> <span style="padding-left:1em">g[i] = not b[i]</span> <br> else <br> <span style="padding-left:1em">g[i] = b[i]</span> <br></code>要么: <br> <code>g = b xor (b logically right shifted 1 time)</code> <br>解码(MSB为0位,b为二进制,g为格雷码): <br> <code>b[0] = g[0] <br> for other bits: <br> b[i] = g[i] xor b[i-1] <br></code> </section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
[格雷码](<https://en.wikipedia.org/wiki/Gray code>)是二进制编码的一种形式,其中连续数字之间的转换仅相差一位。这是一种有用的编码,用于减少硬件数据危险,其值快速变化和/或连接到较慢的硬件作为输入。从左到右或从上到下依次为[卡诺图](<https://en.wikipedia.org/wiki/Karnaugh map>)生成输入也很有用。创建一个函数来编码数字并解码格雷码中的数字。该函数应该有2个参数。第一个是布尔值。该函数应编码为true,解码为false。第二个参数是要编码/解码的数字。显示所有5位二进制数的正常二进制表示,格雷码表示和解码格雷码值(0-31包括0,不需要前导0)。有许多可能的格雷码。以下编码所谓的“二进制反射格雷码”。
|
||||
编码(MSB为0位,b为二进制,g为格雷码): <code><br>if b[i-1] = 1<br><span style='padding-left:1em'>g[i] = not b[i]</span><br>else<br><span style='padding-left:1em'>g[i] = b[i]</span><br></code>要么:
|
||||
`g = b xor (b logically right shifted 1 time)`
|
||||
解码(MSB为0位,b为二进制,g为格雷码):
|
||||
<code>b[0] = g[0]<br>for other bits:<br>b[i] = g[i] xor b[i-1]<br></code>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>gray</code>应该是一个功能。
|
||||
testString: assert(typeof gray=='function');
|
||||
- text: '<code>gray(true,177)</code>应该返回一个数字。'
|
||||
testString: assert(typeof gray(true,177)=='number');
|
||||
- text: '<code>gray(true,177)</code>应该返回<code>233</code> 。'
|
||||
testString: assert.equal(gray(true,177),233);
|
||||
- text: '<code>gray(true,425)</code>应该返回<code>381</code> 。'
|
||||
testString: assert.equal(gray(true,425),381);
|
||||
- text: '<code>gray(true,870)</code>应该返回<code>725</code> 。'
|
||||
testString: assert.equal(gray(true,870),725);
|
||||
- text: '<code>gray(false,233)</code>应该返回<code>177</code> 。'
|
||||
testString: assert.equal(gray(false,233),177);
|
||||
- text: '<code>gray(false,381)</code>应该返回<code>425</code> 。'
|
||||
testString: assert.equal(gray(false,381),425);
|
||||
- text: '<code>gray(false,725)</code>应该返回<code>870</code> 。'
|
||||
testString: assert.equal(gray(false,725),870);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`gray`应该是一个功能。
|
||||
|
||||
```js
|
||||
function gray(enc, number) {
|
||||
// Good luck!
|
||||
}
|
||||
|
||||
assert(typeof gray == 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`gray(true,177)`应该返回一个数字。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert(typeof gray(true, 177) == 'number');
|
||||
```
|
||||
|
||||
/section>
|
||||
`gray(true,177)`应该返回`233` 。
|
||||
|
||||
```js
|
||||
assert.equal(gray(true, 177), 233);
|
||||
```
|
||||
|
||||
`gray(true,425)`应该返回`381` 。
|
||||
|
||||
```js
|
||||
assert.equal(gray(true, 425), 381);
|
||||
```
|
||||
|
||||
`gray(true,870)`应该返回`725` 。
|
||||
|
||||
```js
|
||||
assert.equal(gray(true, 870), 725);
|
||||
```
|
||||
|
||||
`gray(false,233)`应该返回`177` 。
|
||||
|
||||
```js
|
||||
assert.equal(gray(false, 233), 177);
|
||||
```
|
||||
|
||||
`gray(false,381)`应该返回`425` 。
|
||||
|
||||
```js
|
||||
assert.equal(gray(false, 381), 425);
|
||||
```
|
||||
|
||||
`gray(false,725)`应该返回`870` 。
|
||||
|
||||
```js
|
||||
assert.equal(gray(false, 725), 870);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,66 +1,63 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc7e82
|
||||
title: 最大公约数
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 最大公约数
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">编写一个函数,返回两个整数的最大公约数。 </section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
编写一个函数,返回两个整数的最大公约数。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>gcd</code>应该是一个功能。
|
||||
testString: assert(typeof gcd=='function');
|
||||
- text: '<code>gcd(24,36)</code>应该返回一个数字。'
|
||||
testString: assert(typeof gcd(24,36)=='number');
|
||||
- text: '<code>gcd(24,36)</code>应该返回<code>12</code> 。'
|
||||
testString: assert.equal(gcd(24,36),12);
|
||||
- text: '<code>gcd(30,48)</code>应该返回<code>6</code> 。'
|
||||
testString: assert.equal(gcd(30,48),6);
|
||||
- text: '<code>gcd(10,15)</code>应该返回<code>5</code> 。'
|
||||
testString: assert.equal(gcd(10,15),5);
|
||||
- text: '<code>gcd(100,25)</code>应该返回<code>25</code> 。'
|
||||
testString: assert.equal(gcd(100,25),25);
|
||||
- text: '<code>gcd(13,250)</code>应该返回<code>1</code> 。'
|
||||
testString: assert.equal(gcd(13,250),1);
|
||||
- text: '<code>gcd(1300,250)</code>应该返回<code>50</code> 。'
|
||||
testString: assert.equal(gcd(1300,250),50);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`gcd`应该是一个功能。
|
||||
|
||||
```js
|
||||
function gcd(a, b) {
|
||||
// Good luck!
|
||||
}
|
||||
|
||||
assert(typeof gcd == 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`gcd(24,36)`应该返回一个数字。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert(typeof gcd(24, 36) == 'number');
|
||||
```
|
||||
|
||||
/section>
|
||||
`gcd(24,36)`应该返回`12` 。
|
||||
|
||||
```js
|
||||
assert.equal(gcd(24, 36), 12);
|
||||
```
|
||||
|
||||
`gcd(30,48)`应该返回`6` 。
|
||||
|
||||
```js
|
||||
assert.equal(gcd(30, 48), 6);
|
||||
```
|
||||
|
||||
`gcd(10,15)`应该返回`5` 。
|
||||
|
||||
```js
|
||||
assert.equal(gcd(10, 15), 5);
|
||||
```
|
||||
|
||||
`gcd(100,25)`应该返回`25` 。
|
||||
|
||||
```js
|
||||
assert.equal(gcd(100, 25), 25);
|
||||
```
|
||||
|
||||
`gcd(13,250)`应该返回`1` 。
|
||||
|
||||
```js
|
||||
assert.equal(gcd(13, 250), 1);
|
||||
```
|
||||
|
||||
`gcd(1300,250)`应该返回`50` 。
|
||||
|
||||
```js
|
||||
assert.equal(gcd(1300, 250), 50);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,74 +1,74 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc7e84
|
||||
title: 最重要的后续总和
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 最重要的后续总和
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">给定一个整数序列,找到一个连续的子序列,它最大化其元素的总和,也就是说,没有其他单个子序列的元素加起来大于这一个的值。空子序列被认为具有\(0 \)的总和;因此,如果所有元素都是负数,则结果必须是空序列。 </section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
给定一个整数序列,找到一个连续的子序列,它最大化其元素的总和,也就是说,没有其他单个子序列的元素加起来大于这一个的值。空子序列被认为具有\\(0 \\)的总和;因此,如果所有元素都是负数,则结果必须是空序列。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>maximumSubsequence</code>应该是一个函数。
|
||||
testString: assert(typeof maximumSubsequence=='function');
|
||||
- text: '<code>maximumSubsequence("+JSON.stringify(tests[0])+")</code>应该返回一个数组。'
|
||||
testString: assert(Array.isArray(maximumSubsequence([ 1, 2,-1, 3, 10, -10 ])));
|
||||
- text: '<code>maximumSubsequence("+JSON.stringify(tests[0])+")</code>应返回<code>"+JSON.stringify(results[0])+"</code> 。'
|
||||
testString: assert.deepEqual(maximumSubsequence([1,2,-1,3,10,-10]), [ 1, 2, -1, 3, 10 ]);
|
||||
- text: '<code>maximumSubsequence("+JSON.stringify(tests[1])+")</code>应返回<code>"+JSON.stringify(results[1])+"</code> 。'
|
||||
testString: assert.deepEqual(maximumSubsequence([0, 8, 10, -2, -4, -1, -5, -3]), [ 0, 8, 10 ]);
|
||||
- text: '<code>maximumSubsequence("+JSON.stringify(tests[2])+")</code>应返回<code>"+JSON.stringify(results[2])+"</code> 。'
|
||||
testString: assert.deepEqual(maximumSubsequence([ 9, 9, -10, 1 ]), [ 9, 9 ]);
|
||||
- text: '<code>maximumSubsequence("+JSON.stringify(tests[3])+")</code>应返回<code>"+JSON.stringify(results[3])+"</code> 。'
|
||||
testString: assert.deepEqual(maximumSubsequence([ 7, 1, -5, -3, -8, 1 ]), [ 7, 1 ]);
|
||||
- text: '<code>maximumSubsequence("+JSON.stringify(tests[4])+")</code>应返回<code>"+JSON.stringify(results[4])+"</code> 。'
|
||||
testString: assert.deepEqual(maximumSubsequence([ -3, 6, -1, 4, -4, -6 ]), [ 6, -1, 4 ]);
|
||||
- text: '<code>maximumSubsequence("+JSON.stringify(tests[5])+")</code>应返回<code>"+JSON.stringify(results[5])+"</code> 。'
|
||||
testString: assert.deepEqual(maximumSubsequence([ -1, -2, 3, 5, 6, -2, -1, 4, -4, 2, -1 ]), [ 3, 5, 6, -2, -1, 4 ]);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`maximumSubsequence`应该是一个函数。
|
||||
|
||||
```js
|
||||
function maximumSubsequence (population) {
|
||||
// Good luck!
|
||||
}
|
||||
|
||||
assert(typeof maximumSubsequence == 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`maximumSubsequence("+JSON.stringify(tests[0])+")`应该返回一个数组。
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
assert(Array.isArray(maximumSubsequence([1, 2, -1, 3, 10, -10])));
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`maximumSubsequence("+JSON.stringify(tests[0])+")`应返回`"+JSON.stringify(results[0])+"` 。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert.deepEqual(maximumSubsequence([1, 2, -1, 3, 10, -10]), [1, 2, -1, 3, 10]);
|
||||
```
|
||||
|
||||
/section>
|
||||
`maximumSubsequence("+JSON.stringify(tests[1])+")`应返回`"+JSON.stringify(results[1])+"` 。
|
||||
|
||||
```js
|
||||
assert.deepEqual(maximumSubsequence([0, 8, 10, -2, -4, -1, -5, -3]), [
|
||||
0,
|
||||
8,
|
||||
10
|
||||
]);
|
||||
```
|
||||
|
||||
`maximumSubsequence("+JSON.stringify(tests[2])+")`应返回`"+JSON.stringify(results[2])+"` 。
|
||||
|
||||
```js
|
||||
assert.deepEqual(maximumSubsequence([9, 9, -10, 1]), [9, 9]);
|
||||
```
|
||||
|
||||
`maximumSubsequence("+JSON.stringify(tests[3])+")`应返回`"+JSON.stringify(results[3])+"` 。
|
||||
|
||||
```js
|
||||
assert.deepEqual(maximumSubsequence([7, 1, -5, -3, -8, 1]), [7, 1]);
|
||||
```
|
||||
|
||||
`maximumSubsequence("+JSON.stringify(tests[4])+")`应返回`"+JSON.stringify(results[4])+"` 。
|
||||
|
||||
```js
|
||||
assert.deepEqual(maximumSubsequence([-3, 6, -1, 4, -4, -6]), [6, -1, 4]);
|
||||
```
|
||||
|
||||
`maximumSubsequence("+JSON.stringify(tests[5])+")`应返回`"+JSON.stringify(results[5])+"` 。
|
||||
|
||||
```js
|
||||
assert.deepEqual(maximumSubsequence([-1, -2, 3, 5, 6, -2, -1, 4, -4, 2, -1]), [
|
||||
3,
|
||||
5,
|
||||
6,
|
||||
-2,
|
||||
-1,
|
||||
4
|
||||
]);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,66 +1,27 @@
|
||||
---
|
||||
id: 595608ff8bcd7a50bd490181
|
||||
title: 冰雹序列
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 冰雹序列
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"><p> Hailstone数字序列可以从起始正整数n生成: </p>如果n为1,则序列结束。如果n是偶数,那么序列的下一个n <code>= n/2</code>如果n是奇数,那么序列的下一个n <code>= (3 * n) + 1</code> <p> (未经证实的) <a href="https://en.wikipedia.org/wiki/Collatz conjecture" title="wp:Collatz猜想">Collatz猜想</a>是任何起始编号的冰雹序列总是终止。 </p><p>冰雹序列也称为冰雹数(因为这些值通常受到多个下降和上升,如云中的冰雹),或者作为Collatz序列。 </p>任务:创建例程以生成数字的hailstone序列。使用例程表明,对于27号的冰雹序列具有开始与112个元件<code>27, 82, 41, 124</code> ,结束时用<code>8, 4, 2, 1</code>与显示具有最长冰雹序列的数目少于100,000一起序列的长度。 (但不要显示实际的序列!)参见: <a href="http://xkcd.com/710" title="链接:http://xkcd.com/710">xkcd</a> (幽默)。 </section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
<p> Hailstone数字序列可以从起始正整数n生成: </p>如果n为1,则序列结束。如果n是偶数,那么序列的下一个n <code>= n/2</code>如果n是奇数,那么序列的下一个n <code>= (3 \* n) + 1</code> <p> (未经证实的) <a href='https://en.wikipedia.org/wiki/Collatz conjecture' title='wp:Collatz猜想'>Collatz猜想</a>是任何起始编号的冰雹序列总是终止。 </p><p>冰雹序列也称为冰雹数(因为这些值通常受到多个下降和上升,如云中的冰雹),或者作为Collatz序列。 </p>任务:创建例程以生成数字的hailstone序列。使用例程表明,对于27号的冰雹序列具有开始与112个元件<code>27, 82, 41, 124</code> ,结束时用<code>8, 4, 2, 1</code>与显示具有最长冰雹序列的数目少于100,000一起序列的长度。 (但不要显示实际的序列!)参见: <a href='http://xkcd.com/710' title='链接:http://xkcd.com/710'>xkcd</a> (幽默)。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>hailstoneSequence</code>是一个函数。
|
||||
testString: assert(typeof hailstoneSequence === 'function');
|
||||
- text: '<code>[[27,82,41,124,8,4,2,1], [351, 77031]]</code> <code>hailstoneSequence()</code>应返回<code>[[27,82,41,124,8,4,2,1], [351, 77031]]</code>'
|
||||
testString: assert.deepEqual(hailstoneSequence(), res);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`hailstoneSequence`是一个函数。
|
||||
|
||||
```js
|
||||
// noprotect
|
||||
function hailstoneSequence () {
|
||||
const res = [];
|
||||
// Good luck!
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
assert(typeof hailstoneSequence === 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`[[27,82,41,124,8,4,2,1], [351, 77031]]` `hailstoneSequence()`应返回`[[27,82,41,124,8,4,2,1], [351, 77031]]`
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
assert.deepEqual(hailstoneSequence(), res);
|
||||
```
|
||||
|
||||
</div>
|
||||
# --solutions--
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
|
||||
/section>
|
||||
|
@ -1,76 +1,93 @@
|
||||
---
|
||||
id: 594810f028c0303b75339ad1
|
||||
title: 快乐的数字
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 快乐的数字
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"><p>幸福的数字由以下过程定义: </p><p>从任何正整数开始,将数字替换为其数字的平方和,并重复该过程直到数字等于1(它将保持不变),或者它在一个不包括1的循环中无休止地循环。这些数字这个过程在1结束的是幸福的数字,而那些不以1结尾的数字是不愉快的数字。 </p><p>实现一个函数,如果数字是满意的,则返回true,否则返回false。 </p></section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
<p>幸福的数字由以下过程定义: </p><p>从任何正整数开始,将数字替换为其数字的平方和,并重复该过程直到数字等于1(它将保持不变),或者它在一个不包括1的循环中无休止地循环。这些数字这个过程在1结束的是幸福的数字,而那些不以1结尾的数字是不愉快的数字。 </p><p>实现一个函数,如果数字是满意的,则返回true,否则返回false。 </p>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>happy</code>是一种功能。
|
||||
testString: assert(typeof happy === 'function');
|
||||
- text: <code>happy(1)</code>应该返回一个布尔值。
|
||||
testString: assert(typeof happy(1) === 'boolean');
|
||||
- text: <code>happy(1)</code>应该回归真实。
|
||||
testString: assert(happy(1));
|
||||
- text: <code>happy(2)</code>应该返回虚假。
|
||||
testString: assert(!happy(2));
|
||||
- text: <code>happy(7)</code>应该回归真实。
|
||||
testString: assert(happy(7));
|
||||
- text: <code>happy(10)</code>应该回归真实。
|
||||
testString: assert(happy(10));
|
||||
- text: <code>happy(13)</code>应该回归真实。
|
||||
testString: assert(happy(13));
|
||||
- text: <code>happy(19)</code>应该回归真实。
|
||||
testString: assert(happy(19));
|
||||
- text: <code>happy(23)</code>应该回归真实。
|
||||
testString: assert(happy(23));
|
||||
- text: <code>happy(28)</code>应该回归真实。
|
||||
testString: assert(happy(28));
|
||||
- text: <code>happy(31)</code>应该回归真实。
|
||||
testString: assert(happy(31));
|
||||
- text: <code>happy(32)</code>应该回归真实:
|
||||
testString: assert(happy(32));
|
||||
- text: <code>happy(33)</code>应该返回虚假。
|
||||
testString: assert(!happy(33));
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`happy`是一种功能。
|
||||
|
||||
```js
|
||||
function happy (number) {
|
||||
// Good luck!
|
||||
}
|
||||
|
||||
assert(typeof happy === 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`happy(1)`应该返回一个布尔值。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert(typeof happy(1) === 'boolean');
|
||||
```
|
||||
|
||||
/section>
|
||||
`happy(1)`应该回归真实。
|
||||
|
||||
```js
|
||||
assert(happy(1));
|
||||
```
|
||||
|
||||
`happy(2)`应该返回虚假。
|
||||
|
||||
```js
|
||||
assert(!happy(2));
|
||||
```
|
||||
|
||||
`happy(7)`应该回归真实。
|
||||
|
||||
```js
|
||||
assert(happy(7));
|
||||
```
|
||||
|
||||
`happy(10)`应该回归真实。
|
||||
|
||||
```js
|
||||
assert(happy(10));
|
||||
```
|
||||
|
||||
`happy(13)`应该回归真实。
|
||||
|
||||
```js
|
||||
assert(happy(13));
|
||||
```
|
||||
|
||||
`happy(19)`应该回归真实。
|
||||
|
||||
```js
|
||||
assert(happy(19));
|
||||
```
|
||||
|
||||
`happy(23)`应该回归真实。
|
||||
|
||||
```js
|
||||
assert(happy(23));
|
||||
```
|
||||
|
||||
`happy(28)`应该回归真实。
|
||||
|
||||
```js
|
||||
assert(happy(28));
|
||||
```
|
||||
|
||||
`happy(31)`应该回归真实。
|
||||
|
||||
```js
|
||||
assert(happy(31));
|
||||
```
|
||||
|
||||
`happy(32)`应该回归真实:
|
||||
|
||||
```js
|
||||
assert(happy(32));
|
||||
```
|
||||
|
||||
`happy(33)`应该返回虚假。
|
||||
|
||||
```js
|
||||
assert(!happy(33));
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,68 +1,27 @@
|
||||
---
|
||||
id: 595668ca4cfe1af2fb9818d4
|
||||
title: Harshad或Niven系列
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: Harshad或Niven系列
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"><p> <a href="http://mathworld.wolfram.com/HarshadNumber.html" title="链接:http://mathworld.wolfram.com/HarshadNumber.html">Harshad</a>或Niven数是正整数≥1,可以被它们的数字之和整除。 </p><p>例如,42是<a href="http://rosettacode.org/wiki/oeis:A005349" title="OEIS:A005349">Harshad数,</a>因为42可以被(4 + 2)整除而没有余数。 </p>假设系列被定义为按递增顺序排列的数字。任务: <p>实现一个函数来生成Harshad序列的连续成员。 </p><p>使用它列出序列的前20个成员并列出第一个大于1000的Harshad数。 </p></section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
<p> <a href='http://mathworld.wolfram.com/HarshadNumber.html' title='链接:http://mathworld.wolfram.com/HarshadNumber.html'>Harshad</a>或Niven数是正整数≥1,可以被它们的数字之和整除。 </p><p>例如,42是<a href='http://rosettacode.org/wiki/oeis:A005349' title='OEIS:A005349'>Harshad数,</a>因为42可以被(4 + 2)整除而没有余数。 </p>假设系列被定义为按递增顺序排列的数字。任务: <p>实现一个函数来生成Harshad序列的连续成员。 </p><p>使用它列出序列的前20个成员并列出第一个大于1000的Harshad数。 </p>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>isHarshadOrNiven</code>是一个函数。
|
||||
testString: assert(typeof isHarshadOrNiven === 'function');
|
||||
- text: '<code>isHarshadOrNiven()</code>应该返回<code>{"firstTwenty": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 18, 20, 21, 24, 27, 30, 36, 40, 42],"firstOver1000": 1002}</code>'
|
||||
testString: assert.deepEqual(isHarshadOrNiven(), res);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`isHarshadOrNiven`是一个函数。
|
||||
|
||||
```js
|
||||
function isHarshadOrNiven () {
|
||||
const res = {
|
||||
firstTwenty: [],
|
||||
firstOver1000: undefined
|
||||
};
|
||||
// Change after this line
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
assert(typeof isHarshadOrNiven === 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`isHarshadOrNiven()`应该返回`{"firstTwenty": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 18, 20, 21, 24, 27, 30, 36, 40, 42],"firstOver1000": 1002}`
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
assert.deepEqual(isHarshadOrNiven(), res);
|
||||
```
|
||||
|
||||
</div>
|
||||
# --solutions--
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
|
||||
/section>
|
||||
|
@ -1,73 +1,61 @@
|
||||
---
|
||||
id: 595671d4d2cdc305f0d5b36f
|
||||
title: 来自两个数组的哈希
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 来自两个数组的哈希
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">任务: <p>使用两个相等长度的数组,创建一个Hash对象,其中一个数组中的元素(键)链接到另一个数组(值) </p>相关任务: <a href="http://rosettacode.org/wiki/Associative arrays/Creation" title="关联数组/创建">关联数组/创建</a> </section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
任务:
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
使用两个相等长度的数组,创建一个Hash对象,其中一个数组中的元素(键)链接到另一个数组(值)
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>arrToObj</code>是一个功能。
|
||||
testString: assert(typeof arrToObj === 'function');
|
||||
- text: '<code>arrToObj([1, 2, 3, 4, 5], ["a", "b", "c", "d", "e"])</code>应返回<code>{ 1: "a", 2: "b", 3: "c", 4: "d", 5: "e" }</code>'
|
||||
testString: assert.deepEqual(arrToObj(...testCases[0]), res[0]);
|
||||
- text: '<code>arrToObj([1, 2, 3, 4, 5], ["a", "b", "c", "d"])</code>应返回<code>{ 1: "a", 2: "b", 3: "c", 4: "d", 5: undefined }</code>'
|
||||
testString: assert.deepEqual(arrToObj(...testCases[1]), res[1]);
|
||||
- text: '<code>arrToObj([1, 2, 3], ["a", "b", "c", "d", "e"])</code>应返回<code>{ 1: "a", 2: "b", 3: "c" }</code>'
|
||||
testString: assert.deepEqual(arrToObj(...testCases[2]), res[2]);
|
||||
- text: '<code>arrToObj(["a", "b", "c", "d", "e"], [1, 2, 3, 4, 5])</code>应返回<code>{ "a": 1, "b": 2, "c": 3 , "d": 4, "e": 5 }</code>'
|
||||
testString: assert.deepEqual(arrToObj(...testCases[3]), res[3]);
|
||||
- text: '<code>arrToObj(["a", "b", "c", "d", "e"], [1, 2, 3, 4])</code>应返回<code>{ "a": 1, "b": 2, "c": 3 , "d": 4, "e": undefined }</code>'
|
||||
testString: assert.deepEqual(arrToObj(...testCases[4]), res[4]);
|
||||
- text: '<code>arrToObj(["a", "b", "c"], [1, 2, 3, 4, 5])</code>应返回<code>{ "a": 1, "b": 2, "c": 3 }</code>'
|
||||
testString: assert.deepEqual(arrToObj(...testCases[5]), res[5]);
|
||||
相关任务: [关联数组/创建](<http://rosettacode.org/wiki/Associative arrays/Creation> "关联数组/创建")
|
||||
|
||||
```
|
||||
# --hints--
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`arrToObj`是一个功能。
|
||||
|
||||
```js
|
||||
function arrToObj (keys, vals) {
|
||||
// Good luck!
|
||||
return true;
|
||||
}
|
||||
|
||||
assert(typeof arrToObj === 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`arrToObj([1, 2, 3, 4, 5], ["a", "b", "c", "d", "e"])`应返回`{ 1: "a", 2: "b", 3: "c", 4: "d", 5: "e" }`
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
assert.deepEqual(arrToObj(...testCases[0]), res[0]);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`arrToObj([1, 2, 3, 4, 5], ["a", "b", "c", "d"])`应返回`{ 1: "a", 2: "b", 3: "c", 4: "d", 5: undefined }`
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert.deepEqual(arrToObj(...testCases[1]), res[1]);
|
||||
```
|
||||
|
||||
/section>
|
||||
`arrToObj([1, 2, 3], ["a", "b", "c", "d", "e"])`应返回`{ 1: "a", 2: "b", 3: "c" }`
|
||||
|
||||
```js
|
||||
assert.deepEqual(arrToObj(...testCases[2]), res[2]);
|
||||
```
|
||||
|
||||
`arrToObj(["a", "b", "c", "d", "e"], [1, 2, 3, 4, 5])`应返回`{ "a": 1, "b": 2, "c": 3 , "d": 4, "e": 5 }`
|
||||
|
||||
```js
|
||||
assert.deepEqual(arrToObj(...testCases[3]), res[3]);
|
||||
```
|
||||
|
||||
`arrToObj(["a", "b", "c", "d", "e"], [1, 2, 3, 4])`应返回`{ "a": 1, "b": 2, "c": 3 , "d": 4, "e": undefined }`
|
||||
|
||||
```js
|
||||
assert.deepEqual(arrToObj(...testCases[4]), res[4]);
|
||||
```
|
||||
|
||||
`arrToObj(["a", "b", "c"], [1, 2, 3, 4, 5])`应返回`{ "a": 1, "b": 2, "c": 3 }`
|
||||
|
||||
```js
|
||||
assert.deepEqual(arrToObj(...testCases[5]), res[5]);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,12 +1,13 @@
|
||||
---
|
||||
id: 5956795bc9e2c415eb244de1
|
||||
title: 哈希加入
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 哈希加入
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"><p> <a href="https://en.wikipedia.org/wiki/Join_(SQL)#Inner_join" title="wp:Join_(SQL)#Inner_join">内连接</a>是一种操作,它根据匹配的列值将两个数据表组合到一个表中。实现此操作的最简单方法是<a href="https://en.wikipedia.org/wiki/Nested loop join" title="wp:嵌套循环连接">嵌套循环连接</a>算法,但更可扩展的替代方法是<a href="https://en.wikipedia.org/wiki/hash join" title="wp:哈希联接">散列连接</a>算法。 </p><p>实现“散列连接”算法,并演示它通过下面列出的测试用例。 </p><p>您应该将表表示为在编程语言中感觉自然的数据结构。 </p><p> “散列连接”算法包含两个步骤: </p>哈希阶段:从两个表中的一个表创建一个<a href="https://en.wikipedia.org/wiki/Multimap" title="wp:Multimap">多</a>图,从每个连接列值映射到包含它的所有行。多图必须支持基于散列的查找,它比简单的线性搜索更好地扩展,因为这是该算法的重点。理想情况下,我们应该为较小的表创建多图,从而最小化其创建时间和内存大小。加入阶段:扫描另一个表,通过查看之前创建的多图来查找匹配的行。 <p>在伪代码中,算法可以表示如下: </p><pre>让A =第一个输入表(或理想情况下,更大的输入表)
|
||||
# --description--
|
||||
|
||||
<p> <a href='https://en.wikipedia.org/wiki/Join_(SQL)#Inner_join' title='wp:Join_(SQL)#Inner_join'>内连接</a>是一种操作,它根据匹配的列值将两个数据表组合到一个表中。实现此操作的最简单方法是<a href='https://en.wikipedia.org/wiki/Nested loop join' title='wp:嵌套循环连接'>嵌套循环连接</a>算法,但更可扩展的替代方法是<a href='https://en.wikipedia.org/wiki/hash join' title='wp:哈希联接'>散列连接</a>算法。 </p><p>实现“散列连接”算法,并演示它通过下面列出的测试用例。 </p><p>您应该将表表示为在编程语言中感觉自然的数据结构。 </p><p> “散列连接”算法包含两个步骤: </p>哈希阶段:从两个表中的一个表创建一个<a href='https://en.wikipedia.org/wiki/Multimap' title='wp:Multimap'>多</a>图,从每个连接列值映射到包含它的所有行。多图必须支持基于散列的查找,它比简单的线性搜索更好地扩展,因为这是该算法的重点。理想情况下,我们应该为较小的表创建多图,从而最小化其创建时间和内存大小。加入阶段:扫描另一个表,通过查看之前创建的多图来查找匹配的行。 <p>在伪代码中,算法可以表示如下: </p><pre>让A =第一个输入表(或理想情况下,更大的输入表)
|
||||
设B =第二个输入表(或理想情况下,较小的输入表)
|
||||
令j <sub>A</sub> =表A的连接列ID
|
||||
令j <sub>B</sub> =表B的连接列ID
|
||||
@ -18,58 +19,21 @@ title: 哈希加入
|
||||
对于a(j <sub>A</sub> )项下多图M <sub>B中的</sub>每一行b:
|
||||
设c =第a行和第b行的串联
|
||||
将行c放在表C中<p></p>
|
||||
</pre>测试用例<p>输入</p><table><tbody><tr><td style="padding: 4px; margin: 5px;"><table style="border:none; border-collapse:collapse;"><tbody><tr><td style="border:none"> <i>A =</i> </td><td style="border:none"><table><tbody><tr><th style="padding: 4px; margin: 5px;">年龄</th><th style="padding: 4px; margin: 5px;">名称</th></tr><tr><td style="padding: 4px; margin: 5px;"> 27 </td><td style="padding: 4px; margin: 5px;">约拿</td></tr><tr><td style="padding: 4px; margin: 5px;"> 18 </td><td style="padding: 4px; margin: 5px;">艾伦</td></tr><tr><td style="padding: 4px; margin: 5px;"> 28 </td><td style="padding: 4px; margin: 5px;">荣耀</td></tr><tr><td style="padding: 4px; margin: 5px;"> 18 </td><td style="padding: 4px; margin: 5px;">大力水手</td></tr><tr><td style="padding: 4px; margin: 5px;"> 28 </td><td style="padding: 4px; margin: 5px;">艾伦</td></tr></tbody></table></td><td style="border:none; padding-left:1.5em;" rowspan="2"></td><td style="border:none"> <i>B =</i> </td><td style="border:none"><table><tbody><tr><th style="padding: 4px; margin: 5px;">字符</th><th style="padding: 4px; margin: 5px;">复仇者</th></tr><tr><td style="padding: 4px; margin: 5px;">约拿</td><td style="padding: 4px; margin: 5px;">鲸鱼</td></tr><tr><td style="padding: 4px; margin: 5px;">约拿</td><td style="padding: 4px; margin: 5px;">蜘蛛</td></tr><tr><td style="padding: 4px; margin: 5px;">艾伦</td><td style="padding: 4px; margin: 5px;">鬼</td></tr><tr><td style="padding: 4px; margin: 5px;">艾伦</td><td style="padding: 4px; margin: 5px;">植物大战僵尸</td></tr><tr><td style="padding: 4px; margin: 5px;">荣耀</td><td style="padding: 4px; margin: 5px;">巴菲</td></tr></tbody></table></td></tr><tr><td style="border:none"> <i>j <sub>A</sub> =</i> </td><td style="border:none"> <i><code>Name</code> (即第1栏)</i> </td><td style="border:none"> <i>j <sub>B</sub> =</i> </td><td style="border:none"> <i><code>Character</code> (即第0列)</i> </td></tr></tbody></table></td><td style="padding: 4px; margin: 5px;"></td></tr></tbody></table><p>产量</p><table><tbody><tr><th style="padding: 4px; margin: 5px;"> A.Age </th><th style="padding: 4px; margin: 5px;">一个名字</th><th style="padding: 4px; margin: 5px;"> B.Character </th><th style="padding: 4px; margin: 5px;"> B.Nemesis </th></tr><tr><td style="padding: 4px; margin: 5px;"> 27 </td><td style="padding: 4px; margin: 5px;">约拿</td><td style="padding: 4px; margin: 5px;">约拿</td><td style="padding: 4px; margin: 5px;">鲸鱼</td></tr><tr><td style="padding: 4px; margin: 5px;"> 27 </td><td style="padding: 4px; margin: 5px;">约拿</td><td style="padding: 4px; margin: 5px;">约拿</td><td style="padding: 4px; margin: 5px;">蜘蛛</td></tr><tr><td style="padding: 4px; margin: 5px;"> 18 </td><td style="padding: 4px; margin: 5px;">艾伦</td><td style="padding: 4px; margin: 5px;">艾伦</td><td style="padding: 4px; margin: 5px;">鬼</td></tr><tr><td style="padding: 4px; margin: 5px;"> 18 </td><td style="padding: 4px; margin: 5px;">艾伦</td><td style="padding: 4px; margin: 5px;">艾伦</td><td style="padding: 4px; margin: 5px;">植物大战僵尸</td></tr><tr><td style="padding: 4px; margin: 5px;"> 28 </td><td style="padding: 4px; margin: 5px;">荣耀</td><td style="padding: 4px; margin: 5px;">荣耀</td><td style="padding: 4px; margin: 5px;">巴菲</td></tr><tr><td style="padding: 4px; margin: 5px;"> 28 </td><td style="padding: 4px; margin: 5px;">艾伦</td><td style="padding: 4px; margin: 5px;">艾伦</td><td style="padding: 4px; margin: 5px;">鬼</td></tr><tr><td style="padding: 4px; margin: 5px;"> 28 </td><td style="padding: 4px; margin: 5px;">艾伦</td><td style="padding: 4px; margin: 5px;">艾伦</td><td style="padding: 4px; margin: 5px;">植物大战僵尸</td></tr></tbody></table><p></p><p></p><p>输出表中行的顺序并不重要。 </p><p>如果你使用数字索引数组来表示表行(而不是按名称引用列),你可以用<code style="white-space:nowrap">[[27, "Jonah"], ["Jonah", "Whales"]]</code>的形式表示输出行。 。 </p><hr></section>
|
||||
</pre>测试用例<p>输入</p><table><tbody><tr><td style='padding: 4px; margin: 5px;'><table style='border:none; border-collapse:collapse;'><tbody><tr><td style='border:none'> <i>A =</i> </td><td style='border:none'><table><tbody><tr><th style='padding: 4px; margin: 5px;'>年龄</th><th style='padding: 4px; margin: 5px;'>名称</th></tr><tr><td style='padding: 4px; margin: 5px;'> 27 </td><td style='padding: 4px; margin: 5px;'>约拿</td></tr><tr><td style='padding: 4px; margin: 5px;'> 18 </td><td style='padding: 4px; margin: 5px;'>艾伦</td></tr><tr><td style='padding: 4px; margin: 5px;'> 28 </td><td style='padding: 4px; margin: 5px;'>荣耀</td></tr><tr><td style='padding: 4px; margin: 5px;'> 18 </td><td style='padding: 4px; margin: 5px;'>大力水手</td></tr><tr><td style='padding: 4px; margin: 5px;'> 28 </td><td style='padding: 4px; margin: 5px;'>艾伦</td></tr></tbody></table></td><td style='border:none; padding-left:1.5em;' rowspan='2'></td><td style='border:none'> <i>B =</i> </td><td style='border:none'><table><tbody><tr><th style='padding: 4px; margin: 5px;'>字符</th><th style='padding: 4px; margin: 5px;'>复仇者</th></tr><tr><td style='padding: 4px; margin: 5px;'>约拿</td><td style='padding: 4px; margin: 5px;'>鲸鱼</td></tr><tr><td style='padding: 4px; margin: 5px;'>约拿</td><td style='padding: 4px; margin: 5px;'>蜘蛛</td></tr><tr><td style='padding: 4px; margin: 5px;'>艾伦</td><td style='padding: 4px; margin: 5px;'>鬼</td></tr><tr><td style='padding: 4px; margin: 5px;'>艾伦</td><td style='padding: 4px; margin: 5px;'>植物大战僵尸</td></tr><tr><td style='padding: 4px; margin: 5px;'>荣耀</td><td style='padding: 4px; margin: 5px;'>巴菲</td></tr></tbody></table></td></tr><tr><td style='border:none'> <i>j <sub>A</sub> =</i> </td><td style='border:none'> <i><code>Name</code> (即第1栏)</i> </td><td style='border:none'> <i>j <sub>B</sub> =</i> </td><td style='border:none'> <i><code>Character</code> (即第0列)</i> </td></tr></tbody></table></td><td style='padding: 4px; margin: 5px;'></td></tr></tbody></table><p>产量</p><table><tbody><tr><th style='padding: 4px; margin: 5px;'> A.Age </th><th style='padding: 4px; margin: 5px;'>一个名字</th><th style='padding: 4px; margin: 5px;'> B.Character </th><th style='padding: 4px; margin: 5px;'> B.Nemesis </th></tr><tr><td style='padding: 4px; margin: 5px;'> 27 </td><td style='padding: 4px; margin: 5px;'>约拿</td><td style='padding: 4px; margin: 5px;'>约拿</td><td style='padding: 4px; margin: 5px;'>鲸鱼</td></tr><tr><td style='padding: 4px; margin: 5px;'> 27 </td><td style='padding: 4px; margin: 5px;'>约拿</td><td style='padding: 4px; margin: 5px;'>约拿</td><td style='padding: 4px; margin: 5px;'>蜘蛛</td></tr><tr><td style='padding: 4px; margin: 5px;'> 18 </td><td style='padding: 4px; margin: 5px;'>艾伦</td><td style='padding: 4px; margin: 5px;'>艾伦</td><td style='padding: 4px; margin: 5px;'>鬼</td></tr><tr><td style='padding: 4px; margin: 5px;'> 18 </td><td style='padding: 4px; margin: 5px;'>艾伦</td><td style='padding: 4px; margin: 5px;'>艾伦</td><td style='padding: 4px; margin: 5px;'>植物大战僵尸</td></tr><tr><td style='padding: 4px; margin: 5px;'> 28 </td><td style='padding: 4px; margin: 5px;'>荣耀</td><td style='padding: 4px; margin: 5px;'>荣耀</td><td style='padding: 4px; margin: 5px;'>巴菲</td></tr><tr><td style='padding: 4px; margin: 5px;'> 28 </td><td style='padding: 4px; margin: 5px;'>艾伦</td><td style='padding: 4px; margin: 5px;'>艾伦</td><td style='padding: 4px; margin: 5px;'>鬼</td></tr><tr><td style='padding: 4px; margin: 5px;'> 28 </td><td style='padding: 4px; margin: 5px;'>艾伦</td><td style='padding: 4px; margin: 5px;'>艾伦</td><td style='padding: 4px; margin: 5px;'>植物大战僵尸</td></tr></tbody></table><p></p><p></p><p>输出表中行的顺序并不重要。 </p><p>如果你使用数字索引数组来表示表行(而不是按名称引用列),你可以用<code style='white-space:nowrap'>[[27, "Jonah"], ["Jonah", "Whales"]]</code>的形式表示输出行。 。 </p><hr>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
# --hints--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>hashJoin</code>是一个函数。
|
||||
testString: assert(typeof hashJoin === 'function');
|
||||
- text: '<code>hashJoin([{ age: 27, name: "Jonah" }, { age: 18, name: "Alan" }, { age: 28, name: "Glory" }, { age: 18, name: "Popeye" }, { age: 28, name: "Alan" }], [{ character: "Jonah", nemesis: "Whales" }, { character: "Jonah", nemesis: "Spiders" }, { character: "Alan", nemesis: "Ghosts" }, { character:"Alan", nemesis: "Zombies" }, { character: "Glory", nemesis: "Buffy" }, { character: "Bob", nemesis: "foo" }])</code>应该返回<code>[{"A_age": 27,"A_name": "Jonah", "B_character": "Jonah", "B_nemesis": "Whales"}, {"A_age": 27,"A_name": "Jonah", "B_character": "Jonah", "B_nemesis": "Spiders"}, {"A_age": 18,"A_name": "Alan", "B_character": "Alan", "B_nemesis": "Ghosts"}, {"A_age": 18,"A_name": "Alan", "B_character": "Alan", "B_nemesis": "Zombies"}, {"A_age": 28,"A_name": "Glory", "B_character": "Glory", "B_nemesis": "Buffy"}, {"A_age": 28,"A_name": "Alan", "B_character": "Alan", "B_nemesis": "Ghosts"}, {"A_age": 28,"A_name": "Alan", "B_character": "Alan", "B_nemesis": "Zombies"}]</code>'
|
||||
testString: assert.deepEqual(hashJoin(hash1, hash2), res);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`hashJoin`是一个函数。
|
||||
|
||||
```js
|
||||
function hashJoin (hash1, hash2) {
|
||||
// Good luck!
|
||||
return [];
|
||||
}
|
||||
|
||||
assert(typeof hashJoin === 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`hashJoin([{ age: 27, name: "Jonah" }, { age: 18, name: "Alan" }, { age: 28, name: "Glory" }, { age: 18, name: "Popeye" }, { age: 28, name: "Alan" }], [{ character: "Jonah", nemesis: "Whales" }, { character: "Jonah", nemesis: "Spiders" }, { character: "Alan", nemesis: "Ghosts" }, { character:"Alan", nemesis: "Zombies" }, { character: "Glory", nemesis: "Buffy" }, { character: "Bob", nemesis: "foo" }])`应该返回`[{"A_age": 27,"A_name": "Jonah", "B_character": "Jonah", "B_nemesis": "Whales"}, {"A_age": 27,"A_name": "Jonah", "B_character": "Jonah", "B_nemesis": "Spiders"}, {"A_age": 18,"A_name": "Alan", "B_character": "Alan", "B_nemesis": "Ghosts"}, {"A_age": 18,"A_name": "Alan", "B_character": "Alan", "B_nemesis": "Zombies"}, {"A_age": 28,"A_name": "Glory", "B_character": "Glory", "B_nemesis": "Buffy"}, {"A_age": 28,"A_name": "Alan", "B_character": "Alan", "B_nemesis": "Ghosts"}, {"A_age": 28,"A_name": "Alan", "B_character": "Alan", "B_nemesis": "Zombies"}]`
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
assert.deepEqual(hashJoin(hash1, hash2), res);
|
||||
```
|
||||
|
||||
</div>
|
||||
# --solutions--
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
|
||||
/section>
|
||||
|
@ -1,71 +1,45 @@
|
||||
---
|
||||
id: 595b98f8b5a2245e243aa831
|
||||
title: 苍鹭三角形
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 苍鹭三角形
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"><p>给出三边<big>a,</big> <big>b</big>和<big>c</big>长度的三角形区域的<a href="https://en.wikipedia.org/wiki/Heron's formula" title="wp:苍鹭的配方">英雄公式</a>由下<a href="https://en.wikipedia.org/wiki/Heron's formula" title="wp:苍鹭的配方">式</a>给出: </p><p> <big>$$ A = \ sqrt {s(sa)(sb)(sc)},$$</big> </p><p>其中<big>s</big>是三角形的一半周长;那是, </p><p> <big>$$ S = \压裂{A + B + C} {2} $$。</big> </p><p> <a href="http://www.had2know.com/academics/heronian-triangles-generator-calculator.html" title="链接:http://www.had2know.com/academics/heronian-triangles-generator-calculator.html">Heronian三角形</a>是三角形,其边和面都是整数。 </p><p>一个例子是边长为3,4,5的三角形,其面积为6(其周长为12)。 </p><p>注意任何三角形的边都是3,4,5的整数倍;如6,8,10,也将是一个苍鹭三角形。 </p><p>将原始的Heronian三角形定义为最大公约数的Heronian三角形</p><p>三方都是1(统一)。 </p><p>这将排除例如三角形6,8,10。 </p>任务: <p>实现一个基于Hero公式的函数,该函数返回数组数组中的前<code>n <sub>th</sub></code>有序三角形。 </p></section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
<p>给出三边<big>a,</big> <big>b</big>和<big>c</big>长度的三角形区域的<a href='https://en.wikipedia.org/wiki/Heron's formula' title='wp:苍鹭的配方'>英雄公式</a>由下<a href='https://en.wikipedia.org/wiki/Heron's formula' title='wp:苍鹭的配方'>式</a>给出: </p><p> <big>$$ A = \ sqrt {s(sa)(sb)(sc)},$$</big> </p><p>其中<big>s</big>是三角形的一半周长;那是, </p><p> <big>$$ S = \压裂{A + B + C} {2} $$。</big> </p><p> <a href='http://www.had2know.com/academics/heronian-triangles-generator-calculator.html' title='链接:http://www.had2know.com/academics/heronian-triangles-generator-calculator.html'>Heronian三角形</a>是三角形,其边和面都是整数。 </p><p>一个例子是边长为3,4,5的三角形,其面积为6(其周长为12)。 </p><p>注意任何三角形的边都是3,4,5的整数倍;如6,8,10,也将是一个苍鹭三角形。 </p><p>将原始的Heronian三角形定义为最大公约数的Heronian三角形</p><p>三方都是1(统一)。 </p><p>这将排除例如三角形6,8,10。 </p>任务: <p>实现一个基于Hero公式的函数,该函数返回数组数组中的前<code>n <sub>th</sub></code>有序三角形。 </p>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>heronianTriangle</code>是一个函数。
|
||||
testString: assert(typeof heronianTriangle === 'function');
|
||||
- text: '<code>heronianTriangle()</code>应返回<code>[[3, 4, 5], [5, 5, 6], [5, 5, 8], [4, 13, 15], [5, 12, 13], [9, 10, 17], [3, 25, 26], [7, 15, 20], [10, 13, 13], [8, 15, 17]]</code> <code>heronianTriangle()</code> <code>[[3, 4, 5], [5, 5, 6], [5, 5, 8], [4, 13, 15], [5, 12, 13], [9, 10, 17], [3, 25, 26], [7, 15, 20], [10, 13, 13], [8, 15, 17]]</code> <code>heronianTriangle()</code> <code>[[3, 4, 5], [5, 5, 6], [5, 5, 8], [4, 13, 15], [5, 12, 13], [9, 10, 17], [3, 25, 26], [7, 15, 20], [10, 13, 13], [8, 15, 17]]</code> <code>heronianTriangle()</code> <code>[[3, 4, 5], [5, 5, 6], [5, 5, 8], [4, 13, 15], [5, 12, 13], [9, 10, 17], [3, 25, 26], [7, 15, 20], [10, 13, 13], [8, 15, 17]]</code> <code>heronianTriangle()</code> <code>[[3, 4, 5], [5, 5, 6], [5, 5, 8], [4, 13, 15], [5, 12, 13], [9, 10, 17], [3, 25, 26], [7, 15, 20], [10, 13, 13], [8, 15, 17]]</code>'
|
||||
testString: assert.deepEqual(heronianTriangle(testCases[0]), res[0]);
|
||||
- text: '<code>heronianTriangle()</code>应返回<code>[[3, 4, 5], [5, 5, 6], [5, 5, 8], [4, 13, 15], [5, 12, 13], [9, 10, 17], [3, 25, 26], [7, 15, 20], [10, 13, 13], [8, 15, 17], [13, 13, 24], [6, 25, 29], [11, 13, 20], [5, 29, 30], [13, 14, 15]],</code> <code>heronianTriangle()</code> <code>[[3, 4, 5], [5, 5, 6], [5, 5, 8], [4, 13, 15], [5, 12, 13], [9, 10, 17], [3, 25, 26], [7, 15, 20], [10, 13, 13], [8, 15, 17], [13, 13, 24], [6, 25, 29], [11, 13, 20], [5, 29, 30], [13, 14, 15]],</code> <code>heronianTriangle()</code> <code>[[3, 4, 5], [5, 5, 6], [5, 5, 8], [4, 13, 15], [5, 12, 13], [9, 10, 17], [3, 25, 26], [7, 15, 20], [10, 13, 13], [8, 15, 17], [13, 13, 24], [6, 25, 29], [11, 13, 20], [5, 29, 30], [13, 14, 15]],</code> <code>heronianTriangle()</code> <code>[[3, 4, 5], [5, 5, 6], [5, 5, 8], [4, 13, 15], [5, 12, 13], [9, 10, 17], [3, 25, 26], [7, 15, 20], [10, 13, 13], [8, 15, 17], [13, 13, 24], [6, 25, 29], [11, 13, 20], [5, 29, 30], [13, 14, 15]],</code> <code>heronianTriangle()</code> <code>[[3, 4, 5], [5, 5, 6], [5, 5, 8], [4, 13, 15], [5, 12, 13], [9, 10, 17], [3, 25, 26], [7, 15, 20], [10, 13, 13], [8, 15, 17], [13, 13, 24], [6, 25, 29], [11, 13, 20], [5, 29, 30], [13, 14, 15]],</code>'
|
||||
testString: assert.deepEqual(heronianTriangle(testCases[1]), res[1]);
|
||||
- text: '<code>heronianTriangle()</code>应返回<code>[[3, 4, 5], [5, 5, 6], [5, 5, 8], [4, 13, 15], [5, 12, 13], [9, 10, 17], [3, 25, 26], [7, 15, 20], [10, 13, 13], [8, 15, 17], [13, 13, 24], [6, 25, 29], [11, 13, 20], [5, 29, 30], [13, 14, 15], [10, 17, 21], [7, 24, 25], [8, 29, 35], [12, 17, 25], [4, 51, 53]],</code> <code>heronianTriangle()</code> <code>[[3, 4, 5], [5, 5, 6], [5, 5, 8], [4, 13, 15], [5, 12, 13], [9, 10, 17], [3, 25, 26], [7, 15, 20], [10, 13, 13], [8, 15, 17], [13, 13, 24], [6, 25, 29], [11, 13, 20], [5, 29, 30], [13, 14, 15], [10, 17, 21], [7, 24, 25], [8, 29, 35], [12, 17, 25], [4, 51, 53]],</code> <code>heronianTriangle()</code> <code>[[3, 4, 5], [5, 5, 6], [5, 5, 8], [4, 13, 15], [5, 12, 13], [9, 10, 17], [3, 25, 26], [7, 15, 20], [10, 13, 13], [8, 15, 17], [13, 13, 24], [6, 25, 29], [11, 13, 20], [5, 29, 30], [13, 14, 15], [10, 17, 21], [7, 24, 25], [8, 29, 35], [12, 17, 25], [4, 51, 53]],</code> <code>heronianTriangle()</code> <code>[[3, 4, 5], [5, 5, 6], [5, 5, 8], [4, 13, 15], [5, 12, 13], [9, 10, 17], [3, 25, 26], [7, 15, 20], [10, 13, 13], [8, 15, 17], [13, 13, 24], [6, 25, 29], [11, 13, 20], [5, 29, 30], [13, 14, 15], [10, 17, 21], [7, 24, 25], [8, 29, 35], [12, 17, 25], [4, 51, 53]],</code> <code>heronianTriangle()</code> <code>[[3, 4, 5], [5, 5, 6], [5, 5, 8], [4, 13, 15], [5, 12, 13], [9, 10, 17], [3, 25, 26], [7, 15, 20], [10, 13, 13], [8, 15, 17], [13, 13, 24], [6, 25, 29], [11, 13, 20], [5, 29, 30], [13, 14, 15], [10, 17, 21], [7, 24, 25], [8, 29, 35], [12, 17, 25], [4, 51, 53]],</code>'
|
||||
testString: assert.deepEqual(heronianTriangle(testCases[2]), res[2]);
|
||||
- text: '<code>heronianTriangle()</code>应返回<code>[[3, 4, 5], [5, 5, 6], [5, 5, 8], [4, 13, 15], [5, 12, 13], [9, 10, 17], [3, 25, 26], [7, 15, 20], [10, 13, 13], [8, 15, 17], [13, 13, 24], [6, 25, 29], [11, 13, 20], [5, 29, 30], [13, 14, 15], [10, 17, 21], [7, 24, 25], [8, 29, 35], [12, 17, 25], [4, 51, 53], [19, 20, 37],[16, 17, 17], [17, 17, 30], [16, 25, 39], [13, 20, 21]]</code> <code>heronianTriangle()</code> <code>[[3, 4, 5], [5, 5, 6], [5, 5, 8], [4, 13, 15], [5, 12, 13], [9, 10, 17], [3, 25, 26], [7, 15, 20], [10, 13, 13], [8, 15, 17], [13, 13, 24], [6, 25, 29], [11, 13, 20], [5, 29, 30], [13, 14, 15], [10, 17, 21], [7, 24, 25], [8, 29, 35], [12, 17, 25], [4, 51, 53], [19, 20, 37],[16, 17, 17], [17, 17, 30], [16, 25, 39], [13, 20, 21]]</code> <code>heronianTriangle()</code> <code>[[3, 4, 5], [5, 5, 6], [5, 5, 8], [4, 13, 15], [5, 12, 13], [9, 10, 17], [3, 25, 26], [7, 15, 20], [10, 13, 13], [8, 15, 17], [13, 13, 24], [6, 25, 29], [11, 13, 20], [5, 29, 30], [13, 14, 15], [10, 17, 21], [7, 24, 25], [8, 29, 35], [12, 17, 25], [4, 51, 53], [19, 20, 37],[16, 17, 17], [17, 17, 30], [16, 25, 39], [13, 20, 21]]</code> <code>heronianTriangle()</code> <code>[[3, 4, 5], [5, 5, 6], [5, 5, 8], [4, 13, 15], [5, 12, 13], [9, 10, 17], [3, 25, 26], [7, 15, 20], [10, 13, 13], [8, 15, 17], [13, 13, 24], [6, 25, 29], [11, 13, 20], [5, 29, 30], [13, 14, 15], [10, 17, 21], [7, 24, 25], [8, 29, 35], [12, 17, 25], [4, 51, 53], [19, 20, 37],[16, 17, 17], [17, 17, 30], [16, 25, 39], [13, 20, 21]]</code> <code>heronianTriangle()</code> <code>[[3, 4, 5], [5, 5, 6], [5, 5, 8], [4, 13, 15], [5, 12, 13], [9, 10, 17], [3, 25, 26], [7, 15, 20], [10, 13, 13], [8, 15, 17], [13, 13, 24], [6, 25, 29], [11, 13, 20], [5, 29, 30], [13, 14, 15], [10, 17, 21], [7, 24, 25], [8, 29, 35], [12, 17, 25], [4, 51, 53], [19, 20, 37],[16, 17, 17], [17, 17, 30], [16, 25, 39], [13, 20, 21]]</code>'
|
||||
testString: assert.deepEqual(heronianTriangle(testCases[3]), res[3]);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`heronianTriangle`是一个函数。
|
||||
|
||||
```js
|
||||
// noprotect
|
||||
function heronianTriangle (n) {
|
||||
// Good luck!
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
assert(typeof heronianTriangle === 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`heronianTriangle()`应返回`[[3, 4, 5], [5, 5, 6], [5, 5, 8], [4, 13, 15], [5, 12, 13], [9, 10, 17], [3, 25, 26], [7, 15, 20], [10, 13, 13], [8, 15, 17]]` `heronianTriangle()` `[[3, 4, 5], [5, 5, 6], [5, 5, 8], [4, 13, 15], [5, 12, 13], [9, 10, 17], [3, 25, 26], [7, 15, 20], [10, 13, 13], [8, 15, 17]]` `heronianTriangle()` `[[3, 4, 5], [5, 5, 6], [5, 5, 8], [4, 13, 15], [5, 12, 13], [9, 10, 17], [3, 25, 26], [7, 15, 20], [10, 13, 13], [8, 15, 17]]` `heronianTriangle()` `[[3, 4, 5], [5, 5, 6], [5, 5, 8], [4, 13, 15], [5, 12, 13], [9, 10, 17], [3, 25, 26], [7, 15, 20], [10, 13, 13], [8, 15, 17]]` `heronianTriangle()` `[[3, 4, 5], [5, 5, 6], [5, 5, 8], [4, 13, 15], [5, 12, 13], [9, 10, 17], [3, 25, 26], [7, 15, 20], [10, 13, 13], [8, 15, 17]]`
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
assert.deepEqual(heronianTriangle(testCases[0]), res[0]);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`heronianTriangle()`应返回`[[3, 4, 5], [5, 5, 6], [5, 5, 8], [4, 13, 15], [5, 12, 13], [9, 10, 17], [3, 25, 26], [7, 15, 20], [10, 13, 13], [8, 15, 17], [13, 13, 24], [6, 25, 29], [11, 13, 20], [5, 29, 30], [13, 14, 15]],` `heronianTriangle()` `[[3, 4, 5], [5, 5, 6], [5, 5, 8], [4, 13, 15], [5, 12, 13], [9, 10, 17], [3, 25, 26], [7, 15, 20], [10, 13, 13], [8, 15, 17], [13, 13, 24], [6, 25, 29], [11, 13, 20], [5, 29, 30], [13, 14, 15]],` `heronianTriangle()` `[[3, 4, 5], [5, 5, 6], [5, 5, 8], [4, 13, 15], [5, 12, 13], [9, 10, 17], [3, 25, 26], [7, 15, 20], [10, 13, 13], [8, 15, 17], [13, 13, 24], [6, 25, 29], [11, 13, 20], [5, 29, 30], [13, 14, 15]],` `heronianTriangle()` `[[3, 4, 5], [5, 5, 6], [5, 5, 8], [4, 13, 15], [5, 12, 13], [9, 10, 17], [3, 25, 26], [7, 15, 20], [10, 13, 13], [8, 15, 17], [13, 13, 24], [6, 25, 29], [11, 13, 20], [5, 29, 30], [13, 14, 15]],` `heronianTriangle()` `[[3, 4, 5], [5, 5, 6], [5, 5, 8], [4, 13, 15], [5, 12, 13], [9, 10, 17], [3, 25, 26], [7, 15, 20], [10, 13, 13], [8, 15, 17], [13, 13, 24], [6, 25, 29], [11, 13, 20], [5, 29, 30], [13, 14, 15]],`
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert.deepEqual(heronianTriangle(testCases[1]), res[1]);
|
||||
```
|
||||
|
||||
/section>
|
||||
`heronianTriangle()`应返回`[[3, 4, 5], [5, 5, 6], [5, 5, 8], [4, 13, 15], [5, 12, 13], [9, 10, 17], [3, 25, 26], [7, 15, 20], [10, 13, 13], [8, 15, 17], [13, 13, 24], [6, 25, 29], [11, 13, 20], [5, 29, 30], [13, 14, 15], [10, 17, 21], [7, 24, 25], [8, 29, 35], [12, 17, 25], [4, 51, 53]],` `heronianTriangle()` `[[3, 4, 5], [5, 5, 6], [5, 5, 8], [4, 13, 15], [5, 12, 13], [9, 10, 17], [3, 25, 26], [7, 15, 20], [10, 13, 13], [8, 15, 17], [13, 13, 24], [6, 25, 29], [11, 13, 20], [5, 29, 30], [13, 14, 15], [10, 17, 21], [7, 24, 25], [8, 29, 35], [12, 17, 25], [4, 51, 53]],` `heronianTriangle()` `[[3, 4, 5], [5, 5, 6], [5, 5, 8], [4, 13, 15], [5, 12, 13], [9, 10, 17], [3, 25, 26], [7, 15, 20], [10, 13, 13], [8, 15, 17], [13, 13, 24], [6, 25, 29], [11, 13, 20], [5, 29, 30], [13, 14, 15], [10, 17, 21], [7, 24, 25], [8, 29, 35], [12, 17, 25], [4, 51, 53]],` `heronianTriangle()` `[[3, 4, 5], [5, 5, 6], [5, 5, 8], [4, 13, 15], [5, 12, 13], [9, 10, 17], [3, 25, 26], [7, 15, 20], [10, 13, 13], [8, 15, 17], [13, 13, 24], [6, 25, 29], [11, 13, 20], [5, 29, 30], [13, 14, 15], [10, 17, 21], [7, 24, 25], [8, 29, 35], [12, 17, 25], [4, 51, 53]],` `heronianTriangle()` `[[3, 4, 5], [5, 5, 6], [5, 5, 8], [4, 13, 15], [5, 12, 13], [9, 10, 17], [3, 25, 26], [7, 15, 20], [10, 13, 13], [8, 15, 17], [13, 13, 24], [6, 25, 29], [11, 13, 20], [5, 29, 30], [13, 14, 15], [10, 17, 21], [7, 24, 25], [8, 29, 35], [12, 17, 25], [4, 51, 53]],`
|
||||
|
||||
```js
|
||||
assert.deepEqual(heronianTriangle(testCases[2]), res[2]);
|
||||
```
|
||||
|
||||
`heronianTriangle()`应返回`[[3, 4, 5], [5, 5, 6], [5, 5, 8], [4, 13, 15], [5, 12, 13], [9, 10, 17], [3, 25, 26], [7, 15, 20], [10, 13, 13], [8, 15, 17], [13, 13, 24], [6, 25, 29], [11, 13, 20], [5, 29, 30], [13, 14, 15], [10, 17, 21], [7, 24, 25], [8, 29, 35], [12, 17, 25], [4, 51, 53], [19, 20, 37],[16, 17, 17], [17, 17, 30], [16, 25, 39], [13, 20, 21]]` `heronianTriangle()` `[[3, 4, 5], [5, 5, 6], [5, 5, 8], [4, 13, 15], [5, 12, 13], [9, 10, 17], [3, 25, 26], [7, 15, 20], [10, 13, 13], [8, 15, 17], [13, 13, 24], [6, 25, 29], [11, 13, 20], [5, 29, 30], [13, 14, 15], [10, 17, 21], [7, 24, 25], [8, 29, 35], [12, 17, 25], [4, 51, 53], [19, 20, 37],[16, 17, 17], [17, 17, 30], [16, 25, 39], [13, 20, 21]]` `heronianTriangle()` `[[3, 4, 5], [5, 5, 6], [5, 5, 8], [4, 13, 15], [5, 12, 13], [9, 10, 17], [3, 25, 26], [7, 15, 20], [10, 13, 13], [8, 15, 17], [13, 13, 24], [6, 25, 29], [11, 13, 20], [5, 29, 30], [13, 14, 15], [10, 17, 21], [7, 24, 25], [8, 29, 35], [12, 17, 25], [4, 51, 53], [19, 20, 37],[16, 17, 17], [17, 17, 30], [16, 25, 39], [13, 20, 21]]` `heronianTriangle()` `[[3, 4, 5], [5, 5, 6], [5, 5, 8], [4, 13, 15], [5, 12, 13], [9, 10, 17], [3, 25, 26], [7, 15, 20], [10, 13, 13], [8, 15, 17], [13, 13, 24], [6, 25, 29], [11, 13, 20], [5, 29, 30], [13, 14, 15], [10, 17, 21], [7, 24, 25], [8, 29, 35], [12, 17, 25], [4, 51, 53], [19, 20, 37],[16, 17, 17], [17, 17, 30], [16, 25, 39], [13, 20, 21]]` `heronianTriangle()` `[[3, 4, 5], [5, 5, 6], [5, 5, 8], [4, 13, 15], [5, 12, 13], [9, 10, 17], [3, 25, 26], [7, 15, 20], [10, 13, 13], [8, 15, 17], [13, 13, 24], [6, 25, 29], [11, 13, 20], [5, 29, 30], [13, 14, 15], [10, 17, 21], [7, 24, 25], [8, 29, 35], [12, 17, 25], [4, 51, 53], [19, 20, 37],[16, 17, 17], [17, 17, 30], [16, 25, 39], [13, 20, 21]]`
|
||||
|
||||
```js
|
||||
assert.deepEqual(heronianTriangle(testCases[3]), res[3]);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,87 +1,87 @@
|
||||
---
|
||||
id: 59622f89e4e137560018a40e
|
||||
title: Hofstadter图 - 图序列
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: Hofstadter图 - 图序列
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"><p>这两个正整数序列定义为: </p><p> <big>$$ R(1)= 1 \; \ S(1)= 2 \\ R(n)= R(n-1)+ S(n-1),\ quad n> 1. $$</big> </p><p>序列<big>$ S(n)$</big>进一步定义为<big>$ R(n)$中</big>不存在的正整数序列。 </p><p>序列<big>$ R $</big>开始: </p><p> 1,3,7,12,18 ...... </p><p>序列<big>$ S $</big>开始: </p><p> 2,4,5,6,8 ...... </p>任务:创建两个名为ffr和ffs的函数,当给定n分别返回R(n)或S(n)时(注意R(1)= 1且S(1)= 2以避免逐个错误) 。不应假设n的最大值。 Sloane的<a href="http://oeis.org/A005228" title="链接:http://oeis.org/A005228">A005228</a>和<a href="http://oeis.org/A030124" title="链接:http://oeis.org/A030124">A030124</a> 。 <a href="http://mathworld.wolfram.com/HofstadterFigure-FigureSequence.html" title="链接:http://mathworld.wolfram.com/HofstadterFigure-FigureSequence.html">Wolfram MathWorld</a>维基百科: <a href="https://en.wikipedia.org/wiki/Hofstadter_sequence#Hofstadter_Figure-Figure_sequences" title="wp:Hofstadter_sequence#Hofstadter_Figure-Figure_sequences">Hofstadter图 - 图序列</a> 。 </section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
<p>这两个正整数序列定义为: </p><p> <big>$$ R(1)= 1 \; \ S(1)= 2 \\ R(n)= R(n-1)+ S(n-1),\ quad n> 1. $$</big> </p><p>序列<big>$ S(n)$</big>进一步定义为<big>$ R(n)$中</big>不存在的正整数序列。 </p><p>序列<big>$ R $</big>开始: </p><p> 1,3,7,12,18 ...... </p><p>序列<big>$ S $</big>开始: </p><p> 2,4,5,6,8 ...... </p>任务:创建两个名为ffr和ffs的函数,当给定n分别返回R(n)或S(n)时(注意R(1)= 1且S(1)= 2以避免逐个错误) 。不应假设n的最大值。 Sloane的<a href='http://oeis.org/A005228' title='链接:http://oeis.org/A005228'>A005228</a>和<a href='http://oeis.org/A030124' title='链接:http://oeis.org/A030124'>A030124</a> 。 <a href='http://mathworld.wolfram.com/HofstadterFigure-FigureSequence.html' title='链接:http://mathworld.wolfram.com/HofstadterFigure-FigureSequence.html'>Wolfram MathWorld</a>维基百科: <a href='https://en.wikipedia.org/wiki/Hofstadter_sequence#Hofstadter_Figure-Figure_sequences' title='wp:Hofstadter_sequence#Hofstadter_Figure-Figure_sequences'>Hofstadter图 - 图序列</a> 。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>ffr</code>是一个功能。
|
||||
testString: assert(typeof ffr === 'function');
|
||||
- text: <code>ffs</code>是一个函数。
|
||||
testString: assert(typeof ffs === 'function');
|
||||
- text: <code>ffr</code>应该返回整数。
|
||||
testString: assert(Number.isInteger(ffr(1)));
|
||||
- text: <code>ffs</code>应该返回整数。
|
||||
testString: assert(Number.isInteger(ffs(1)));
|
||||
- text: <code>ffr()</code>应该返回<code>69</code>
|
||||
testString: assert.equal(ffr(ffrParamRes[0][0]), ffrParamRes[0][1]);
|
||||
- text: <code>ffr()</code>应返回<code>1509</code>
|
||||
testString: assert.equal(ffr(ffrParamRes[1][0]), ffrParamRes[1][1]);
|
||||
- text: <code>ffr()</code>应返回<code>5764</code>
|
||||
testString: assert.equal(ffr(ffrParamRes[2][0]), ffrParamRes[2][1]);
|
||||
- text: <code>ffr()</code>应返回<code>526334</code>
|
||||
testString: assert.equal(ffr(ffrParamRes[3][0]), ffrParamRes[3][1]);
|
||||
- text: <code>ffs()</code>应该返回<code>14</code>
|
||||
testString: assert.equal(ffs(ffsParamRes[0][0]), ffsParamRes[0][1]);
|
||||
- text: <code>ffs()</code>应该返回<code>59</code>
|
||||
testString: assert.equal(ffs(ffsParamRes[1][0]), ffsParamRes[1][1]);
|
||||
- text: <code>ffs()</code>应该返回<code>112</code>
|
||||
testString: assert.equal(ffs(ffsParamRes[2][0]), ffsParamRes[2][1]);
|
||||
- text: <code>ffs()</code>应该返回<code>1041</code>
|
||||
testString: assert.equal(ffs(ffsParamRes[3][0]), ffsParamRes[3][1]);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`ffr`是一个功能。
|
||||
|
||||
```js
|
||||
// noprotect
|
||||
function ffr(n) {
|
||||
return n;
|
||||
}
|
||||
|
||||
function ffs(n) {
|
||||
return n;
|
||||
}
|
||||
|
||||
assert(typeof ffr === 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`ffs`是一个函数。
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
assert(typeof ffs === 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`ffr`应该返回整数。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert(Number.isInteger(ffr(1)));
|
||||
```
|
||||
|
||||
/section>
|
||||
`ffs`应该返回整数。
|
||||
|
||||
```js
|
||||
assert(Number.isInteger(ffs(1)));
|
||||
```
|
||||
|
||||
`ffr()`应该返回`69`
|
||||
|
||||
```js
|
||||
assert.equal(ffr(ffrParamRes[0][0]), ffrParamRes[0][1]);
|
||||
```
|
||||
|
||||
`ffr()`应返回`1509`
|
||||
|
||||
```js
|
||||
assert.equal(ffr(ffrParamRes[1][0]), ffrParamRes[1][1]);
|
||||
```
|
||||
|
||||
`ffr()`应返回`5764`
|
||||
|
||||
```js
|
||||
assert.equal(ffr(ffrParamRes[2][0]), ffrParamRes[2][1]);
|
||||
```
|
||||
|
||||
`ffr()`应返回`526334`
|
||||
|
||||
```js
|
||||
assert.equal(ffr(ffrParamRes[3][0]), ffrParamRes[3][1]);
|
||||
```
|
||||
|
||||
`ffs()`应该返回`14`
|
||||
|
||||
```js
|
||||
assert.equal(ffs(ffsParamRes[0][0]), ffsParamRes[0][1]);
|
||||
```
|
||||
|
||||
`ffs()`应该返回`59`
|
||||
|
||||
```js
|
||||
assert.equal(ffs(ffsParamRes[1][0]), ffsParamRes[1][1]);
|
||||
```
|
||||
|
||||
`ffs()`应该返回`112`
|
||||
|
||||
```js
|
||||
assert.equal(ffs(ffsParamRes[2][0]), ffsParamRes[2][1]);
|
||||
```
|
||||
|
||||
`ffs()`应该返回`1041`
|
||||
|
||||
```js
|
||||
assert.equal(ffs(ffsParamRes[3][0]), ffsParamRes[3][1]);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,71 +1,51 @@
|
||||
---
|
||||
id: 59637c4d89f6786115efd814
|
||||
title: Hofstadter Q序列
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: Hofstadter Q序列
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"><p> <a href="https://en.wikipedia.org/wiki/Hofstadter_sequence#Hofstadter_Q_sequence" title="wp:Hofstadter_sequence#Hofstadter_Q_sequence">Hofstadter Q序列</a>定义为: </p><p> $ Q(1)= Q(2)= 1,\\ Q(n)= Q \ big(nQ(n-1)\ big)+ Q \ big(nQ(n-2)),\ quad n> 2. $ </p><p>它定义为<a href="http://rosettacode.org/wiki/Fibonacci sequence" title="斐波那契序列">Fibonacci序列</a> ,但<a href="http://rosettacode.org/wiki/Fibonacci sequence" title="斐波那契序列">Fibonacci序列中</a>的下一个术语是前两个术语的总和,在Q序列中,前两个术语告诉您在Q序列中返回多远以找到两个数字总结以制作序列的下一个术语。 </p>任务:将Hofstadter Q Sequence方程实现为JavaScript </section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
<p> <a href='https://en.wikipedia.org/wiki/Hofstadter_sequence#Hofstadter_Q_sequence' title='wp:Hofstadter_sequence#Hofstadter_Q_sequence'>Hofstadter Q序列</a>定义为: </p><p> $ Q(1)= Q(2)= 1,\\ Q(n)= Q \ big(nQ(n-1)\ big)+ Q \ big(nQ(n-2)),\ quad n> 2. $ </p><p>它定义为<a href='http://rosettacode.org/wiki/Fibonacci sequence' title='斐波那契序列'>Fibonacci序列</a> ,但<a href='http://rosettacode.org/wiki/Fibonacci sequence' title='斐波那契序列'>Fibonacci序列中</a>的下一个术语是前两个术语的总和,在Q序列中,前两个术语告诉您在Q序列中返回多远以找到两个数字总结以制作序列的下一个术语。 </p>任务:将Hofstadter Q Sequence方程实现为JavaScript
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>hofstadterQ</code>是一个函数。
|
||||
testString: assert(typeof hofstadterQ === 'function');
|
||||
- text: <code>hofstadterQ()</code>应该返回<code>integer</code>
|
||||
testString: assert(Number.isInteger(hofstadterQ(1000)));
|
||||
- text: <code>hofstadterQ(1000)</code>应该返回<code>502</code>
|
||||
testString: assert.equal(hofstadterQ(testCase[0]), res[0]);
|
||||
- text: <code>hofstadterQ(1500)</code>应该返回<code>755</code>
|
||||
testString: assert.equal(hofstadterQ(testCase[1]), res[1]);
|
||||
- text: <code>hofstadterQ(2000)</code>应该返回<code>1005</code>
|
||||
testString: assert.equal(hofstadterQ(testCase[2]), res[2]);
|
||||
- text: <code>hofstadterQ(2500)</code>应该返回<code>1261</code>
|
||||
testString: assert.equal(hofstadterQ(testCase[3]), res[3]);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`hofstadterQ`是一个函数。
|
||||
|
||||
```js
|
||||
function hofstadterQ (n) {
|
||||
// Good luck!
|
||||
return n;
|
||||
}
|
||||
|
||||
assert(typeof hofstadterQ === 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`hofstadterQ()`应该返回`integer`
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
assert(Number.isInteger(hofstadterQ(1000)));
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`hofstadterQ(1000)`应该返回`502`
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert.equal(hofstadterQ(testCase[0]), res[0]);
|
||||
```
|
||||
|
||||
/section>
|
||||
`hofstadterQ(1500)`应该返回`755`
|
||||
|
||||
```js
|
||||
assert.equal(hofstadterQ(testCase[1]), res[1]);
|
||||
```
|
||||
|
||||
`hofstadterQ(2000)`应该返回`1005`
|
||||
|
||||
```js
|
||||
assert.equal(hofstadterQ(testCase[2]), res[2]);
|
||||
```
|
||||
|
||||
`hofstadterQ(2500)`应该返回`1261`
|
||||
|
||||
```js
|
||||
assert.equal(hofstadterQ(testCase[3]), res[3]);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,66 +1,68 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc7eb0
|
||||
title: 我在E之前除了C之后
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 我在E之前除了C之后
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">短语<a href="https://en.wikipedia.org/wiki/I before E except after C">“我在E之前,除了C之后”</a>是一个广为人知的助记符,它应该有助于拼写英语单词。使用提供的单词,检查短语的两个子句是否合理: <ol><li style="margin-bottom: 5px;"> <i>“我在E之前没有前面的C”。</i> </li><li> <i>“在我之前的C之前是C”。</i> </li></ol>如果两个子短语都是合理的,则原始短语可以说是合理的。编写一个接受单词的函数,并检查单词是否遵循此规则。如果该函数遵循规则,则该函数应返回true,否则返回false。 </section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
短语[“我在E之前,除了C之后”](<https://en.wikipedia.org/wiki/I before E except after C>)是一个广为人知的助记符,它应该有助于拼写英语单词。使用提供的单词,检查短语的两个子句是否合理:
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
1. *“我在E之前没有前面的C”。*
|
||||
2. *“在我之前的C之前是C”。*
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>IBeforeExceptC</code>应该是一个函数。
|
||||
testString: assert(typeof IBeforeExceptC=='function');
|
||||
- text: <code>IBeforeExceptC("receive")</code>应该返回一个布尔值。
|
||||
testString: assert(typeof IBeforeExceptC("receive")=='boolean');
|
||||
- text: <code>IBeforeExceptC("receive")</code>应该返回<code>true</code> 。
|
||||
testString: assert.equal(IBeforeExceptC("receive"),true);
|
||||
- text: <code>IBeforeExceptC("science")</code>应该返回<code>false</code> 。
|
||||
testString: assert.equal(IBeforeExceptC("science"),false);
|
||||
- text: <code>IBeforeExceptC("imperceivable")</code>应该返回<code>true</code> 。
|
||||
testString: assert.equal(IBeforeExceptC("imperceivable"),true);
|
||||
- text: <code>IBeforeExceptC("inconceivable")</code>应该返回<code>true</code> 。
|
||||
testString: assert.equal(IBeforeExceptC("inconceivable"),true);
|
||||
- text: <code>IBeforeExceptC("insufficient")</code>应返回<code>false</code> 。
|
||||
testString: assert.equal(IBeforeExceptC("insufficient"),false);
|
||||
- text: <code>IBeforeExceptC("omniscient")</code>应该返回<code>false</code> 。
|
||||
testString: assert.equal(IBeforeExceptC("omniscient"),false);
|
||||
如果两个子短语都是合理的,则原始短语可以说是合理的。编写一个接受单词的函数,并检查单词是否遵循此规则。如果该函数遵循规则,则该函数应返回true,否则返回false。
|
||||
|
||||
```
|
||||
# --hints--
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`IBeforeExceptC`应该是一个函数。
|
||||
|
||||
```js
|
||||
function IBeforeExceptC (word) {
|
||||
// Good luck!
|
||||
}
|
||||
|
||||
assert(typeof IBeforeExceptC == 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`IBeforeExceptC("receive")`应该返回一个布尔值。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert(typeof IBeforeExceptC('receive') == 'boolean');
|
||||
```
|
||||
|
||||
/section>
|
||||
`IBeforeExceptC("receive")`应该返回`true` 。
|
||||
|
||||
```js
|
||||
assert.equal(IBeforeExceptC('receive'), true);
|
||||
```
|
||||
|
||||
`IBeforeExceptC("science")`应该返回`false` 。
|
||||
|
||||
```js
|
||||
assert.equal(IBeforeExceptC('science'), false);
|
||||
```
|
||||
|
||||
`IBeforeExceptC("imperceivable")`应该返回`true` 。
|
||||
|
||||
```js
|
||||
assert.equal(IBeforeExceptC('imperceivable'), true);
|
||||
```
|
||||
|
||||
`IBeforeExceptC("inconceivable")`应该返回`true` 。
|
||||
|
||||
```js
|
||||
assert.equal(IBeforeExceptC('inconceivable'), true);
|
||||
```
|
||||
|
||||
`IBeforeExceptC("insufficient")`应返回`false` 。
|
||||
|
||||
```js
|
||||
assert.equal(IBeforeExceptC('insufficient'), false);
|
||||
```
|
||||
|
||||
`IBeforeExceptC("omniscient")`应该返回`false` 。
|
||||
|
||||
```js
|
||||
assert.equal(IBeforeExceptC('omniscient'), false);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,72 +1,63 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc7eaf
|
||||
title: 他们
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 他们
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"> <a href="https://en.wikipedia.org/wiki/International_Bank_Account_Number">国际银行账号(IBAN)</a>是一种国际商定的识别跨国界银行账户的方法,降低了传播<a href="https://en.wikipedia.org/wiki/Transcription_error">抄写错误的</a>风险。 IBAN最多包含34个字母数字字符: <ul><li>首先是两个字母的ISO 3166-1 alpha-2国家代码</li><li>然后是两个校验位,和</li><li>最后是国家特定的基本银行帐号(BBAN)。 </li></ul>通过检查数字,即使在提交交易之前,也可以对银行帐号进行健全性检查以确认其完整性。编写一个以IBAN字符串作为参数的函数。如果有效则返回true。否则,返回false。 </section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
[国际银行账号(IBAN)](https://en.wikipedia.org/wiki/International_Bank_Account_Number)是一种国际商定的识别跨国界银行账户的方法,降低了传播[抄写错误的](https://en.wikipedia.org/wiki/Transcription_error)风险。 IBAN最多包含34个字母数字字符:
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
- 首先是两个字母的ISO 3166-1 alpha-2国家代码
|
||||
- 然后是两个校验位,和
|
||||
- 最后是国家特定的基本银行帐号(BBAN)。
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>isValid</code>应该是一个函数。
|
||||
testString: assert(typeof isValid=='function');
|
||||
- text: <code>isValid(""+tests[0]+"")</code>应该返回一个布尔值。
|
||||
testString: assert(typeof isValid('GB82 WEST 1234 5698 7654 32')=='boolean');
|
||||
- text: <code>isValid(""+tests[0]+"")</code>应该返回<code>true</code> 。
|
||||
testString: assert.equal(isValid('GB82 WEST 1234 5698 7654 32'),true);
|
||||
- text: <code>isValid(""+tests[1]+"")</code>应返回<code>false</code> 。
|
||||
testString: assert.equal(isValid('GB82 WEST 1.34 5698 7654 32'),false);
|
||||
- text: <code>isValid(""+tests[2]+"")</code>应返回<code>false</code> 。
|
||||
testString: assert.equal(isValid('GB82 WEST 1234 5698 7654 325'),false);
|
||||
- text: <code>isValid(""+tests[3]+"")</code>应该返回<code>false</code> 。
|
||||
testString: assert.equal(isValid('GB82 TEST 1234 5698 7654 32'),false);
|
||||
- text: <code>isValid(""+tests[4]+"")</code>应该返回<code>true</code> 。
|
||||
testString: assert.equal(isValid('SA03 8000 0000 6080 1016 7519'),true);
|
||||
通过检查数字,即使在提交交易之前,也可以对银行帐号进行健全性检查以确认其完整性。编写一个以IBAN字符串作为参数的函数。如果有效则返回true。否则,返回false。
|
||||
|
||||
```
|
||||
# --hints--
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`isValid`应该是一个函数。
|
||||
|
||||
```js
|
||||
function isValid (iban) {
|
||||
// Good luck!
|
||||
}
|
||||
|
||||
assert(typeof isValid == 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`isValid(""+tests[0]+"")`应该返回一个布尔值。
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
assert(typeof isValid('GB82 WEST 1234 5698 7654 32') == 'boolean');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`isValid(""+tests[0]+"")`应该返回`true` 。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert.equal(isValid('GB82 WEST 1234 5698 7654 32'), true);
|
||||
```
|
||||
|
||||
/section>
|
||||
`isValid(""+tests[1]+"")`应返回`false` 。
|
||||
|
||||
```js
|
||||
assert.equal(isValid('GB82 WEST 1.34 5698 7654 32'), false);
|
||||
```
|
||||
|
||||
`isValid(""+tests[2]+"")`应返回`false` 。
|
||||
|
||||
```js
|
||||
assert.equal(isValid('GB82 WEST 1234 5698 7654 325'), false);
|
||||
```
|
||||
|
||||
`isValid(""+tests[3]+"")`应该返回`false` 。
|
||||
|
||||
```js
|
||||
assert.equal(isValid('GB82 TEST 1234 5698 7654 32'), false);
|
||||
```
|
||||
|
||||
`isValid(""+tests[4]+"")`应该返回`true` 。
|
||||
|
||||
```js
|
||||
assert.equal(isValid('SA03 8000 0000 6080 1016 7519'), true);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,70 +1,51 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc7eb1
|
||||
title: 身份矩阵
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 身份矩阵
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"> <i>单位矩阵</i>是大小为\(n \次n \)的方阵,其中对角元素都是<b>1</b> s(1),所有其他元素都是<b>0</b> s(零)。 \ begin {bmatrix} 1&0&0 \ cr 0&1&0 \ cr 0&0&1 \ cr \ end {bmatrix}编写一个以数字'n'作为参数并返回单位矩阵的函数订单nx n。 </section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
*单位矩阵*是大小为\\(n \\次n \\)的方阵,其中对角元素都是**1** s(1),所有其他元素都是**0** s(零)。 \\ begin {bmatrix} 1&0&0 \\ cr 0&1&0 \\ cr 0&0&1 \\ cr \\ end {bmatrix}编写一个以数字'n'作为参数并返回单位矩阵的函数订单nx n。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>idMatrix</code>应该是一个功能。
|
||||
testString: assert(typeof idMatrix=='function');
|
||||
- text: <code>idMatrix(1)</code>应该返回一个数组。
|
||||
testString: assert(Array.isArray(idMatrix(1)));
|
||||
- text: '<code>idMatrix(1)</code>应返回<code>"+JSON.stringify(results[0])+"</code> 。'
|
||||
testString: assert.deepEqual(idMatrix(1),results[0]);
|
||||
- text: '<code>idMatrix(2)</code>应返回<code>"+JSON.stringify(results[1])+"</code> 。'
|
||||
testString: assert.deepEqual(idMatrix(2),results[1]);
|
||||
- text: '<code>idMatrix(3)</code>应返回<code>"+JSON.stringify(results[2])+"</code> 。'
|
||||
testString: assert.deepEqual(idMatrix(3),results[2]);
|
||||
- text: '<code>idMatrix(4)</code>应返回<code>"+JSON.stringify(results[3])+"</code> 。'
|
||||
testString: assert.deepEqual(idMatrix(4),results[3]);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`idMatrix`应该是一个功能。
|
||||
|
||||
```js
|
||||
function idMatrix (n) {
|
||||
// Good luck!
|
||||
}
|
||||
|
||||
assert(typeof idMatrix == 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`idMatrix(1)`应该返回一个数组。
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
assert(Array.isArray(idMatrix(1)));
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`idMatrix(1)`应返回`"+JSON.stringify(results[0])+"` 。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert.deepEqual(idMatrix(1), results[0]);
|
||||
```
|
||||
|
||||
/section>
|
||||
`idMatrix(2)`应返回`"+JSON.stringify(results[1])+"` 。
|
||||
|
||||
```js
|
||||
assert.deepEqual(idMatrix(2), results[1]);
|
||||
```
|
||||
|
||||
`idMatrix(3)`应返回`"+JSON.stringify(results[2])+"` 。
|
||||
|
||||
```js
|
||||
assert.deepEqual(idMatrix(3), results[2]);
|
||||
```
|
||||
|
||||
`idMatrix(4)`应返回`"+JSON.stringify(results[3])+"` 。
|
||||
|
||||
```js
|
||||
assert.deepEqual(idMatrix(4), results[3]);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,67 +1,70 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc7ec1
|
||||
title: 迭代的数字平方
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 迭代的数字平方
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">如果添加自然数(大于零的整数)的数字的平方,则始终以1或89结尾: <pre> 15 - > 26 - > 40 - > 16 - > 37 - > 58 - > 89
|
||||
7 - > 49 - > 97 - > 130 - > 10 - > 1 </pre>编写一个函数,该函数将数字作为参数,并在执行上述过程后返回1或89。 </section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>iteratedSquare</code>应该是一个函数。
|
||||
testString: assert(typeof iteratedSquare=='function');
|
||||
- text: <code>iteratedSquare(4)</code>应该返回一个数字。
|
||||
testString: assert(typeof iteratedSquare(4)=='number');
|
||||
- text: <code>iteratedSquare(4)</code>应该返回<code>89</code> 。
|
||||
testString: assert.equal(iteratedSquare(4),89);
|
||||
- text: <code>iteratedSquare(7)</code>应该返回<code>1</code> 。
|
||||
testString: assert.equal(iteratedSquare(7),1);
|
||||
- text: <code>iteratedSquare(15)</code>应该返回<code>89</code> 。
|
||||
testString: assert.equal(iteratedSquare(15),89);
|
||||
- text: <code>iteratedSquare(20)</code>应该返回<code>89</code> 。
|
||||
testString: assert.equal(iteratedSquare(20),89);
|
||||
- text: <code>iteratedSquare(70)</code>应该返回<code>1</code> 。
|
||||
testString: assert.equal(iteratedSquare(70),1);
|
||||
- text: <code>iteratedSquare(100)</code>应该返回<code>1</code> 。
|
||||
testString: assert.equal(iteratedSquare(100),1);
|
||||
如果添加自然数(大于零的整数)的数字的平方,则始终以1或89结尾:
|
||||
|
||||
```
|
||||
15 - > 26 - > 40 - > 16 - > 37 - > 58 - > 89
|
||||
7 - > 49 - > 97 - > 130 - > 10 - > 1
|
||||
```
|
||||
|
||||
</section>
|
||||
编写一个函数,该函数将数字作为参数,并在执行上述过程后返回1或89。
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
# --hints--
|
||||
|
||||
<div id='js-seed'>
|
||||
`iteratedSquare`应该是一个函数。
|
||||
|
||||
```js
|
||||
function iteratedSquare (n) {
|
||||
// Good luck!
|
||||
}
|
||||
|
||||
assert(typeof iteratedSquare == 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`iteratedSquare(4)`应该返回一个数字。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert(typeof iteratedSquare(4) == 'number');
|
||||
```
|
||||
|
||||
/section>
|
||||
`iteratedSquare(4)`应该返回`89` 。
|
||||
|
||||
```js
|
||||
assert.equal(iteratedSquare(4), 89);
|
||||
```
|
||||
|
||||
`iteratedSquare(7)`应该返回`1` 。
|
||||
|
||||
```js
|
||||
assert.equal(iteratedSquare(7), 1);
|
||||
```
|
||||
|
||||
`iteratedSquare(15)`应该返回`89` 。
|
||||
|
||||
```js
|
||||
assert.equal(iteratedSquare(15), 89);
|
||||
```
|
||||
|
||||
`iteratedSquare(20)`应该返回`89` 。
|
||||
|
||||
```js
|
||||
assert.equal(iteratedSquare(20), 89);
|
||||
```
|
||||
|
||||
`iteratedSquare(70)`应该返回`1` 。
|
||||
|
||||
```js
|
||||
assert.equal(iteratedSquare(70), 1);
|
||||
```
|
||||
|
||||
`iteratedSquare(100)`应该返回`1` 。
|
||||
|
||||
```js
|
||||
assert.equal(iteratedSquare(100), 1);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,72 +1,69 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc7ec2
|
||||
title: Jaro距离
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: Jaro距离
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"> Jaro距离是两个弦之间相似性的度量。两个弦的Jaro距离越高,弦越相似。对得分进行归一化,使得<b>0</b>等于没有相似性, <b>1</b>等于完全匹配。定义两个给定字符串\(s_1 \)和\(s_2 \)的Jaro距离\(d_j \)是\ begin {align} d_j = \ begin {cases} 0 && \ text {if} m = 0 \\\ \ {\ frac {1} {3}} \ left({\ frac {m} {| s_ {1} |}} + {\ frac {m} {| s_ {2} |}} + {\ frac { mt} {m}} \ right)&& \ text {otherwise} \ end {cases} \ end {align}其中: <ul><li> \(m \)是<i>匹配字符</i>的数量; </li><li> \(t \)是<i>换位</i>次数的一半。 </li></ul>分别来自\(s_1 \)和\(s_2 \)的两个字符只有在相同且不远于\(\ left \ lfloor \ frac {\ max(| s_1 |,| s_2 |)}时才被认为是<i>匹配的</i> {2} \右\ rfloor-1 \)。将\(s_1 \)的每个字符与\(s_2 \)中的所有匹配字符进行比较。匹配(但不同的序列顺序)字符除以2的数量定义了<i>转置</i>的数量。 <b>示例</b>给定字符串\(s_1 \) <i>DWAYNE</i>和\(s_2 \) <i>DUANE</i>我们发现: <ul><li> \(m = 4 \) </li><li> \(| s_1 | = 6 \) </li><li> \(| s_2 | = 5 \) </li><li> \(t = 0 \) </li></ul>我们发现Jaro得分为:\(d_j = \ frac {1} {3} \ left(\ frac {4} {6} + \ frac {4} {5} + \ frac {4-0} {4} \ right)= 0.822 \)。编写一个函数a,它接受两个字符串作为参数并返回相关的Jaro距离。 </section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
Jaro距离是两个弦之间相似性的度量。两个弦的Jaro距离越高,弦越相似。对得分进行归一化,使得**0**等于没有相似性, **1**等于完全匹配。定义两个给定字符串\\(s_1 \\)和\\(s_2 \\)的Jaro距离\\(d_j \\)是\\ begin {align} d_j = \\ begin {cases} 0 && \\ text {if} m = 0 \\\\\\ \\ {\\ frac {1} {3}} \\ left({\\ frac {m} {| s\_ {1} |}} + {\\ frac {m} {| s\_ {2} |}} + {\\ frac { mt} {m}} \\ right)&& \\ text {otherwise} \\ end {cases} \\ end {align}其中:
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
- \\(m \\)是*匹配字符*的数量;
|
||||
- \\(t \\)是*换位*次数的一半。
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>jaro</code>应该是一个功能。
|
||||
testString: assert(typeof jaro=='function');
|
||||
- text: <code>jaro(""+tests[0][0]+"",""+tests[0][1]+"")</code>应返回一个数字。
|
||||
testString: assert(typeof jaro('MARTHA', 'MARHTA')=='number');
|
||||
- text: <code>jaro(""+tests[0][0]+"",""+tests[0][1]+"")</code>应该返回<code>"+results[0]+"</code> 。
|
||||
testString: assert.equal(jaro('MARTHA', 'MARHTA'), 0.9444444444444445);
|
||||
- text: <code>jaro(""+tests[1][0]+"",""+tests[1][1]+"")</code>应返回<code>"+results[1]+"</code> 。
|
||||
testString: assert.equal(jaro('DIXON', 'DICKSONX'), 0.7666666666666666);
|
||||
- text: <code>jaro(""+tests[2][0]+"",""+tests[2][1]+"")</code>应返回<code>"+results[2]+"</code> 。
|
||||
testString: assert.equal(jaro('JELLYFISH', 'SMELLYFISH'), 0.8962962962962964);
|
||||
- text: <code>jaro(""+tests[3][0]+"",""+tests[3][1]+"")</code>应返回<code>"+results[3]+"</code> 。
|
||||
testString: assert.equal(jaro('HELLOS', 'CHELLO'), 0.888888888888889);
|
||||
- text: <code>jaro(""+tests[4][0]+"",""+tests[4][1]+"")</code>应该返回<code>"+results[4]+"</code> 。
|
||||
testString: assert.equal(jaro('ABCD', 'BCDA'), 0.8333333333333334);
|
||||
分别来自\\(s_1 \\)和\\(s_2 \\)的两个字符只有在相同且不远于\\(\\ left \\ lfloor \\ frac {\\ max(| s_1 |,| s_2 |)}时才被认为是*匹配的* {2} \\右\\ rfloor-1 \\)。将\\(s_1 \\)的每个字符与\\(s_2 \\)中的所有匹配字符进行比较。匹配(但不同的序列顺序)字符除以2的数量定义了*转置*的数量。 **示例**给定字符串\\(s_1 \\) *DWAYNE*和\\(s_2 \\) *DUANE*我们发现:
|
||||
|
||||
```
|
||||
- \\(m = 4 \\)
|
||||
- \\(| s_1 | = 6 \\)
|
||||
- \\(| s_2 | = 5 \\)
|
||||
- \\(t = 0 \\)
|
||||
|
||||
</section>
|
||||
我们发现Jaro得分为:\\(d_j = \\ frac {1} {3} \\ left(\\ frac {4} {6} + \\ frac {4} {5} + \\ frac {4-0} {4} \\ right)= 0.822 \\)。编写一个函数a,它接受两个字符串作为参数并返回相关的Jaro距离。
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
# --hints--
|
||||
|
||||
<div id='js-seed'>
|
||||
`jaro`应该是一个功能。
|
||||
|
||||
```js
|
||||
function jaro (s, t) {
|
||||
// Good luck!
|
||||
}
|
||||
|
||||
assert(typeof jaro == 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`jaro(""+tests[0][0]+"",""+tests[0][1]+"")`应返回一个数字。
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
assert(typeof jaro('MARTHA', 'MARHTA') == 'number');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`jaro(""+tests[0][0]+"",""+tests[0][1]+"")`应该返回`"+results[0]+"` 。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert.equal(jaro('MARTHA', 'MARHTA'), 0.9444444444444445);
|
||||
```
|
||||
|
||||
/section>
|
||||
`jaro(""+tests[1][0]+"",""+tests[1][1]+"")`应返回`"+results[1]+"` 。
|
||||
|
||||
```js
|
||||
assert.equal(jaro('DIXON', 'DICKSONX'), 0.7666666666666666);
|
||||
```
|
||||
|
||||
`jaro(""+tests[2][0]+"",""+tests[2][1]+"")`应返回`"+results[2]+"` 。
|
||||
|
||||
```js
|
||||
assert.equal(jaro('JELLYFISH', 'SMELLYFISH'), 0.8962962962962964);
|
||||
```
|
||||
|
||||
`jaro(""+tests[3][0]+"",""+tests[3][1]+"")`应返回`"+results[3]+"` 。
|
||||
|
||||
```js
|
||||
assert.equal(jaro('HELLOS', 'CHELLO'), 0.888888888888889);
|
||||
```
|
||||
|
||||
`jaro(""+tests[4][0]+"",""+tests[4][1]+"")`应该返回`"+results[4]+"` 。
|
||||
|
||||
```js
|
||||
assert.equal(jaro('ABCD', 'BCDA'), 0.8333333333333334);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,74 +1,63 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc7ec4
|
||||
title: JortSort
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: JortSort
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"> jortSort是一个排序工具集,它使用户可以完成工作并保证效率,因为您不必再次排序。它最初是由着名的<a href="https://www.youtube.com/watch?v=pj4U_W0OFoE">JSConf的</a> Jenn“Moneydollars”Schiffer <a href="https://www.youtube.com/watch?v=pj4U_W0OFoE">提出的</a> 。 jortSort是一个函数,它将一个可比较对象数组作为其参数。然后它按升序对数组进行排序,并将排序后的数组与最初提供的数组进行比较。如果数组匹配(即原始数组已经排序),则该函数返回true。如果数组不匹配(即原始数组未排序),则该函数返回false。 </section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
jortSort是一个排序工具集,它使用户可以完成工作并保证效率,因为您不必再次排序。它最初是由着名的[JSConf的](https://www.youtube.com/watch?v=pj4U_W0OFoE) Jenn“Moneydollars”Schiffer [提出的](https://www.youtube.com/watch?v=pj4U_W0OFoE) 。 jortSort是一个函数,它将一个可比较对象数组作为其参数。然后它按升序对数组进行排序,并将排序后的数组与最初提供的数组进行比较。如果数组匹配(即原始数组已经排序),则该函数返回true。如果数组不匹配(即原始数组未排序),则该函数返回false。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>jortsort</code>应该是一个功能。
|
||||
testString: assert(typeof jortsort=='function');
|
||||
- text: '<code>jortsort("+JSON.stringify(tests[0])+")</code>应该返回一个布尔值。'
|
||||
testString: assert(typeof jortsort([1,2,3,4,5])=='boolean');
|
||||
- text: '<code>jortsort("+JSON.stringify(tests[0])+")</code>应该返回<code>true</code> 。'
|
||||
testString: assert.equal(jortsort([1,2,3,4,5]),true);
|
||||
- text: '<code>jortsort("+JSON.stringify(tests[1])+")</code>应该返回<code>false</code> 。'
|
||||
testString: assert.equal(jortsort([1,2,13,4,5]),false);
|
||||
- text: '<code>jortsort("+JSON.stringify(tests[2])+")</code>应该返回<code>false</code> 。'
|
||||
testString: assert.equal(jortsort([12,4,51,2,4]),false);
|
||||
- text: '<code>jortsort("+JSON.stringify(tests[3])+")</code>应该返回<code>true</code> 。'
|
||||
testString: assert.equal(jortsort([1,2]),true);
|
||||
- text: '<code>jortsort("+JSON.stringify(tests[4])+")</code>应该返回<code>false</code> 。'
|
||||
testString: assert.equal(jortsort([5,4,3,2,1]),false);
|
||||
- text: '<code>jortsort("+JSON.stringify(tests[5])+")</code>应该返回<code>true</code> 。'
|
||||
testString: assert.equal(jortsort([1,1,1,1,1]),true);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`jortsort`应该是一个功能。
|
||||
|
||||
```js
|
||||
function jortsort (array) {
|
||||
// Good luck!
|
||||
}
|
||||
|
||||
assert(typeof jortsort == 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`jortsort("+JSON.stringify(tests[0])+")`应该返回一个布尔值。
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
assert(typeof jortsort([1, 2, 3, 4, 5]) == 'boolean');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`jortsort("+JSON.stringify(tests[0])+")`应该返回`true` 。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert.equal(jortsort([1, 2, 3, 4, 5]), true);
|
||||
```
|
||||
|
||||
/section>
|
||||
`jortsort("+JSON.stringify(tests[1])+")`应该返回`false` 。
|
||||
|
||||
```js
|
||||
assert.equal(jortsort([1, 2, 13, 4, 5]), false);
|
||||
```
|
||||
|
||||
`jortsort("+JSON.stringify(tests[2])+")`应该返回`false` 。
|
||||
|
||||
```js
|
||||
assert.equal(jortsort([12, 4, 51, 2, 4]), false);
|
||||
```
|
||||
|
||||
`jortsort("+JSON.stringify(tests[3])+")`应该返回`true` 。
|
||||
|
||||
```js
|
||||
assert.equal(jortsort([1, 2]), true);
|
||||
```
|
||||
|
||||
`jortsort("+JSON.stringify(tests[4])+")`应该返回`false` 。
|
||||
|
||||
```js
|
||||
assert.equal(jortsort([5, 4, 3, 2, 1]), false);
|
||||
```
|
||||
|
||||
`jortsort("+JSON.stringify(tests[5])+")`应该返回`true` 。
|
||||
|
||||
```js
|
||||
assert.equal(jortsort([1, 1, 1, 1, 1]), true);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,64 +1,57 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc7ec5
|
||||
title: 约瑟夫斯问题
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 约瑟夫斯问题
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"> <a href="https://en.wikipedia.org/wiki/Josephus problem">约瑟夫斯问题</a>是一个严峻描述的数学难题:$ n $囚犯站在一个圆圈上,顺序编号从$ 0 $到$ n-1 $。一名刽子手沿着圈子走,从囚犯$ 0 $开始,移走每个$ k $囚犯并杀死他。随着过程的继续,圆圈变得越来越小,直到只剩下一个囚犯,然后被释放。例如,如果$ n = 5 $囚犯和$ k = 2 $,囚犯被杀的命令(我们称之为“杀戮序列”)将是1,3,0和4,幸存者将是#2。鉴于任何<big>$ n,k> 0 $</big> ,找出哪个囚犯将成为最后的幸存者。在一个这样的事件中,有41个囚犯和每3 <sup>次</sup>囚犯被杀死<big>($ K = 3 $)。</big>其中有一个聪明的名字约瑟夫斯,他解决了这个问题,站在幸存的位置,并继续讲述这个故事。他是哪个号码?写一个函数,以囚犯的初始数量和'k'作为参数,并返回幸存的囚犯的数量。 </section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
[约瑟夫斯问题](<https://en.wikipedia.org/wiki/Josephus problem>)是一个严峻描述的数学难题:$ n $囚犯站在一个圆圈上,顺序编号从$ 0 $到$ n-1 $。一名刽子手沿着圈子走,从囚犯$ 0 $开始,移走每个$ k $囚犯并杀死他。随着过程的继续,圆圈变得越来越小,直到只剩下一个囚犯,然后被释放。例如,如果$ n = 5 $囚犯和$ k = 2 $,囚犯被杀的命令(我们称之为“杀戮序列”)将是1,3,0和4,幸存者将是#2。鉴于任何$ n,k> 0 $ ,找出哪个囚犯将成为最后的幸存者。在一个这样的事件中,有41个囚犯和每3 <sup>次</sup>囚犯被杀死($ K = 3 $)。其中有一个聪明的名字约瑟夫斯,他解决了这个问题,站在幸存的位置,并继续讲述这个故事。他是哪个号码?写一个函数,以囚犯的初始数量和'k'作为参数,并返回幸存的囚犯的数量。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>josephus</code>应该是一个功能。
|
||||
testString: assert(typeof josephus=='function');
|
||||
- text: '<code>josephus(30,3)</code>应该返回一个数字。'
|
||||
testString: assert(typeof josephus(30,3)=='number');
|
||||
- text: '<code>josephus(30,3)</code>应该回<code>29</code> 。'
|
||||
testString: assert.equal(josephus(30,3),29);
|
||||
- text: '<code>josephus(30,5)</code>应该返回<code>3</code> 。'
|
||||
testString: assert.equal(josephus(30,5),3);
|
||||
- text: '<code>josephus(20,2)</code>应该返回<code>9</code> 。'
|
||||
testString: assert.equal(josephus(20,2),9);
|
||||
- text: '<code>josephus(17,6)</code>应该回归<code>2</code> 。'
|
||||
testString: assert.equal(josephus(17,6),2);
|
||||
- text: '<code>josephus(29,4)</code>应该返回<code>2</code> 。'
|
||||
testString: assert.equal(josephus(29,4),2);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`josephus`应该是一个功能。
|
||||
|
||||
```js
|
||||
function josephus (init, kill) {
|
||||
// Good luck!
|
||||
}
|
||||
|
||||
assert(typeof josephus == 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`josephus(30,3)`应该返回一个数字。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert(typeof josephus(30, 3) == 'number');
|
||||
```
|
||||
|
||||
/section>
|
||||
`josephus(30,3)`应该回`29` 。
|
||||
|
||||
```js
|
||||
assert.equal(josephus(30, 3), 29);
|
||||
```
|
||||
|
||||
`josephus(30,5)`应该返回`3` 。
|
||||
|
||||
```js
|
||||
assert.equal(josephus(30, 5), 3);
|
||||
```
|
||||
|
||||
`josephus(20,2)`应该返回`9` 。
|
||||
|
||||
```js
|
||||
assert.equal(josephus(20, 2), 9);
|
||||
```
|
||||
|
||||
`josephus(17,6)`应该回归`2` 。
|
||||
|
||||
```js
|
||||
assert.equal(josephus(17, 6), 2);
|
||||
```
|
||||
|
||||
`josephus(29,4)`应该返回`2` 。
|
||||
|
||||
```js
|
||||
assert.equal(josephus(29, 4), 2);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -0,0 +1,468 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc7ecb
|
||||
title: K-d tree
|
||||
challengeType: 5
|
||||
forumTopicId: 302295
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
A k-d tree (short for *k*-dimensional tree) is a space-partitioning data structure for organizing points in a k-dimensional space. k-d trees are a useful data structure for several applications, such as searches involving a multidimensional search key (e.g. range searches and nearest neighbor searches). k-d trees are a special case of binary space partitioning trees. k-d trees are not suitable, however, for efficiently finding the nearest neighbor in high dimensional spaces. As a general rule, if the dimensionality is *k*, the number of points in the data, *N*, should be *N* ≫ 2<sup><i>k</i></sup>. Otherwise, when k-d trees are used with high-dimensional data, most of the points in the tree will be evaluated and the efficiency is no better than exhaustive search, and other methods such as approximate nearest-neighbor are used instead.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Write a function to perform a nearest neighbour search using k-d tree. The function takes two parameters: an array of k-dimensional points, and a single k-dimensional point whose nearest neighbour should be returned by the function. A k-dimensional point will be given as an array of k elements.
|
||||
|
||||
# --hints--
|
||||
|
||||
`kdNN` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof kdNN == 'function');
|
||||
```
|
||||
|
||||
`kdNN([[[2, 3], [5, 4], [9, 6], [4, 7], [8, 1], [7, 2]], [9, 2])` should return an array.
|
||||
|
||||
```js
|
||||
assert(
|
||||
Array.isArray(
|
||||
kdNN(
|
||||
[
|
||||
[2, 3],
|
||||
[5, 4],
|
||||
[9, 6],
|
||||
[4, 7],
|
||||
[8, 1],
|
||||
[7, 2]
|
||||
],
|
||||
[9, 2]
|
||||
)
|
||||
)
|
||||
);
|
||||
```
|
||||
|
||||
`kdNN([[[2, 3], [5, 4], [9, 6], [4, 7], [8, 1], [7, 2]], [9, 2])` should return `[ 8, 1 ]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
kdNN(
|
||||
[
|
||||
[2, 3],
|
||||
[5, 4],
|
||||
[9, 6],
|
||||
[4, 7],
|
||||
[8, 1],
|
||||
[7, 2]
|
||||
],
|
||||
[9, 2]
|
||||
),
|
||||
[8, 1]
|
||||
);
|
||||
```
|
||||
|
||||
`kdNN([[[2, 3], [5, 4], [9, 6], [4, 7], [8, 1], [7, 2]], [7, 1])` should return `[ 8, 1 ]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
kdNN(
|
||||
[
|
||||
[2, 3],
|
||||
[5, 4],
|
||||
[9, 6],
|
||||
[4, 7],
|
||||
[8, 1],
|
||||
[7, 2]
|
||||
],
|
||||
[7, 1]
|
||||
),
|
||||
[8, 1]
|
||||
);
|
||||
```
|
||||
|
||||
`kdNN([[[2, 3], [5, 4], [9, 6], [4, 7], [8, 1], [7, 2]], [3, 2])` should return `[ 2, 3 ]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
kdNN(
|
||||
[
|
||||
[2, 3],
|
||||
[5, 4],
|
||||
[9, 6],
|
||||
[4, 7],
|
||||
[8, 1],
|
||||
[7, 2]
|
||||
],
|
||||
[3, 2]
|
||||
),
|
||||
[2, 3]
|
||||
);
|
||||
```
|
||||
|
||||
`kdNN([[2, 3, 1], [9, 4, 5], [4, 6, 7], [1, 2, 5], [7, 8, 9], [3, 6, 1]], [1, 2, 3])` should return `[ 1, 2, 5 ]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
kdNN(
|
||||
[
|
||||
[2, 3, 1],
|
||||
[9, 4, 5],
|
||||
[4, 6, 7],
|
||||
[1, 2, 5],
|
||||
[7, 8, 9],
|
||||
[3, 6, 1]
|
||||
],
|
||||
[1, 2, 3]
|
||||
),
|
||||
[1, 2, 5]
|
||||
);
|
||||
```
|
||||
|
||||
`kdNN([[2, 3, 1], [9, 4, 5], [4, 6, 7], [1, 2, 5], [7, 8, 9], [3, 6, 1]], [4, 5, 6])` should return `[ 4, 6, 7 ]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
kdNN(
|
||||
[
|
||||
[2, 3, 1],
|
||||
[9, 4, 5],
|
||||
[4, 6, 7],
|
||||
[1, 2, 5],
|
||||
[7, 8, 9],
|
||||
[3, 6, 1]
|
||||
],
|
||||
[4, 5, 6]
|
||||
),
|
||||
[4, 6, 7]
|
||||
);
|
||||
```
|
||||
|
||||
`kdNN([[2, 3, 1], [9, 4, 5], [4, 6, 7], [1, 2, 5], [7, 8, 9], [3, 6, 1]], [8, 8, 8])` should return `[ 7, 8, 9 ]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
kdNN(
|
||||
[
|
||||
[2, 3, 1],
|
||||
[9, 4, 5],
|
||||
[4, 6, 7],
|
||||
[1, 2, 5],
|
||||
[7, 8, 9],
|
||||
[3, 6, 1]
|
||||
],
|
||||
[8, 8, 8]
|
||||
),
|
||||
[7, 8, 9]
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function kdNN(fpoints, fpoint) {
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function kdNN(fpoints, fpoint) {
|
||||
function Node(obj, dimension, parent) {
|
||||
this.obj = obj;
|
||||
this.left = null;
|
||||
this.right = null;
|
||||
this.parent = parent;
|
||||
this.dimension = dimension;
|
||||
}
|
||||
|
||||
function kdTree(points, metric, dimensions) {
|
||||
var self = this;
|
||||
|
||||
function buildTree(points, depth, parent) {
|
||||
var dim = depth % dimensions.length,
|
||||
median,
|
||||
node;
|
||||
|
||||
if (points.length === 0) {
|
||||
return null;
|
||||
}
|
||||
if (points.length === 1) {
|
||||
return new Node(points[0], dim, parent);
|
||||
}
|
||||
|
||||
points.sort(function(a, b) {
|
||||
return a[dimensions[dim]] - b[dimensions[dim]];
|
||||
});
|
||||
|
||||
median = Math.floor(points.length / 2);
|
||||
node = new Node(points[median], dim, parent);
|
||||
node.left = buildTree(points.slice(0, median), depth + 1, node);
|
||||
node.right = buildTree(points.slice(median + 1), depth + 1, node);
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
this.root = buildTree(points, 0, null);
|
||||
|
||||
this.insert = function(point) {
|
||||
function innerSearch(node, parent) {
|
||||
if (node === null) {
|
||||
return parent;
|
||||
}
|
||||
|
||||
var dimension = dimensions[node.dimension];
|
||||
if (point[dimension] < node.obj[dimension]) {
|
||||
return innerSearch(node.left, node);
|
||||
} else {
|
||||
return innerSearch(node.right, node);
|
||||
}
|
||||
}
|
||||
|
||||
var insertPosition = innerSearch(this.root, null),
|
||||
newNode,
|
||||
dimension;
|
||||
|
||||
if (insertPosition === null) {
|
||||
this.root = new Node(point, 0, null);
|
||||
return;
|
||||
}
|
||||
|
||||
newNode = new Node(
|
||||
point,
|
||||
(insertPosition.dimension + 1) % dimensions.length,
|
||||
insertPosition
|
||||
);
|
||||
dimension = dimensions[insertPosition.dimension];
|
||||
|
||||
if (point[dimension] < insertPosition.obj[dimension]) {
|
||||
insertPosition.left = newNode;
|
||||
} else {
|
||||
insertPosition.right = newNode;
|
||||
}
|
||||
};
|
||||
|
||||
this.nearest = function(point, maxNodes, maxDistance) {
|
||||
var i, result, bestNodes;
|
||||
|
||||
bestNodes = new BinaryHeap(function(e) {
|
||||
return -e[1];
|
||||
});
|
||||
|
||||
function nearestSearch(node) {
|
||||
var bestChild,
|
||||
dimension = dimensions[node.dimension],
|
||||
ownDistance = metric(point, node.obj),
|
||||
linearPoint = {},
|
||||
linearDistance,
|
||||
otherChild,
|
||||
i;
|
||||
|
||||
function saveNode(node, distance) {
|
||||
bestNodes.push([node, distance]);
|
||||
if (bestNodes.size() > maxNodes) {
|
||||
bestNodes.pop();
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < dimensions.length; i += 1) {
|
||||
if (i === node.dimension) {
|
||||
linearPoint[dimensions[i]] = point[dimensions[i]];
|
||||
} else {
|
||||
linearPoint[dimensions[i]] = node.obj[dimensions[i]];
|
||||
}
|
||||
}
|
||||
|
||||
linearDistance = metric(linearPoint, node.obj);
|
||||
|
||||
if (node.right === null && node.left === null) {
|
||||
if (
|
||||
bestNodes.size() < maxNodes ||
|
||||
ownDistance < bestNodes.peek()[1]
|
||||
) {
|
||||
saveNode(node, ownDistance);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (node.right === null) {
|
||||
bestChild = node.left;
|
||||
} else if (node.left === null) {
|
||||
bestChild = node.right;
|
||||
} else {
|
||||
if (point[dimension] < node.obj[dimension]) {
|
||||
bestChild = node.left;
|
||||
} else {
|
||||
bestChild = node.right;
|
||||
}
|
||||
}
|
||||
|
||||
nearestSearch(bestChild);
|
||||
|
||||
if (bestNodes.size() < maxNodes || ownDistance < bestNodes.peek()[1]) {
|
||||
saveNode(node, ownDistance);
|
||||
}
|
||||
|
||||
if (
|
||||
bestNodes.size() < maxNodes ||
|
||||
Math.abs(linearDistance) < bestNodes.peek()[1]
|
||||
) {
|
||||
if (bestChild === node.left) {
|
||||
otherChild = node.right;
|
||||
} else {
|
||||
otherChild = node.left;
|
||||
}
|
||||
if (otherChild !== null) {
|
||||
nearestSearch(otherChild);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (maxDistance) {
|
||||
for (i = 0; i < maxNodes; i += 1) {
|
||||
bestNodes.push([null, maxDistance]);
|
||||
}
|
||||
}
|
||||
|
||||
if (self.root) nearestSearch(self.root);
|
||||
|
||||
result = [];
|
||||
|
||||
for (i = 0; i < Math.min(maxNodes, bestNodes.content.length); i += 1) {
|
||||
if (bestNodes.content[i][0]) {
|
||||
result.push([bestNodes.content[i][0].obj, bestNodes.content[i][1]]);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
};
|
||||
}
|
||||
|
||||
function BinaryHeap(scoreFunction) {
|
||||
this.content = [];
|
||||
this.scoreFunction = scoreFunction;
|
||||
}
|
||||
|
||||
BinaryHeap.prototype = {
|
||||
push: function(element) {
|
||||
// Add the new element to the end of the array.
|
||||
this.content.push(element);
|
||||
// Allow it to bubble up.
|
||||
this.bubbleUp(this.content.length - 1);
|
||||
},
|
||||
|
||||
pop: function() {
|
||||
// Store the first element so we can return it later.
|
||||
var result = this.content[0];
|
||||
// Get the element at the end of the array.
|
||||
var end = this.content.pop();
|
||||
// If there are any elements left, put the end element at the
|
||||
// start, and let it sink down.
|
||||
if (this.content.length > 0) {
|
||||
this.content[0] = end;
|
||||
this.sinkDown(0);
|
||||
}
|
||||
return result;
|
||||
},
|
||||
|
||||
peek: function() {
|
||||
return this.content[0];
|
||||
},
|
||||
|
||||
size: function() {
|
||||
return this.content.length;
|
||||
},
|
||||
|
||||
bubbleUp: function(n) {
|
||||
// Fetch the element that has to be moved.
|
||||
var element = this.content[n];
|
||||
// When at 0, an element can not go up any further.
|
||||
while (n > 0) {
|
||||
// Compute the parent element's index, and fetch it.
|
||||
var parentN = Math.floor((n + 1) / 2) - 1,
|
||||
parent = this.content[parentN];
|
||||
// Swap the elements if the parent is greater.
|
||||
if (this.scoreFunction(element) < this.scoreFunction(parent)) {
|
||||
this.content[parentN] = element;
|
||||
this.content[n] = parent;
|
||||
// Update 'n' to continue at the new position.
|
||||
n = parentN;
|
||||
}
|
||||
// Found a parent that is less, no need to move it further.
|
||||
else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
sinkDown: function(n) {
|
||||
// Look up the target element and its score.
|
||||
var length = this.content.length,
|
||||
element = this.content[n],
|
||||
elemScore = this.scoreFunction(element);
|
||||
|
||||
while (true) {
|
||||
// Compute the indices of the child elements.
|
||||
var child2N = (n + 1) * 2,
|
||||
child1N = child2N - 1;
|
||||
// This is used to store the new position of the element,
|
||||
// if any.
|
||||
var swap = null;
|
||||
// If the first child exists (is inside the array)...
|
||||
if (child1N < length) {
|
||||
// Look it up and compute its score.
|
||||
var child1 = this.content[child1N],
|
||||
child1Score = this.scoreFunction(child1);
|
||||
// If the score is less than our element's, we need to swap.
|
||||
if (child1Score < elemScore) swap = child1N;
|
||||
}
|
||||
// Do the same checks for the other child.
|
||||
if (child2N < length) {
|
||||
var child2 = this.content[child2N],
|
||||
child2Score = this.scoreFunction(child2);
|
||||
if (child2Score < (swap == null ? elemScore : child1Score)) {
|
||||
swap = child2N;
|
||||
}
|
||||
}
|
||||
|
||||
// If the element needs to be moved, swap it, and continue.
|
||||
if (swap != null) {
|
||||
this.content[n] = this.content[swap];
|
||||
this.content[swap] = element;
|
||||
n = swap;
|
||||
}
|
||||
// Otherwise, we are done.
|
||||
else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var dims = [];
|
||||
|
||||
for (var i = 0; i < fpoint.length; i++) dims.push(i);
|
||||
|
||||
var tree = new kdTree(
|
||||
fpoints,
|
||||
function(e1, e2) {
|
||||
var d = 0;
|
||||
var e3 = e1;
|
||||
if (!Array.isArray(e1)) {
|
||||
e3 = [];
|
||||
for (var key in e1) e3.push(e1[key]);
|
||||
|
||||
e1 = e3;
|
||||
}
|
||||
e1.forEach(function(e, i) {
|
||||
var sqd = e1[i] - e2[i];
|
||||
d += sqd * sqd;
|
||||
});
|
||||
return d;
|
||||
},
|
||||
dims
|
||||
);
|
||||
|
||||
return tree.nearest(fpoint, 1, 1000)[0][0];
|
||||
}
|
||||
```
|
@ -0,0 +1,109 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc7eca
|
||||
title: Kaprekar numbers
|
||||
challengeType: 5
|
||||
forumTopicId: 302296
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
A positive integer is a [Kaprekar number](<https://en.wikipedia.org/wiki/Kaprekar number>) if:
|
||||
|
||||
<ul>
|
||||
<li>It is 1, or,</li>
|
||||
<li>The decimal representation of its square may be split once into two parts consisting of positive integers which sum to the original number. </li>
|
||||
</ul>
|
||||
|
||||
Note that a split resulting in a part consisting purely of 0s is not valid, as 0 is not considered positive.Example
|
||||
|
||||
Kaprekar numbers:
|
||||
|
||||
<ul>
|
||||
<li><code>2223</code> is a Kaprekar number, as <code>2223 * 2223 = 4941729</code>, <code>4941729</code> may be split to <code>494</code> and <code>1729</code>, and <code>494 + 1729 = 2223</code></li>
|
||||
<li>The series of Kaprekar numbers is known as <a href='https://oeis.org/A006886' target='_blank'>A006886</a>, and begins as <code>1, 9, 45, 55, ...</code></li>
|
||||
</ul>
|
||||
|
||||
# --instructions--
|
||||
|
||||
Write a function that takes a number $n$, a base $bs$, and returns true if the number is a Kaprekar number for the given base. Otherwise, the function returns false.
|
||||
|
||||
# --hints--
|
||||
|
||||
`isKaprekar` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof isKaprekar == 'function');
|
||||
```
|
||||
|
||||
`isKaprekar(1, 10)` should return a boolean.
|
||||
|
||||
```js
|
||||
assert(typeof isKaprekar(1, 10) == 'boolean');
|
||||
```
|
||||
|
||||
`isKaprekar(1, 10)` should return `true`.
|
||||
|
||||
```js
|
||||
assert.equal(isKaprekar(1, 10), true);
|
||||
```
|
||||
|
||||
`isKaprekar(9, 10)` should return `true`.
|
||||
|
||||
```js
|
||||
assert.equal(isKaprekar(9, 10), true);
|
||||
```
|
||||
|
||||
`isKaprekar(2223, 10)` should return `true`.
|
||||
|
||||
```js
|
||||
assert.equal(isKaprekar(2223, 10), true);
|
||||
```
|
||||
|
||||
`isKaprekar(22823, 10)` should return `false`.
|
||||
|
||||
```js
|
||||
assert.equal(isKaprekar(22823, 10), false);
|
||||
```
|
||||
|
||||
`isKaprekar(9, 17)` should return `false`.
|
||||
|
||||
```js
|
||||
assert.equal(isKaprekar(9, 17), false);
|
||||
```
|
||||
|
||||
`isKaprekar(225, 17)` should return `true`.
|
||||
|
||||
```js
|
||||
assert.equal(isKaprekar(225, 17), true);
|
||||
```
|
||||
|
||||
`isKaprekar(999, 17)` should return `false`.
|
||||
|
||||
```js
|
||||
assert.equal(isKaprekar(999, 17), false);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function isKaprekar(n, bs) {
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function isKaprekar(n, bs) {
|
||||
if (n < 1) return false;
|
||||
if (n == 1) return true;
|
||||
for (var a = n * n, b = 0, s = 1; a; s *= bs) {
|
||||
b += (a % bs) * s;
|
||||
a = Math.floor(a / bs);
|
||||
if (b && a + b == n) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
```
|
@ -0,0 +1,254 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc7ed1
|
||||
title: Knapsack problem/0-1
|
||||
challengeType: 5
|
||||
forumTopicId: 323649
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
The 0-1 knapsack problem is defined as follows:
|
||||
|
||||
You are given an array of objects representing items to be put in a knapsack. The objects have 3 attributes: name, weight, and value. The items need to be selected so that the total weight does not exceed the maximum weight and the value is maximized.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Write a function to solve the knapsack problem. The function is given the array of objects and the maximum weight as parameters. It should return the maximum total value possible.
|
||||
|
||||
# --hints--
|
||||
|
||||
`knapsack([{ name:'map', weight:9, value:150 }, { name:'compass', weight:13, value:35 }, { name:'water', weight:153, value:200 }, { name:'sandwich', weight:50, value:160 }, { name:'glucose', weight:15, value:60 }, { name:'tin', weight:68, value:45 }, { name:'banana', weight:27, value:60 }, { name:'apple', weight:39, value:40 }], 100)` should return `405`.
|
||||
|
||||
```js
|
||||
assert.equal(
|
||||
knapsack(
|
||||
[
|
||||
{ name: 'map', weight: 9, value: 150 },
|
||||
{ name: 'compass', weight: 13, value: 35 },
|
||||
{ name: 'water', weight: 153, value: 200 },
|
||||
{ name: 'sandwich', weight: 50, value: 160 },
|
||||
{ name: 'glucose', weight: 15, value: 60 },
|
||||
{ name: 'tin', weight: 68, value: 45 },
|
||||
{ name: 'banana', weight: 27, value: 60 },
|
||||
{ name: 'apple', weight: 39, value: 40 }
|
||||
],
|
||||
100
|
||||
),
|
||||
405
|
||||
);
|
||||
```
|
||||
|
||||
`knapsack([{ name:'map', weight:9, value:150 }, { name:'compass', weight:13, value:35 }, { name:'water', weight:153, value:200 }, { name:'sandwich', weight:50, value:160 }, { name:'glucose', weight:15, value:60 }, { name:'tin', weight:68, value:45 }, { name:'banana', weight:27, value:60 }, { name:'apple', weight:39, value:40 }], 200)` should return `510`.
|
||||
|
||||
```js
|
||||
assert.equal(
|
||||
knapsack(
|
||||
[
|
||||
{ name: 'map', weight: 9, value: 150 },
|
||||
{ name: 'compass', weight: 13, value: 35 },
|
||||
{ name: 'water', weight: 153, value: 200 },
|
||||
{ name: 'sandwich', weight: 50, value: 160 },
|
||||
{ name: 'glucose', weight: 15, value: 60 },
|
||||
{ name: 'tin', weight: 68, value: 45 },
|
||||
{ name: 'banana', weight: 27, value: 60 },
|
||||
{ name: 'apple', weight: 39, value: 40 }
|
||||
],
|
||||
200
|
||||
),
|
||||
510
|
||||
);
|
||||
```
|
||||
|
||||
`knapsack([{ name:'cheese', weight:23, value:30 }, { name:'beer', weight:52, value:10 }, { name:'suntan cream', weight:11, value:70 }, { name:'camera', weight:32, value:30 }, { name:'T-shirt', weight:24, value:15 }, { name:'trousers', weight:48, value:10 }, { name:'umbrella', weight:73, value:40 }], 100)` should return `145`.
|
||||
|
||||
```js
|
||||
assert.equal(
|
||||
knapsack(
|
||||
[
|
||||
{ name: 'cheese', weight: 23, value: 30 },
|
||||
{ name: 'beer', weight: 52, value: 10 },
|
||||
{ name: 'suntan cream', weight: 11, value: 70 },
|
||||
{ name: 'camera', weight: 32, value: 30 },
|
||||
{ name: 'T-shirt', weight: 24, value: 15 },
|
||||
{ name: 'trousers', weight: 48, value: 10 },
|
||||
{ name: 'umbrella', weight: 73, value: 40 }
|
||||
],
|
||||
100
|
||||
),
|
||||
145
|
||||
);
|
||||
```
|
||||
|
||||
`knapsack([{ name:'cheese', weight:23, value:30 }, { name:'beer', weight:52, value:10 }, { name:'suntan cream', weight:11, value:70 }, { name:'camera', weight:32, value:30 }, { name:'T-shirt', weight:24, value:15 }, { name:'trousers', weight:48, value:10 }, { name:'umbrella', weight:73, value:40 }], 200)` should return `185`.
|
||||
|
||||
```js
|
||||
assert.equal(
|
||||
knapsack(
|
||||
[
|
||||
{ name: 'cheese', weight: 23, value: 30 },
|
||||
{ name: 'beer', weight: 52, value: 10 },
|
||||
{ name: 'suntan cream', weight: 11, value: 70 },
|
||||
{ name: 'camera', weight: 32, value: 30 },
|
||||
{ name: 'T-shirt', weight: 24, value: 15 },
|
||||
{ name: 'trousers', weight: 48, value: 10 },
|
||||
{ name: 'umbrella', weight: 73, value: 40 }
|
||||
],
|
||||
200
|
||||
),
|
||||
185
|
||||
);
|
||||
```
|
||||
|
||||
`knapsack([{ name:'waterproof trousers', weight:42, value:70 }, { name:'waterproof overclothes', weight:43, value:75 }, { name:'note-case', weight:22, value:80 }, { name:'sunglasses', weight:7, value:20 }, { name:'towel', weight:18, value:12 }, { name:'socks', weight:4, value:50 }, { name:'book', weight:30, value:10 }], 100)` should return `237`.
|
||||
|
||||
```js
|
||||
assert.equal(
|
||||
knapsack(
|
||||
[
|
||||
{ name: 'waterproof trousers', weight: 42, value: 70 },
|
||||
{ name: 'waterproof overclothes', weight: 43, value: 75 },
|
||||
{ name: 'note-case', weight: 22, value: 80 },
|
||||
{ name: 'sunglasses', weight: 7, value: 20 },
|
||||
{ name: 'towel', weight: 18, value: 12 },
|
||||
{ name: 'socks', weight: 4, value: 50 },
|
||||
{ name: 'book', weight: 30, value: 10 }
|
||||
],
|
||||
100
|
||||
),
|
||||
237
|
||||
);
|
||||
```
|
||||
|
||||
`knapsack([{ name:'waterproof trousers', weight:42, value:70 }, { name:'waterproof overclothes', weight:43, value:75 }, { name:'note-case', weight:22, value:80 }, { name:'sunglasses', weight:7, value:20 }, { name:'towel', weight:18, value:12 }, { name:'socks', weight:4, value:50 }, { name:'book', weight:30, value:10 }], 200)` should return `317`.'
|
||||
|
||||
```js
|
||||
assert.equal(
|
||||
knapsack(
|
||||
[
|
||||
{ name: 'waterproof trousers', weight: 42, value: 70 },
|
||||
{ name: 'waterproof overclothes', weight: 43, value: 75 },
|
||||
{ name: 'note-case', weight: 22, value: 80 },
|
||||
{ name: 'sunglasses', weight: 7, value: 20 },
|
||||
{ name: 'towel', weight: 18, value: 12 },
|
||||
{ name: 'socks', weight: 4, value: 50 },
|
||||
{ name: 'book', weight: 30, value: 10 }
|
||||
],
|
||||
200
|
||||
),
|
||||
317
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function knapsack(items, maxweight) {
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function knapsack(items, maxweight) {
|
||||
var _ = {
|
||||
max: function(e) {
|
||||
var mx = e[0];
|
||||
e.forEach(function(f) {
|
||||
if (mx < f) mx = f;
|
||||
});
|
||||
return mx;
|
||||
},
|
||||
map: function(array, func) {
|
||||
return array.map(func);
|
||||
},
|
||||
isUndefined: function(a) {
|
||||
if (a) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
},
|
||||
range: function(start, end, step) {
|
||||
var a = [];
|
||||
var f = (f = (i, end) => i < end);
|
||||
if (start > end) f = (i, end) => i > end;
|
||||
|
||||
for (var i = start; f(i, end); i += step) a.push(i);
|
||||
return a;
|
||||
}
|
||||
};
|
||||
|
||||
var valuefn = e => e.value;
|
||||
var weightfn = e => e.weight;
|
||||
var _epsilon = 0.01;
|
||||
var _p = _.max(_.map(items, valuefn));
|
||||
var _k = (_epsilon * _p) / items.length;
|
||||
|
||||
var _memo = (function() {
|
||||
var _mem = {};
|
||||
var _key = function(i, w) {
|
||||
return i + '::' + w;
|
||||
};
|
||||
return {
|
||||
get: function(i, w) {
|
||||
return _mem[_key(i, w)];
|
||||
},
|
||||
put: function(i, w, r) {
|
||||
_mem[_key(i, w)] = r;
|
||||
return r;
|
||||
}
|
||||
};
|
||||
})();
|
||||
|
||||
var _m = function(i, w) {
|
||||
i = Math.round(i);
|
||||
w = Math.round(w);
|
||||
|
||||
if (i < 0 || w === 0) {
|
||||
// empty base case
|
||||
return { items: [], totalWeight: 0, totalValue: 0 };
|
||||
}
|
||||
|
||||
var mm = _memo.get(i, w);
|
||||
if (!_.isUndefined(mm)) {
|
||||
return mm;
|
||||
}
|
||||
|
||||
var item = items[i];
|
||||
if (weightfn(item) > w) {
|
||||
//item does not fit, try the next item
|
||||
return _memo.put(i, w, _m(i - 1, w));
|
||||
}
|
||||
// this item could fit.
|
||||
// are we better off excluding it?
|
||||
var excluded = _m(i - 1, w);
|
||||
// or including it?
|
||||
var included = _m(i - 1, w - weightfn(item));
|
||||
if (
|
||||
included.totalValue + Math.floor(valuefn(item) / _k) >
|
||||
excluded.totalValue
|
||||
) {
|
||||
// better off including it
|
||||
// make a copy of the list
|
||||
var i1 = included.items.slice();
|
||||
i1.push(item);
|
||||
return _memo.put(i, w, {
|
||||
items: i1,
|
||||
totalWeight: included.totalWeight + weightfn(item),
|
||||
totalValue: included.totalValue + Math.floor(valuefn(item) / _k)
|
||||
});
|
||||
}
|
||||
//better off excluding it
|
||||
return _memo.put(i, w, excluded);
|
||||
};
|
||||
var scaled = _m(items.length - 1, maxweight);
|
||||
|
||||
var val = 0;
|
||||
scaled.items.forEach(function(e) {
|
||||
val += e.value;
|
||||
});
|
||||
return val;
|
||||
}
|
||||
```
|
@ -0,0 +1,211 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc7ed2
|
||||
title: Knapsack problem/Bounded
|
||||
challengeType: 5
|
||||
forumTopicId: 323652
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
The bounded knapsack problem is defined as follows:
|
||||
|
||||
You are given an array of objects representing items to be put in a knapsack. The objects have 4 attributes: name, pieces (the number of the particular item), weight, and value. The items need to be selected so that the total weight does not exceed the maximum weight and the value is maximized. Keep in mind that each item can appear between 0 and `pieces` times.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Write a function to solve the knapsack problem. The function is given the array of objects and the maximum weight as parameters. It should return the maximum total value possible.
|
||||
|
||||
# --hints--
|
||||
|
||||
`findBestPack([{ name:'map', weight:9, value:150, pieces:1 }, { name:'compass', weight:13, value:35, pieces:1 }, { name:'water', weight:153, value:200, pieces:2 }, { name:'sandwich', weight:50, value:60, pieces:2 }, { name:'glucose', weight:15, value:60, pieces:2 }, { name:'tin', weight:68, value:45, pieces:3 }, { name:'banana', weight:27, value:60, pieces:3 }, { name:'apple', weight:39, value:40, pieces:3 }, { name:'cheese', weight:23, value:30, pieces:1 }, { name:'beer', weight:52, value:10, pieces:3 }, { name:'suntan, cream', weight:11, value:70, pieces:1 }, { name:'camera', weight:32, value:30, pieces:1 }, { name:'T-shirt', weight:24, value:15, pieces:2 }], 300)` should return `755`.
|
||||
|
||||
```js
|
||||
assert.equal(
|
||||
findBestPack(
|
||||
[
|
||||
{ name: 'map', weight: 9, value: 150, pieces: 1 },
|
||||
{ name: 'compass', weight: 13, value: 35, pieces: 1 },
|
||||
{ name: 'water', weight: 153, value: 200, pieces: 2 },
|
||||
{ name: 'sandwich', weight: 50, value: 60, pieces: 2 },
|
||||
{ name: 'glucose', weight: 15, value: 60, pieces: 2 },
|
||||
{ name: 'tin', weight: 68, value: 45, pieces: 3 },
|
||||
{ name: 'banana', weight: 27, value: 60, pieces: 3 },
|
||||
{ name: 'apple', weight: 39, value: 40, pieces: 3 },
|
||||
{ name: 'cheese', weight: 23, value: 30, pieces: 1 },
|
||||
{ name: 'beer', weight: 52, value: 10, pieces: 3 },
|
||||
{ name: 'suntan, cream', weight: 11, value: 70, pieces: 1 },
|
||||
{ name: 'camera', weight: 32, value: 30, pieces: 1 },
|
||||
{ name: 'T-shirt', weight: 24, value: 15, pieces: 2 }
|
||||
],
|
||||
300
|
||||
),
|
||||
755
|
||||
);
|
||||
```
|
||||
|
||||
`findBestPack([{ name:'map', weight:9, value:150, pieces:1 }, { name:'compass', weight:13, value:35, pieces:1 }, { name:'water', weight:153, value:200, pieces:2 }, { name:'sandwich', weight:50, value:60, pieces:2 }, { name:'glucose', weight:15, value:60, pieces:2 }, { name:'tin', weight:68, value:45, pieces:3 }, { name:'banana', weight:27, value:60, pieces:3 }, { name:'apple', weight:39, value:40, pieces:3 }, { name:'cheese', weight:23, value:30, pieces:1 }, { name:'beer', weight:52, value:10, pieces:3 }, { name:'suntan, cream', weight:11, value:70, pieces:1 }, { name:'camera', weight:32, value:30, pieces:1 }, { name:'T-shirt', weight:24, value:15, pieces:2 }], 400)` should return `875`.
|
||||
|
||||
```js
|
||||
assert.equal(
|
||||
findBestPack(
|
||||
[
|
||||
{ name: 'map', weight: 9, value: 150, pieces: 1 },
|
||||
{ name: 'compass', weight: 13, value: 35, pieces: 1 },
|
||||
{ name: 'water', weight: 153, value: 200, pieces: 2 },
|
||||
{ name: 'sandwich', weight: 50, value: 60, pieces: 2 },
|
||||
{ name: 'glucose', weight: 15, value: 60, pieces: 2 },
|
||||
{ name: 'tin', weight: 68, value: 45, pieces: 3 },
|
||||
{ name: 'banana', weight: 27, value: 60, pieces: 3 },
|
||||
{ name: 'apple', weight: 39, value: 40, pieces: 3 },
|
||||
{ name: 'cheese', weight: 23, value: 30, pieces: 1 },
|
||||
{ name: 'beer', weight: 52, value: 10, pieces: 3 },
|
||||
{ name: 'suntan, cream', weight: 11, value: 70, pieces: 1 },
|
||||
{ name: 'camera', weight: 32, value: 30, pieces: 1 },
|
||||
{ name: 'T-shirt', weight: 24, value: 15, pieces: 2 }
|
||||
],
|
||||
400
|
||||
),
|
||||
875
|
||||
);
|
||||
```
|
||||
|
||||
`findBestPack([{ name:'map', weight:9, value:150, pieces:1 }, { name:'compass', weight:13, value:35, pieces:1 }, { name:'water', weight:153, value:200, pieces:2 }, { name:'sandwich', weight:50, value:60, pieces:2 }, { name:'glucose', weight:15, value:60, pieces:2 }, { name:'tin', weight:68, value:45, pieces:3 }, { name:'banana', weight:27, value:60, pieces:3 }, { name:'apple', weight:39, value:40, pieces:3 }, { name:'cheese', weight:23, value:30, pieces:1 }, { name:'beer', weight:52, value:10, pieces:3 }, { name:'suntan, cream', weight:11, value:70, pieces:1 }, { name:'camera', weight:32, value:30, pieces:1 }, { name:'T-shirt', weight:24, value:15, pieces:2 }], 500)` should return `1015`.
|
||||
|
||||
```js
|
||||
assert.equal(
|
||||
findBestPack(
|
||||
[
|
||||
{ name: 'map', weight: 9, value: 150, pieces: 1 },
|
||||
{ name: 'compass', weight: 13, value: 35, pieces: 1 },
|
||||
{ name: 'water', weight: 153, value: 200, pieces: 2 },
|
||||
{ name: 'sandwich', weight: 50, value: 60, pieces: 2 },
|
||||
{ name: 'glucose', weight: 15, value: 60, pieces: 2 },
|
||||
{ name: 'tin', weight: 68, value: 45, pieces: 3 },
|
||||
{ name: 'banana', weight: 27, value: 60, pieces: 3 },
|
||||
{ name: 'apple', weight: 39, value: 40, pieces: 3 },
|
||||
{ name: 'cheese', weight: 23, value: 30, pieces: 1 },
|
||||
{ name: 'beer', weight: 52, value: 10, pieces: 3 },
|
||||
{ name: 'suntan, cream', weight: 11, value: 70, pieces: 1 },
|
||||
{ name: 'camera', weight: 32, value: 30, pieces: 1 },
|
||||
{ name: 'T-shirt', weight: 24, value: 15, pieces: 2 }
|
||||
],
|
||||
500
|
||||
),
|
||||
1015
|
||||
);
|
||||
```
|
||||
|
||||
`findBestPack([{ name:'map', weight:9, value:150, pieces:1 }, { name:'compass', weight:13, value:35, pieces:1 }, { name:'water', weight:153, value:200, pieces:2 }, { name:'sandwich', weight:50, value:60, pieces:2 }, { name:'glucose', weight:15, value:60, pieces:2 }, { name:'tin', weight:68, value:45, pieces:3 }, { name:'banana', weight:27, value:60, pieces:3 }, { name:'apple', weight:39, value:40, pieces:3 }, { name:'cheese', weight:23, value:30, pieces:1 }, { name:'beer', weight:52, value:10, pieces:3 }, { name:'suntan, cream', weight:11, value:70, pieces:1 }, { name:'camera', weight:32, value:30, pieces:1 }, { name:'T-shirt', weight:24, value:15, pieces:2 }], 600)` should return `1120`.
|
||||
|
||||
```js
|
||||
assert.equal(
|
||||
findBestPack(
|
||||
[
|
||||
{ name: 'map', weight: 9, value: 150, pieces: 1 },
|
||||
{ name: 'compass', weight: 13, value: 35, pieces: 1 },
|
||||
{ name: 'water', weight: 153, value: 200, pieces: 2 },
|
||||
{ name: 'sandwich', weight: 50, value: 60, pieces: 2 },
|
||||
{ name: 'glucose', weight: 15, value: 60, pieces: 2 },
|
||||
{ name: 'tin', weight: 68, value: 45, pieces: 3 },
|
||||
{ name: 'banana', weight: 27, value: 60, pieces: 3 },
|
||||
{ name: 'apple', weight: 39, value: 40, pieces: 3 },
|
||||
{ name: 'cheese', weight: 23, value: 30, pieces: 1 },
|
||||
{ name: 'beer', weight: 52, value: 10, pieces: 3 },
|
||||
{ name: 'suntan, cream', weight: 11, value: 70, pieces: 1 },
|
||||
{ name: 'camera', weight: 32, value: 30, pieces: 1 },
|
||||
{ name: 'T-shirt', weight: 24, value: 15, pieces: 2 }
|
||||
],
|
||||
600
|
||||
),
|
||||
1120
|
||||
);
|
||||
```
|
||||
|
||||
`findBestPack([{ name:'map', weight:9, value:150, pieces:1 }, { name:'compass', weight:13, value:35, pieces:1 }, { name:'water', weight:153, value:200, pieces:2 }, { name:'sandwich', weight:50, value:60, pieces:2 }, { name:'glucose', weight:15, value:60, pieces:2 }, { name:'tin', weight:68, value:45, pieces:3 }, { name:'banana', weight:27, value:60, pieces:3 }, { name:'apple', weight:39, value:40, pieces:3 }, { name:'cheese', weight:23, value:30, pieces:1 }, { name:'beer', weight:52, value:10, pieces:3 }, { name:'suntan, cream', weight:11, value:70, pieces:1 }, { name:'camera', weight:32, value:30, pieces:1 }, { name:'T-shirt', weight:24, value:15, pieces:2 }], 700)` should return `1225`.
|
||||
|
||||
```js
|
||||
assert.equal(
|
||||
findBestPack(
|
||||
[
|
||||
{ name: 'map', weight: 9, value: 150, pieces: 1 },
|
||||
{ name: 'compass', weight: 13, value: 35, pieces: 1 },
|
||||
{ name: 'water', weight: 153, value: 200, pieces: 2 },
|
||||
{ name: 'sandwich', weight: 50, value: 60, pieces: 2 },
|
||||
{ name: 'glucose', weight: 15, value: 60, pieces: 2 },
|
||||
{ name: 'tin', weight: 68, value: 45, pieces: 3 },
|
||||
{ name: 'banana', weight: 27, value: 60, pieces: 3 },
|
||||
{ name: 'apple', weight: 39, value: 40, pieces: 3 },
|
||||
{ name: 'cheese', weight: 23, value: 30, pieces: 1 },
|
||||
{ name: 'beer', weight: 52, value: 10, pieces: 3 },
|
||||
{ name: 'suntan, cream', weight: 11, value: 70, pieces: 1 },
|
||||
{ name: 'camera', weight: 32, value: 30, pieces: 1 },
|
||||
{ name: 'T-shirt', weight: 24, value: 15, pieces: 2 }
|
||||
],
|
||||
700
|
||||
),
|
||||
1225
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function findBestPack(data, maxweight) {
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function findBestPack(data, maxweight) {
|
||||
var m = [[0]]; // maximum pack value found so far
|
||||
var b = [[0]]; // best combination found so far
|
||||
var opts = [0]; // item index for 0 of item 0
|
||||
var P = [1]; // item encoding for 0 of item 0
|
||||
var choose = 0;
|
||||
for (var j = 0; j < data.length; j++) {
|
||||
opts[j + 1] = opts[j] + data[j].pieces; // item index for 0 of item j+1
|
||||
P[j + 1] = P[j] * (1 + data[j].pieces); // item encoding for 0 of item j+1
|
||||
}
|
||||
for (var j = 0; j < opts[data.length]; j++) {
|
||||
m[0][j + 1] = b[0][j + 1] = 0; // best values and combos for empty pack: nothing
|
||||
}
|
||||
for (var w = 1; w <= maxweight; w++) {
|
||||
m[w] = [0];
|
||||
b[w] = [0];
|
||||
for (var j = 0; j < data.length; j++) {
|
||||
var N = data[j].pieces; // how many of these can we have?
|
||||
var base = opts[j]; // what is the item index for 0 of these?
|
||||
for (var n = 1; n <= N; n++) {
|
||||
var W = n * data[j].weight; // how much do these items weigh?
|
||||
var s = w >= W ? 1 : 0; // can we carry this many?
|
||||
var v = s * n * data[j].value; // how much are they worth?
|
||||
var I = base + n; // what is the item number for this many?
|
||||
var wN = w - s * W; // how much other stuff can we be carrying?
|
||||
var C = n * P[j] + b[wN][base]; // encoded combination
|
||||
m[w][I] = Math.max(m[w][I - 1], v + m[wN][base]); // best value
|
||||
choose = b[w][I] = m[w][I] > m[w][I - 1] ? C : b[w][I - 1];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var best = [];
|
||||
for (var j = data.length - 1; j >= 0; j--) {
|
||||
best[j] = Math.floor(choose / P[j]);
|
||||
choose -= best[j] * P[j];
|
||||
}
|
||||
|
||||
var wgt = 0;
|
||||
var val = 0;
|
||||
for (var i = 0; i < best.length; i++) {
|
||||
if (0 == best[i]) continue;
|
||||
wgt += best[i] * data[i].weight;
|
||||
val += best[i] * data[i].value;
|
||||
}
|
||||
|
||||
return val;
|
||||
}
|
||||
```
|
@ -0,0 +1,168 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc7ed3
|
||||
title: Knapsack problem/Continuous
|
||||
challengeType: 5
|
||||
forumTopicId: 323654
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
A thief burgles a butcher's shop, where he can select from some items.
|
||||
|
||||
The thief knows the weights and prices of each items. Because he has a knapsack with a limit on the maximum weight that it can carry, he wants to select the items such that he would have his profit maximized. He may cut the items; the item has a reduced price after cutting that is proportional to the original price by the ratio of masses. That means: half of an item has half the price of the original.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Write a function that takes an array of objects representing the items available in the shop. Each object has 3 attributes: name, weight, and value. The function also takes the maximum weight as a parameter. The function should return the maximum value possible, and the total weight of the selected items should not exceed the maximum weight.
|
||||
|
||||
# --hints--
|
||||
|
||||
`knapContinuous([{ "weight":3.8, "value":36, name:"beef" }, { "weight":5.4, "value":43, name:"pork" }, { "weight":3.6, "value":90, name:"ham" }, { "weight":2.4, "value":45, name:"greaves" }, { "weight":4.0, "value":30, name:"flitch" }, { "weight":2.5, "value":56, name:"brawn" }, { "weight":3.7, "value":67, name:"welt" }, { "weight":3.0, "value":95, name:"salami" }, { "weight":5.9, "value":98, name:"sausage" }], 10)` should return `257.875`.
|
||||
|
||||
```js
|
||||
assert.equal(
|
||||
knapContinuous(
|
||||
[
|
||||
{ weight: 3.8, value: 36, name: 'beef' },
|
||||
{ weight: 5.4, value: 43, name: 'pork' },
|
||||
{ weight: 3.6, value: 90, name: 'ham' },
|
||||
{ weight: 2.4, value: 45, name: 'greaves' },
|
||||
{ weight: 4.0, value: 30, name: 'flitch' },
|
||||
{ weight: 2.5, value: 56, name: 'brawn' },
|
||||
{ weight: 3.7, value: 67, name: 'welt' },
|
||||
{ weight: 3.0, value: 95, name: 'salami' },
|
||||
{ weight: 5.9, value: 98, name: 'sausage' }
|
||||
],
|
||||
10
|
||||
),
|
||||
257.875
|
||||
);
|
||||
```
|
||||
|
||||
`knapContinuous([{ "weight":3.8, "value":36, name:"beef" }, { "weight":5.4, "value":43, name:"pork" }, { "weight":3.6, "value":90, name:"ham" }, { "weight":2.4, "value":45, name:"greaves" }, { "weight":4.0, "value":30, name:"flitch" }, { "weight":2.5, "value":56, name:"brawn" }, { "weight":3.7, "value":67, name:"welt" }, { "weight":3.0, "value":95, name:"salami" }, { "weight":5.9, "value":98, name:"sausage" }], 12)` should return `295.05405405405406`.
|
||||
|
||||
```js
|
||||
assert.equal(
|
||||
knapContinuous(
|
||||
[
|
||||
{ weight: 3.8, value: 36, name: 'beef' },
|
||||
{ weight: 5.4, value: 43, name: 'pork' },
|
||||
{ weight: 3.6, value: 90, name: 'ham' },
|
||||
{ weight: 2.4, value: 45, name: 'greaves' },
|
||||
{ weight: 4.0, value: 30, name: 'flitch' },
|
||||
{ weight: 2.5, value: 56, name: 'brawn' },
|
||||
{ weight: 3.7, value: 67, name: 'welt' },
|
||||
{ weight: 3.0, value: 95, name: 'salami' },
|
||||
{ weight: 5.9, value: 98, name: 'sausage' }
|
||||
],
|
||||
12
|
||||
),
|
||||
295.05405405405406
|
||||
);
|
||||
```
|
||||
|
||||
`knapContinuous([{ "weight":3.8, "value":36, name:"beef" }, { "weight":5.4, "value":43, name:"pork" }, { "weight":3.6, "value":90, name:"ham" }, { "weight":2.4, "value":45, name:"greaves" }, { "weight":4.0, "value":30, name:"flitch" }, { "weight":2.5, "value":56, name:"brawn" }, { "weight":3.7, "value":67, name:"welt" }, { "weight":3.0, "value":95, name:"salami" }, { "weight":5.9, "value":98, name:"sausage" }], 15)` should return `349.3783783783784`.
|
||||
|
||||
```js
|
||||
assert.equal(
|
||||
knapContinuous(
|
||||
[
|
||||
{ weight: 3.8, value: 36, name: 'beef' },
|
||||
{ weight: 5.4, value: 43, name: 'pork' },
|
||||
{ weight: 3.6, value: 90, name: 'ham' },
|
||||
{ weight: 2.4, value: 45, name: 'greaves' },
|
||||
{ weight: 4.0, value: 30, name: 'flitch' },
|
||||
{ weight: 2.5, value: 56, name: 'brawn' },
|
||||
{ weight: 3.7, value: 67, name: 'welt' },
|
||||
{ weight: 3.0, value: 95, name: 'salami' },
|
||||
{ weight: 5.9, value: 98, name: 'sausage' }
|
||||
],
|
||||
15
|
||||
),
|
||||
349.3783783783784
|
||||
);
|
||||
```
|
||||
|
||||
`knapContinuous([{ "weight":3.8, "value":36, name:"beef" }, { "weight":5.4, "value":43, name:"pork" }, { "weight":3.6, "value":90, name:"ham" }, { "weight":2.4, "value":45, name:"greaves" }, { "weight":4.0, "value":30, name:"flitch" }, { "weight":2.5, "value":56, name:"brawn" }, { "weight":3.7, "value":67, name:"welt" }, { "weight":3.0, "value":95, name:"salami" }, { "weight":5.9, "value":98, name:"sausage" }], 22)` should return `459.5263157894737`.
|
||||
|
||||
```js
|
||||
assert.equal(
|
||||
knapContinuous(
|
||||
[
|
||||
{ weight: 3.8, value: 36, name: 'beef' },
|
||||
{ weight: 5.4, value: 43, name: 'pork' },
|
||||
{ weight: 3.6, value: 90, name: 'ham' },
|
||||
{ weight: 2.4, value: 45, name: 'greaves' },
|
||||
{ weight: 4.0, value: 30, name: 'flitch' },
|
||||
{ weight: 2.5, value: 56, name: 'brawn' },
|
||||
{ weight: 3.7, value: 67, name: 'welt' },
|
||||
{ weight: 3.0, value: 95, name: 'salami' },
|
||||
{ weight: 5.9, value: 98, name: 'sausage' }
|
||||
],
|
||||
22
|
||||
),
|
||||
459.5263157894737
|
||||
);
|
||||
```
|
||||
|
||||
`knapContinuous([{ "weight":3.8, "value":36, name:"beef" }, { "weight":5.4, "value":43, name:"pork" }, { "weight":3.6, "value":90, name:"ham" }, { "weight":2.4, "value":45, name:"greaves" }, { "weight":4.0, "value":30, name:"flitch" }, { "weight":2.5, "value":56, name:"brawn" }, { "weight":3.7, "value":67, name:"welt" }, { "weight":3.0, "value":95, name:"salami" }, { "weight":5.9, "value":98, name:"sausage" }], 24)` should return `478.4736842105263`.
|
||||
|
||||
```js
|
||||
assert.equal(
|
||||
knapContinuous(
|
||||
[
|
||||
{ weight: 3.8, value: 36, name: 'beef' },
|
||||
{ weight: 5.4, value: 43, name: 'pork' },
|
||||
{ weight: 3.6, value: 90, name: 'ham' },
|
||||
{ weight: 2.4, value: 45, name: 'greaves' },
|
||||
{ weight: 4.0, value: 30, name: 'flitch' },
|
||||
{ weight: 2.5, value: 56, name: 'brawn' },
|
||||
{ weight: 3.7, value: 67, name: 'welt' },
|
||||
{ weight: 3.0, value: 95, name: 'salami' },
|
||||
{ weight: 5.9, value: 98, name: 'sausage' }
|
||||
],
|
||||
24
|
||||
),
|
||||
478.4736842105263
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function knapContinuous(items, maxweight) {
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function knapContinuous(items, maxweight) {
|
||||
function item_cmp(a, b) {
|
||||
const ua = a.unitVal,
|
||||
ub = b.unitVal;
|
||||
return ua < ub ? 1 : ua > ub ? -1 : 0;
|
||||
}
|
||||
items = items.map(({ value, weight }) => ({
|
||||
unitVal: value / weight,
|
||||
weight
|
||||
}));
|
||||
items.sort(item_cmp);
|
||||
|
||||
let val = 0;
|
||||
let wt = 0;
|
||||
for (let { unitVal, weight } of items) {
|
||||
var portion = Math.min(maxweight - wt, weight);
|
||||
wt += portion;
|
||||
var addVal = portion * unitVal;
|
||||
val += addVal;
|
||||
if (wt >= maxweight) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return val;
|
||||
}
|
||||
```
|
@ -0,0 +1,154 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc7ed4
|
||||
title: Knapsack problem/Unbounded
|
||||
challengeType: 5
|
||||
forumTopicId: 323655
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
A traveler gets diverted and has to make an unscheduled stop in what turns out to be Shangri-La. Opting to leave, he is allowed to take as much as he likes of the items available there, so long as it will fit in his knapsack, and he can carry it.
|
||||
|
||||
He knows that he can carry no more than a particular value of maximum weight in total; and that the capacity of his knapsack has a limited volume.
|
||||
|
||||
Looking just above the bar codes on the items he finds their weights and volumes. He digs out his recent copy of a financial paper and gets the value of each item.
|
||||
|
||||
He can only take whole units of any item, but there is much more of any item than he could ever carry.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Write a function that takes an array of objects, maximum weight, and maximum volume as parameters. Each object has 4 attributes: name, value, weight, and volume. The function should return the maximum value of items the traveller can take with him.
|
||||
|
||||
# --hints--
|
||||
|
||||
`knapsackUnbounded([{ name:"panacea", value:3000, weight:0.3, volume:0.025 }, { name:"ichor", value:1800, weight:0.2, volume:0.015 }, { name:"gold", value:2500, weight:2, volume:0.002 }], 25, 0.25)` should return `54500`.
|
||||
|
||||
```js
|
||||
assert.equal(
|
||||
knapsackUnbounded(
|
||||
[
|
||||
{ name: 'panacea', value: 3000, weight: 0.3, volume: 0.025 },
|
||||
{ name: 'ichor', value: 1800, weight: 0.2, volume: 0.015 },
|
||||
{ name: 'gold', value: 2500, weight: 2, volume: 0.002 }
|
||||
],
|
||||
25,
|
||||
0.25
|
||||
),
|
||||
54500
|
||||
);
|
||||
```
|
||||
|
||||
`knapsackUnbounded([{ name:"panacea", value:3000, weight:0.3, volume:0.025 }, { name:"ichor", value:1800, weight:0.2, volume:0.015 }, { name:"gold", value:2500, weight:2, volume:0.002 }], 55, 0.25)` should return `88400`.
|
||||
|
||||
```js
|
||||
assert.equal(
|
||||
knapsackUnbounded(
|
||||
[
|
||||
{ name: 'panacea', value: 3000, weight: 0.3, volume: 0.025 },
|
||||
{ name: 'ichor', value: 1800, weight: 0.2, volume: 0.015 },
|
||||
{ name: 'gold', value: 2500, weight: 2, volume: 0.002 }
|
||||
],
|
||||
55,
|
||||
0.25
|
||||
),
|
||||
88400
|
||||
);
|
||||
```
|
||||
|
||||
`knapsackUnbounded([{ name:"panacea", value:3000, weight:0.3, volume:0.025 }, { name:"ichor", value:1800, weight:0.2, volume:0.015 }, { name:"gold", value:2500, weight:2, volume:0.002 }], 25, 0.15)` should return `42500`.
|
||||
|
||||
```js
|
||||
assert.equal(
|
||||
knapsackUnbounded(
|
||||
[
|
||||
{ name: 'panacea', value: 3000, weight: 0.3, volume: 0.025 },
|
||||
{ name: 'ichor', value: 1800, weight: 0.2, volume: 0.015 },
|
||||
{ name: 'gold', value: 2500, weight: 2, volume: 0.002 }
|
||||
],
|
||||
25,
|
||||
0.15
|
||||
),
|
||||
42500
|
||||
);
|
||||
```
|
||||
|
||||
`knapsackUnbounded([{ name:"panacea", value:3000, weight:0.3, volume:0.025 }, { name:"ichor", value:1800, weight:0.2, volume:0.015 }, { name:"gold", value:2500, weight:2, volume:0.002 }], 35, 0.35)` should return `75300`.
|
||||
|
||||
```js
|
||||
assert.equal(
|
||||
knapsackUnbounded(
|
||||
[
|
||||
{ name: 'panacea', value: 3000, weight: 0.3, volume: 0.025 },
|
||||
{ name: 'ichor', value: 1800, weight: 0.2, volume: 0.015 },
|
||||
{ name: 'gold', value: 2500, weight: 2, volume: 0.002 }
|
||||
],
|
||||
35,
|
||||
0.35
|
||||
),
|
||||
75300
|
||||
);
|
||||
```
|
||||
|
||||
`knapsackUnbounded([{ name:"panacea", value:3000, weight:0.3, volume:0.025 }, { name:"ichor", value:1800, weight:0.2, volume:0.015 }, { name:"gold", value:2500, weight:2, volume:0.002 }], 15, 0.25)` should return `43200`.
|
||||
|
||||
```js
|
||||
assert.equal(
|
||||
knapsackUnbounded(
|
||||
[
|
||||
{ name: 'panacea', value: 3000, weight: 0.3, volume: 0.025 },
|
||||
{ name: 'ichor', value: 1800, weight: 0.2, volume: 0.015 },
|
||||
{ name: 'gold', value: 2500, weight: 2, volume: 0.002 }
|
||||
],
|
||||
15,
|
||||
0.25
|
||||
),
|
||||
43200
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function knapsackUnbounded(items, maxweight, maxvolume) {
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function knapsackUnbounded(items, maxweight, maxvolume) {
|
||||
var n = items.length;
|
||||
var best_value = 0;
|
||||
var count = new Array(n);
|
||||
var best = new Array(n);
|
||||
function recurseKnapsack(i, value, weight, volume) {
|
||||
var j, m1, m2, m;
|
||||
if (i == n) {
|
||||
if (value > best_value) {
|
||||
best_value = value;
|
||||
for (j = 0; j < n; j++) {
|
||||
best[j] = count[j];
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
m1 = Math.floor(weight / items[i].weight);
|
||||
m2 = Math.floor(volume / items[i].volume);
|
||||
m = m1 < m2 ? m1 : m2;
|
||||
for (count[i] = m; count[i] >= 0; count[i]--) {
|
||||
recurseKnapsack(
|
||||
i + 1,
|
||||
value + count[i] * items[i].value,
|
||||
weight - count[i] * items[i].weight,
|
||||
volume - count[i] * items[i].volume
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
recurseKnapsack(0, 0, maxweight, maxvolume);
|
||||
return best_value;
|
||||
}
|
||||
```
|
@ -0,0 +1,153 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc7ed5
|
||||
title: Knight's tour
|
||||
challengeType: 5
|
||||
forumTopicId: 302297
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
[Knight's Tour](https://en.wikipedia.org/wiki/Knight%27s_tour)Problem: You have an empty `w` \* `h` chessboard, but for a single knight on some square. The knight must perform a sequence of legal moves that result in the knight visiting every square on the chessboard exactly once. Note that it is *not* a requirement that the tour be "closed"; that is, the knight need not end within a single move of its start position.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Write a function that takes `w` and `h` as parameters and returns the number of initial positions from where it is possible to achieve the task stated above.
|
||||
|
||||
# --hints--
|
||||
|
||||
`knightTour` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof knightTour == 'function');
|
||||
```
|
||||
|
||||
`knightTour(6, 6)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof knightTour(6, 6) == 'number');
|
||||
```
|
||||
|
||||
`knightTour(6, 6)` should return `35`.
|
||||
|
||||
```js
|
||||
assert.equal(knightTour(6, 6), 35);
|
||||
```
|
||||
|
||||
`knightTour(5, 6)` should return `20`.
|
||||
|
||||
```js
|
||||
assert.equal(knightTour(5, 6), 20);
|
||||
```
|
||||
|
||||
`knightTour(4, 6)` should return `10`.
|
||||
|
||||
```js
|
||||
assert.equal(knightTour(4, 6), 10);
|
||||
```
|
||||
|
||||
`knightTour(7, 3)` should return `4`.
|
||||
|
||||
```js
|
||||
assert.equal(knightTour(7, 3), 4);
|
||||
```
|
||||
|
||||
`knightTour(8, 6)` should return `47`.
|
||||
|
||||
```js
|
||||
assert.equal(knightTour(8, 6), 47);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function knightTour(w, h) {
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function knightTour(w, h) {
|
||||
var b,
|
||||
cnt = 0;
|
||||
|
||||
var dx = [-2, -2, -1, 1, 2, 2, 1, -1];
|
||||
var dy = [-1, 1, 2, 2, 1, -1, -2, -2];
|
||||
|
||||
function init_board() {
|
||||
var i, j, k, x, y;
|
||||
// * b is board; a is board with 2 rows padded at each side
|
||||
|
||||
for (i = 0; i < h; i++) {
|
||||
for (j = 0; j < w; j++) {
|
||||
b[i][j] = 255;
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < h; i++) {
|
||||
for (j = 0; j < w; j++) {
|
||||
for (k = 0; k < 8; k++) {
|
||||
(x = j + dx[k]), (y = i + dy[k]);
|
||||
if (b[i][j] == 255) b[i][j] = 0;
|
||||
if (x >= 0 && x < w && y >= 0 && y < h) b[i][j]++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function walk_board(x, y) {
|
||||
var i, nx, ny, least;
|
||||
var steps = 0;
|
||||
// printf(E"H"E"J"E"%d;%dH"E"32m[]"E"m", y + 1, 1 + 2 * x);
|
||||
|
||||
while (1) {
|
||||
// * occupy cell
|
||||
b[y][x] = 255;
|
||||
|
||||
// * reduce all neighbors' neighbor count
|
||||
for (i = 0; i < 8; i++)
|
||||
if (y + dy[i] >= 0 && x + dx[i] >= 0 && y + dy[i] < h && x + dx[i] < w)
|
||||
b[y + dy[i]][x + dx[i]]--;
|
||||
|
||||
// find neighbor with lowest neighbor count
|
||||
least = 255;
|
||||
for (i = 0; i < 8; i++) {
|
||||
if (y + dy[i] >= 0 && x + dx[i] >= 0 && y + dy[i] < h && x + dx[i] < w)
|
||||
if (b[y + dy[i]][x + dx[i]] < least) {
|
||||
nx = x + dx[i];
|
||||
ny = y + dy[i];
|
||||
least = b[ny][nx];
|
||||
}
|
||||
}
|
||||
|
||||
if (least > 7) {
|
||||
return steps == w * h - 1;
|
||||
}
|
||||
|
||||
steps++;
|
||||
(x = nx), (y = ny);
|
||||
}
|
||||
}
|
||||
|
||||
function solve(x, y) {
|
||||
b = new Array(h);
|
||||
for (var i = 0; i < h; i++) b[i] = new Array(w);
|
||||
|
||||
init_board();
|
||||
if (walk_board(x, y)) {
|
||||
cnt++;
|
||||
}
|
||||
}
|
||||
|
||||
for (var i = 0; i < h; i++) {
|
||||
for (var j = 0; j < w; j++) {
|
||||
solve(j, i);
|
||||
}
|
||||
}
|
||||
|
||||
return cnt;
|
||||
}
|
||||
```
|
@ -0,0 +1,84 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc7edb
|
||||
title: Largest int from concatenated ints
|
||||
challengeType: 5
|
||||
forumTopicId: 302298
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a set of positive integers, write a function to order the integers in such a way that the concatenation of the numbers forms the largest possible integer and return this integer.
|
||||
|
||||
# --hints--
|
||||
|
||||
`maxCombine` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof maxCombine == 'function');
|
||||
```
|
||||
|
||||
`maxCombine([1, 3, 3, 4, 55])` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof maxCombine([1, 3, 3, 4, 55]) == 'number');
|
||||
```
|
||||
|
||||
`maxCombine([1, 3, 3, 4, 55])` should return `554331`.
|
||||
|
||||
```js
|
||||
assert.equal(maxCombine([1, 3, 3, 4, 55]), 554331);
|
||||
```
|
||||
|
||||
`maxCombine([71, 45, 23, 4, 5])` should return `71545423`.
|
||||
|
||||
```js
|
||||
assert.equal(maxCombine([71, 45, 23, 4, 5]), 71545423);
|
||||
```
|
||||
|
||||
`maxCombine([14, 43, 53, 114, 55])` should return `55534314114`.
|
||||
|
||||
```js
|
||||
assert.equal(maxCombine([14, 43, 53, 114, 55]), 55534314114);
|
||||
```
|
||||
|
||||
`maxCombine([1, 34, 3, 98, 9, 76, 45, 4])` should return `998764543431`.
|
||||
|
||||
```js
|
||||
assert.equal(maxCombine([1, 34, 3, 98, 9, 76, 45, 4]), 998764543431);
|
||||
```
|
||||
|
||||
`maxCombine([54, 546, 548, 60])` should return `6054854654`.
|
||||
|
||||
```js
|
||||
assert.equal(maxCombine([54, 546, 548, 60]), 6054854654);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function maxCombine(xs) {
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function maxCombine(xs) {
|
||||
return parseInt(
|
||||
xs
|
||||
.sort(function(x, y) {
|
||||
var a = x.toString(),
|
||||
b = y.toString(),
|
||||
ab = parseInt(a + b),
|
||||
ba = parseInt(b + a);
|
||||
|
||||
return ab > ba ? -1 : ab < ba ? 1 : 0;
|
||||
})
|
||||
.join(''),
|
||||
10
|
||||
);
|
||||
}
|
||||
```
|
@ -0,0 +1,98 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc7edc
|
||||
title: Last Friday of each month
|
||||
challengeType: 5
|
||||
forumTopicId: 302299
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Write a function that returns the date of the last Friday of a given month for a given year.
|
||||
|
||||
# --hints--
|
||||
|
||||
`lastFriday` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof lastFriday == 'function');
|
||||
```
|
||||
|
||||
`lastFriday(2018, 1)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof lastFriday(2018, 1) == 'number');
|
||||
```
|
||||
|
||||
`lastFriday(2018, 1)` should return `26`.
|
||||
|
||||
```js
|
||||
assert.equal(lastFriday(2018, 1), 26);
|
||||
```
|
||||
|
||||
`lastFriday(2017, 2)` should return `24`.
|
||||
|
||||
```js
|
||||
assert.equal(lastFriday(2017, 2), 24);
|
||||
```
|
||||
|
||||
`lastFriday(2012, 3)` should return `30`.
|
||||
|
||||
```js
|
||||
assert.equal(lastFriday(2012, 3), 30);
|
||||
```
|
||||
|
||||
`lastFriday(1900, 4)` should return `27`.
|
||||
|
||||
```js
|
||||
assert.equal(lastFriday(1900, 4), 27);
|
||||
```
|
||||
|
||||
`lastFriday(2000, 5)` should return `26`.
|
||||
|
||||
```js
|
||||
assert.equal(lastFriday(2000, 5), 26);
|
||||
```
|
||||
|
||||
`lastFriday(2006, 6)` should return `30`.
|
||||
|
||||
```js
|
||||
assert.equal(lastFriday(2006, 6), 30);
|
||||
```
|
||||
|
||||
`lastFriday(2010, 7)` should return `30`.
|
||||
|
||||
```js
|
||||
assert.equal(lastFriday(2010, 7), 30);
|
||||
```
|
||||
|
||||
`lastFriday(2005, 8)` should return `26`.
|
||||
|
||||
```js
|
||||
assert.equal(lastFriday(2005, 8), 26);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function lastFriday(year, month) {
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function lastFriday(year, month) {
|
||||
var i, last_day;
|
||||
i = 0;
|
||||
while (true) {
|
||||
last_day = new Date(year, month, i);
|
||||
if (last_day.getDay() === 5) {
|
||||
return last_day.getDate();
|
||||
}
|
||||
i -= 1;
|
||||
}
|
||||
}
|
||||
```
|
@ -0,0 +1,210 @@
|
||||
---
|
||||
id: 5e4ce2b6ac708cc68c1df25e
|
||||
title: Last letter-first letter
|
||||
challengeType: 5
|
||||
forumTopicId: 385256
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
A certain children's game involves starting with a word in a particular category. Each participant in turn says a word, but that word must begin with the final letter of the previous word. Once a word has been given, it cannot be repeated. If an opponent cannot give a word in the category, they fall out of the game.
|
||||
|
||||
For example, with "animals" as the category,
|
||||
|
||||
<pre>Child 1: dog
|
||||
Child 2: goldfish
|
||||
Child 1: hippopotamus
|
||||
Child 2: snake
|
||||
...
|
||||
</pre>
|
||||
|
||||
# --instructions--
|
||||
|
||||
Write a function that takes an input array of words. The function should return an array of words where the first letter of each word is the same as the last letter of the previous word. Only use the words in the input array, and once a word is used it cannot be repeated. The words in the return array should be selected and sequenced so that that its length is maximized.
|
||||
|
||||
# --hints--
|
||||
|
||||
`findLongestChain` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof findLongestChain == 'function');
|
||||
```
|
||||
|
||||
`findLongestChain(["certain", "each", "game", "involves", "starting", "with", "word"])` should return an array.
|
||||
|
||||
```js
|
||||
assert(
|
||||
Array.isArray(
|
||||
findLongestChain([
|
||||
'certain',
|
||||
'each',
|
||||
'game',
|
||||
'involves',
|
||||
'starting',
|
||||
'with',
|
||||
'word'
|
||||
])
|
||||
)
|
||||
);
|
||||
```
|
||||
|
||||
`findLongestChain(["certain", "each", "game", "involves", "starting", "with", "word"])` should return `["involves", "starting", "game", "each"]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
findLongestChain([
|
||||
'certain',
|
||||
'each',
|
||||
'game',
|
||||
'involves',
|
||||
'starting',
|
||||
'with',
|
||||
'word'
|
||||
]),
|
||||
['involves', 'starting', 'game', 'each']
|
||||
);
|
||||
```
|
||||
|
||||
`findLongestChain(["audino", "bagon", "kangaskhan", "banette", "bidoof", "braviary", "exeggcute", "yamask"])` should return `["braviary", "yamask", "kangaskhan"]`
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
findLongestChain([
|
||||
'audino',
|
||||
'bagon',
|
||||
'kangaskhan',
|
||||
'banette',
|
||||
'bidoof',
|
||||
'braviary',
|
||||
'exeggcute',
|
||||
'yamask'
|
||||
]),
|
||||
['braviary', 'yamask', 'kangaskhan']
|
||||
);
|
||||
```
|
||||
|
||||
`findLongestChain(["harp", "poliwrath", "poochyena", "porygon2", "porygonz", "archana"])` should return `["poliwrath", "harp", "poochyena", "archana"]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
findLongestChain([
|
||||
'harp',
|
||||
'poliwrath',
|
||||
'poochyena',
|
||||
'porygon2',
|
||||
'porygonz',
|
||||
'archana'
|
||||
]),
|
||||
['poliwrath', 'harp', 'poochyena', 'archana']
|
||||
);
|
||||
```
|
||||
|
||||
`findLongestChain(["scolipede", "elephant", "zeaking", "sealeo", "silcoon", "tigers"])` should return `["scolipede", "elephant", "tigers", "sealeo"]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
findLongestChain([
|
||||
'scolipede',
|
||||
'elephant',
|
||||
'zeaking',
|
||||
'sealeo',
|
||||
'silcoon',
|
||||
'tigers'
|
||||
]),
|
||||
['scolipede', 'elephant', 'tigers', 'sealeo']
|
||||
);
|
||||
```
|
||||
|
||||
`findLongestChain(["loudred", "lumineon", "lunatone", "machamp", "magnezone", "nosepass", "petilil", "pidgeotto", "pikachu"])` should return `["machamp", "petilil", "lumineon", "nosepass"]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
findLongestChain([
|
||||
'loudred',
|
||||
'lumineon',
|
||||
'lunatone',
|
||||
'machamp',
|
||||
'magnezone',
|
||||
'nosepass',
|
||||
'petilil',
|
||||
'pidgeotto',
|
||||
'pikachu'
|
||||
]),
|
||||
['machamp', 'petilil', 'lumineon', 'nosepass']
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function findLongestChain(items) {
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function findLongestChain(items) {
|
||||
function Ref(index, first_char, last_char) {
|
||||
this.index = index;
|
||||
this.first_char = first_char;
|
||||
this.last_char = last_char;
|
||||
}
|
||||
|
||||
var items_len = items.length
|
||||
var refs_len = items_len;
|
||||
var refs = []
|
||||
|
||||
// enough space for all items
|
||||
var longest_path_refs_len = 0;
|
||||
var longest_path_refs = new Array(items_len);
|
||||
|
||||
function search(curr_len) {
|
||||
if (curr_len > longest_path_refs_len) {
|
||||
longest_path_refs_len = curr_len;
|
||||
|
||||
for (var i = 0; i < curr_len; i++) {
|
||||
longest_path_refs[i] = refs[i];
|
||||
}
|
||||
}
|
||||
|
||||
// recursive search
|
||||
var last_char = refs[curr_len - 1].last_char;
|
||||
for (var i = curr_len; i < refs_len; i++)
|
||||
if (refs[i].first_char == last_char) {
|
||||
var aux = refs[curr_len];
|
||||
refs[curr_len] = refs[i];
|
||||
refs[i] = aux;
|
||||
search(curr_len + 1);
|
||||
refs[i] = refs[curr_len];
|
||||
refs[curr_len] = aux;
|
||||
}
|
||||
}
|
||||
|
||||
for (var i = 0; i < items_len; i++) {
|
||||
var itemsi_len = items[i].length;
|
||||
refs.push(new Ref(i, items[i][0], items[i][itemsi_len - 1]));
|
||||
}
|
||||
|
||||
// try each item as possible start
|
||||
for (var i = 0; i < items_len; i++) {
|
||||
var aux = refs[0];
|
||||
refs[0] = refs[i];
|
||||
refs[i] = aux;
|
||||
search(1);
|
||||
refs[i] = refs[0];
|
||||
refs[0] = aux;
|
||||
}
|
||||
|
||||
var longest_path_len = longest_path_refs_len;
|
||||
var longest_path = new Array(longest_path_len);
|
||||
|
||||
for (var i = 0; i < longest_path_len; i++)
|
||||
longest_path[i] = items[longest_path_refs[i].index];
|
||||
|
||||
return longest_path;
|
||||
}
|
||||
```
|
@ -0,0 +1,78 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc7ede
|
||||
title: Leap year
|
||||
challengeType: 5
|
||||
forumTopicId: 302300
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Determine whether a given year is a leap year in the Gregorian calendar.
|
||||
|
||||
# --hints--
|
||||
|
||||
`isLeapYear` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof isLeapYear == 'function');
|
||||
```
|
||||
|
||||
`isLeapYear()` should return a boolean.
|
||||
|
||||
```js
|
||||
assert(typeof isLeapYear(2018) == 'boolean');
|
||||
```
|
||||
|
||||
`isLeapYear(2018)` should return `false`.
|
||||
|
||||
```js
|
||||
assert.equal(isLeapYear(2018), false);
|
||||
```
|
||||
|
||||
`isLeapYear(2016)` should return `true`.
|
||||
|
||||
```js
|
||||
assert.equal(isLeapYear(2016), true);
|
||||
```
|
||||
|
||||
`isLeapYear(2000)` should return `true`.
|
||||
|
||||
```js
|
||||
assert.equal(isLeapYear(2000), true);
|
||||
```
|
||||
|
||||
`isLeapYear(1900)` should return `false`.
|
||||
|
||||
```js
|
||||
assert.equal(isLeapYear(1900), false);
|
||||
```
|
||||
|
||||
`isLeapYear(1996)` should return `true`.
|
||||
|
||||
```js
|
||||
assert.equal(isLeapYear(1996), true);
|
||||
```
|
||||
|
||||
`isLeapYear(1800)` should return `false`.
|
||||
|
||||
```js
|
||||
assert.equal(isLeapYear(1800), false);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function isLeapYear(year) {
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function isLeapYear(year) {
|
||||
return year % 100 === 0 ? year % 400 === 0 : year % 4 === 0;
|
||||
}
|
||||
```
|
@ -0,0 +1,86 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc7edf
|
||||
title: Least common multiple
|
||||
challengeType: 5
|
||||
forumTopicId: 302301
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
The least common multiple of 12 and 18 is 36, because 12 is a factor (12 × 3 = 36), and 18 is a factor (18 × 2 = 36), and there is no positive integer less than 36 that has both factors. As a special case, if either *m* or *n* is zero, then the least common multiple is zero. One way to calculate the least common multiple is to iterate all the multiples of *m*, until you find one that is also a multiple of *n*. If you already have *gcd* for [greatest common divisor](<https://rosettacode.org/wiki/greatest common divisor>), then this formula calculates *lcm*. ( \\operatorname{lcm}(m, n) = \\frac{|m \\times n|}{\\operatorname{gcd}(m, n)} )
|
||||
|
||||
# --instructions--
|
||||
|
||||
Compute the least common multiple of an array of integers. Given *m* and *n*, the least common multiple is the smallest positive integer that has both *m* and *n* as factors.
|
||||
|
||||
# --hints--
|
||||
|
||||
`LCM` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof LCM == 'function');
|
||||
```
|
||||
|
||||
`LCM([2, 4, 8])` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof LCM([2, 4, 8]) == 'number');
|
||||
```
|
||||
|
||||
`LCM([2, 4, 8])` should return `8`.
|
||||
|
||||
```js
|
||||
assert.equal(LCM([2, 4, 8]), 8);
|
||||
```
|
||||
|
||||
`LCM([4, 8, 12])` should return `24`.
|
||||
|
||||
```js
|
||||
assert.equal(LCM([4, 8, 12]), 24);
|
||||
```
|
||||
|
||||
`LCM([3, 4, 5, 12, 40])` should return `120`.
|
||||
|
||||
```js
|
||||
assert.equal(LCM([3, 4, 5, 12, 40]), 120);
|
||||
```
|
||||
|
||||
`LCM([11, 33, 90])` should return `990`.
|
||||
|
||||
```js
|
||||
assert.equal(LCM([11, 33, 90]), 990);
|
||||
```
|
||||
|
||||
`LCM([-50, 25, -45, -18, 90, 447])` should return `67050`.
|
||||
|
||||
```js
|
||||
assert.equal(LCM([-50, 25, -45, -18, 90, 447]), 67050);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function LCM(A) {
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function LCM(A) {
|
||||
var n = A.length,
|
||||
a = Math.abs(A[0]);
|
||||
for (var i = 1; i < n; i++) {
|
||||
var b = Math.abs(A[i]),
|
||||
c = a;
|
||||
while (a && b) {
|
||||
a > b ? (a %= b) : (b %= a);
|
||||
}
|
||||
a = Math.abs(c * A[i]) / (a + b);
|
||||
}
|
||||
return a;
|
||||
}
|
||||
```
|
@ -0,0 +1,114 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc7ee0
|
||||
title: Left factorials
|
||||
challengeType: 5
|
||||
forumTopicId: 302302
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
**Left factorials**, $ !n $, may refer to either *subfactorials* or to *factorial sums*. The same notation can be confusingly seen used for the two different definitions. Sometimes, *subfactorials* (also known as *derangements*) may use any of the notations:
|
||||
|
||||
<ul>
|
||||
<li>$!n`$</li>
|
||||
<li>$!n$</li>
|
||||
<li>$n¡$</li>
|
||||
</ul>
|
||||
|
||||
(It may not be visually obvious, but the last example uses an upside-down exclamation mark.) This task will be using this formula for **left factorial**:
|
||||
|
||||
$ !n = \\sum\_{k=0}^{n-1} k! $
|
||||
|
||||
where $!0 = 0$
|
||||
|
||||
# --instructions--
|
||||
|
||||
Write a function to calculate the left factorial of a given number.
|
||||
|
||||
# --hints--
|
||||
|
||||
`leftFactorial` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof leftFactorial == 'function');
|
||||
```
|
||||
|
||||
`leftFactorial(0)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof leftFactorial(0) == 'number');
|
||||
```
|
||||
|
||||
`leftFactorial(0)` should return `0`.
|
||||
|
||||
```js
|
||||
assert.equal(leftFactorial(0), 0);
|
||||
```
|
||||
|
||||
`leftFactorial(1)` should return `1`.
|
||||
|
||||
```js
|
||||
assert.equal(leftFactorial(1), 1);
|
||||
```
|
||||
|
||||
`leftFactorial(2)` should return `2`.
|
||||
|
||||
```js
|
||||
assert.equal(leftFactorial(2), 2);
|
||||
```
|
||||
|
||||
`leftFactorial(3)` should return `4`.
|
||||
|
||||
```js
|
||||
assert.equal(leftFactorial(3), 4);
|
||||
```
|
||||
|
||||
`leftFactorial(10)` should return `409114`.
|
||||
|
||||
```js
|
||||
assert.equal(leftFactorial(10), 409114);
|
||||
```
|
||||
|
||||
`leftFactorial(17)` should return `22324392524314`.
|
||||
|
||||
```js
|
||||
assert.equal(leftFactorial(17), 22324392524314);
|
||||
```
|
||||
|
||||
`leftFactorial(19)` should return `6780385526348314`.
|
||||
|
||||
```js
|
||||
assert.equal(leftFactorial(19), 6780385526348314);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function leftFactorial(n) {
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function leftFactorial(n) {
|
||||
if (n == 0) return 0;
|
||||
if (n == 1) return 1;
|
||||
|
||||
// Note: for n>=20, the result may not be correct.
|
||||
// This is because JavaScript uses 53 bit integers and
|
||||
// for n>=20 result becomes too large.
|
||||
|
||||
let res = 2,
|
||||
fact = 2;
|
||||
for (var i = 2; i < n; i++) {
|
||||
res += fact;
|
||||
fact *= i + 1;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
```
|
@ -0,0 +1,208 @@
|
||||
---
|
||||
id: 5e4ce2bbac708cc68c1df25f
|
||||
title: Letter frequency
|
||||
challengeType: 5
|
||||
forumTopicId: 385263
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a string, calculate the frequency of each character.
|
||||
|
||||
All characters should be counted. This includes lower and upper case letters, digits, whitespace, special characters, or any other distinct characters.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Write a function to count the occurrences of each character in a given string.
|
||||
|
||||
The function should return a 2D array with each of the elements in the following form: `['char', freq]`. The character should be a string with a length of 1, and frequency is a number denoting the count.
|
||||
|
||||
For example, given the string "ab", your function should return `[['a', 1], ['b', 1]]`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`letterFrequency` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof letterFrequency == 'function');
|
||||
```
|
||||
|
||||
`letterFrequency("Not all that Mrs. Bennet, however")` should return an array.
|
||||
|
||||
```js
|
||||
assert(Array.isArray(letterFrequency('Not all that Mrs. Bennet, however')));
|
||||
```
|
||||
|
||||
`letterFrequency("Not all that Mrs. Bennet, however")` should return `[[" ", 5], [", ", 1], [".", 1], ["B", 1], ["M", 1], ["N", 1], ["a", 2], ["e", 4], ["h", 2], ["l", 2], ["n", 2], ["o", 2], ["r", 2], ["s", 1], ["t", 4], ["v", 1], ["w", 1]]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(letterFrequency('Not all that Mrs. Bennet, however'), [
|
||||
[' ', 5],
|
||||
[',', 1],
|
||||
['.', 1],
|
||||
['B', 1],
|
||||
['M', 1],
|
||||
['N', 1],
|
||||
['a', 2],
|
||||
['e', 4],
|
||||
['h', 2],
|
||||
['l', 2],
|
||||
['n', 2],
|
||||
['o', 2],
|
||||
['r', 2],
|
||||
['s', 1],
|
||||
['t', 4],
|
||||
['v', 1],
|
||||
['w', 1]
|
||||
]);
|
||||
```
|
||||
|
||||
`letterFrequency("daughters, could ask on the ")` should return `[[' ',5],[',',1],['a',2],['c',1],['d',2],['e',2],['g',1],['h',2],['k',1],['l',1],['n',1],['o',2],['r',1],['s',2],['t',2],['u',2]]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(letterFrequency('daughters, could ask on the '), [
|
||||
[' ', 5],
|
||||
[',', 1],
|
||||
['a', 2],
|
||||
['c', 1],
|
||||
['d', 2],
|
||||
['e', 2],
|
||||
['g', 1],
|
||||
['h', 2],
|
||||
['k', 1],
|
||||
['l', 1],
|
||||
['n', 1],
|
||||
['o', 2],
|
||||
['r', 1],
|
||||
['s', 2],
|
||||
['t', 2],
|
||||
['u', 2]
|
||||
]);
|
||||
```
|
||||
|
||||
`letterFrequency("husband any satisfactory description")` should return `[[" ", 3], ["a", 4], ["b", 1], ["c", 2], ["d", 2], ["e", 1], ["f", 1], ["h", 1], ["i", 3], ["n", 3], ["o", 2], ["p", 1], ["r", 2], ["s", 4], ["t", 3], ["u", 1], ["y", 2]]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(letterFrequency('husband any satisfactory description'), [
|
||||
[' ', 3],
|
||||
['a', 4],
|
||||
['b', 1],
|
||||
['c', 2],
|
||||
['d', 2],
|
||||
['e', 1],
|
||||
['f', 1],
|
||||
['h', 1],
|
||||
['i', 3],
|
||||
['n', 3],
|
||||
['o', 2],
|
||||
['p', 1],
|
||||
['r', 2],
|
||||
['s', 4],
|
||||
['t', 3],
|
||||
['u', 1],
|
||||
['y', 2]
|
||||
]);
|
||||
```
|
||||
|
||||
`letterFrequency("in various ways--with barefaced")` should return `[[" ", 3], ["-", 2], ["a", 4], ["b", 1], ["c", 1], ["d", 1], ["e", 2], ["f", 1], ["h", 1], ["i", 3], ["n", 1], ["o", 1], ["r", 2], ["s", 2], ["t", 1], ["u", 1], ["v", 1], ["w", 2], ["y", 1]]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(letterFrequency('in various ways--with barefaced'), [
|
||||
[' ', 3],
|
||||
['-', 2],
|
||||
['a', 4],
|
||||
['b', 1],
|
||||
['c', 1],
|
||||
['d', 1],
|
||||
['e', 2],
|
||||
['f', 1],
|
||||
['h', 1],
|
||||
['i', 3],
|
||||
['n', 1],
|
||||
['o', 1],
|
||||
['r', 2],
|
||||
['s', 2],
|
||||
['t', 1],
|
||||
['u', 1],
|
||||
['v', 1],
|
||||
['w', 2],
|
||||
['y', 1]
|
||||
]);
|
||||
```
|
||||
|
||||
`letterFrequency("distant surmises; but he eluded")` should return `[[" ", 4], ["; ", 1], ["a", 1], ["b", 1], ["d", 3], ["e", 4], ["h", 1], ["i", 2], ["l", 1], ["m", 1], ["n", 1], ["r", 1], ["s", 4], ["t", 3], ["u", 3]]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(letterFrequency('distant surmises; but he eluded'), [
|
||||
[' ', 4],
|
||||
[';', 1],
|
||||
['a', 1],
|
||||
['b', 1],
|
||||
['d', 3],
|
||||
['e', 4],
|
||||
['h', 1],
|
||||
['i', 2],
|
||||
['l', 1],
|
||||
['m', 1],
|
||||
['n', 1],
|
||||
['r', 1],
|
||||
['s', 4],
|
||||
['t', 3],
|
||||
['u', 3]
|
||||
]);
|
||||
```
|
||||
|
||||
`letterFrequency("last obliged to accept the second-hand,")` should return `[[" ", 5], [", ", 1], ["-", 1], ["a", 3], ["b", 1], ["c", 3], ["d", 3], ["e", 4], ["g", 1], ["h", 2], ["i", 1], ["l", 2], ["n", 2], ["o", 3], ["p", 1], ["s", 2], ["t", 4]]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(letterFrequency('last obliged to accept the second-hand,'), [
|
||||
[' ', 5],
|
||||
[',', 1],
|
||||
['-', 1],
|
||||
['a', 3],
|
||||
['b', 1],
|
||||
['c', 3],
|
||||
['d', 3],
|
||||
['e', 4],
|
||||
['g', 1],
|
||||
['h', 2],
|
||||
['i', 1],
|
||||
['l', 2],
|
||||
['n', 2],
|
||||
['o', 3],
|
||||
['p', 1],
|
||||
['s', 2],
|
||||
['t', 4]
|
||||
]);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function letterFrequency(txt) {
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function letterFrequency(txt) {
|
||||
var cs = txt.split(''),
|
||||
i = cs.length,
|
||||
dct = {},
|
||||
c = '',
|
||||
keys;
|
||||
|
||||
while (i--) {
|
||||
c = cs[i];
|
||||
dct[c] = (dct[c] || 0) + 1;
|
||||
}
|
||||
|
||||
keys = Object.keys(dct);
|
||||
keys.sort();
|
||||
return keys.map(function (c) { return [c, dct[c]]; });
|
||||
}
|
||||
```
|
@ -0,0 +1,104 @@
|
||||
---
|
||||
id: 5e4ce2eaac708cc68c1df260
|
||||
title: Levenshtein distance
|
||||
challengeType: 5
|
||||
forumTopicId: 385264
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
In information theory and computer science, the **Levenshtein distance** is a [metric](<https://en.wikipedia.org/wiki/string metric>) for measuring the amount of difference between two sequences (i.e. an [edit distance](<https://en.wikipedia.org/wiki/edit distance>)). The Levenshtein distance between two strings is defined as the minimum number of edits needed to transform one string into the other, with the allowable edit operations being insertion, deletion, or substitution of a single character.
|
||||
|
||||
Example:
|
||||
|
||||
The Levenshtein distance between "**kitten**" and "**sitting**" is 3, since the following three edits change one into the other, and there isn't a way to do it with fewer than three edits:
|
||||
|
||||
<ul>
|
||||
<li><strong>k</strong>itten <strong>s</strong>itten (substitution of 'k' with 's')</li>
|
||||
<li>sitt<strong>e</strong>n sitt<strong>i</strong>n (substitution of 'e' with 'i')</li>
|
||||
<li>sittin sittin<strong>g</strong> (insert 'g' at the end).</li>
|
||||
</ul>
|
||||
|
||||
*The Levenshtein distance between "**rosettacode**", "**raisethysword**" is **8**.*
|
||||
|
||||
*The distance between two strings is same as that when both strings are reversed.*
|
||||
|
||||
# --instructions--
|
||||
|
||||
Write a function that returns the Levenshtein distance between two strings given as parameters.
|
||||
|
||||
# --hints--
|
||||
|
||||
`levenshtein` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof levenshtein == 'function');
|
||||
```
|
||||
|
||||
`levenshtein("mist", "dist")` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof levenshtein('mist', 'dist') == 'number');
|
||||
```
|
||||
|
||||
`levenshtein("mist", "dist")` should return `1`.
|
||||
|
||||
```js
|
||||
assert.equal(levenshtein('mist', 'dist'), 1);
|
||||
```
|
||||
|
||||
`levenshtein("tier", "tor")` should return `2`.
|
||||
|
||||
```js
|
||||
assert.equal(levenshtein('tier', 'tor'), 2);
|
||||
```
|
||||
|
||||
`levenshtein("kitten", "sitting")` should return `3`.
|
||||
|
||||
```js
|
||||
assert.equal(levenshtein('kitten', 'sitting'), 3);
|
||||
```
|
||||
|
||||
`levenshtein("stop", "tops")` should return `2`.
|
||||
|
||||
```js
|
||||
assert.equal(levenshtein('stop', 'tops'), 2);
|
||||
```
|
||||
|
||||
`levenshtein("rosettacode", "raisethysword")` should return `8`.
|
||||
|
||||
```js
|
||||
assert.equal(levenshtein('rosettacode', 'raisethysword'), 8);
|
||||
```
|
||||
|
||||
`levenshtein("mississippi", "swiss miss")` should return `8`.
|
||||
|
||||
```js
|
||||
assert.equal(levenshtein('mississippi', 'swiss miss'), 8);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function levenshtein(a, b) {
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function levenshtein(a, b) {
|
||||
var t = [], u, i, j, m = a.length, n = b.length;
|
||||
if (!m) { return n; }
|
||||
if (!n) { return m; }
|
||||
for (j = 0; j <= n; j++) { t[j] = j; }
|
||||
for (i = 1; i <= m; i++) {
|
||||
for (u = [i], j = 1; j <= n; j++) {
|
||||
u[j] = a[i - 1] === b[j - 1] ? t[j - 1] : Math.min(t[j - 1], t[j], u[j - 1]) + 1;
|
||||
} t = u;
|
||||
} return u[n];
|
||||
}
|
||||
```
|
@ -0,0 +1,96 @@
|
||||
---
|
||||
id: 5e4ce2f5ac708cc68c1df261
|
||||
title: Linear congruential generator
|
||||
challengeType: 5
|
||||
forumTopicId: 385266
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
The [linear congruential generator](<https://en.wikipedia.org/wiki/linear congruential generator>) is a very simple example of a [random number generator](<http://rosettacode.org/wiki/random number generator>). All linear congruential generators use this formula:
|
||||
|
||||
$$r\_{n + 1} = a \\times r_n + c \\pmod m$$
|
||||
|
||||
Where:
|
||||
|
||||
<ul>
|
||||
<li>$ r_0 $ is a seed.</li>
|
||||
<li>$r_1$, $r_2$, $r_3$, ..., are the random numbers.</li>
|
||||
<li>$a$, $c$, $m$ are constants.</li>
|
||||
</ul>
|
||||
|
||||
If one chooses the values of $a$, $c$ and $m$ with care, then the generator produces a uniform distribution of integers from $0$ to $m - 1$.
|
||||
|
||||
LCG numbers have poor quality. $r_n$ and $r\_{n + 1}$ are not independent, as true random numbers would be. Anyone who knows $r_n$ can predict $r\_{n + 1}$, therefore LCG is not cryptographically secure. The LCG is still good enough for simple tasks like [Miller-Rabin primality test](<http://rosettacode.org/wiki/Miller-Rabin primality test>), or [FreeCell deals](<http://rosettacode.org/wiki/deal cards for FreeCell>). Among the benefits of the LCG, one can easily reproduce a sequence of numbers, from the same $r_0$. One can also reproduce such sequence with a different programming language, because the formula is so simple.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Write a function that takes $r_0,a,c,m,n$ as parameters and returns $r_n$.
|
||||
|
||||
# --hints--
|
||||
|
||||
`linearCongGenerator` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof linearCongGenerator == 'function');
|
||||
```
|
||||
|
||||
`linearCongGenerator(324, 1145, 177, 2148, 3)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof linearCongGenerator(324, 1145, 177, 2148, 3) == 'number');
|
||||
```
|
||||
|
||||
`linearCongGenerator(324, 1145, 177, 2148, 3)` should return `855`.
|
||||
|
||||
```js
|
||||
assert.equal(linearCongGenerator(324, 1145, 177, 2148, 3), 855);
|
||||
```
|
||||
|
||||
`linearCongGenerator(234, 11245, 145, 83648, 4)` should return `1110`.
|
||||
|
||||
```js
|
||||
assert.equal(linearCongGenerator(234, 11245, 145, 83648, 4), 1110);
|
||||
```
|
||||
|
||||
`linearCongGenerator(85, 11, 1234, 214748, 5)` should return `62217`.
|
||||
|
||||
```js
|
||||
assert.equal(linearCongGenerator(85, 11, 1234, 214748, 5), 62217);
|
||||
```
|
||||
|
||||
`linearCongGenerator(0, 1103515245, 12345, 2147483648, 1)` should return `12345`.
|
||||
|
||||
```js
|
||||
assert.equal(linearCongGenerator(0, 1103515245, 12345, 2147483648, 1), 12345);
|
||||
```
|
||||
|
||||
`linearCongGenerator(0, 1103515245, 12345, 2147483648, 2)` should return `1406932606`.
|
||||
|
||||
```js
|
||||
assert.equal(
|
||||
linearCongGenerator(0, 1103515245, 12345, 2147483648, 2),
|
||||
1406932606
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function linearCongGenerator(r0, a, c, m, n) {
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function linearCongGenerator(r0, a, c, m, n) {
|
||||
for (let i = 0; i < n; i++) {
|
||||
r0 = (a * r0 + c) % m;
|
||||
}
|
||||
return r0;
|
||||
}
|
||||
```
|
@ -0,0 +1,110 @@
|
||||
---
|
||||
id: 5e4ce2a1ac708cc68c1df25d
|
||||
title: Long multiplication
|
||||
challengeType: 5
|
||||
forumTopicId: 385269
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Explicitly implement [long multiplication](<https://en.wikipedia.org/wiki/long multiplication>).
|
||||
|
||||
This is one possible approach to arbitrary-precision integer algebra.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Write a function that takes two strings of large numbers as parameters. Your function should return the product of these two large numbers as a string.
|
||||
|
||||
**Note:** In JavaScript, arithmetic operations are inaccurate with large numbers, so you will have to implement precise multiplication yourself.
|
||||
|
||||
# --hints--
|
||||
|
||||
`mult` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof mult == 'function');
|
||||
```
|
||||
|
||||
`mult("18446744073709551616", "18446744073709551616")` should return a string.
|
||||
|
||||
```js
|
||||
assert(typeof mult('18446744073709551616', '18446744073709551616') == 'string');
|
||||
```
|
||||
|
||||
`mult("18446744073709551616", "18446744073709551616")` should return `"340282366920938463463374607431768211456"`.
|
||||
|
||||
```js
|
||||
assert.equal(
|
||||
mult('18446744073709551616', '18446744073709551616'),
|
||||
'340282366920938463463374607431768211456'
|
||||
);
|
||||
```
|
||||
|
||||
`mult("31844674073709551616", "1844674407309551616")` should return `"58743055272886011737990786529368211456"`.
|
||||
|
||||
```js
|
||||
assert.equal(
|
||||
mult('31844674073709551616', '1844674407309551616'),
|
||||
'58743055272886011737990786529368211456'
|
||||
);
|
||||
```
|
||||
|
||||
`mult("1846744073709551616", "44844644073709551616")` should return `"82816580680737279241781007431768211456"`.
|
||||
|
||||
```js
|
||||
assert.equal(
|
||||
mult('1846744073709551616', '44844644073709551616'),
|
||||
'82816580680737279241781007431768211456'
|
||||
);
|
||||
```
|
||||
|
||||
`mult("1844674407370951616", "1844674407709551616")` should return `"3402823669833978308014392742590611456"`.
|
||||
|
||||
```js
|
||||
assert.equal(
|
||||
mult('1844674407370951616', '1844674407709551616'),
|
||||
'3402823669833978308014392742590611456'
|
||||
);
|
||||
```
|
||||
|
||||
`mult("2844674407370951616", "1844674407370955616")` should return `"5247498076580334548376218009219475456"`.
|
||||
|
||||
```js
|
||||
assert.equal(
|
||||
mult('2844674407370951616', '1844674407370955616'),
|
||||
'5247498076580334548376218009219475456'
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function mult(strNum1, strNum2) {
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function mult(strNum1, strNum2) {
|
||||
var a1 = strNum1.split("").reverse();
|
||||
var a2 = strNum2.toString().split("").reverse();
|
||||
var aResult = new Array;
|
||||
|
||||
for ( var iterNum1 = 0; iterNum1 < a1.length; iterNum1++ ) {
|
||||
for ( var iterNum2 = 0; iterNum2 < a2.length; iterNum2++ ) {
|
||||
var idxIter = iterNum1 + iterNum2; // Get the current array position.
|
||||
aResult[idxIter] = a1[iterNum1] * a2[iterNum2] + ( idxIter >= aResult.length ? 0 : aResult[idxIter] );
|
||||
|
||||
if ( aResult[idxIter] > 9 ) { // Carrying
|
||||
aResult[idxIter + 1] = Math.floor( aResult[idxIter] / 10 ) + ( idxIter + 1 >= aResult.length ? 0 : aResult[idxIter + 1] );
|
||||
aResult[idxIter] %= 10;
|
||||
}
|
||||
}
|
||||
}
|
||||
return aResult.reverse().join("");
|
||||
}
|
||||
```
|
@ -0,0 +1,101 @@
|
||||
---
|
||||
id: 5e6dd1278e6ca105cde40ea9
|
||||
title: Longest common subsequence
|
||||
challengeType: 5
|
||||
forumTopicId: 385271
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
The **longest common subsequence** (or [**LCS**](http://en.wikipedia.org/wiki/Longest_common_subsequence_problem)) of groups A and B is the longest group of elements from A and B that are common between the two groups and in the same order in each group. For example, the sequences "1234" and "1224533324" have an LCS of "1234":
|
||||
|
||||
***1234***
|
||||
|
||||
***12***245***3***332***4***
|
||||
|
||||
For a string example, consider the sequences "thisisatest" and "testing123testing". An LCS would be "tsitest":
|
||||
|
||||
***t***hi***si***sa***test***
|
||||
|
||||
***t***e***s***t***i***ng123***test***ing.
|
||||
|
||||
Your code only needs to deal with strings.
|
||||
|
||||
For more information on this problem please see [Wikipedia](https://en.wikipedia.org/wiki/Longest_common_subsequence_problem).
|
||||
|
||||
# --instructions--
|
||||
|
||||
Write a case-sensitive function that returns the LCS of two strings. You don't need to show multiple LCS's.
|
||||
|
||||
# --hints--
|
||||
|
||||
`lcs` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof lcs == 'function');
|
||||
```
|
||||
|
||||
`lcs("thisisatest", "testing123testing")` should return a string.
|
||||
|
||||
```js
|
||||
assert(typeof lcs('thisisatest', 'testing123testing') == 'string');
|
||||
```
|
||||
|
||||
`lcs("thisisatest", "testing123testing")` should return `"tsitest"`.
|
||||
|
||||
```js
|
||||
assert.equal(lcs('thisisatest', 'testing123testing'), 'tsitest');
|
||||
```
|
||||
|
||||
`lcs("ABCDGH", "AEDFHR")` should return `"ADH"`.
|
||||
|
||||
```js
|
||||
assert.equal(lcs('ABCDGH', 'AEDFHR'), 'ADH');
|
||||
```
|
||||
|
||||
`lcs("AGGTAB", "GXTXAYB")` should return `"GTAB"`.
|
||||
|
||||
```js
|
||||
assert.equal(lcs('AGGTAB', 'GXTXAYB'), 'GTAB');
|
||||
```
|
||||
|
||||
`lcs("BDACDB", "BDCB")` should return `"BDCB"`.
|
||||
|
||||
```js
|
||||
assert.equal(lcs('BDACDB', 'BDCB'), 'BDCB');
|
||||
```
|
||||
|
||||
`lcs("ABAZDC", "BACBAD")` should return `"ABAD"`.
|
||||
|
||||
```js
|
||||
assert.equal(lcs('ABAZDC', 'BACBAD'), 'ABAD');
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function lcs(a, b) {
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function lcs(a, b) {
|
||||
var aSub = a.substr(0, a.length - 1);
|
||||
var bSub = b.substr(0, b.length - 1);
|
||||
|
||||
if (a.length === 0 || b.length === 0) {
|
||||
return '';
|
||||
} else if (a.charAt(a.length - 1) === b.charAt(b.length - 1)) {
|
||||
return lcs(aSub, bSub) + a.charAt(a.length - 1);
|
||||
} else {
|
||||
var x = lcs(a, bSub);
|
||||
var y = lcs(aSub, b);
|
||||
return (x.length > y.length) ? x : y;
|
||||
}
|
||||
}
|
||||
```
|
@ -0,0 +1,119 @@
|
||||
---
|
||||
id: 5e6dd139859c290b6ab80292
|
||||
title: Longest increasing subsequence
|
||||
challengeType: 5
|
||||
forumTopicId: 385272
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
The longest increasing subsequence problem is to find a subsequence of a given sequence in which the subsequence's elements are in sorted order, lowest to highest, and in which the subsequence is as long as possible. An example:
|
||||
|
||||
For the following array:
|
||||
|
||||
$\\{3, 10, 2, 1, 20\\}$
|
||||
|
||||
Longest increasing sequence is:
|
||||
|
||||
$\\{3, 10, 20\\}$
|
||||
|
||||
For more information on this problem please see [Wikipedia](<https://en.wikipedia.org/wiki/Longest increasing subsequence>).
|
||||
|
||||
# --instructions--
|
||||
|
||||
Write a function that takes an array of numbers as a parameter and returns the longest increasing subsequence.
|
||||
|
||||
It is guaranteed that every array will have a longest increasing subsequence.
|
||||
|
||||
# --hints--
|
||||
|
||||
`findSequence` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof findSequence == 'function');
|
||||
```
|
||||
|
||||
`findSequence([3, 10, 2, 1, 20])` should return a array.
|
||||
|
||||
```js
|
||||
assert(Array.isArray(findSequence([3, 10, 2, 1, 20])));
|
||||
```
|
||||
|
||||
`findSequence([3, 10, 2, 1, 20])` should return `[3, 10, 20]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(findSequence([3, 10, 2, 1, 20]), [3, 10, 20]);
|
||||
```
|
||||
|
||||
`findSequence([2, 7, 3, 5, 8])` should return `[2, 3, 5, 8]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(findSequence([2, 7, 3, 5, 8]), [2, 3, 5, 8]);
|
||||
```
|
||||
|
||||
`findSequence([2, 6, 4, 5, 1])` should return `[2, 4, 5]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(findSequence([2, 6, 4, 5, 1]), [2, 4, 5]);
|
||||
```
|
||||
|
||||
`findSequence([10, 22, 9, 33, 21, 50, 60, 80])` should return `[10, 22, 33, 50, 60, 80]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(findSequence([10, 22, 9, 33, 21, 50, 60, 80]), [
|
||||
10,
|
||||
22,
|
||||
33,
|
||||
50,
|
||||
60,
|
||||
80
|
||||
]);
|
||||
```
|
||||
|
||||
`findSequence([0, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15])` should return `[0, 2, 6, 9, 11, 15`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
findSequence([0, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15]),
|
||||
[0, 2, 6, 9, 11, 15]
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function findSequence(input) {
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function findSequence(input) {
|
||||
var len = input.length;
|
||||
var result = []
|
||||
for (var i = 0; i < len; i++) result.push(1)
|
||||
|
||||
for (var i = 0; i < len; i++)
|
||||
for (var j = i - 1; j >= 0; j--)
|
||||
if (input[i] > input[j] && result[j] >= result[i])
|
||||
result[i] = result[j] + 1;
|
||||
|
||||
var maxValue = Math.max.apply(null, result);
|
||||
var maxIndex = result.indexOf(Math.max.apply(Math, result));
|
||||
var output = [];
|
||||
output.push(input[maxIndex]);
|
||||
for (var i = maxIndex; i >= 0; i--) {
|
||||
if (maxValue == 0) break;
|
||||
if (input[maxIndex] > input[i] && result[i] == maxValue - 1) {
|
||||
output.push(input[i]);
|
||||
maxValue--;
|
||||
}
|
||||
}
|
||||
output.reverse();
|
||||
return output;
|
||||
}
|
||||
```
|
@ -0,0 +1,100 @@
|
||||
---
|
||||
id: 5e6dd14192286d95fc43046e
|
||||
title: Longest string challenge
|
||||
challengeType: 5
|
||||
forumTopicId: 385275
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
In this challenge, you have to find the strings that are the longest among the given strings.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Write a function that takes an array of strings and returns the strings that have a length equal to the longest length.
|
||||
|
||||
# --hints--
|
||||
|
||||
`longestString` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof longestString == 'function');
|
||||
```
|
||||
|
||||
`longestString(["a", "bb", "ccc", "ee", "f", "ggg"])` should return a array.
|
||||
|
||||
```js
|
||||
assert(Array.isArray(longestString(['a', 'bb', 'ccc', 'ee', 'f', 'ggg'])));
|
||||
```
|
||||
|
||||
`longestString(["a", "bb", "ccc", "ee", "f", "ggg"])` should return `["ccc", "ggg"]'`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(longestString(['a', 'bb', 'ccc', 'ee', 'f', 'ggg']), [
|
||||
'ccc',
|
||||
'ggg'
|
||||
]);
|
||||
```
|
||||
|
||||
`longestString(["afedg", "bb", "sdccc", "efdee", "f", "geegg"])` should return `["afedg", "sdccc", "efdee", "geegg"]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
longestString(['afedg', 'bb', 'sdccc', 'efdee', 'f', 'geegg']),
|
||||
['afedg', 'sdccc', 'efdee', 'geegg']
|
||||
);
|
||||
```
|
||||
|
||||
`longestString(["a", "bhghgb", "ccc", "efde", "fssdrr", "ggg"])` should return `["bhghgb", "fssdrr"]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
longestString(['a', 'bhghgb', 'ccc', 'efde', 'fssdrr', 'ggg']),
|
||||
['bhghgb', 'fssdrr']
|
||||
);
|
||||
```
|
||||
|
||||
`longestString(["ahgfhg", "bdsfsb", "ccc", "ee", "f", "ggdsfg"])` should return `["ahgfhg", "bdsfsb", "ggdsfg"]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
longestString(['ahgfhg', 'bdsfsb', 'ccc', 'ee', 'f', 'ggdsfg']),
|
||||
['ahgfhg', 'bdsfsb', 'ggdsfg']
|
||||
);
|
||||
```
|
||||
|
||||
`longestString(["a", "bbdsf", "ccc", "edfe", "gzzzgg"])` should return `["gzzzgg"]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(longestString(['a', 'bbdsf', 'ccc', 'edfe', 'gzzzgg']), [
|
||||
'gzzzgg'
|
||||
]);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function longestString(strings) {
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function longestString(strings) {
|
||||
var mx = 0;
|
||||
var result = []
|
||||
strings.forEach(function (e) {
|
||||
if (e.length > mx) {
|
||||
mx = e.length
|
||||
result = [e]
|
||||
} else if (e.length == mx)
|
||||
result.push(e)
|
||||
})
|
||||
|
||||
return result
|
||||
}
|
||||
```
|
@ -0,0 +1,91 @@
|
||||
---
|
||||
id: 5e6dd14797f5ce267c2f19d0
|
||||
title: Look-and-say sequence
|
||||
challengeType: 5
|
||||
forumTopicId: 385277
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
The [Look and say sequence](<https://en.wikipedia.org/wiki/Look and say sequence>) is a recursively defined sequence of numbers.
|
||||
|
||||
Sequence Definition
|
||||
|
||||
<ul><li>Take a decimal number</li>
|
||||
<li><span>Look</span> at the number, visually grouping consecutive runs of the same digit.</li>
|
||||
<li><span>Say</span> the number, from left to right, group by group; as how many of that digit there are - followed by the digit grouped.</li></ul><span> This becomes the next number of the sequence.</span>
|
||||
|
||||
An example:
|
||||
|
||||
<ul><li>Starting with the number 1, you have <span>one</span> 1 which produces 11</li>
|
||||
<li>Starting with 11, you have <span>two</span> 1's. I.E.: 21</li>
|
||||
<li>Starting with 21, you have <span>one</span> 2, then <span>one</span> 1. I.E.: (12)(11) which becomes 1211</li>
|
||||
<li>Starting with 1211, you have <span>one</span> 1, <span>one</span> 2, then <span>two</span> 1's. I.E.: (11)(12)(21) which becomes 111221</li></ul>
|
||||
|
||||
# --instructions--
|
||||
|
||||
Write a function that accepts a string as a parameter, processes it, and returns the resultant string.
|
||||
|
||||
# --hints--
|
||||
|
||||
`lookAndSay` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof lookAndSay == 'function');
|
||||
```
|
||||
|
||||
`lookAndSay("1")` should return a string.
|
||||
|
||||
```js
|
||||
assert(typeof lookAndSay('1') == 'string');
|
||||
```
|
||||
|
||||
`lookAndSay("1")` should return `"11"`.
|
||||
|
||||
```js
|
||||
assert.equal(lookAndSay('1'), '11');
|
||||
```
|
||||
|
||||
`lookAndSay("11")` should return `"21"`.
|
||||
|
||||
```js
|
||||
assert.equal(lookAndSay('11'), '21');
|
||||
```
|
||||
|
||||
`lookAndSay("21")` should return `"1211"`.
|
||||
|
||||
```js
|
||||
assert.equal(lookAndSay('21'), '1211');
|
||||
```
|
||||
|
||||
`lookAndSay("1211")` should return `"111221"`.
|
||||
|
||||
```js
|
||||
assert.equal(lookAndSay('1211'), '111221');
|
||||
```
|
||||
|
||||
`lookAndSay("3542")` should return `"13151412"`.
|
||||
|
||||
```js
|
||||
assert.equal(lookAndSay('3542'), '13151412');
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function lookAndSay(str) {
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function lookAndSay(str) {
|
||||
return str.replace(/(.)\1*/g, function(seq, p1) {
|
||||
return seq.length.toString() + p1;
|
||||
});
|
||||
}
|
||||
```
|
@ -0,0 +1,134 @@
|
||||
---
|
||||
id: 5e6dd15004c88cf00d2a78b3
|
||||
title: Loop over multiple arrays simultaneously
|
||||
challengeType: 5
|
||||
forumTopicId: 385279
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Loop over multiple arrays and create a new array whose $i^{th}$ element is the concatenation of $i^{th}$ element of each of the given.
|
||||
|
||||
For this example, if you are given this array of arrays:
|
||||
|
||||
`[ ["a", "b", "c"], ["A", "B", "C"], [1, 2, 3] ]`
|
||||
|
||||
the output should be:
|
||||
|
||||
`["aA1","bB2","cC3"]`
|
||||
|
||||
# --instructions--
|
||||
|
||||
Write a function that takes an array of arrays as a parameter and returns an array of strings satisfying the given description.
|
||||
|
||||
# --hints--
|
||||
|
||||
`loopSimult` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof loopSimult == 'function');
|
||||
```
|
||||
|
||||
`loopSimult([["a", "b", "c"], ["A", "B", "C"], [1, 2, 3]])` should return a array.
|
||||
|
||||
```js
|
||||
assert(
|
||||
Array.isArray(
|
||||
loopSimult([
|
||||
['a', 'b', 'c'],
|
||||
['A', 'B', 'C'],
|
||||
[1, 2, 3]
|
||||
])
|
||||
)
|
||||
);
|
||||
```
|
||||
|
||||
`loopSimult([["a", "b", "c"], ["A", "B", "C"], [1, 2, 3]])` should return `["aA1", "bB2", "cC3"]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
loopSimult([
|
||||
['a', 'b', 'c'],
|
||||
['A', 'B', 'C'],
|
||||
[1, 2, 3]
|
||||
]),
|
||||
['aA1', 'bB2', 'cC3']
|
||||
);
|
||||
```
|
||||
|
||||
`loopSimult([["c", "b", "c"], ["4", "5", "C"], [7, 7, 3]])` should return `["c47", "b57", "cC3"]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
loopSimult([
|
||||
['c', 'b', 'c'],
|
||||
['4', '5', 'C'],
|
||||
[7, 7, 3]
|
||||
]),
|
||||
['c47', 'b57', 'cC3']
|
||||
);
|
||||
```
|
||||
|
||||
`loopSimult([["a", "b", "c", "d"], ["A", "B", "C", "d"], [1, 2, 3, 4]])` should return `["aA1", "bB2", "cC3", "dd4"]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
loopSimult([
|
||||
['a', 'b', 'c', 'd'],
|
||||
['A', 'B', 'C', 'd'],
|
||||
[1, 2, 3, 4]
|
||||
]),
|
||||
['aA1', 'bB2', 'cC3', 'dd4']
|
||||
);
|
||||
```
|
||||
|
||||
`loopSimult([["a", "b"], ["A", "B"], [1, 2]])` should return `["aA1", "bB2"]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
loopSimult([
|
||||
['a', 'b'],
|
||||
['A', 'B'],
|
||||
[1, 2]
|
||||
]),
|
||||
['aA1', 'bB2']
|
||||
);
|
||||
```
|
||||
|
||||
`loopSimult([["b", "c"], ["B", "C"], [2, 3]])` should return `["bB2", "cC3"]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
loopSimult([
|
||||
['b', 'c'],
|
||||
['B', 'C'],
|
||||
[2, 3]
|
||||
]),
|
||||
['bB2', 'cC3']
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function loopSimult(A) {
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function loopSimult(A) {
|
||||
var res = [], output;
|
||||
for (var i = 0; i < A[0].length; i += 1) {
|
||||
output = "";
|
||||
for (var j = 0; j < A.length; j++)
|
||||
output += A[j][i];
|
||||
res.push(output);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
```
|
@ -0,0 +1,317 @@
|
||||
---
|
||||
id: 5e6decd8ec8d7db960950d1c
|
||||
title: LU decomposition
|
||||
challengeType: 5
|
||||
forumTopicId: 385280
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Every square matrix $A$ can be decomposed into a product of a lower triangular matrix $L$ and a upper triangular matrix $U$, as described in [LU decomposition](<https://en.wikipedia.org/wiki/LU decomposition>).
|
||||
|
||||
$A = LU$
|
||||
|
||||
It is a modified form of Gaussian elimination.
|
||||
|
||||
While the [Cholesky decomposition](<http://rosettacode.org/wiki/Cholesky decomposition>) only works for symmetric, positive definite matrices, the more general LU decomposition works for any square matrix.
|
||||
|
||||
There are several algorithms for calculating $L$ and $U$.
|
||||
|
||||
To derive *Crout's algorithm* for a 3x3 example, we have to solve the following system:
|
||||
|
||||
\\begin{align}A = \\begin{pmatrix} a\_{11} & a\_{12} & a\_{13}\\\\ a\_{21} & a\_{22} & a\_{23}\\\\ a\_{31} & a\_{32} & a\_{33}\\\\ \\end{pmatrix}= \\begin{pmatrix} l\_{11} & 0 & 0 \\\\ l\_{21} & l\_{22} & 0 \\\\ l\_{31} & l\_{32} & l\_{33}\\\\ \\end{pmatrix} \\begin{pmatrix} u\_{11} & u\_{12} & u\_{13} \\\\ 0 & u\_{22} & u\_{23} \\\\ 0 & 0 & u\_{33} \\end{pmatrix} = LU\\end{align}
|
||||
|
||||
We now would have to solve 9 equations with 12 unknowns. To make the system uniquely solvable, usually the diagonal elements of $L$ are set to 1
|
||||
|
||||
$l\_{11}=1$
|
||||
|
||||
$l\_{22}=1$
|
||||
|
||||
$l\_{33}=1$
|
||||
|
||||
so we get a solvable system of 9 unknowns and 9 equations.
|
||||
|
||||
\\begin{align}A = \\begin{pmatrix} a\_{11} & a\_{12} & a\_{13}\\\\ a\_{21} & a\_{22} & a\_{23}\\\\ a\_{31} & a\_{32} & a\_{33}\\\\ \\end{pmatrix} = \\begin{pmatrix} 1 & 0 & 0 \\\\ l\_{21} & 1 & 0 \\\\ l\_{31} & l\_{32} & 1\\\\ \\end{pmatrix} \\begin{pmatrix} u\_{11} & u\_{12} & u\_{13} \\\\ 0 & u\_{22} & u\_{23} \\\\ 0 & 0 & u\_{33} \\end{pmatrix} = \\begin{pmatrix} u\_{11} & u\_{12} & u\_{13} \\\\ u\_{11}l\_{21} & u\_{12}l\_{21}+u\_{22} & u\_{13}l\_{21}+u\_{23} \\\\ u\_{11}l\_{31} & u\_{12}l\_{31}+u\_{22}l\_{32} & u\_{13}l\_{31} + u\_{23}l\_{32}+u\_{33} \\end{pmatrix} = LU\\end{align}
|
||||
|
||||
Solving for the other $l$ and $u$, we get the following equations:
|
||||
|
||||
$u\_{11}=a\_{11}$
|
||||
|
||||
$u\_{12}=a\_{12}$
|
||||
|
||||
$u\_{13}=a\_{13}$
|
||||
|
||||
$u\_{22}=a\_{22} - u\_{12}l\_{21}$
|
||||
|
||||
$u\_{23}=a\_{23} - u\_{13}l\_{21}$
|
||||
|
||||
$u\_{33}=a\_{33} - (u\_{13}l\_{31} + u\_{23}l\_{32})$
|
||||
|
||||
and for $l$:
|
||||
|
||||
$l\_{21}=\\frac{1}{u\_{11}} a\_{21}$
|
||||
|
||||
$l\_{31}=\\frac{1}{u\_{11}} a\_{31}$
|
||||
|
||||
$l\_{32}=\\frac{1}{u\_{22}} (a\_{32} - u\_{12}l\_{31})$
|
||||
|
||||
We see that there is a calculation pattern, which can be expressed as the following formulas, first for $U$
|
||||
|
||||
$u\_{ij} = a\_{ij} - \\sum\_{k=1}^{i-1} u\_{kj}l\_{ik}$
|
||||
|
||||
and then for $L$
|
||||
|
||||
$l\_{ij} = \\frac{1}{u\_{jj}} (a\_{ij} - \\sum\_{k=1}^{j-1} u\_{kj}l\_{ik})$
|
||||
|
||||
We see in the second formula that to get the $l\_{ij}$ below the diagonal, we have to divide by the diagonal element (pivot) $u\_{jj}$, so we get problems when $u\_{jj}$ is either 0 or very small, which leads to numerical instability.
|
||||
|
||||
The solution to this problem is *pivoting* $A$, which means rearranging the rows of $A$, prior to the $LU$ decomposition, in a way that the largest element of each column gets onto the diagonal of $A$. Rearranging the rows means to multiply $A$ by a permutation matrix $P$:
|
||||
|
||||
$PA \\Rightarrow A'$
|
||||
|
||||
Example:
|
||||
|
||||
\\begin{align} \\begin{pmatrix} 0 & 1 \\\\ 1 & 0 \\end{pmatrix} \\begin{pmatrix} 1 & 4 \\\\ 2 & 3 \\end{pmatrix} \\Rightarrow \\begin{pmatrix} 2 & 3 \\\\ 1 & 4 \\end{pmatrix} \\end{align}
|
||||
|
||||
The decomposition algorithm is then applied on the rearranged matrix so that
|
||||
|
||||
$PA = LU$
|
||||
|
||||
# --instructions--
|
||||
|
||||
The task is to implement a routine which will take a square nxn matrix $A$ and return a lower triangular matrix $L$, a upper triangular matrix $U$ and a permutation matrix $P$, so that the above equation is fullfilled. The returned value should be in the form `[L, U, P]`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`luDecomposition` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof luDecomposition == 'function');
|
||||
```
|
||||
|
||||
`luDecomposition([[1, 3, 5], [2, 4, 7], [1, 1, 0]])` should return a array.
|
||||
|
||||
```js
|
||||
assert(
|
||||
Array.isArray(
|
||||
luDecomposition([
|
||||
[1, 3, 5],
|
||||
[2, 4, 7],
|
||||
[1, 1, 0]
|
||||
])
|
||||
)
|
||||
);
|
||||
```
|
||||
|
||||
`luDecomposition([[1, 3, 5], [2, 4, 7], [1, 1, 0]])` should return `[[[1, 0, 0], [0.5, 1, 0], [0.5, -1, 1]], [[2, 4, 7], [0, 1, 1.5], [0, 0, -2]], [[0, 1, 0], [1, 0, 0], [0, 0, 1]]]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
luDecomposition([
|
||||
[1, 3, 5],
|
||||
[2, 4, 7],
|
||||
[1, 1, 0]
|
||||
]),
|
||||
[
|
||||
[
|
||||
[1, 0, 0],
|
||||
[0.5, 1, 0],
|
||||
[0.5, -1, 1]
|
||||
],
|
||||
[
|
||||
[2, 4, 7],
|
||||
[0, 1, 1.5],
|
||||
[0, 0, -2]
|
||||
],
|
||||
[
|
||||
[0, 1, 0],
|
||||
[1, 0, 0],
|
||||
[0, 0, 1]
|
||||
]
|
||||
]
|
||||
);
|
||||
```
|
||||
|
||||
`luDecomposition([[11, 9, 24, 2], [1, 5, 2, 6], [3, 17, 18, 1], [2, 5, 7, 1]])` should return `[[[1, 0, 0, 0], [0.2727272727272727, 1, 0, 0], [0.09090909090909091, 0.2875, 1, 0], [0.18181818181818182, 0.23124999999999996, 0.0035971223021580693, 1]], [[11, 9, 24, 2], [0, 14.545454545454547, 11.454545454545455, 0.4545454545454546], [0, 0, -3.4749999999999996, 5.6875], [0, 0, 0, 0.510791366906476]], [[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1]]]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
luDecomposition([
|
||||
[11, 9, 24, 2],
|
||||
[1, 5, 2, 6],
|
||||
[3, 17, 18, 1],
|
||||
[2, 5, 7, 1]
|
||||
]),
|
||||
[
|
||||
[
|
||||
[1, 0, 0, 0],
|
||||
[0.2727272727272727, 1, 0, 0],
|
||||
[0.09090909090909091, 0.2875, 1, 0],
|
||||
[0.18181818181818182, 0.23124999999999996, 0.0035971223021580693, 1]
|
||||
],
|
||||
[
|
||||
[11, 9, 24, 2],
|
||||
[0, 14.545454545454547, 11.454545454545455, 0.4545454545454546],
|
||||
[0, 0, -3.4749999999999996, 5.6875],
|
||||
[0, 0, 0, 0.510791366906476]
|
||||
],
|
||||
[
|
||||
[1, 0, 0, 0],
|
||||
[0, 0, 1, 0],
|
||||
[0, 1, 0, 0],
|
||||
[0, 0, 0, 1]
|
||||
]
|
||||
]
|
||||
);
|
||||
```
|
||||
|
||||
`luDecomposition([[1, 1, 1], [4, 3, -1], [3, 5, 3]])` should return `[[[1, 0, 0], [0.75, 1, 0], [0.25, 0.09090909090909091, 1]], [[4, 3, -1], [0, 2.75, 3.75], [0, 0, 0.9090909090909091]], [[0, 1, 0], [0, 0, 1], [1, 0, 0]]]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
luDecomposition([
|
||||
[1, 1, 1],
|
||||
[4, 3, -1],
|
||||
[3, 5, 3]
|
||||
]),
|
||||
[
|
||||
[
|
||||
[1, 0, 0],
|
||||
[0.75, 1, 0],
|
||||
[0.25, 0.09090909090909091, 1]
|
||||
],
|
||||
[
|
||||
[4, 3, -1],
|
||||
[0, 2.75, 3.75],
|
||||
[0, 0, 0.9090909090909091]
|
||||
],
|
||||
[
|
||||
[0, 1, 0],
|
||||
[0, 0, 1],
|
||||
[1, 0, 0]
|
||||
]
|
||||
]
|
||||
);
|
||||
```
|
||||
|
||||
`luDecomposition([[1, -2, 3], [2, -5, 12], [0, 2, -10]])` should return `[[[1, 0, 0], [0, 1, 0], [0.5, 0.25, 1]], [[2, -5, 12], [0, 2, -10], [0, 0, -0.5]], [[0, 1, 0], [0, 0, 1], [1, 0, 0]]]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
luDecomposition([
|
||||
[1, -2, 3],
|
||||
[2, -5, 12],
|
||||
[0, 2, -10]
|
||||
]),
|
||||
[
|
||||
[
|
||||
[1, 0, 0],
|
||||
[0, 1, 0],
|
||||
[0.5, 0.25, 1]
|
||||
],
|
||||
[
|
||||
[2, -5, 12],
|
||||
[0, 2, -10],
|
||||
[0, 0, -0.5]
|
||||
],
|
||||
[
|
||||
[0, 1, 0],
|
||||
[0, 0, 1],
|
||||
[1, 0, 0]
|
||||
]
|
||||
]
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function luDecomposition(A) {
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function luDecomposition(A) {
|
||||
|
||||
function dotProduct(a, b) {
|
||||
var sum = 0;
|
||||
for (var i = 0; i < a.length; i++)
|
||||
sum += a[i] * b[i]
|
||||
return sum;
|
||||
}
|
||||
|
||||
function matrixMul(A, B) {
|
||||
var result = new Array(A.length);
|
||||
for (var i = 0; i < A.length; i++)
|
||||
result[i] = new Array(B[0].length)
|
||||
var aux = new Array(B.length);
|
||||
|
||||
for (var j = 0; j < B[0].length; j++) {
|
||||
|
||||
for (var k = 0; k < B.length; k++)
|
||||
aux[k] = B[k][j];
|
||||
|
||||
for (var i = 0; i < A.length; i++)
|
||||
result[i][j] = dotProduct(A[i], aux);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function pivotize(m) {
|
||||
var n = m.length;
|
||||
var id = new Array(n);
|
||||
for (var i = 0; i < n; i++) {
|
||||
id[i] = new Array(n);
|
||||
id[i].fill(0)
|
||||
id[i][i] = 1;
|
||||
}
|
||||
|
||||
for (var i = 0; i < n; i++) {
|
||||
var maxm = m[i][i];
|
||||
var row = i;
|
||||
for (var j = i; j < n; j++)
|
||||
if (m[j][i] > maxm) {
|
||||
maxm = m[j][i];
|
||||
row = j;
|
||||
}
|
||||
|
||||
if (i != row) {
|
||||
var tmp = id[i];
|
||||
id[i] = id[row];
|
||||
id[row] = tmp;
|
||||
}
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
var n = A.length;
|
||||
var L = new Array(n);
|
||||
for (var i = 0; i < n; i++) { L[i] = new Array(n); L[i].fill(0) }
|
||||
var U = new Array(n);
|
||||
for (var i = 0; i < n; i++) { U[i] = new Array(n); U[i].fill(0) }
|
||||
var P = pivotize(A);
|
||||
var A2 = matrixMul(P, A);
|
||||
|
||||
for (var j = 0; j < n; j++) {
|
||||
L[j][j] = 1;
|
||||
for (var i = 0; i < j + 1; i++) {
|
||||
var s1 = 0;
|
||||
for (var k = 0; k < i; k++)
|
||||
s1 += U[k][j] * L[i][k];
|
||||
U[i][j] = A2[i][j] - s1;
|
||||
}
|
||||
for (var i = j; i < n; i++) {
|
||||
var s2 = 0;
|
||||
for (var k = 0; k < j; k++)
|
||||
s2 += U[k][j] * L[i][k];
|
||||
L[i][j] = (A2[i][j] - s2) / U[j][j];
|
||||
}
|
||||
}
|
||||
return [L, U, P];
|
||||
}
|
||||
```
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user