chore(i8n,learn): processed translations
This commit is contained in:
committed by
Mrugesh Mohapatra
parent
15047f2d90
commit
e5c44a3ae5
@ -1,30 +1,34 @@
|
||||
---
|
||||
id: 594810f028c0303b75339acb
|
||||
title: 100门
|
||||
title: 100 doors
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302217
|
||||
dashedName: 100-doors
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
<p>连续100个门都是最初关闭的。你可以在门口进行100次通行证。第一次通过,访问每扇门并“切换”门(如果门关闭,打开它;如果它打开,关闭它)。第二次,只访问每个第二个门(即门#2,#4,#6,......)并切换它。第三次,访问每个第3门(即3号门,#6号,#9号,......)等,直到您只访问第100个门。 </p><p>实现一个功能,以确定最后一次通过后门的状态。将最终结果返回到数组中,如果数组打开,则只包含数字中包含的门号。 </p>
|
||||
There are 100 doors in a row that are all initially closed. You make 100 passes by the doors. The first time through, visit every door and 'toggle' the door (if the door is closed, open it; if it is open, close it). The second time, only visit every 2nd door (i.e., door #2, #4, #6, ...) and toggle it. The third time, visit every 3rd door (i.e., door #3, #6, #9, ...), etc., until you only visit the 100th door.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Implement a function to determine the state of the doors after the last pass. Return the final result in an array, with only the door number included in the array if it is open.
|
||||
|
||||
# --hints--
|
||||
|
||||
`getFinalOpenedDoors`是一个函数。
|
||||
`getFinalOpenedDoors` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof getFinalOpenedDoors === 'function');
|
||||
```
|
||||
|
||||
`getFinalOpenedDoors`应该返回一个数组。
|
||||
`getFinalOpenedDoors` should return an array.
|
||||
|
||||
```js
|
||||
assert(Array.isArray(getFinalOpenedDoors(100)));
|
||||
```
|
||||
|
||||
`getFinalOpenedDoors`没有产生正确的结果。
|
||||
`getFinalOpenedDoors` should produce the correct result.
|
||||
|
||||
```js
|
||||
assert.deepEqual(getFinalOpenedDoors(100), solution);
|
||||
|
@ -1,45 +1,66 @@
|
||||
---
|
||||
id: 5951e88f64ebf159166a1176
|
||||
title: 24场比赛
|
||||
title: 24 game
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302218
|
||||
dashedName: 24-game
|
||||
---
|
||||
|
||||
# --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>
|
||||
The [24 Game](https://en.wikipedia.org/wiki/24_Game) tests a person's mental arithmetic.
|
||||
|
||||
The aim of the game is to arrange four numbers in a way that when evaluated, the result is 24
|
||||
|
||||
# --instructions--
|
||||
|
||||
Implement a function that takes a string of four digits as its argument, with each digit from 1 to 9 (inclusive) with repetitions allowed, and returns an arithmetic expression that evaluates to the number 24. If no such solution exists, return "no solution exists".
|
||||
|
||||
**Rules:**
|
||||
<ul>
|
||||
<li> Only the following operators/functions are allowed: multiplication, division, addition, subtraction. </li>
|
||||
<li> Division should use floating point or rational arithmetic, etc, to preserve remainders. </li>
|
||||
<li> Forming multiple digit numbers from the supplied digits is disallowed. (So an answer of 12+12 when given 1, 2, 2, and 1 is wrong). </li>
|
||||
<li> The order of the digits when given does not have to be preserved. </li>
|
||||
</ul>
|
||||
|
||||
| Example input | Example output |
|
||||
| ------------------------- | ------------------------- |
|
||||
| <code>solve24("4878");</code> | <code>(7-8/8)\*4</code> |
|
||||
| <code>solve24("1234");</code> | <code>3\*1\*4\*2</code> |
|
||||
| <code>solve24("6789");</code> | <code>(6\*8)/(9-7)</code> |
|
||||
| <code>solve24("1127");</code> | <code>(1+7)\*(2+1)</code> |
|
||||
|
||||
# --hints--
|
||||
|
||||
`solve24`是一个函数。
|
||||
`solve24` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof solve24 === 'function');
|
||||
```
|
||||
|
||||
`solve24("4878")`应返回`(7-8/8)*4`或`4*(7-8/8)`
|
||||
`solve24("4878")` should return `(7-8/8)*4` or `4*(7-8/8)`
|
||||
|
||||
```js
|
||||
assert(include(answers[0], solve24(testCases[0])));
|
||||
assert(include(answers[0], removeParentheses(solve24(testCases[0]))));
|
||||
```
|
||||
|
||||
`solve24("1234")`应返回`1*2*3*4`任何排列
|
||||
`solve24("1234")` should return any arrangement of `1*2*3*4`
|
||||
|
||||
```js
|
||||
assert(include(answers[1], solve24(testCases[1])));
|
||||
assert(include(answers[1], removeParentheses(solve24(testCases[1]))));
|
||||
```
|
||||
|
||||
`solve24("6789")`应返回`(6*8)/(9-7)`或`(8*6)/(9-7)`
|
||||
`solve24("6789")` should return `(6*8)/(9-7)` or `(8*6)/(9-7)`
|
||||
|
||||
```js
|
||||
assert(include(answers[2], solve24(testCases[2])));
|
||||
assert(include(answers[2], removeParentheses(solve24(testCases[2]))));
|
||||
```
|
||||
|
||||
`solve24("1127")`应该返回`(1+7)*(1*2)`的排列
|
||||
`solve24("1127")` should return a permutation of `(1+7)*(1+2)`
|
||||
|
||||
```js
|
||||
assert(include(answers[3], solve24(testCases[3])));
|
||||
assert(include(answers[3], removeParentheses(solve24(testCases[3]))));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
@ -1,60 +1,84 @@
|
||||
---
|
||||
id: 5949b579404977fbaefcd736
|
||||
title: 90亿上帝的名字整数
|
||||
title: 9 billion names of God the integer
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302219
|
||||
dashedName: 9-billion-names-of-god-the-integer
|
||||
---
|
||||
|
||||
# --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>
|
||||
This task is a variation of the [short story by Arthur C. Clarke](https://en.wikipedia.org/wiki/The Nine Billion Names of God#Plot_summary "wp: The Nine Billion Names of God#Plot_summary").
|
||||
|
||||
(Solvers should be aware of the consequences of completing this task.)
|
||||
|
||||
In detail, to specify what is meant by a "name":
|
||||
|
||||
<ul>
|
||||
<li>The integer 1 has 1 name "1".</li>
|
||||
<li>The integer 2 has 2 names "1+1" and "2".</li>
|
||||
<li>The integer 3 has 3 names "1+1+1", "2+1", and "3".</li>
|
||||
<li>The integer 4 has 5 names "1+1+1+1", "2+1+1", "2+2", "3+1", "4".</li>
|
||||
<li>The integer 5 has 7 names "1+1+1+1+1", "2+1+1+1", "2+2+1", "3+1+1", "3+2", "4+1", "5".</li>
|
||||
</ul>
|
||||
|
||||
This can be visualized in the following form:
|
||||
|
||||
<pre> 1
|
||||
1 1
|
||||
1 1 1
|
||||
1 2 1 1
|
||||
1 2 2 1 1
|
||||
1 3 3 2 1 1
|
||||
</pre>
|
||||
|
||||
Where row $n$ corresponds to integer $n$, and each column $C$ in row $m$ from left to right corresponds to the number of names beginning with $C$.
|
||||
|
||||
Optionally note that the sum of the $n$-th row $P(n)$ is the integer partition function.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Implement a function that returns the sum of the $n$-th row.
|
||||
|
||||
# --hints--
|
||||
|
||||
`numberOfNames`是一个函数。
|
||||
`numberOfNames` should be function.
|
||||
|
||||
```js
|
||||
assert(typeof numberOfNames === 'function');
|
||||
```
|
||||
|
||||
`numberOfNames(5)`应该等于7。
|
||||
`numberOfNames(5)` should equal 7.
|
||||
|
||||
```js
|
||||
assert.equal(numberOfNames(5), 7);
|
||||
```
|
||||
|
||||
`numberOfNames(12)`应该等于77。
|
||||
`numberOfNames(12)` should equal 77.
|
||||
|
||||
```js
|
||||
assert.equal(numberOfNames(12), 77);
|
||||
```
|
||||
|
||||
`numberOfNames(18)`应该等于385。
|
||||
`numberOfNames(18)` should equal 385.
|
||||
|
||||
```js
|
||||
assert.equal(numberOfNames(18), 385);
|
||||
```
|
||||
|
||||
`numberOfNames(23)`应该等于1255。
|
||||
`numberOfNames(23)` should equal 1255.
|
||||
|
||||
```js
|
||||
assert.equal(numberOfNames(23), 1255);
|
||||
```
|
||||
|
||||
`numberOfNames(42)`应该等于53174。
|
||||
`numberOfNames(42)` should equal 53174.
|
||||
|
||||
```js
|
||||
assert.equal(numberOfNames(42), 53174);
|
||||
```
|
||||
|
||||
`numberOfNames(123)`应该等于2552338241。
|
||||
`numberOfNames(123)` should equal 2552338241.
|
||||
|
||||
```js
|
||||
assert.equal(numberOfNames(123), 2552338241);
|
||||
|
@ -1,60 +1,93 @@
|
||||
---
|
||||
id: 594810f028c0303b75339acc
|
||||
title: ABC问题
|
||||
title: ABC Problem
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302220
|
||||
dashedName: abc-problem
|
||||
---
|
||||
|
||||
# --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>
|
||||
You are given a collection of ABC blocks (e.g., childhood alphabet blocks). There are 20 blocks with two letters on each block. A complete alphabet is guaranteed amongst all sides of the blocks. The sample collection of blocks:
|
||||
|
||||
<pre>(B O)
|
||||
(X K)
|
||||
(D Q)
|
||||
(C P)
|
||||
(N A)
|
||||
(G T)
|
||||
(R E)
|
||||
(T G)
|
||||
(Q D)
|
||||
(F S)
|
||||
(J W)
|
||||
(H U)
|
||||
(V I)
|
||||
(A N)
|
||||
(O B)
|
||||
(E R)
|
||||
(F S)
|
||||
(L Y)
|
||||
(P C)
|
||||
(Z M)
|
||||
</pre>
|
||||
|
||||
# --instructions--
|
||||
|
||||
Implement a function that takes a string (word) and determines whether the word can be spelled with the given collection of blocks.
|
||||
|
||||
Some rules to keep in mind:
|
||||
|
||||
<ul>
|
||||
<li>Once a letter on a block is used, that block cannot be used again.</li>
|
||||
<li>The function should be case-insensitive.</li>
|
||||
</ul>
|
||||
|
||||
# --hints--
|
||||
|
||||
`canMakeWord`是一个功能。
|
||||
`canMakeWord` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof canMakeWord === 'function');
|
||||
```
|
||||
|
||||
`canMakeWord`应该返回一个布尔值。
|
||||
`canMakeWord` should return a boolean.
|
||||
|
||||
```js
|
||||
assert(typeof canMakeWord('hi') === 'boolean');
|
||||
```
|
||||
|
||||
`canMakeWord("bark")`应该返回true。
|
||||
`canMakeWord("bark")` should return true.
|
||||
|
||||
```js
|
||||
assert(canMakeWord(words[0]));
|
||||
```
|
||||
|
||||
`canMakeWord("BooK")`应该返回false。
|
||||
`canMakeWord("BooK")` should return false.
|
||||
|
||||
```js
|
||||
assert(!canMakeWord(words[1]));
|
||||
```
|
||||
|
||||
`canMakeWord("TReAT")`应该返回true。
|
||||
`canMakeWord("TReAT")` should return true.
|
||||
|
||||
```js
|
||||
assert(canMakeWord(words[2]));
|
||||
```
|
||||
|
||||
`canMakeWord("COMMON")`应返回false。
|
||||
`canMakeWord("COMMON")` should return false.
|
||||
|
||||
```js
|
||||
assert(!canMakeWord(words[3]));
|
||||
```
|
||||
|
||||
`canMakeWord("squAD")`应该返回true。
|
||||
`canMakeWord("squAD")` should return true.
|
||||
|
||||
```js
|
||||
assert(canMakeWord(words[4]));
|
||||
```
|
||||
|
||||
`canMakeWord("conFUSE")`应该返回true。
|
||||
`canMakeWord("conFUSE")` should return true.
|
||||
|
||||
```js
|
||||
assert(canMakeWord(words[5]));
|
||||
|
@ -1,36 +1,50 @@
|
||||
---
|
||||
id: 594810f028c0303b75339acd
|
||||
title: 丰富,不足和完善的数字分类
|
||||
title: 'Abundant, deficient and perfect number classifications'
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302221
|
||||
dashedName: abundant-deficient-and-perfect-number-classifications
|
||||
---
|
||||
|
||||
# --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>
|
||||
These define three classifications of positive integers based on their [proper divisors](https://rosettacode.org/wiki/Proper divisors "Proper divisors").
|
||||
|
||||
Let $P(n)$ be the sum of the proper divisors of `n` where proper divisors are all positive integers `n` other than `n` itself.
|
||||
|
||||
If `P(n) < n` then `n` is classed as `deficient`
|
||||
|
||||
If `P(n) === n` then `n` is classed as `perfect`
|
||||
|
||||
If `P(n) > n` then `n` is classed as `abundant`
|
||||
|
||||
**Example**: `6` has proper divisors of `1`, `2`, and `3`. `1 + 2 + 3 = 6`, so `6` is classed as a perfect number.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Implement a function that calculates how many of the integers from `1` to `20,000` (inclusive) are in each of the three classes. Output the result as an array in the following format `[deficient, perfect, abundant]`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`getDPA`是一个功能。
|
||||
`getDPA` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof getDPA === 'function');
|
||||
```
|
||||
|
||||
`getDPA`应该返回一个数组。
|
||||
`getDPA` should return an array.
|
||||
|
||||
```js
|
||||
assert(Array.isArray(getDPA(100)));
|
||||
```
|
||||
|
||||
`getDPA`返回值的长度应为3。
|
||||
`getDPA` return value should have a length of 3.
|
||||
|
||||
```js
|
||||
assert(getDPA(100).length === 3);
|
||||
```
|
||||
|
||||
`getDPA(20000)`应该等于[15043,4,4953]
|
||||
`getDPA(20000)` should equal [15043, 4, 4953]
|
||||
|
||||
```js
|
||||
assert.deepEqual(getDPA(20000), solution);
|
||||
|
@ -1,36 +1,48 @@
|
||||
---
|
||||
id: 594810f028c0303b75339ace
|
||||
title: 蓄能器工厂
|
||||
title: Accumulator factory
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302222
|
||||
dashedName: accumulator-factory
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
<p>创建一个带有单个(数字)参数的函数,并返回另一个作为累加器的函数。返回的累加器函数又接受一个数字参数,并返回到目前为止传递给该累加器的所有数值的总和(包括创建累加器时传递的初始值)。 </p><p>规则: </p><p>不要使用全局变量。 </p><p>暗示: </p><p>闭包可以保存外部状态。 </p>
|
||||
A problem posed by [Paul Graham](https://en.wikipedia.org/wiki/Paul_Graham_(programmer)) is that of creating a function that takes a single (numeric) argument and which returns another function that is an accumulator. The returned accumulator function in turn also takes a single numeric argument, and returns the sum of all the numeric values passed in so far to that accumulator (including the initial value passed when the accumulator was created).
|
||||
|
||||
# --instructions--
|
||||
|
||||
Create a function that takes a number $n$ and generates accumulator functions that return the sum of every number ever passed to them.
|
||||
|
||||
**Rules:**
|
||||
|
||||
Do not use global variables.
|
||||
|
||||
**Hint:**
|
||||
|
||||
Closures save outer state.
|
||||
|
||||
# --hints--
|
||||
|
||||
`accumulator`是一个功能。
|
||||
`accumulator` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof accumulator === 'function');
|
||||
```
|
||||
|
||||
`accumulator(0)`应该返回一个函数。
|
||||
`accumulator(0)` should return a function.
|
||||
|
||||
```js
|
||||
assert(typeof accumulator(0) === 'function');
|
||||
```
|
||||
|
||||
`accumulator(0)(2)`应该返回一个数字。
|
||||
`accumulator(0)(2)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof accumulator(0)(2) === 'number');
|
||||
```
|
||||
|
||||
传递值3,-4,1.5和5应返回5.5。
|
||||
Passing in the values 3, -4, 1.5, and 5 should return 5.5.
|
||||
|
||||
```js
|
||||
assert(testFn(5) === 5.5);
|
||||
|
@ -1,42 +1,52 @@
|
||||
---
|
||||
id: 594810f028c0303b75339acf
|
||||
title: 阿克曼功能
|
||||
title: Ackermann function
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302223
|
||||
dashedName: ackermann-function
|
||||
---
|
||||
|
||||
# --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>
|
||||
The Ackermann function is a classic example of a recursive function, notable especially because it is not a primitive recursive function. It grows very quickly in value, as does the size of its call tree.
|
||||
|
||||
The Ackermann function is usually defined as follows:
|
||||
|
||||
$A(m, n) = \\begin{cases} n+1 & \\mbox{if } m = 0 \\\\ A(m-1, 1) & \\mbox{if } m > 0 \\mbox{ and } n = 0 \\\\ A(m-1, A(m, n-1)) & \\mbox{if } m > 0 \\mbox{ and } n > 0. \\end{cases}$
|
||||
|
||||
Its arguments are never negative and it always terminates.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Write a function which returns the value of $A(m, n)$. Arbitrary precision is preferred (since the function grows so quickly), but not required.
|
||||
|
||||
# --hints--
|
||||
|
||||
`ack`是一个功能。
|
||||
`ack` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof ack === 'function');
|
||||
```
|
||||
|
||||
`ack(0, 0)`应该返回1。
|
||||
`ack(0, 0)` should return 1.
|
||||
|
||||
```js
|
||||
assert(ack(0, 0) === 1);
|
||||
```
|
||||
|
||||
`ack(1, 1)`应该返回3。
|
||||
`ack(1, 1)` should return 3.
|
||||
|
||||
```js
|
||||
assert(ack(1, 1) === 3);
|
||||
```
|
||||
|
||||
`ack(2, 5)`应该返回13。
|
||||
`ack(2, 5)` should return 13.
|
||||
|
||||
```js
|
||||
assert(ack(2, 5) === 13);
|
||||
```
|
||||
|
||||
`ack(3, 3)`应该返回61。
|
||||
`ack(3, 3)` should return 61.
|
||||
|
||||
```js
|
||||
assert(ack(3, 3) === 61);
|
||||
|
@ -1,46 +1,64 @@
|
||||
---
|
||||
id: 594810f028c0303b75339ad0
|
||||
title: 对齐列
|
||||
title: Align columns
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302224
|
||||
dashedName: align-columns
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
<p>给定一个包含许多行的文本文件,其中一行中的字段由单个<code>$</code>字符描述,编写一个程序,通过确保每列中的单词至少由一个空格分隔来对齐每列字段。此外,允许列中的每个单词在其列中左对齐,右对齐或居中对齐。 </p><p>使用以下文本测试您的程序: </p><pre>的$ $多行给出$ A $文本$文件$
|
||||
其中$领域内$ A $线$ $
|
||||
是$由$ A $单个$ '美元' $字符划定$
|
||||
写$ A $程序
|
||||
即$对齐$ $场的每个$列$
|
||||
由$ $确保该$字美元$ $每
|
||||
列$ $是在$最少$一张价值$空间被分隔$ $。
|
||||
此外,$允许为$ $ $中的每个字$ $ A $ $列到$为$要么离开$ $
|
||||
对齐,右$ $有道理
|
||||
或$ $中心内$有道理$ $其列。
|
||||
</pre><p>注意: </p>示例输入文本行可以或可以不具有尾随美元字符。所有列应共享相同的对齐方式。为了完成任务,在行尾附近产生的连续空格字符是无关紧要的。输出文本将以纯文本编辑器或基本终端上的单行间隔字体查看。列之间的最小间距应根据文本计算,而不是硬编码。不需要在列之间或列周围添加分隔字符。
|
||||
Given a text file of many lines, where fields within a line are delineated by a single `$` 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.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Use the following text to test your programs:
|
||||
|
||||
<pre>
|
||||
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.
|
||||
</pre>
|
||||
|
||||
**Note that:**
|
||||
|
||||
<ul>
|
||||
<li>The example input texts lines may, or may not, have trailing dollar characters.</li>
|
||||
<li>All columns should share the same alignment.</li>
|
||||
<li>Consecutive space characters produced adjacent to the end of lines are insignificant for the purposes of the task.</li>
|
||||
<li>Output text will be viewed in a mono-spaced font on a plain text editor or basic terminal.</li>
|
||||
<li>The minimum space between columns should be computed from the text and not hard-coded.</li>
|
||||
<li>It is not a requirement to add separating characters between or around columns.</li>
|
||||
</ul>
|
||||
|
||||
# --hints--
|
||||
|
||||
`formatText`是一个函数。
|
||||
`formatText` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof formatText === 'function');
|
||||
```
|
||||
|
||||
具有上述输入和“右”对齐的`formatText`应产生以下内容:
|
||||
`formatText` with the above input and "right" justification should produce the following:
|
||||
|
||||
```js
|
||||
assert.strictEqual(formatText(testInput, 'right'), rightAligned);
|
||||
```
|
||||
|
||||
具有上述输入和“左”对齐的`formatText`应产生以下内容:
|
||||
`formatText` with the above input and "left" justification should produce the following:
|
||||
|
||||
```js
|
||||
assert.strictEqual(formatText(testInput, 'left'), leftAligned);
|
||||
```
|
||||
|
||||
具有上述输入和“居中”对齐的`formatText`应产生以下内容:
|
||||
`formatText` with the above input and "center" justification should produce the following:
|
||||
|
||||
```js
|
||||
assert.strictEqual(formatText(testInput, 'center'), centerAligned);
|
||||
|
@ -1,36 +1,49 @@
|
||||
---
|
||||
id: 5949b579404977fbaefcd737
|
||||
title: 友好的对
|
||||
title: Amicable pairs
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302225
|
||||
dashedName: amicable-pairs
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
如果$ 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> "等分序列分类")及其友好分类。
|
||||
Two integers $N$ and $M$ are said to be [amicable pairs](https://en.wikipedia.org/wiki/Amicable numbers "wp: Amicable numbers") if $N \\neq M$ and the sum of the [proper divisors](https://rosettacode.org/wiki/Proper divisors "Proper divisors") of $N$ ($\\mathrm{sum}(\\mathrm{propDivs}(N))$) $= M$ as well as $\\mathrm{sum}(\\mathrm{propDivs}(M)) = N$.
|
||||
|
||||
**Example:**
|
||||
|
||||
**1184** and **1210** are an amicable pair, with proper divisors:
|
||||
|
||||
<ul>
|
||||
<li>1, 2, 4, 8, 16, 32, 37, 74, 148, 296, 592 and</li>
|
||||
<li>1, 2, 5, 10, 11, 22, 55, 110, 121, 242, 605 respectively.</li>
|
||||
</ul>
|
||||
|
||||
# --instructions--
|
||||
|
||||
Calculate and show here the Amicable pairs below 20,000 (there are eight).
|
||||
|
||||
# --hints--
|
||||
|
||||
`amicablePairsUpTo`是一个函数。
|
||||
`amicablePairsUpTo` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof amicablePairsUpTo === 'function');
|
||||
```
|
||||
|
||||
`[[220,284]]` `amicablePairsUpTo(300)`应返回`[[220,284]]` 。
|
||||
`amicablePairsUpTo(300)` should return `[[220,284]]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(amicablePairsUpTo(300), answer300);
|
||||
```
|
||||
|
||||
`[[220,284],[1184,1210],[2620,2924]]` `amicablePairsUpTo(3000)`应返回`[[220,284],[1184,1210],[2620,2924]]` 。
|
||||
`amicablePairsUpTo(3000)` should return `[[220,284],[1184,1210],[2620,2924]]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(amicablePairsUpTo(3000), answer3000);
|
||||
```
|
||||
|
||||
`[[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]]` 。
|
||||
`amicablePairsUpTo(20000)` should return `[[220,284],[1184,1210],[2620,2924],[5020,5564],[6232,6368],[10744,10856],[12285,14595],[17296,18416]]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(amicablePairsUpTo(20000), answer20000);
|
||||
|
@ -1,30 +1,34 @@
|
||||
---
|
||||
id: 594d8d0ab97724821379b1e6
|
||||
title: 平均值模式
|
||||
title: Averages/Mode
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302226
|
||||
dashedName: averagesmode
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
<p>编写程序以查找集合的<a href='https://en.wikipedia.org/wiki/Mode (statistics)' title='wp:模式(统计)'>模式</a>值。 </p><p>可以忽略集合为空的情况。必须小心处理模式不唯一的情况。 </p><p>如果不适合或不可能支持常规集合,请尽可能使用向量(数组)。如果不适合或不可能支持未指定的值类型,请使用整数。 </p>
|
||||
Write a program to find the [mode](https://en.wikipedia.org/wiki/Mode (statistics) "wp: Mode (statistics)") value of a collection.
|
||||
|
||||
The case where the collection is empty may be ignored. Care must be taken to handle the case where the mode is non-unique.
|
||||
|
||||
If it is not appropriate or possible to support a general collection, use a vector (array), if possible. If it is not appropriate or possible to support an unspecified value type, use integers.
|
||||
|
||||
# --hints--
|
||||
|
||||
`mode`是一种功能。
|
||||
`mode` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof mode === 'function');
|
||||
```
|
||||
|
||||
`mode([1, 3, 6, 6, 6, 6, 7, 7, 12, 12, 17])`应该相等`[6]`
|
||||
`mode([1, 3, 6, 6, 6, 6, 7, 7, 12, 12, 17])` should equal `[6]`
|
||||
|
||||
```js
|
||||
assert.deepEqual(mode(arr1), [6]);
|
||||
```
|
||||
|
||||
`mode([1, 2, 4, 4, 1])`应该等于`[1, 4]` 。
|
||||
`mode([1, 2, 4, 4, 1])` should equal `[1, 4]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(mode(arr2).sort(), [1, 4]);
|
||||
|
@ -1,32 +1,52 @@
|
||||
---
|
||||
id: 594d966a1467eb84194f0086
|
||||
title: 平均值 - 毕达哥拉斯指的是
|
||||
title: Averages/Pythagorean means
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302227
|
||||
dashedName: averagespythagorean-means
|
||||
---
|
||||
|
||||
# --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?是'
|
||||
Compute all three of the [Pythagorean means](https://en.wikipedia.org/wiki/Pythagorean means "wp: Pythagorean means") of the set of integers $1$ through $10$ (inclusive).
|
||||
|
||||
Show that $A(x_1,\\ldots,x_n) \\geq G(x_1,\\ldots,x_n) \\geq H(x_1,\\ldots,x_n)$ for this set of positive integers.
|
||||
|
||||
<ul>
|
||||
<li>The most common of the three means, the <a class='rosetta__link--rosetta' href='https://rosettacode.org/wiki/Averages/Arithmetic mean' title='Averages/Arithmetic mean' target='_blank'>arithmetic mean</a>, is the sum of the list divided by its length:<br>
|
||||
<big>$ A(x_1, \ldots, x_n) = \frac{x_1 + \cdots + x_n}{n}$</big></li>
|
||||
<li>The <a class='rosetta__link--wiki' href='https://en.wikipedia.org/wiki/Geometric mean' title='wp: Geometric mean' target='_blank'>geometric mean</a> is the $n$th root of the product of the list:<br>
|
||||
<big>$ G(x_1, \ldots, x_n) = \sqrt[n]{x_1 \cdots x_n} $</big></li>
|
||||
<li>The <a class='rosetta__link--wiki' href='https://en.wikipedia.org/wiki/Harmonic mean' title='wp: Harmonic mean' target='_blank'>harmonic mean</a> is $n$ divided by the sum of the reciprocal of each item in the list:<br>
|
||||
<big>$ H(x_1, \ldots, x_n) = \frac{n}{\frac{1}{x_1} + \cdots + \frac{1}{x_n}} $</big></li>
|
||||
</ul>
|
||||
|
||||
# --instructions--
|
||||
|
||||
When writing your function, assume the input is an ordered array of all inclusive numbers.
|
||||
|
||||
For the answer, please output an object in the following format:
|
||||
|
||||
```js
|
||||
{
|
||||
values: {
|
||||
Arithmetic: 5.5,
|
||||
Geometric: 4.528728688116765,
|
||||
Harmonic: 3.414171521474055
|
||||
},
|
||||
test: 'is A >= G >= H ? yes'
|
||||
}
|
||||
</pre>
|
||||
```
|
||||
|
||||
# --hints--
|
||||
|
||||
`pythagoreanMeans`是一种功能。
|
||||
`pythagoreanMeans` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof pythagoreanMeans === 'function');
|
||||
```
|
||||
|
||||
`pythagoreanMeans([1, 2, ..., 10])`应该等于上面相同的输出。
|
||||
`pythagoreanMeans([1, 2, ..., 10])` should equal the same output above.
|
||||
|
||||
```js
|
||||
assert.deepEqual(pythagoreanMeans(range1), answer1);
|
||||
|
@ -1,24 +1,30 @@
|
||||
---
|
||||
id: 594da033de4190850b893874
|
||||
title: 平均值 - 均方根
|
||||
title: Averages/Root mean square
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302228
|
||||
dashedName: averagesroot-mean-square
|
||||
---
|
||||
|
||||
# --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>
|
||||
Compute the [Root mean square](https://en.wikipedia.org/wiki/Root mean square "wp: Root mean square") of the numbers 1 through 10 inclusive.
|
||||
|
||||
The *root mean square* is also known by its initials RMS (or rms), and as the **quadratic mean**.
|
||||
|
||||
The RMS is calculated as the mean of the squares of the numbers, square-rooted:
|
||||
|
||||
$$x\_{\\mathrm{rms}} = \\sqrt {{{x_1}^2 + {x_2}^2 + \\cdots + {x_n}^2} \\over n}. $$
|
||||
|
||||
# --hints--
|
||||
|
||||
`rms`是一个功能。
|
||||
`rms` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof rms === 'function');
|
||||
```
|
||||
|
||||
`rms([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])`应等于`6.2048368229954285` 。
|
||||
`rms([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])` should equal `6.2048368229954285`.
|
||||
|
||||
```js
|
||||
assert.equal(rms(arr1), answer1);
|
||||
|
@ -1,24 +1,37 @@
|
||||
---
|
||||
id: 594db4d0dedb4c06a2a4cefd
|
||||
title: 巴贝奇问题
|
||||
title: Babbage problem
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302229
|
||||
dashedName: babbage-problem
|
||||
---
|
||||
|
||||
# --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>
|
||||
[Charles Babbage](https://en.wikipedia.org/wiki/Charles_Babbage "wp: Charles_Babbage"), looking ahead to the sorts of problems his Analytical Engine would be able to solve, gave this example:
|
||||
|
||||
<blockquote>
|
||||
What is the smallest positive integer whose square ends in the digits 269,696?
|
||||
<footer style='margin-left: 2em;'>Babbage, letter to Lord Bowden, 1837; see Hollingdale and Tootill, <i>Electronic Computers</i>, second edition, 1970, p. 125.</footer>
|
||||
</blockquote>
|
||||
|
||||
He thought the answer might be 99,736, whose square is 9,947,269,696; but he couldn't be certain.
|
||||
|
||||
The task is to find out if Babbage had the right answer.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Implement a function to return the lowest integer that satisfies the Babbage problem. If Babbage was right, return Babbage's number.
|
||||
|
||||
# --hints--
|
||||
|
||||
`babbage`是一种功能。
|
||||
`babbage` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof babbage === 'function');
|
||||
```
|
||||
|
||||
`babbage(99736, 269696)`不应该返回99736(答案较小)。
|
||||
`babbage(99736, 269696)` should not return 99736 (there is a smaller answer).
|
||||
|
||||
```js
|
||||
assert.equal(babbage(babbageAns, endDigits), answer);
|
||||
|
@ -1,126 +1,136 @@
|
||||
---
|
||||
id: 594dc6c729e5700999302b45
|
||||
title: 平衡括号
|
||||
title: Balanced brackets
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302230
|
||||
dashedName: balanced-brackets
|
||||
---
|
||||
|
||||
# --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>
|
||||
Determine whether a generated string of brackets is balanced; that is, whether it consists entirely of pairs of opening/closing brackets (in that order), none of which mis-nest.
|
||||
|
||||
**Examples:**
|
||||
| Input | Output |
|
||||
| ------------------------- | ------ |
|
||||
| <code>\[]</code> | true |
|
||||
| <code>]\[</code> | false |
|
||||
| <code>[][]</code> | true |
|
||||
| <code>]\[]</code> | false |
|
||||
| <code>\[]]\[\[]</code> | false |
|
||||
| <code>\[\[\[\[]]]]</code> | true |
|
||||
|
||||
# --hints--
|
||||
|
||||
`isBalanced`是一个函数。
|
||||
`isBalanced` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof isBalanced === 'function');
|
||||
```
|
||||
|
||||
`isBalanced("[]")`应该返回true。
|
||||
`isBalanced("[]")` should return true.
|
||||
|
||||
```js
|
||||
assert(isBalanced(testCases[0]));
|
||||
```
|
||||
|
||||
`isBalanced("]][[[][][][]][")`应该返回false。
|
||||
`isBalanced("]][[[][][][]][")` should return false.
|
||||
|
||||
```js
|
||||
assert(!isBalanced(testCases[1]));
|
||||
```
|
||||
|
||||
`isBalanced("[][[[[][][[[]]]]]]")`应该返回true。
|
||||
`isBalanced("[][[[[][][[[]]]]]]")` should return true.
|
||||
|
||||
```js
|
||||
assert(isBalanced(testCases[2]));
|
||||
```
|
||||
|
||||
`isBalanced("][")`应该返回true。
|
||||
`isBalanced("][")` should return false.
|
||||
|
||||
```js
|
||||
assert(!isBalanced(testCases[3]));
|
||||
```
|
||||
|
||||
`isBalanced("[[[]]]][[]")`应该返回true。
|
||||
`isBalanced("[[[]]]][[]")` should return false.
|
||||
|
||||
```js
|
||||
assert(!isBalanced(testCases[4]));
|
||||
```
|
||||
|
||||
`isBalanced("][[]")`应该返回true。
|
||||
`isBalanced("][[]")` should return false.
|
||||
|
||||
```js
|
||||
assert(!isBalanced(testCases[5]));
|
||||
```
|
||||
|
||||
`isBalanced("][[][]][[[]]")`应该返回true。
|
||||
`isBalanced("][[][]][[[]]")` should return false.
|
||||
|
||||
```js
|
||||
assert(!isBalanced(testCases[6]));
|
||||
```
|
||||
|
||||
`isBalanced("[[][]]][")`应该返回true。
|
||||
`isBalanced("[[][]]][")` should return false.
|
||||
|
||||
```js
|
||||
assert(!isBalanced(testCases[7]));
|
||||
```
|
||||
|
||||
`isBalanced("[[[]]][[]]]][][[")`应该返回true。
|
||||
`isBalanced("[[[]]][[]]]][][[")` should return false.
|
||||
|
||||
```js
|
||||
assert(!isBalanced(testCases[8]));
|
||||
```
|
||||
|
||||
`isBalanced("[]][[]]][[[[][]]")`应该返回true。
|
||||
`isBalanced("[]][[]]][[[[][]]")` should return false.
|
||||
|
||||
```js
|
||||
assert(!isBalanced(testCases[9]));
|
||||
```
|
||||
|
||||
`isBalanced("][]][[][")`应该返回true。
|
||||
`isBalanced("][]][[][")` should return false.
|
||||
|
||||
```js
|
||||
assert(!isBalanced(testCases[10]));
|
||||
```
|
||||
|
||||
`isBalanced("[[]][[][]]")`应该返回true。
|
||||
`isBalanced("[[]][[][]]")` should return true.
|
||||
|
||||
```js
|
||||
assert(isBalanced(testCases[11]));
|
||||
```
|
||||
|
||||
`isBalanced("[[]]")`应该返回true。
|
||||
`isBalanced("[[]]")` should return true.
|
||||
|
||||
```js
|
||||
assert(isBalanced(testCases[12]));
|
||||
```
|
||||
|
||||
`isBalanced("]][]][[]][[[")`应该返回true。
|
||||
`isBalanced("]][]][[]][[[")` should return false.
|
||||
|
||||
```js
|
||||
assert(!isBalanced(testCases[13]));
|
||||
```
|
||||
|
||||
`isBalanced("][]][][[")`应该返回true。
|
||||
`isBalanced("][]][][[")` should return false.
|
||||
|
||||
```js
|
||||
assert(!isBalanced(testCases[14]));
|
||||
```
|
||||
|
||||
`isBalanced("][][")`应该返回true。
|
||||
`isBalanced("][][")` should return false.
|
||||
|
||||
```js
|
||||
assert(!isBalanced(testCases[15]));
|
||||
```
|
||||
|
||||
`isBalanced("[[]]][][][[]][")`应该返回true。
|
||||
`isBalanced("[]]]")` should return false.
|
||||
|
||||
```js
|
||||
assert(!isBalanced(testCases[16]));
|
||||
```
|
||||
|
||||
`isBalanced("")`应该返回true。
|
||||
`isBalanced("")` should return true.
|
||||
|
||||
```js
|
||||
assert(isBalanced(testCases[17]));
|
||||
|
@ -1,54 +1,79 @@
|
||||
---
|
||||
id: 5951815dd895584b06884620
|
||||
title: 给定半径的圆圈通过两个点
|
||||
title: Circles of given radius through two points
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302231
|
||||
dashedName: circles-of-given-radius-through-two-points
|
||||
---
|
||||
|
||||
# --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>圆心
|
||||
Given two points on a plane and a radius, usually two circles of given radius can be drawn through the points.
|
||||
|
||||
**Exceptions:**
|
||||
|
||||
<ul>
|
||||
<li>A radius of zero should be treated as never describing circles (except in the case where the points are coincident).</li>
|
||||
<li>If the points are coincident then an infinite number of circles with the point on their circumference can be drawn, unless the radius is equal to zero as well which then collapses the circles to a point.</li>
|
||||
<li>If the points form a diameter then return a single circle.</li>
|
||||
<li>If the points are too far apart then no circles can be drawn.</li>
|
||||
</ul>
|
||||
|
||||
# --instructions--
|
||||
|
||||
Implement a function that takes two points and a radius and returns the two circles through those points. For each resulting circle, provide the coordinates for the center of each circle rounded to four decimal digits. Return each coordinate as an array, and coordinates as an array of arrays.
|
||||
|
||||
**For edge cases, return the following:**
|
||||
|
||||
<ul>
|
||||
<li>If points are on the diameter, return one point. If the radius is also zero however, return <code>"Radius Zero"</code>.</li>
|
||||
<li>If points are coincident, return <code>"Coincident point. Infinite solutions"</code>.</li>
|
||||
<li>If points are farther apart than the diameter, return <code>"No intersection. Points further apart than circle diameter"</code>.</li>
|
||||
</ul>
|
||||
|
||||
**Sample inputs:**
|
||||
|
||||
<pre> p1 p2 r
|
||||
0.1234, 0.9876 0.8765, 0.2345 2.0
|
||||
0.0000, 2.0000 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>
|
||||
|
||||
# --hints--
|
||||
|
||||
`getCircles`是一个函数。
|
||||
`getCircles` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof getCircles === 'function');
|
||||
```
|
||||
|
||||
`getCircles([0.1234, 0.9876], [0.8765, 0.2345], 2.0)`应该返回`[[1.8631, 1.9742], [-0.8632, -0.7521]]` 。
|
||||
`getCircles([0.1234, 0.9876], [0.8765, 0.2345], 2.0)` should return `[[1.8631, 1.9742], [-0.8632, -0.7521]]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(getCircles(...testCases[0]), answers[0]);
|
||||
```
|
||||
|
||||
`getCircles([0.0000, 2.0000], [0.0000, 0.0000], 1.0)`应该返回`[0, 1]`
|
||||
`getCircles([0.0000, 2.0000], [0.0000, 0.0000], 1.0)` should return `[0, 1]`
|
||||
|
||||
```js
|
||||
assert.deepEqual(getCircles(...testCases[1]), answers[1]);
|
||||
```
|
||||
|
||||
`getCircles([0.1234, 0.9876], [0.1234, 0.9876], 2.0)`应返回`Coincident point. Infinite solutions`
|
||||
`getCircles([0.1234, 0.9876], [0.1234, 0.9876], 2.0)` should return `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`
|
||||
`getCircles([0.1234, 0.9876], [0.8765, 0.2345], 0.5)` should return `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`
|
||||
`getCircles([0.1234, 0.9876], [0.1234, 0.9876], 0.0)` should return `Radius Zero`
|
||||
|
||||
```js
|
||||
assert.deepEqual(getCircles(...testCases[4]), answers[4]);
|
||||
|
@ -1,86 +1,96 @@
|
||||
---
|
||||
id: 5951a53863c8a34f02bf1bdc
|
||||
title: 最近对的问题
|
||||
title: Closest-pair problem
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302232
|
||||
dashedName: closest-pair-problem
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
任务:
|
||||
Provide a function to find the closest two points among a set of given points in two dimensions, i.e. to solve the [Closest pair of points problem](https://en.wikipedia.org/wiki/Closest pair of points problem "wp: Closest pair of points problem") in the *planar* case.
|
||||
|
||||
提供一个函数来在二维中找到一组给定点中最接近的两个点,即求解平面情况下的[最近点对问题](<https://en.wikipedia.org/wiki/Closest pair of points problem> "wp:最近点的问题") 。
|
||||
The straightforward solution is a O(n<sup>2</sup>) algorithm (which we can call *brute-force algorithm*); the pseudo-code (using indexes) could be simply:
|
||||
|
||||
直接的解决方案是O(n <sup>2</sup> )算法(我们可以称之为强力算法);伪代码(使用索引)可以简单地:
|
||||
<pre><strong>bruteForceClosestPair</strong> of P(1), P(2), ... P(N)
|
||||
<strong>if</strong> N < 2 <strong>then</strong>
|
||||
<strong>return</strong> ∞
|
||||
<strong>else</strong>
|
||||
minDistance ← |P(1) - P(2)|
|
||||
minPoints ← { P(1), P(2) }
|
||||
<strong>foreach</strong> i ∈ [1, N-1]
|
||||
<strong>foreach</strong> j ∈ [i+1, N]
|
||||
<strong>if</strong> |P(i) - P(j)| < minDistance <strong>then</strong>
|
||||
minDistance ← |P(i) - P(j)|
|
||||
minPoints ← { P(i), P(j) }
|
||||
<strong>endif</strong>
|
||||
<strong>endfor</strong>
|
||||
<strong>endfor</strong>
|
||||
<strong>return</strong> minDistance, minPoints
|
||||
<strong>endif</strong>
|
||||
</pre>
|
||||
|
||||
```
|
||||
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
|
||||
万一
|
||||
```
|
||||
A better algorithm is based on the recursive divide and conquer approach, as explained also at [Wikipedia's Closest pair of points problem](https://en.wikipedia.org/wiki/Closest pair of points problem#Planar_case "wp: Closest pair of points problem#Planar_case"), which is `O(nlog(n))` a pseudo-code could be:
|
||||
|
||||
</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那么
|
||||
使用强力算法返回xP的最近点
|
||||
其他
|
||||
xL←xP点从1到⌈N/2⌉
|
||||
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}
|
||||
(dL,pairL)←nearestPair(xL,yL)
|
||||
(dR,pairR)←nearestRair(xR,yR)
|
||||
(dmin,pairMin)←(dR,pairR)
|
||||
如果dL <dR则
|
||||
(dmin,pairMin)←(dL,pairL)
|
||||
万一
|
||||
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)| <最接近的
|
||||
(最近,最近的公里)←(| yS(k) - yS(i)|,{yS(k),yS(i)})
|
||||
万一
|
||||
k←k + 1
|
||||
ENDWHILE
|
||||
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>
|
||||
<pre><strong>closestPair</strong> of (xP, yP)
|
||||
where xP is P(1) .. P(N) sorted by x coordinate, and
|
||||
yP is P(1) .. P(N) sorted by y coordinate (ascending order)
|
||||
<strong>if</strong> N ≤ 3 <strong>then</strong>
|
||||
<strong>return</strong> closest points of xP using brute-force algorithm
|
||||
<strong>else</strong>
|
||||
xL ← points of xP from 1 to ⌈N/2⌉
|
||||
xR ← points of xP from ⌈N/2⌉+1 to N
|
||||
xm ← xP(⌈N/2⌉)<sub>x</sub>
|
||||
yL ← { p ∈ yP : p<sub>x</sub> ≤ xm }
|
||||
yR ← { p ∈ yP : p<sub>x</sub> > xm }
|
||||
(dL, pairL) ← closestPair of (xL, yL)
|
||||
(dR, pairR) ← closestPair of (xR, yR)
|
||||
(dmin, pairMin) ← (dR, pairR)
|
||||
<strong>if</strong> dL < dR <strong>then</strong>
|
||||
(dmin, pairMin) ← (dL, pairL)
|
||||
<strong>endif</strong>
|
||||
yS ← { p ∈ yP : |xm - p<sub>x</sub>| < dmin }
|
||||
nS ← number of points in yS
|
||||
(closest, closestPair) ← (dmin, pairMin)
|
||||
<strong>for</strong> i <strong>from</strong> 1 <strong>to</strong> nS - 1
|
||||
k ← i + 1
|
||||
<strong>while</strong> k ≤ nS <strong>and</strong> yS(k)<sub>y</sub> - yS(i)<sub>y</sub> < dmin
|
||||
<strong>if</strong> |yS(k) - yS(i)| < closest <strong>then</strong>
|
||||
(closest, closestPair) ← (|yS(k) - yS(i)|, {yS(k), yS(i)})
|
||||
<strong>endif</strong>
|
||||
k ← k + 1
|
||||
<strong>endwhile</strong>
|
||||
<strong>endfor</strong>
|
||||
<strong>return</strong> closest, closestPair
|
||||
<strong>endif</strong>
|
||||
</pre>
|
||||
|
||||
For the input, expect the argument to be an array of objects (points) with `x` and `y` members set to numbers. For the output, return an object containing the key:value pairs for `distance` and `pair` (the pair of two closest points).
|
||||
|
||||
**References and further readings:**
|
||||
|
||||
<ul>
|
||||
<li><a href='https://en.wikipedia.org/wiki/Closest pair of points problem' title='wp: Closest pair of points problem' target='_blank'>Closest pair of points problem</a></li>
|
||||
<li><a href='https://www.cs.mcgill.ca/~cs251/ClosestPair/ClosestPairDQ.html' target='_blank'>Closest Pair (McGill)</a></li>
|
||||
<li><a href='https://www.cs.ucsb.edu/~suri/cs235/ClosestPair.pdf' target='_blank'>Closest Pair (UCSB)</a></li>
|
||||
<li><a href='https://classes.cec.wustl.edu/~cse241/handouts/closestpair.pdf' target='_blank'>Closest pair (WUStL)</a></li>
|
||||
</ul>
|
||||
|
||||
# --hints--
|
||||
|
||||
`getClosestPair`是一个函数。
|
||||
`getClosestPair` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof getClosestPair === 'function');
|
||||
```
|
||||
|
||||
距离应如下。
|
||||
Distance should be the following.
|
||||
|
||||
```js
|
||||
assert.equal(getClosestPair(points1).distance, answer1.distance);
|
||||
```
|
||||
|
||||
要点应如下。
|
||||
Points should be the following.
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
@ -89,13 +99,13 @@ assert.deepEqual(
|
||||
);
|
||||
```
|
||||
|
||||
距离应如下。
|
||||
Distance should be the following.
|
||||
|
||||
```js
|
||||
assert.equal(getClosestPair(points2).distance, answer2.distance);
|
||||
```
|
||||
|
||||
要点应如下。
|
||||
Points should be the following.
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
|
@ -1,23 +1,20 @@
|
||||
---
|
||||
id: 5958469238c0d8d2632f46db
|
||||
title: 组合
|
||||
title: Combinations
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302233
|
||||
dashedName: combinations
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
任务:
|
||||
Given non-negative integers `m` and `n`, generate all size `m` combinations of the integers from `0` (zero) to `n-1` in sorted order (each combination is sorted and the entire table is sorted).
|
||||
|
||||
给定非负整数m和n ,以排序顺序生成从0 (零)到n-1的整数的所有大小m个 [组合](http://mathworld.wolfram.com/Combination.html "链接:http://mathworld.wolfram.com/Combination.html") (每个组合被排序并且整个表被排序)。
|
||||
**Example:**
|
||||
|
||||
例:
|
||||
`3` comb `5` is:
|
||||
|
||||
3梳子5是:
|
||||
|
||||
```
|
||||
0 1 2
|
||||
<pre>0 1 2
|
||||
0 1 3
|
||||
0 1 4
|
||||
0 2 3
|
||||
@ -27,25 +24,23 @@ dashedName: combinations
|
||||
1 2 4
|
||||
1 3 4
|
||||
2 3 4
|
||||
```
|
||||
|
||||
</pre>
|
||||
|
||||
# --hints--
|
||||
|
||||
`combinations`是一种功能。
|
||||
`combinations` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof combinations === 'function');
|
||||
```
|
||||
|
||||
`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]]` 。
|
||||
`combinations(3, 5)` should return `[[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
|
||||
assert.deepEqual(combinations(testInput1[0], testInput1[1]), testOutput1);
|
||||
```
|
||||
|
||||
`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]]`
|
||||
`combinations(4, 6)` should return `[[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
|
||||
assert.deepEqual(combinations(testInput2[0], testInput2[1]), testOutput2);
|
||||
|
@ -1,48 +1,70 @@
|
||||
---
|
||||
id: 596e414344c3b2872167f0fe
|
||||
title: 逗号狡猾
|
||||
title: Comma quibbling
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302234
|
||||
dashedName: comma-quibbling
|
||||
---
|
||||
|
||||
# --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>
|
||||
Comma quibbling is a task originally set by Eric Lippert in his [blog](https://blogs.msdn.com/b/ericlippert/archive/2009/04/15/comma-quibbling.aspx).
|
||||
|
||||
# --instructions--
|
||||
|
||||
Write a function to generate a string output which is the concatenation of input words from a list/sequence where:
|
||||
|
||||
<ol>
|
||||
<li>An input of no words produces the output string of just the two brace characters (<code>"{}"</code>)</li>
|
||||
<li>An input of just one word, e.g. <code>["ABC"]</code>, produces the output string of the word inside the two braces, e.g. <code>"{ABC}"</code></li>
|
||||
<li>An input of two words, e.g. <code>["ABC", "DEF"]</code>, produces the output string of the two words inside the two braces with the words separated by the string <code>" and "</code>, e.g. <code>"{ABC and DEF}"</code></li>
|
||||
<li>An input of three or more words, e.g. <code>["ABC", "DEF", "G", "H"]</code>, produces the output string of all but the last word separated by <code>", "</code> with the last word separated by <code>" and "</code> and all within braces; e.g. <code>"{ABC, DEF, G and H}"</code></li>
|
||||
</ol>
|
||||
|
||||
Test your function with the following series of inputs showing your output here on this page:
|
||||
|
||||
<ul>
|
||||
<li>[] # (No input words).</li>
|
||||
<li>["ABC"]</li>
|
||||
<li>["ABC", "DEF"]</li>
|
||||
<li>["ABC", "DEF", "G", "H"]</li>
|
||||
</ul>
|
||||
|
||||
**Note:** Assume words are non-empty strings of uppercase characters for this task.
|
||||
|
||||
# --hints--
|
||||
|
||||
`quibble`是一种功能。
|
||||
`quibble` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof quibble === 'function');
|
||||
```
|
||||
|
||||
`quibble(["ABC"])`应该返回一个字符串。
|
||||
`quibble(["ABC"])` should return a string.
|
||||
|
||||
```js
|
||||
assert(typeof quibble(['ABC']) === 'string');
|
||||
```
|
||||
|
||||
`quibble([])`应返回“{}”。
|
||||
`quibble([])` should return "{}".
|
||||
|
||||
```js
|
||||
assert.equal(quibble(testCases[0]), results[0]);
|
||||
```
|
||||
|
||||
`quibble(["ABC"])`应该返回“{ABC}”。
|
||||
`quibble(["ABC"])` should return "{ABC}".
|
||||
|
||||
```js
|
||||
assert.equal(quibble(testCases[1]), results[1]);
|
||||
```
|
||||
|
||||
`quibble(["ABC", "DEF"])`应返回“{ABC和DEF}”。
|
||||
`quibble(["ABC", "DEF"])` should return "{ABC and DEF}".
|
||||
|
||||
```js
|
||||
assert.equal(quibble(testCases[2]), results[2]);
|
||||
```
|
||||
|
||||
`quibble(["ABC", "DEF", "G", "H"])`应返回“{ABC,DEF,G和H}”。
|
||||
`quibble(["ABC", "DEF", "G", "H"])` should return "{ABC,DEF,G and H}".
|
||||
|
||||
```js
|
||||
assert.equal(quibble(testCases[3]), results[3]);
|
||||
|
@ -1,84 +1,89 @@
|
||||
---
|
||||
id: 596e457071c35c882915b3e4
|
||||
title: 比较字符串列表
|
||||
title: Compare a list of strings
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302235
|
||||
dashedName: compare-a-list-of-strings
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
<p>给定一个任意多个字符串的<a href='https://en.wikipedia.org/wiki/List_(abstract_data_type)' title='wp:List_(abstract_data_type)'>列表</a> ,为以下每个条件实现一个函数: </p>测试它们是否都是词法上相等的测试,如果每个字符串在词法上小于它之后的字符串(即列表是否按严格的升序排列)
|
||||
Given a [list](https://en.wikipedia.org/wiki/List_(abstract_data_type) "wp: List\_(abstract_data_type)") of arbitrarily many strings, implement a function for each of the following conditions:
|
||||
|
||||
<ul>
|
||||
<li>test if they are all lexically equal</li>
|
||||
<li>test if every string is lexically less than the one after it (i.e. whether the list is in strict ascending order)</li>
|
||||
</ul>
|
||||
|
||||
# --hints--
|
||||
|
||||
`allEqual`是一个函数。
|
||||
`allEqual` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof allEqual === 'function');
|
||||
```
|
||||
|
||||
`azSorted`是一个函数。
|
||||
`azSorted` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof azSorted === 'function');
|
||||
```
|
||||
|
||||
`allEqual(["AA", "AA", "AA", "AA"])`返回true。
|
||||
`allEqual(["AA", "AA", "AA", "AA"])` should return true.
|
||||
|
||||
```js
|
||||
assert(allEqual(testCases[0]));
|
||||
```
|
||||
|
||||
`azSorted(["AA", "AA", "AA", "AA"])`返回false。
|
||||
`azSorted(["AA", "AA", "AA", "AA"])` should return false.
|
||||
|
||||
```js
|
||||
assert(!azSorted(testCases[0]));
|
||||
```
|
||||
|
||||
`allEqual(["AA", "ACB", "BB", "CC"])`返回false。
|
||||
`allEqual(["AA", "ACB", "BB", "CC"])` should return false.
|
||||
|
||||
```js
|
||||
assert(!allEqual(testCases[1]));
|
||||
```
|
||||
|
||||
`azSorted(["AA", "ACB", "BB", "CC"])`返回true。
|
||||
`azSorted(["AA", "ACB", "BB", "CC"])` should return true.
|
||||
|
||||
```js
|
||||
assert(azSorted(testCases[1]));
|
||||
```
|
||||
|
||||
`allEqual([])`返回true。
|
||||
`allEqual([])` should return true.
|
||||
|
||||
```js
|
||||
assert(allEqual(testCases[2]));
|
||||
```
|
||||
|
||||
`azSorted([])`返回true。
|
||||
`azSorted([])` should return true.
|
||||
|
||||
```js
|
||||
assert(azSorted(testCases[2]));
|
||||
```
|
||||
|
||||
`allEqual(["AA"])`返回true。
|
||||
`allEqual(["AA"])` should return true.
|
||||
|
||||
```js
|
||||
assert(allEqual(testCases[3]));
|
||||
```
|
||||
|
||||
`azSorted(["AA"])`返回true。
|
||||
`azSorted(["AA"])` should return true.
|
||||
|
||||
```js
|
||||
assert(azSorted(testCases[3]));
|
||||
```
|
||||
|
||||
`allEqual(["BB", "AA"])`返回false。
|
||||
`allEqual(["BB", "AA"])` should return false.
|
||||
|
||||
```js
|
||||
assert(!allEqual(testCases[4]));
|
||||
```
|
||||
|
||||
`azSorted(["BB", "AA"])`返回false。
|
||||
`azSorted(["BB", "AA"])` should return false.
|
||||
|
||||
```js
|
||||
assert(!azSorted(testCases[4]));
|
||||
|
@ -1,66 +1,76 @@
|
||||
---
|
||||
id: 596fd036dc1ab896c5db98b1
|
||||
title: 将秒转换为复合持续时间
|
||||
title: Convert seconds to compound duration
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302236
|
||||
dashedName: convert-seconds-to-compound-duration
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
任务:
|
||||
Implement a function which:
|
||||
|
||||
实现一个功能:
|
||||
<ul>
|
||||
<li>takes a positive integer representing a duration in seconds as input (e.g., <code>100</code>), and</li>
|
||||
<li>returns a string which shows the same duration decomposed into weeks, days, hours, minutes, and seconds as detailed below (e.g., <code>1 min, 40 sec</code>).</li>
|
||||
</ul>
|
||||
|
||||
取一个表示以秒为单位的持续时间的正整数作为输入(例如, `100` ),并返回一个字符串,该字符串显示分解为周,日,小时,分钟和秒的相同持续时间,如下所述(例如,“ `1 min, 40 sec` “)。
|
||||
Demonstrate that it passes the following three test-cases:
|
||||
|
||||
证明它通过以下三个测试用例:
|
||||
<div style='font-size:115%; font-weight: bold;'>Test Cases</div>
|
||||
|
||||
测试用例
|
||||
| Input number | Output number |
|
||||
| ------------ | ------------------------- |
|
||||
| 7259 | <code>2 hr, 59 sec</code> |
|
||||
| 728640059 | <code>1 d</code> |
|
||||
| 6000000 | <code>9 wk, 6 d, 10 hr, 40 min</code> |
|
||||
|
||||
| 输入号码 | 输出数量 |
|
||||
| ------- | -------------------------- |
|
||||
| 7259 | `2 hr, 59 sec` |
|
||||
| 86400 | `1 d` |
|
||||
| 6000000 | `9 wk, 6 d, 10 hr, 40 min` |
|
||||
<div style="font-size:115%; font-weight: bold;">Details</div>
|
||||
<ul>
|
||||
<li>
|
||||
The following five units should be used:
|
||||
|
||||
细节
|
||||
| Unit | Suffix used in Output | Conversion |
|
||||
| ------ | --------------------- | --------------------- |
|
||||
| week |!!crwdBlockTags_18_sgaTkcolBdwrc!! | 1 week = 7 days |
|
||||
| day |!!crwdBlockTags_19_sgaTkcolBdwrc!! | 1 day = 24 hours |
|
||||
| hour |!!crwdBlockTags_20_sgaTkcolBdwrc!! | 1 hour = 60 minutes |
|
||||
| minute |!!crwdBlockTags_21_sgaTkcolBdwrc!! | 1 minute = 60 seconds |
|
||||
| second |!!crwdBlockTags_22_sgaTkcolBdwrc!! | --- |
|
||||
|
||||
应使用以下五个单位:
|
||||
|
||||
| 单元 | 输出中使用的后缀 | 转变 |
|
||||
| -- | -------- | --------- |
|
||||
| 周 | `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` )模拟测试用例中显示的格式(从最大单位到最小单位的数量,以逗号+空格分隔;数值和单位每个数量用空格分隔)。
|
||||
|
||||
* * *
|
||||
</li>
|
||||
<li>
|
||||
However, <strong>only</strong> include quantities with non-zero values in the output (e.g., return <code>1 d</code> and not <code>0 wk, 1 d, 0 hr, 0 min, 0 sec</code>).
|
||||
</li>
|
||||
<li>
|
||||
Give larger units precedence over smaller ones as much as possible (e.g., return <code>2 min, 10 sec</code> and not <code>1 min, 70 sec</code> or <code>130 sec</code>).
|
||||
</li>
|
||||
<li>
|
||||
Mimic the formatting shown in the test-cases (quantities sorted from largest unit to smallest and separated by comma+space; value and unit of each quantity separated by space).
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
# --hints--
|
||||
|
||||
`convertSeconds`是一个函数。
|
||||
`convertSeconds` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof convertSeconds === 'function');
|
||||
```
|
||||
|
||||
`convertSeconds(7259)`应该返回`2 hr, 59 sec` 。
|
||||
`convertSeconds(7259)` should return `2 hr, 59 sec`.
|
||||
|
||||
```js
|
||||
assert.equal(convertSeconds(testCases[0]), results[0]);
|
||||
```
|
||||
|
||||
`convertSeconds(86400)`应返回`1 d` 。
|
||||
`convertSeconds(86400)` should return `1 d`.
|
||||
|
||||
```js
|
||||
assert.equal(convertSeconds(testCases[1]), results[1]);
|
||||
```
|
||||
|
||||
`convertSeconds(6000000)`应该返回`9 wk, 6 d, 10 hr, 40 min` 。
|
||||
`convertSeconds(6000000)` should return `9 wk, 6 d, 10 hr, 40 min`.
|
||||
|
||||
```js
|
||||
assert.equal(convertSeconds(testCases[2]), results[2]);
|
||||
|
@ -1,48 +1,49 @@
|
||||
---
|
||||
id: 596fda99c69f779975a1b67d
|
||||
title: 计算子字符串的出现次数
|
||||
title: Count occurrences of a substring
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302237
|
||||
dashedName: count-occurrences-of-a-substring
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
任务:
|
||||
Create a function, or show a built-in function, to count the number of non-overlapping occurrences of a substring inside a string.
|
||||
|
||||
创建函数或显示内置函数,以计算字符串中子字符串的非重叠出现次数。
|
||||
The function should take two arguments:
|
||||
|
||||
该函数应该有两个参数:
|
||||
<ul>
|
||||
<li>the first argument being the string to search, and</li>
|
||||
<li>the second a substring to be searched for.</li>
|
||||
</ul>
|
||||
|
||||
第一个参数是要搜索的字符串,第二个参数是要搜索的子字符串。
|
||||
It should return an integer count.
|
||||
|
||||
它应该返回一个整数计数。
|
||||
The matching should yield the highest number of non-overlapping matches.
|
||||
|
||||
匹配应产生最多数量的非重叠匹配。
|
||||
|
||||
通常,这实质上意味着从左到右或从右到左匹配。
|
||||
In general, this essentially means matching from left-to-right or right-to-left.
|
||||
|
||||
# --hints--
|
||||
|
||||
`countSubstring`是一个函数。
|
||||
`countSubstring` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof countSubstring === 'function');
|
||||
```
|
||||
|
||||
`countSubstring("the three truths", "th")`应该返回`3` 。
|
||||
`countSubstring("the three truths", "th")` should return `3`.
|
||||
|
||||
```js
|
||||
assert.equal(countSubstring(testCases[0], searchString[0]), results[0]);
|
||||
```
|
||||
|
||||
`countSubstring("ababababab", "abab")`应返回`2` 。
|
||||
`countSubstring("ababababab", "abab")` should return `2`.
|
||||
|
||||
```js
|
||||
assert.equal(countSubstring(testCases[1], searchString[1]), results[1]);
|
||||
```
|
||||
|
||||
`countSubstring("abaabba*bbaba*bbab", "a*b")`应返回`2` 。
|
||||
`countSubstring("abaabba*bbaba*bbab", "a*b")` should return `2`.
|
||||
|
||||
```js
|
||||
assert.equal(countSubstring(testCases[2], searchString[2]), results[2]);
|
||||
|
@ -1,24 +1,46 @@
|
||||
---
|
||||
id: 59713bd26bdeb8a594fb9413
|
||||
title: 计算硬币
|
||||
title: Count the coins
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302238
|
||||
dashedName: count-the-coins
|
||||
---
|
||||
|
||||
# --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> 。
|
||||
There are four types of common coins in [US](https://en.wikipedia.org/wiki/United_States) currency:
|
||||
|
||||
<ul>
|
||||
<li>quarters (25 cents)</li>
|
||||
<li>dimes (10 cents)</li>
|
||||
<li>nickels (5 cents), and</li>
|
||||
<li>pennies (1 cent)</li>
|
||||
</ul>
|
||||
|
||||
<p>There are six ways to make change for 15 cents:</p>
|
||||
|
||||
<ul>
|
||||
<li>A dime and a nickel</li>
|
||||
<li>A dime and 5 pennies</li>
|
||||
<li>3 nickels</li>
|
||||
<li>2 nickels and 5 pennies</li>
|
||||
<li>A nickel and 10 pennies</li>
|
||||
<li>15 pennies</li>
|
||||
</ul>
|
||||
|
||||
# --instructions--
|
||||
|
||||
Implement a function to determine how many ways there are to make change for a dollar using these common coins (1 dollar = 100 cents)
|
||||
|
||||
# --hints--
|
||||
|
||||
`countCoins`是一个函数。
|
||||
`countCoins` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof countCoins === 'function');
|
||||
```
|
||||
|
||||
`countCoints()`应该返回242。
|
||||
`countCoins()` should return 242.
|
||||
|
||||
```js
|
||||
assert.equal(countCoins(), 242);
|
||||
|
@ -1,30 +1,50 @@
|
||||
---
|
||||
id: 59713da0a428c1a62d7db430
|
||||
title: 克莱默的统治
|
||||
title: Cramer's rule
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302239
|
||||
dashedName: cramers-rule
|
||||
---
|
||||
|
||||
# --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>
|
||||
In [linear algebra](https://en.wikipedia.org/wiki/linear algebra "wp: linear algebra"), [Cramer's rule](https://en.wikipedia.org/wiki/Cramer's rule "wp: Cramer's rule") is an explicit formula for the solution of a [system of linear equations](https://en.wikipedia.org/wiki/system of linear equations "wp: system of linear equations") with as many equations as unknowns, valid whenever the system has a unique solution. It expresses the solution in terms of the determinants of the (square) coefficient matrix and of matrices obtained from it by replacing one column by the vector of right hand sides of the equations.
|
||||
|
||||
Given
|
||||
|
||||
$\\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&= {\\color{red}d_3}\\end{matrix}\\right.$
|
||||
|
||||
which in matrix format is
|
||||
|
||||
$\\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}.$
|
||||
|
||||
Then the values of $x, y$ and $z$ can be found as follows:
|
||||
|
||||
$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{ and }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} }.$
|
||||
|
||||
# --instructions--
|
||||
|
||||
Given the following system of equations:
|
||||
|
||||
$\\begin{cases} 2w-x+5y+z=-3 \\\\ 3w+2x+2y-6z=-32 \\\\ w+3x+3y-z=-47 \\\\ 5w-2x-3y+3z=49 \\\\ \\end{cases}$
|
||||
|
||||
solve for $w$, $x$, $y$ and $z$, using Cramer's rule.
|
||||
|
||||
# --hints--
|
||||
|
||||
`cramersRule`是一个函数。
|
||||
`cramersRule` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof cramersRule === 'function');
|
||||
```
|
||||
|
||||
`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]` 。
|
||||
`cramersRule([[2, -1, 5, 1], [3, 2, 2, -6], [1, 3, 3, -1], [5, -2, -3, 3]], [-3, -32, -47, 49])` should return `[2, -12, -4, 1]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(cramersRule(matrices[0], freeTerms[0]), answers[0]);
|
||||
```
|
||||
|
||||
`cramersRule([[3, 1, 1], [2, 2, 5], [1, -3, -4]], [3, -1, 2])`应返回`[1, 1, -1]` 。
|
||||
`cramersRule([[3, 1, 1], [2, 2, 5], [1, -3, -4]], [3, -1, 2])` should return `[1, 1, -1]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(cramersRule(matrices[1], freeTerms[1]), answers[1]);
|
||||
|
@ -8,7 +8,7 @@ dashedName: cumulative-standard-deviation
|
||||
|
||||
# --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.
|
||||
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--
|
||||
|
||||
|
@ -18,7 +18,7 @@ A given rectangle is made from *m* × *n* squares. If *m* and *n* are not both o
|
||||
.s2 { fill: #fbf }
|
||||
.d { stroke:black; fill:none}
|
||||
</style>
|
||||
<defs> <g id="m">
|
||||
<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>
|
||||
|
@ -1,44 +1,43 @@
|
||||
---
|
||||
id: 59669d08d75b60482359409f
|
||||
title: 日期格式
|
||||
title: Date format
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302243
|
||||
dashedName: date-format
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
任务:
|
||||
Return an array with the current date in the formats:
|
||||
|
||||
返回包含以下格式的当前日期的数组:
|
||||
<ul>
|
||||
<li>2007-11-23</li>
|
||||
<li>Friday, November 23, 2007</li>
|
||||
</ul>
|
||||
|
||||
\- 2007-11-23和
|
||||
|
||||
\- 2007年11月23日星期日
|
||||
|
||||
示例输出: `['2007-11-23', 'Sunday, November 23, 2007']`
|
||||
Example output: `['2007-11-23', 'Friday, November 23, 2007']`
|
||||
|
||||
# --hints--
|
||||
|
||||
`getDateFormats`是一个函数。
|
||||
`getDateFormats` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof getDateFormats === 'function');
|
||||
```
|
||||
|
||||
应该返回一个对象。
|
||||
`getDateFormats` should return an object.
|
||||
|
||||
```js
|
||||
assert(typeof getDateFormats() === 'object');
|
||||
```
|
||||
|
||||
应该返回一个包含2个元素的数组。
|
||||
`getDateFormats` should return an array with 2 elements.
|
||||
|
||||
```js
|
||||
assert(getDateFormats().length === 2);
|
||||
```
|
||||
|
||||
应以正确的格式返回正确的日期
|
||||
`getDateFormats` should return the correct date in the right format
|
||||
|
||||
```js
|
||||
assert.deepEqual(getDateFormats(), dates, equalsMessage);
|
||||
|
@ -1,42 +1,34 @@
|
||||
---
|
||||
id: 5966c21cf732a95f1b67dd28
|
||||
title: 日期操纵
|
||||
title: Date manipulation
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302244
|
||||
dashedName: date-manipulation
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
任务:
|
||||
Given a date string in EST, output the given date as a string with 12 hours added to the time. Time zone should be preserved.
|
||||
|
||||
给定EST中的日期字符串,将给定日期输出为字符串,并添加12小时。
|
||||
Example input: `"March 6 2009 7:30pm EST"`
|
||||
|
||||
时区应该保留。
|
||||
|
||||
示例输入:
|
||||
|
||||
`"March 7 2009 7:30pm EST"`
|
||||
|
||||
输出示例:
|
||||
|
||||
`"March 8 2009 7:30am EST"`
|
||||
Example output: `"March 7 2009 7:30am EST"`
|
||||
|
||||
# --hints--
|
||||
|
||||
`add12Hours`是一个功能。
|
||||
`add12Hours` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof add12Hours === 'function');
|
||||
```
|
||||
|
||||
`add12Hours(dateString)`应该返回一个字符串。
|
||||
`add12Hours(dateString)` should return a string.
|
||||
|
||||
```js
|
||||
assert(typeof add12Hours('January 17 2017 11:43am EST') === 'string');
|
||||
```
|
||||
|
||||
`add12Hours("" + tests[0] + "")`应该返回`"" + answers[0] + ""`
|
||||
`add12Hours("January 17 2017 11:43am EST")` should return `"January 17 2017 11:43pm EST"`
|
||||
|
||||
```js
|
||||
assert(
|
||||
@ -44,25 +36,25 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
汉德尔应该改变一天。 `add12Hours("" + tests[1] + "")`应返回`"" + answers[1] + ""`
|
||||
Should handle day change. `add12Hours("March 6 2009 7:30pm EST")` should return `"March 7 2009 7:30am EST"`
|
||||
|
||||
```js
|
||||
assert(add12Hours('March 7 2009 7:30pm EST') === 'March 8 2009 7:30am EST');
|
||||
assert(add12Hours('March 6 2009 7:30pm EST') === 'March 7 2009 7:30am EST');
|
||||
```
|
||||
|
||||
汉德尔月份应该在闰年中发生变化。 `add12Hours("" + tests[2] + "")`应返回`"" + answers[2] + ""`
|
||||
Should handle month change in a leap years. `add12Hours("February 29 2004 9:15pm EST")` should return `"March 1 2004 9:15am EST"`
|
||||
|
||||
```js
|
||||
assert(add12Hours('February 29 2004 9:15pm EST') === 'March 1 2004 9:15am EST');
|
||||
```
|
||||
|
||||
应该在一个共同的年份改变汉德尔月份。 `add12Hours("" + tests[3] + "")`应该返回`"" + answers[3] + ""`
|
||||
Should handle month change in a common years. `add12Hours("February 28 1999 3:15pm EST")` should return `"March 1 1999 3:15am EST"`
|
||||
|
||||
```js
|
||||
assert(add12Hours('February 28 1999 3:15pm EST') === 'March 1 1999 3:15am EST');
|
||||
```
|
||||
|
||||
汉德尔应该改变一年。 `add12Hours("" + tests[4] + "")`应该返回`"" + answers[4] + ""`
|
||||
Should handle year change. `add12Hours("December 31 2020 1:45pm EST")` should return `"January 1 2021 1:45am EST"`
|
||||
|
||||
```js
|
||||
assert(
|
||||
|
@ -1,36 +1,40 @@
|
||||
---
|
||||
id: 5966f99c45e8976909a85575
|
||||
title: 一周中的天
|
||||
title: Day of the week
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302245
|
||||
dashedName: day-of-the-week
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
<p>一家公司决定,每当圣诞节落在星期天,他们将给予他们的工人所有额外带薪假期,这样,在任何公共假期,工人将不必在下一周(12月25日到1月1日之间)工作。 </p><p>任务: </p><p>编写一个开始年份和结束年份的函数,并返回12月25日为星期日的所有年份的数组。 </p>
|
||||
A company decides that whenever Xmas falls on a Sunday they will give their workers all extra paid holidays so that, together with any public holidays, workers will not have to work the following week (between the 25th of December and the first of January).
|
||||
|
||||
# --instructions--
|
||||
|
||||
Write a function that takes a start year and an end year and return an array of all the years where the 25th of December will be a Sunday.
|
||||
|
||||
# --hints--
|
||||
|
||||
`findXmasSunday`是一个函数。
|
||||
`findXmasSunday` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof findXmasSunday === 'function');
|
||||
```
|
||||
|
||||
`findChristmasSunday(2000, 2100)`应该返回一个数组。
|
||||
`findXmasSunday(2000, 2100)` should return an array.
|
||||
|
||||
```js
|
||||
assert(typeof findXmasSunday(2000, 2100) === 'object');
|
||||
```
|
||||
|
||||
`findChristmasSunday(2008, 2121`应该回归[1977,1983,1988,1994,2005,2011,2016]
|
||||
`findXmasSunday(1970, 2017)` should return `[1977, 1983, 1988, 1994, 2005, 2011, 2016]`
|
||||
|
||||
```js
|
||||
assert.deepEqual(findXmasSunday(1970, 2017), firstSolution);
|
||||
```
|
||||
|
||||
`findChristmasSunday(2008, 2121`应该返回[2011,2016,2022,2033,2039,2044,2050,2061,2067,2072,2078,2089,2095,2101,2107,2112,2118]
|
||||
`findXmasSunday(2008, 2121)` should return `[2011, 2016, 2022, 2033, 2039, 2044, 2050, 2061, 2067, 2072, 2078, 2089, 2095, 2101, 2107, 2112, 2118]`
|
||||
|
||||
```js
|
||||
assert.deepEqual(findXmasSunday(2008, 2121), secondSolution);
|
||||
|
@ -1,64 +1,113 @@
|
||||
---
|
||||
id: 59694356a6e7011f7f1c5f4e
|
||||
title: FreeCell的交易卡
|
||||
title: Deal cards for FreeCell
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302246
|
||||
dashedName: deal-cards-for-freecell
|
||||
---
|
||||
|
||||
# --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
|
||||
*FreeCell* is the solitaire card game that Paul Alfille introduced to the PLATO system in 1978. Jim Horne, at Microsoft, changed the name to FreeCell and reimplemented the game for [DOS](https://rosettacode.org/wiki/DOS "DOS"), then [Windows](https://rosettacode.org/wiki/Windows "Windows"). This version introduced 32000 numbered deals.
|
||||
|
||||
As the game became popular, Jim Horne disclosed the algorithm, and other implementations of FreeCell began to reproduce the Microsoft deals. These deals are numbered from 1 to 32000. Newer versions from Microsoft have 1 million deals, numbered from 1 to 1000000; some implementations allow numbers outside that range.
|
||||
|
||||
The algorithm uses this [linear congruential generator](https://rosettacode.org/wiki/linear congruential generator "linear congruential generator") from Microsoft C:
|
||||
|
||||
<ul>
|
||||
<li>$state_{n + 1} \equiv 214013 \times state_n + 2531011 \pmod{2^{31}}$</li>
|
||||
<li>$rand_n = state_n \div 2^{16}$</li>
|
||||
<li>$rand_n$ is in range 0 to 32767.</li>
|
||||
</ul>
|
||||
|
||||
The algorithm follows:
|
||||
|
||||
<ol>
|
||||
<li>Seed the RNG with the number of the deal.
|
||||
</li><li>Create an <a href='https://rosettacode.org/wiki/array' title='array' target='_blank'>array</a> of 52 cards: Ace of Clubs, Ace of Diamonds, Ace of Hearts, Ace of Spades, 2 of Clubs, 2 of Diamonds, and so on through the ranks: Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King. The array indexes are 0 to 51, with Ace of Clubs at 0, and King of Spades at 51.</li>
|
||||
<li>Until the array is empty:</li>
|
||||
<li>Choose a random card at index ≡ next random number (mod array length).</li>
|
||||
<ul>
|
||||
<li>Swap this random card with the last card of the array.</li>
|
||||
<li>Remove this random card from the array. (Array length goes down by 1.)</li>
|
||||
<li>Deal this random card.</li>
|
||||
</ul>
|
||||
<li>Deal all 52 cards, face up, across 8 columns. The first 8 cards go in 8 columns, the next 8 cards go on the first 8 cards, and so on.</li>
|
||||
</ol>
|
||||
|
||||
**Example:**
|
||||
|
||||
**Order to deal cards**
|
||||
|
||||
<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']
|
||||
] </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>
|
||||
49 50 51 52</pre>
|
||||
|
||||
**Game #1**
|
||||
|
||||
```js
|
||||
[
|
||||
['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']
|
||||
]
|
||||
```
|
||||
|
||||
**Game #617**
|
||||
|
||||
```js
|
||||
[
|
||||
['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']
|
||||
]
|
||||
```
|
||||
|
||||
# --instructions--
|
||||
|
||||
Write a function to take a deal number and deal cards in the same order as this algorithm. The function must return a two dimensional array representing the FreeCell board.
|
||||
|
||||
Deals can also be checked against [FreeCell solutions to 1000000 games](https://freecellgamesolutions.com/). (Summon a video solution, and it displays the initial deal.)
|
||||
|
||||
# --hints--
|
||||
|
||||
`dealFreeCell`是一个功能。
|
||||
`dealFreeCell` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof dealFreeCell === 'function');
|
||||
```
|
||||
|
||||
`dealFreeCell(seed)`应该返回一个对象。
|
||||
`dealFreeCell(seed)` should return an object.
|
||||
|
||||
```js
|
||||
assert(typeof dealFreeCell(1) === 'object');
|
||||
```
|
||||
|
||||
`dealFreeCell(seed)`应该返回一个长度为7的数组。
|
||||
`dealFreeCell(seed)` should return an array of length 7.
|
||||
|
||||
```js
|
||||
assert(dealFreeCell(1).length === 7);
|
||||
```
|
||||
|
||||
`dealFreeCell(1)`应该返回一个与示例“Game#1”相同的数组
|
||||
`dealFreeCell(1)` should return an array identical to example "Game #1"
|
||||
|
||||
```js
|
||||
assert.deepEqual(dealFreeCell(1), game1);
|
||||
```
|
||||
|
||||
`dealFreeCell(617)`应该返回一个与示例“Game#617”相同的数组
|
||||
`dealFreeCell(617)` should return an array identical to example "Game #617"
|
||||
|
||||
```js
|
||||
assert.deepEqual(dealFreeCell(617), game617);
|
||||
|
@ -1,50 +1,51 @@
|
||||
---
|
||||
id: 596a8888ab7c01048de257d5
|
||||
title: deepcopy的
|
||||
title: Deepcopy
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302247
|
||||
dashedName: deepcopy
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
任务:
|
||||
Write a function that returns a deep copy of a given object. The copy must not be the same object that was given.
|
||||
|
||||
编写一个返回给定对象的深层副本的函数。
|
||||
This task will not test for:
|
||||
|
||||
副本不得与给定的对象相同。
|
||||
|
||||
此任务不会测试:
|
||||
|
||||
具有属性属性的对象Date对象或具有Date对象属性的对象RegEx或具有RegEx对象属性的对象原型复制
|
||||
<ul>
|
||||
<li>Objects with properties that are functions</li>
|
||||
<li>Date objects or object with properties that are Date objects</li>
|
||||
<li>RegEx or object with properties that are RegEx objects</li>
|
||||
<li>Prototype copying</li>
|
||||
</ul>
|
||||
|
||||
# --hints--
|
||||
|
||||
`deepcopy`应该是一个功能。
|
||||
`deepcopy` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof deepcopy === 'function');
|
||||
```
|
||||
|
||||
`deepcopy({test: "test"})`应返回一个对象。
|
||||
`deepcopy({test: "test"})` should return an object.
|
||||
|
||||
```js
|
||||
assert(typeof deepcopy(obj1) === 'object');
|
||||
```
|
||||
|
||||
不应该返回提供的相同对象。
|
||||
`deepcopy` should not return the same object that was provided.
|
||||
|
||||
```js
|
||||
assert(deepcopy(obj2) != obj2);
|
||||
```
|
||||
|
||||
传递包含数组的对象时,应返回该对象的深层副本。
|
||||
When passed an object containing an array, `deepcopy` should return a deep copy of the object.
|
||||
|
||||
```js
|
||||
assert.deepEqual(deepcopy(obj2), obj2);
|
||||
```
|
||||
|
||||
传递包含另一个对象的对象时,应返回该对象的深层副本。
|
||||
When passed an object containing another object, `deepcopy` should return a deep copy of the object.
|
||||
|
||||
```js
|
||||
assert.deepEqual(deepcopy(obj3), obj3);
|
||||
|
@ -1,100 +1,103 @@
|
||||
---
|
||||
id: 597089c87eec450c68aa1643
|
||||
title: 定义原始数据类型
|
||||
title: Define a primitive data type
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302248
|
||||
dashedName: define-a-primitive-data-type
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
任务:
|
||||
Define a type that behaves like an integer but has a lowest valid value of 1 and a highest valid value of 10.
|
||||
|
||||
定义一个行为类似于整数但最低有效值为1且最高有效值为10的类型。
|
||||
Error handling:
|
||||
|
||||
错误:如果您尝试实例化一个值超出1 - 10的`Num` ,它应该抛出一个错误消息为`'Out of range'`的`TypeError` 。如果您尝试使用不是`Num`的值实例化`Num` ,则应抛出`TypeError`并显示错误消息`'Not a Number'` 。
|
||||
<ul>
|
||||
<li>If you try to instantiate a <code>Num</code> with a value outside of 1 - 10, it should throw a <code>TypeError</code> with an error message of <code>'Out of range'</code>.</li>
|
||||
<li>If you try to instantiate a <code>Num</code> with a value that is not a number, it should throw a <code>TypeError</code> with an error message of <code>'Not a Number'</code>.</li>
|
||||
</ul>
|
||||
|
||||
# --hints--
|
||||
|
||||
`Num`应该是一个功能。
|
||||
`Num` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof Num === 'function');
|
||||
```
|
||||
|
||||
`new Num(4)`应返回一个对象。
|
||||
`new Num(4)` should return an object.
|
||||
|
||||
```js
|
||||
assert(typeof new Num(4) === 'object');
|
||||
```
|
||||
|
||||
`new Num(\'test\')`应抛出一个带有消息“非数字”的TypeError。
|
||||
`new Num('test')` should throw a TypeError with message 'Not a Number'.
|
||||
|
||||
```js
|
||||
assert.throws(() => new Num('test'), TypeError);
|
||||
```
|
||||
|
||||
`new Num(0)`应该抛出一个带有消息“超出范围”的TypeError。
|
||||
`new Num(0)` should throw a TypeError with message 'Out of range'.
|
||||
|
||||
```js
|
||||
assert.throws(() => new Num(0), TypeError);
|
||||
```
|
||||
|
||||
`new Num(-5)`应该抛出一个带有消息“超出范围”的TypeError。
|
||||
`new Num(-5)` should throw a TypeError with message 'Out of range'.
|
||||
|
||||
```js
|
||||
assert.throws(() => new Num(-5), TypeError);
|
||||
```
|
||||
|
||||
`new Num(10)`应抛出一个带有消息“超出范围”的TypeError。
|
||||
`new Num(10)` should throw a TypeError with message 'Out of range'.
|
||||
|
||||
```js
|
||||
assert.throws(() => new Num(11), TypeError);
|
||||
```
|
||||
|
||||
`new Num(20)`应抛出一个带有消息“超出范围”的TypeError。
|
||||
`new Num(20)` should throw a TypeError with message 'Out of range'.
|
||||
|
||||
```js
|
||||
assert.throws(() => new Num(20), TypeError);
|
||||
```
|
||||
|
||||
`new Num(3) + new Num(4)`应该等于7。
|
||||
`new Num(3) + new Num(4)` should equal 7.
|
||||
|
||||
```js
|
||||
assert.equal(new Num(3) + new Num(4), 7);
|
||||
```
|
||||
|
||||
`new Num(3) - new Num(4)`应该等于-1。
|
||||
`new Num(3) - new Num(4)` should equal -1.
|
||||
|
||||
```js
|
||||
assert.equal(new Num(3) - new Num(4), -1);
|
||||
```
|
||||
|
||||
`new Num(3) * new Num(4)`应该等于12。
|
||||
`new Num(3) * new Num(4)` should equal 12.
|
||||
|
||||
```js
|
||||
assert.equal(new Num(3) * new Num(4), 12);
|
||||
```
|
||||
|
||||
`new Num(3) / new Num(4)`应该等于0.75。
|
||||
`new Num(3) / new Num(4)` should equal 0.75.
|
||||
|
||||
```js
|
||||
assert.equal(new Num(3) / new Num(4), 0.75);
|
||||
```
|
||||
|
||||
`new Num(3) < new Num(4)`应该是真的。
|
||||
`new Num(3) < new Num(4)` should be true.
|
||||
|
||||
```js
|
||||
assert(new Num(3) < new Num(4));
|
||||
```
|
||||
|
||||
`new Num(3) > new Num(4)`应该是假的。
|
||||
`new Num(3) > new Num(4)` should be false.
|
||||
|
||||
```js
|
||||
assert(!(new Num(3) > new Num(4)));
|
||||
```
|
||||
|
||||
`(new Num(5)).toString()`应返回\\'5 \\'
|
||||
`(new Num(5)).toString()` should return '5'
|
||||
|
||||
```js
|
||||
assert.equal(new Num(5).toString(), '5');
|
||||
|
@ -1,36 +1,60 @@
|
||||
---
|
||||
id: 59f40b17e79dbf1ab720ed7a
|
||||
title: 部门编号
|
||||
title: Department Numbers
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302249
|
||||
dashedName: department-numbers
|
||||
---
|
||||
|
||||
# --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>
|
||||
There is a highly organized city that has decided to assign a number to each of their departments:
|
||||
|
||||
<ul>
|
||||
<li>Police department</li>
|
||||
<li>Sanitation department</li>
|
||||
<li>Fire department</li>
|
||||
</ul>
|
||||
|
||||
Each department can have a number between 1 and 7 (inclusive).
|
||||
|
||||
The three department numbers are to be unique (different from each other) and must add up to the number 12.
|
||||
|
||||
The Chief of the Police doesn't like odd numbers and wants to have an even number for his department.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Write a program which outputs all valid combinations as an array.
|
||||
|
||||
```js
|
||||
[2, 3, 7] [2, 4, 6] [2, 6, 4]
|
||||
[2, 7, 3] [4, 1, 7] [4, 2, 6]
|
||||
[4, 3, 5] [4, 5, 3] [4, 6, 2]
|
||||
[4, 7, 1] [6, 1, 5] [6, 2, 4]
|
||||
[6, 4, 2] [6, 5, 1]
|
||||
```
|
||||
|
||||
# --hints--
|
||||
|
||||
`combinations`应该是一个功能。
|
||||
`combinations` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof combinations === 'function');
|
||||
```
|
||||
|
||||
`combinations([1, 2, 3], 6)`应该返回一个数组。
|
||||
`combinations([1, 2, 3], 6)` should return an Array.
|
||||
|
||||
```js
|
||||
assert(Array.isArray(combinations([1, 2, 3], 6)));
|
||||
```
|
||||
|
||||
`combinations([1, 2, 3, 4, 5, 6, 7], 12)`应返回长度为14的数组。
|
||||
`combinations([1, 2, 3, 4, 5, 6, 7], 12)` should return an array of length 14.
|
||||
|
||||
```js
|
||||
assert(combinations(nums, total).length === len);
|
||||
```
|
||||
|
||||
`combinations([1, 2, 3, 4, 5, 6, 7], 12)`应返回所有有效组合。
|
||||
`combinations([1, 2, 3, 4, 5, 6, 7], 12)` should return all valid combinations.
|
||||
|
||||
```js
|
||||
assert.deepEqual(combinations(nums, total), result);
|
||||
|
@ -1,26 +1,24 @@
|
||||
---
|
||||
id: 59f4eafba0343628bb682785
|
||||
title: Discordian日期
|
||||
title: Discordian date
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302250
|
||||
dashedName: discordian-date
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
任务:
|
||||
|
||||
将给定日期从[公历](<https://en.wikipedia.org/wiki/Gregorian calendar> "wp:阳历")转换为[Discordian日历](<https://en.wikipedia.org/wiki/Discordian calendar> "wp:Discordian日历") 。
|
||||
Convert a given date from the [Gregorian calendar](https://en.wikipedia.org/wiki/Gregorian calendar "wp: Gregorian calendar") to the [Discordian calendar](https://en.wikipedia.org/wiki/Discordian calendar "wp: Discordian calendar").
|
||||
|
||||
# --hints--
|
||||
|
||||
`discordianDate`是一个函数。
|
||||
`discordianDate` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof discordianDate === 'function');
|
||||
```
|
||||
|
||||
`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"` 。
|
||||
`discordianDate(new Date(2010, 6, 22))` should return `"Pungenday, the 57th day of Confusion in the YOLD 3176"`.
|
||||
|
||||
```js
|
||||
assert(
|
||||
@ -29,7 +27,7 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
`discordianDate(new Date(2012, 1, 28))`应该返回`"Prickle-Prickle, the 59th day of Chaos in the YOLD 3178"` 。
|
||||
`discordianDate(new Date(2012, 1, 28))` should return `"Prickle-Prickle, the 59th day of Chaos in the YOLD 3178"`.
|
||||
|
||||
```js
|
||||
assert(
|
||||
@ -38,7 +36,7 @@ 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, 1, 29))`应该返回`"Setting Orange, the 60th day of Chaos in the YOLD 3178. Celebrate St. Tib's Day!"` 。
|
||||
`discordianDate(new Date(2012, 1, 29))` should return `"Setting Orange, the 60th day of Chaos in the YOLD 3178. Celebrate St. Tib\'s Day!"`.
|
||||
|
||||
```js
|
||||
assert(
|
||||
@ -47,7 +45,7 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
`discordianDate(new Date(2012, 2, 1))`应该返回`"Setting Orange, the 60th day of Chaos in the YOLD 3178"` 。
|
||||
`discordianDate(new Date(2012, 2, 1))` should return `"Setting Orange, the 60th day of Chaos in the YOLD 3178"`.
|
||||
|
||||
```js
|
||||
assert(
|
||||
@ -56,7 +54,7 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
`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!"` 。
|
||||
`discordianDate(new Date(2010, 0, 5))` should return `"Setting Orange, the 5th day of Chaos in the YOLD 3176. Celebrate Mungday!"`.
|
||||
|
||||
```js
|
||||
assert(
|
||||
@ -65,7 +63,7 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
`discordianDate(new Date(2011, 4, 3))`应该返回`"Pungenday, the 50th day of Discord in the YOLD 3177. Celebrate Discoflux!"` 。
|
||||
`discordianDate(new Date(2011, 4, 3))` should return `"Pungenday, the 50th day of Discord in the YOLD 3177. Celebrate Discoflux!"`.
|
||||
|
||||
```js
|
||||
assert(
|
||||
@ -74,7 +72,7 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
`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"` 。
|
||||
`discordianDate(new Date(2015, 9, 19))` should return `"Boomtime, the 73rd day of Bureaucracy in the YOLD 3181"`.
|
||||
|
||||
```js
|
||||
assert(
|
||||
|
@ -8,7 +8,7 @@ dashedName: dot-product
|
||||
|
||||
# --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.
|
||||
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--
|
||||
|
||||
|
@ -1,24 +1,36 @@
|
||||
---
|
||||
id: 599c333915e0ea32d04d4bec
|
||||
title: 元素操作
|
||||
title: Element-wise operations
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302252
|
||||
dashedName: element-wise-operations
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
<p>实现基本的元素矩阵 - 矩阵和标量矩阵运算。 </p><p>实行: </p><p> :: *另外</p><p> :: *减法</p><p> :: *乘法</p><p> :: *分裂</p><p> :: *取幂</p><p>第一个参数是要执行的操作,例如:用于矩阵加法的“m_add”和用于标量加法的“s_add”。第二和第三参数将是要在其上执行操作的矩阵。 </p>
|
||||
Implement basic element-wise matrix-matrix and scalar-matrix operations.
|
||||
|
||||
**Implement:**
|
||||
|
||||
<ul>
|
||||
<li>addition</li>
|
||||
<li>subtraction</li>
|
||||
<li>multiplication</li>
|
||||
<li>division</li>
|
||||
<li>exponentiation</li>
|
||||
</ul>
|
||||
|
||||
The first parameter will be the operation to be performed, for example, "m_add" for matrix addition and "s_add" for scalar addition. The second and third parameters will be the matrices on which the operations are to be performed.
|
||||
|
||||
# --hints--
|
||||
|
||||
`operation`是一种功能。
|
||||
`operation` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof operation === 'function');
|
||||
```
|
||||
|
||||
`operation("m_add",[[1,2],[3,4]],[[1,2],[3,4]])`应返回`[[2,4],[6,8]]` 。
|
||||
`operation("m_add",[[1,2],[3,4]],[[1,2],[3,4]])` should return `[[2,4],[6,8]]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
@ -40,7 +52,7 @@ assert.deepEqual(
|
||||
);
|
||||
```
|
||||
|
||||
`operation("s_add",[[1,2],[3,4]],[[1,2],[3,4]])`应返回`[[3,4],[5,6]]` 。
|
||||
`operation("s_add",[[1,2],[3,4]],2)` should return `[[3,4],[5,6]]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
@ -59,7 +71,7 @@ assert.deepEqual(
|
||||
);
|
||||
```
|
||||
|
||||
`operation("m_sub",[[1,2],[3,4]],[[1,2],[3,4]])`应返回`[[0,0],[0,0]]` 。
|
||||
`operation("m_sub",[[1,2],[3,4]],[[1,2],[3,4]])` should return `[[0,0],[0,0]]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
@ -81,7 +93,7 @@ assert.deepEqual(
|
||||
);
|
||||
```
|
||||
|
||||
`operation("m_mult",[[1,2],[3,4]],[[1,2],[3,4]])`应该返回`[[1,4],[9,16]]` 。
|
||||
`operation("m_mult",[[1,2],[3,4]],[[1,2],[3,4]])` should return `[[1,4],[9,16]]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
@ -103,7 +115,7 @@ assert.deepEqual(
|
||||
);
|
||||
```
|
||||
|
||||
`operation("m_div",[[1,2],[3,4]],[[1,2],[3,4]])`应返回`[[1,1],[1,1]]` 。
|
||||
`operation("m_div",[[1,2],[3,4]],[[1,2],[3,4]])` should return `[[1,1],[1,1]]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
@ -125,7 +137,7 @@ assert.deepEqual(
|
||||
);
|
||||
```
|
||||
|
||||
`operation("m_exp",[[1,2],[3,4]],[[1,2],[3,4]])`应返回`[[1,4],[27,256]]` 。
|
||||
`operation("m_exp",[[1,2],[3,4]],[[1,2],[3,4]])` should return `[[1,4],[27,256]]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
@ -147,7 +159,7 @@ 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]]` 。
|
||||
`operation("m_add",[[1,2,3,4],[5,6,7,8]],[[9,10,11,12],[13,14,15,16]])` should return `[[10,12,14,16],[18,20,22,24]]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
|
@ -1,24 +1,37 @@
|
||||
---
|
||||
id: 599d0ba974141b0f508b37d5
|
||||
title: Emirp奖金
|
||||
title: Emirp primes
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302253
|
||||
dashedName: emirp-primes
|
||||
---
|
||||
|
||||
# --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>
|
||||
An emirp (**prime** spelled backwards) are primes that when reversed (in their decimal representation) are a different prime.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Write a function that:
|
||||
|
||||
<ul>
|
||||
<li>Shows the first <code>n</code> emirp numbers.</li>
|
||||
<li>Shows the emirp numbers in a range.</li>
|
||||
<li>Shows the number of emirps in a range.</li>
|
||||
<li>Shows the <code>n<sup>th</sup></code> emirp number.</li>
|
||||
</ul>
|
||||
|
||||
The function should accept two parameters. The first will receive `n` or the range as an array. The second will receive a boolean, that specifies if the function returns the emirps as an array or a single number (the number of primes in the range or the <code>n<sup>th</sup></code> prime). According to the parameters the function should return an array or a number.
|
||||
|
||||
# --hints--
|
||||
|
||||
`emirps`是一个功能。
|
||||
`emirps` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof emirps === 'function');
|
||||
```
|
||||
|
||||
`emirps(20,true)`应该返回`[13,17,31,37,71,73,79,97,107,113,149,157,167,179,199,311,337,347,359,389]`
|
||||
`emirps(20,true)` should return `[13,17,31,37,71,73,79,97,107,113,149,157,167,179,199,311,337,347,359,389]`
|
||||
|
||||
```js
|
||||
assert.deepEqual(emirps(20, true), [
|
||||
@ -45,13 +58,13 @@ assert.deepEqual(emirps(20, true), [
|
||||
]);
|
||||
```
|
||||
|
||||
`emirps(10000)`应该返回`948349`
|
||||
`emirps(1000)` should return `70529`
|
||||
|
||||
```js
|
||||
assert.deepEqual(emirps(1000), 70529);
|
||||
```
|
||||
|
||||
`emirps([7700,8000],true)`应该返回`[7717,7757,7817,7841,7867,7879,7901,7927,7949,7951,7963]`
|
||||
`emirps([7700,8000],true)` should return `[7717,7757,7817,7841,7867,7879,7901,7927,7949,7951,7963]`
|
||||
|
||||
```js
|
||||
assert.deepEqual(emirps([7700, 8000], true), [
|
||||
@ -69,7 +82,7 @@ assert.deepEqual(emirps([7700, 8000], true), [
|
||||
]);
|
||||
```
|
||||
|
||||
`emirps([7700,8000],true)`应该返回`11`
|
||||
`emirps([7700,8000],true)` should return `11`
|
||||
|
||||
```js
|
||||
assert.deepEqual(emirps([7700, 8000], false), 11);
|
||||
|
@ -1,62 +1,60 @@
|
||||
---
|
||||
id: 599d15309e88c813a40baf58
|
||||
title: 熵
|
||||
title: Entropy
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302254
|
||||
dashedName: entropy
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
任务:
|
||||
Calculate the Shannon entropy H of a given input string.
|
||||
|
||||
计算给定输入字符串的香农熵H.
|
||||
Given the discreet random variable $X$ that is a string of $N$ "symbols" (total characters) consisting of $n$ different characters (n=2 for binary), the Shannon entropy of X in bits/symbol is:
|
||||
|
||||
给定谨慎的随机变量$ 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)$
|
||||
|
||||
$ H*2(X)= - \\ sum* {i = 1} ^ n \\ frac {count_i} {N} \\ log_2 \\ left(\\ frac {count_i} {N} \\ right)$
|
||||
|
||||
其中$ count_i $是字符$ n_i $的计数。
|
||||
where $count_i$ is the count of character $n_i$.
|
||||
|
||||
# --hints--
|
||||
|
||||
`entropy`是一种功能。
|
||||
`entropy` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof entropy === 'function');
|
||||
```
|
||||
|
||||
`entropy("0")`应该返回`0`
|
||||
`entropy("0")` should return `0`
|
||||
|
||||
```js
|
||||
assert.equal(entropy('0'), 0);
|
||||
```
|
||||
|
||||
`entropy("01")`应该返回`1`
|
||||
`entropy("01")` should return `1`
|
||||
|
||||
```js
|
||||
assert.equal(entropy('01'), 1);
|
||||
```
|
||||
|
||||
`entropy("0123")`应该返回`2`
|
||||
`entropy("0123")` should return `2`
|
||||
|
||||
```js
|
||||
assert.equal(entropy('0123'), 2);
|
||||
```
|
||||
|
||||
`entropy("01234567")`应该返回`3`
|
||||
`entropy("01234567")` should return `3`
|
||||
|
||||
```js
|
||||
assert.equal(entropy('01234567'), 3);
|
||||
```
|
||||
|
||||
`entropy("0123456789abcdef")`应返回`4`
|
||||
`entropy("0123456789abcdef")` should return `4`
|
||||
|
||||
```js
|
||||
assert.equal(entropy('0123456789abcdef'), 4);
|
||||
```
|
||||
|
||||
`entropy("1223334444")`应返回`1.8464393446710154`
|
||||
`entropy("1223334444")` should return `1.8464393446710154`
|
||||
|
||||
```js
|
||||
assert.equal(entropy('1223334444'), 1.8464393446710154);
|
||||
|
@ -1,54 +1,88 @@
|
||||
---
|
||||
id: 5987fd532b954e0f21b5d3f6
|
||||
title: 均衡指数
|
||||
title: Equilibrium index
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302255
|
||||
dashedName: equilibrium-index
|
||||
---
|
||||
|
||||
# --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>
|
||||
An equilibrium index of a sequence is an index into the sequence such that the sum of elements at lower indices is equal to the sum of elements at higher indices.
|
||||
|
||||
For example, in a sequence $A$:
|
||||
|
||||
<ul style='list-style: none;'>
|
||||
<li><big>$A_0 = -7$</big></li>
|
||||
<li><big>$A_1 = 1$</big></li>
|
||||
<li><big>$A_2 = 5$</big></li>
|
||||
<li><big>$A_3 = 2$</big></li>
|
||||
<li><big>$A_4 = -4$</big></li>
|
||||
<li><big>$A_5 = 3$</big></li>
|
||||
<li><big>$A_6 = 0$</big></li>
|
||||
</ul>
|
||||
|
||||
`3` is an equilibrium index, because:
|
||||
|
||||
<ul style='list-style: none;'>
|
||||
<li><big>$A_0 + A_1 + A_2 = A_4 + A_5 + A_6$</big></li>
|
||||
</ul>
|
||||
|
||||
`6` is also an equilibrium index, because:
|
||||
|
||||
<ul style='list-style: none;'>
|
||||
<li><big>$A_0 + A_1 + A_2 + A_3 + A_4 + A_5 = 0$</big></li>
|
||||
</ul>
|
||||
|
||||
(sum of zero elements is zero)
|
||||
|
||||
`7` is not an equilibrium index, because it is not a valid index of sequence $A$.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Write a function that, given a sequence, returns its equilibrium indices (if any).
|
||||
|
||||
Assume that the sequence may be very long.
|
||||
|
||||
# --hints--
|
||||
|
||||
`equilibrium`是一种功能。
|
||||
`equilibrium` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof equilibrium === 'function');
|
||||
```
|
||||
|
||||
`equilibrium([-7, 1, 5, 2, -4, 3, 0])` `[3,6]` `equilibrium([-7, 1, 5, 2, -4, 3, 0])`应该返回`[3,6]` 。
|
||||
`equilibrium([-7, 1, 5, 2, -4, 3, 0])` should return `[3,6]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(equilibrium(equilibriumTests[0]), ans[0]);
|
||||
```
|
||||
|
||||
`equilibrium([2, 4, 6])`应该返回`[]` 。
|
||||
`equilibrium([2, 4, 6])` should return `[]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(equilibrium(equilibriumTests[1]), ans[1]);
|
||||
```
|
||||
|
||||
`equilibrium([2, 9, 2])`应该返回`[1]` 。
|
||||
`equilibrium([2, 9, 2])` should return `[1]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(equilibrium(equilibriumTests[2]), ans[2]);
|
||||
```
|
||||
|
||||
`equilibrium([1, -1, 1, -1, 1, -1, 1])`应该返回`[0,1,2,3,4,5,6]` 。
|
||||
`equilibrium([1, -1, 1, -1, 1, -1, 1])` should return `[0,1,2,3,4,5,6]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(equilibrium(equilibriumTests[3]), ans[3]);
|
||||
```
|
||||
|
||||
`equilibrium([1])`应该返回`[0]` 。
|
||||
`equilibrium([1])` should return `[0]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(equilibrium(equilibriumTests[4]), ans[4]);
|
||||
```
|
||||
|
||||
`equilibrium([])`应该返回`[]` 。
|
||||
`equilibrium([])` should return `[]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(equilibrium(equilibriumTests[5]), ans[5]);
|
||||
|
@ -1,48 +1,121 @@
|
||||
---
|
||||
id: 599d1566a02b571412643b84
|
||||
title: 埃塞俄比亚的乘法
|
||||
title: Ethiopian multiplication
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302257
|
||||
dashedName: ethiopian-multiplication
|
||||
---
|
||||
|
||||
# --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>
|
||||
Ethiopian multiplication is a method of multiplying integers using only addition, doubling, and halving.
|
||||
|
||||
**Method:**
|
||||
|
||||
<ol>
|
||||
<li>Take two numbers to be multiplied and write them down at the top of two columns</li>
|
||||
<li>In the left-hand column repeatedly halve the last number, discarding any remainders, and write the result below the last in the same column, until you write a value of <code>1</code></li>
|
||||
<li>In the right-hand column repeatedly double the last number and write the result below. stop when you add a result in the same row as where the left hand column shows <code>1</code></li>
|
||||
<li>Examine the table produced and discard any row where the value in the left column is even</li>
|
||||
<li>Sum the values in the right-hand column that remain to produce the result of multiplying the original two numbers together</li>
|
||||
</ol>
|
||||
|
||||
**For example:** `17 × 34`
|
||||
|
||||
<pre>17 34
|
||||
</pre>
|
||||
|
||||
Halving the first column:
|
||||
|
||||
<pre>17 34
|
||||
8
|
||||
4
|
||||
2
|
||||
1
|
||||
</pre>
|
||||
|
||||
Doubling the second column:
|
||||
|
||||
<pre>17 34
|
||||
8 68
|
||||
4 136
|
||||
2 272
|
||||
1 544
|
||||
</pre>
|
||||
|
||||
Strike-out rows whose first cell is even:
|
||||
|
||||
<pre>17 34
|
||||
8 <strike>68</strike>
|
||||
4 <strike>136</strike>
|
||||
2 <strike>272</strike>
|
||||
1 544
|
||||
</pre>
|
||||
|
||||
Sum the remaining numbers in the right-hand column:
|
||||
|
||||
<!-- markdownlint-disable MD003 -->
|
||||
|
||||
<pre>17 34
|
||||
8 --
|
||||
4 ---
|
||||
2 ---
|
||||
1 544
|
||||
====
|
||||
578
|
||||
</pre>
|
||||
|
||||
<!-- markdownlint-enable MD003 -->
|
||||
|
||||
So `17` multiplied by `34`, by the Ethiopian method is `578`.
|
||||
|
||||
# --instructions--
|
||||
|
||||
The task is to define three named functions/methods/procedures/subroutines:
|
||||
|
||||
<ol>
|
||||
<li>one to halve an integer,</li>
|
||||
<li>one to double an integer, and</li>
|
||||
<li>one to state if an integer is even</li>
|
||||
</ol>
|
||||
|
||||
Use these functions to create a function that does Ethiopian multiplication.
|
||||
|
||||
<!-- markdownlint-disable MD046-->
|
||||
|
||||
# --hints--
|
||||
|
||||
`eth_mult`是一个功能。
|
||||
`eth_mult` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof eth_mult === 'function');
|
||||
```
|
||||
|
||||
`eth_mult(17,34)`应该返回`578` 。
|
||||
`eth_mult(17,34)` should return `578`.
|
||||
|
||||
```js
|
||||
assert.equal(eth_mult(17, 34), 578);
|
||||
```
|
||||
|
||||
`eth_mult(23,46)`应该返回`1058` 。
|
||||
`eth_mult(23,46)` should return `1058`.
|
||||
|
||||
```js
|
||||
assert.equal(eth_mult(23, 46), 1058);
|
||||
```
|
||||
|
||||
`eth_mult(12,27)`应该返回`324` 。
|
||||
`eth_mult(12,27)` should return `324`.
|
||||
|
||||
```js
|
||||
assert.equal(eth_mult(12, 27), 324);
|
||||
```
|
||||
|
||||
`eth_mult(56,98)`应该返回`5488` 。
|
||||
`eth_mult(56,98)` should return `5488`.
|
||||
|
||||
```js
|
||||
assert.equal(eth_mult(56, 98), 5488);
|
||||
```
|
||||
|
||||
`eth_mult(63,74)`应该返回`4662` 。
|
||||
`eth_mult(63,74)` should return `4662`.
|
||||
|
||||
```js
|
||||
assert.equal(eth_mult(63, 74), 4662);
|
||||
|
@ -1,42 +1,125 @@
|
||||
---
|
||||
id: 59880443fb36441083c6c20e
|
||||
title: 欧拉方法
|
||||
title: Euler method
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302258
|
||||
dashedName: euler-method
|
||||
---
|
||||
|
||||
# --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>
|
||||
Euler's method numerically approximates solutions of first-order ordinary differential equations (ODEs) with a given initial value. It is an explicit method for solving initial value problems (IVPs), as described in [the wikipedia page](https://en.wikipedia.org/wiki/Euler method "wp: Euler method").
|
||||
|
||||
The ODE has to be provided in the following form:
|
||||
|
||||
<ul style='list-style: none;'>
|
||||
<li><big>$\frac{dy(t)}{dt} = f(t,y(t))$</big></li>
|
||||
</ul>
|
||||
|
||||
with an initial value
|
||||
|
||||
<ul style='list-style: none;'>
|
||||
<li><big>$y(t_0) = y_0$</big></li>
|
||||
</ul>
|
||||
|
||||
To get a numeric solution, we replace the derivative on the LHS with a finite difference approximation:
|
||||
|
||||
<ul style='list-style: none;'>
|
||||
<li><big>$\frac{dy(t)}{dt} \approx \frac{y(t+h)-y(t)}{h}$</big></li>
|
||||
</ul>
|
||||
|
||||
then solve for $y(t+h)$:
|
||||
|
||||
<ul style='list-style: none;'>
|
||||
<li><big>$y(t+h) \approx y(t) + h \, \frac{dy(t)}{dt}$</big></li>
|
||||
</ul>
|
||||
|
||||
which is the same as
|
||||
|
||||
<ul style='list-style: none;'>
|
||||
<li><big>$y(t+h) \approx y(t) + h \, f(t,y(t))$</big></li>
|
||||
</ul>
|
||||
|
||||
The iterative solution rule is then:
|
||||
|
||||
<ul style='list-style: none;'>
|
||||
<li><big>$y_{n+1} = y_n + h \, f(t_n, y_n)$</big></li>
|
||||
</ul>
|
||||
|
||||
where $h$ is the step size, the most relevant parameter for accuracy of the solution. A smaller step size increases accuracy but also the computation cost, so it has always has to be hand-picked according to the problem at hand.
|
||||
|
||||
**Example: Newton's Cooling Law**
|
||||
|
||||
Newton's cooling law describes how an object of initial temperature $T(t_0) = T_0$ cools down in an environment of temperature $T_R$:
|
||||
|
||||
<ul style='list-style: none;'>
|
||||
<li><big>$\frac{dT(t)}{dt} = -k \, \Delta T$</big></li>
|
||||
</ul>
|
||||
|
||||
or
|
||||
|
||||
<ul style='list-style: none;'>
|
||||
<li><big>$\frac{dT(t)}{dt} = -k \, (T(t) - T_R)$</big></li>
|
||||
</ul>
|
||||
|
||||
It says that the cooling rate $\\frac{dT(t)}{dt}$ of the object is proportional to the current temperature difference $\\Delta T = (T(t) - T_R)$ to the surrounding environment.
|
||||
|
||||
The analytical solution, which we will compare to the numerical approximation, is
|
||||
|
||||
<ul style='list-style: none;'>
|
||||
<li><big>$T(t) = T_R + (T_0 - T_R) \; e^{-k t}$</big></li>
|
||||
</ul>
|
||||
|
||||
# --instructions--
|
||||
|
||||
Implement a routine of Euler's method and then use it to solve the given example of Newton's cooling law for three different step sizes of:
|
||||
|
||||
<ul>
|
||||
<li><code>2 s</code></li>
|
||||
<li><code>5 s</code> and</li>
|
||||
<li><code>10 s</code></li>
|
||||
</ul>
|
||||
|
||||
and compare with the analytical solution.
|
||||
|
||||
**Initial values:**
|
||||
|
||||
<ul>
|
||||
<li>initial temperature <big>$T_0$</big> shall be <code>100 °C</code></li>
|
||||
<li>room temperature <big>$T_R$</big> shall be <code>20 °C</code></li>
|
||||
<li>cooling constant <big>$k$</big> shall be <code>0.07</code></li>
|
||||
<li>time interval to calculate shall be from <code>0 s</code> to <code>100 s</code></li>
|
||||
</ul>
|
||||
|
||||
First parameter to the function is initial time, second parameter is initial temperature, third parameter is elapsed time and fourth parameter is step size.
|
||||
|
||||
# --hints--
|
||||
|
||||
`eulersMethod`是一个函数。
|
||||
`eulersMethod` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof eulersMethod === 'function');
|
||||
```
|
||||
|
||||
`eulersMethod(0, 100, 100, 10)`应该返回一个数字。
|
||||
`eulersMethod(0, 100, 100, 2)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof eulersMethod(0, 100, 100, 10) === 'number');
|
||||
assert(typeof eulersMethod(0, 100, 100, 2) === 'number');
|
||||
```
|
||||
|
||||
`eulersMethod(0, 100, 100, 10)`应返回20.0424631833732。
|
||||
`eulersMethod(0, 100, 100, 2)` should return 20.0424631833732.
|
||||
|
||||
```js
|
||||
assert.equal(eulersMethod(0, 100, 100, 2), 20.0424631833732);
|
||||
```
|
||||
|
||||
`eulersMethod(0, 100, 100, 10)`应返回20.01449963666907。
|
||||
`eulersMethod(0, 100, 100, 5)` should return 20.01449963666907.
|
||||
|
||||
```js
|
||||
assert.equal(eulersMethod(0, 100, 100, 5), 20.01449963666907);
|
||||
```
|
||||
|
||||
`eulersMethod(0, 100, 100, 10)`应返回20.000472392。
|
||||
`eulersMethod(0, 100, 100, 10)` should return 20.000472392.
|
||||
|
||||
```js
|
||||
assert.equal(eulersMethod(0, 100, 100, 10), 20.000472392);
|
||||
|
@ -1,48 +1,52 @@
|
||||
---
|
||||
id: 598de241872ef8353c58a7a2
|
||||
title: 评估二项式系数
|
||||
title: Evaluate binomial coefficients
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302259
|
||||
dashedName: evaluate-binomial-coefficients
|
||||
---
|
||||
|
||||
# --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} $
|
||||
Write a function to calculate the binomial coefficient for the given value of n and k.
|
||||
|
||||
This formula is recommended:
|
||||
|
||||
$\\binom{n}{k} = \\frac{n!}{(n-k)!k!} = \\frac{n(n-1)(n-2)\\ldots(n-k+1)}{k(k-1)(k-2)\\ldots 1}$
|
||||
|
||||
# --hints--
|
||||
|
||||
`binom`是一个功能。
|
||||
`binom` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof binom === 'function');
|
||||
```
|
||||
|
||||
`binom(5,3)`应该返回10。
|
||||
`binom(5,3)` should return 10.
|
||||
|
||||
```js
|
||||
assert.equal(binom(5, 3), 10);
|
||||
```
|
||||
|
||||
`binom(7,2)`应该返回21。
|
||||
`binom(7,2)` should return 21.
|
||||
|
||||
```js
|
||||
assert.equal(binom(7, 2), 21);
|
||||
```
|
||||
|
||||
`binom(10,4)`应该返回210。
|
||||
`binom(10,4)` should return 210.
|
||||
|
||||
```js
|
||||
assert.equal(binom(10, 4), 210);
|
||||
```
|
||||
|
||||
`binom(6,1)`应该返回6。
|
||||
`binom(6,1)` should return 6.
|
||||
|
||||
```js
|
||||
assert.equal(binom(6, 1), 6);
|
||||
```
|
||||
|
||||
`binom(12,8)`应该返回495。
|
||||
`binom(12,8)` should return 495.
|
||||
|
||||
```js
|
||||
assert.equal(binom(12, 8), 495);
|
||||
|
@ -1,18 +1,16 @@
|
||||
---
|
||||
id: 59e09e6d412c5939baa02d16
|
||||
title: 执行马尔可夫算法
|
||||
title: Execute a Markov algorithm
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302260
|
||||
dashedName: execute-a-markov-algorithm
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
任务:
|
||||
Create an interpreter for a [Markov Algorithm](https://en.wikipedia.org/wiki/Markov algorithm "wp: Markov algorithm").
|
||||
|
||||
为[马尔可夫算法](<https://en.wikipedia.org/wiki/Markov algorithm> "wp:马尔可夫算法")创建解释器。
|
||||
|
||||
规则的语法如下:
|
||||
Rules have the syntax:
|
||||
|
||||
<pre>[ruleset] ::= (([comment] | [rule]) [newline]+)*
|
||||
[comment] ::= # {[any character]}
|
||||
@ -20,60 +18,60 @@ dashedName: execute-a-markov-algorithm
|
||||
[whitespace] ::= ([tab] | [space]) [[whitespace]]
|
||||
</pre>
|
||||
|
||||
每行有一条规则。
|
||||
There is one rule per line.
|
||||
|
||||
如果在 \[replacement] 之前有一个 `.`(句点),那么这就是一个终止规则。在这种情况下,解释器必须停止执行。
|
||||
If there is a `.` (period) present before the \[replacement], then this is a terminating rule in which case the interpreter must halt execution.
|
||||
|
||||
规则集由一系列规则组成,可能包含一些注释。
|
||||
A ruleset consists of a sequence of rules, with optional comments.
|
||||
|
||||
规则集
|
||||
Rulesets
|
||||
|
||||
我们会对你提交的代码进行如下测试:
|
||||
Use the following tests on entries:
|
||||
|
||||
**规则集 1:**
|
||||
**Ruleset 1:**
|
||||
|
||||
<pre># 此条规则来自 Wikipedia:
|
||||
<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>
|
||||
|
||||
对于这段文本:
|
||||
Sample text of:
|
||||
|
||||
`I bought a B of As from T S.`
|
||||
|
||||
应该输出:
|
||||
Should generate the output:
|
||||
|
||||
`I bought a bag of apples from my brother.`
|
||||
|
||||
**规则集 2:**
|
||||
**Ruleset 2:**
|
||||
|
||||
终止规则的测试
|
||||
A test of the terminating rule
|
||||
|
||||
<pre># 基于 Wikipedia 的规则稍做修改
|
||||
<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>
|
||||
|
||||
对于这段文本:
|
||||
Sample text of:
|
||||
|
||||
`I bought a B of As from T S.`
|
||||
|
||||
应该输出:
|
||||
Should generate:
|
||||
|
||||
`I bought a bag of apples from T shop.`
|
||||
|
||||
**规则集 3:**
|
||||
**Ruleset 3:**
|
||||
|
||||
这条不仅可以用来测试替换顺序是否正确,还可以测试你的代码中对正则表达式的处理是否完备。如果你的代码没有对正则表达式进行正确的转义处理,那在替换的时候就会出现问题。
|
||||
This tests for correct substitution order and may trap simple regexp based replacement routines if special regexp characters are not escaped.
|
||||
|
||||
<pre># BNF Syntax testing rules
|
||||
A -> apple
|
||||
@ -85,31 +83,32 @@ W -> WW
|
||||
S -> .shop
|
||||
T -> the
|
||||
the shop -> my brother
|
||||
(终止规则) -> .
|
||||
a never used -> .terminating rule
|
||||
</pre>
|
||||
|
||||
对于这段文本:
|
||||
Sample text of:
|
||||
|
||||
`I bought a B of As W my Bgage from T S.`
|
||||
|
||||
应该输出:
|
||||
Should generate:
|
||||
|
||||
`I bought a bag of apples with my money from T shop.`
|
||||
`I bought a bag of apples with my money from T shop.`
|
||||
|
||||
**规则集 4:**
|
||||
**Ruleset 4:**
|
||||
|
||||
这条是用来测试规则扫描的顺序是否正确,并可能捕获以错误顺序扫描的替换例程。这里我们选取了通用的一元乘法引擎(请注意,在此实现中,输入的表达式必须放在两个下划线之间)。
|
||||
This tests for correct order of scanning of rules, and may trap replacement routines that scan in the wrong order. It implements a general unary multiplication engine. (Note that the input expression must be placed within underscores in this implementation.)
|
||||
|
||||
<pre> ##一元乘法引擎,用于测试马尔可夫算法实现
|
||||
### by Donal Fellows
|
||||
# 一元加法引擎
|
||||
<pre>### Unary Multiplication Engine, for testing Markov Algorithm implementations
|
||||
### By Donal Fellows.
|
||||
# Unary addition engine
|
||||
_+1 -> _1+
|
||||
1+1 -> 11+
|
||||
# 将乘法转换为普通加法
|
||||
# Pass for converting from the splitting of multiplication into ordinary
|
||||
# addition
|
||||
1! -> !1
|
||||
,! -> !+
|
||||
_! -> _
|
||||
# 一元乘法,左侧为被乘数,右侧为乘数
|
||||
# Unary multiplication by duplicating left side, right side times
|
||||
1*1 -> x,@y
|
||||
1x -> xX
|
||||
X, -> 1,1
|
||||
@ -118,91 +117,94 @@ _x -> _X
|
||||
,x -> ,X
|
||||
y1 -> 1y
|
||||
y_ -> _
|
||||
# 下一阶段
|
||||
# Next phase of applying
|
||||
1@1 -> x,@y
|
||||
1@_ -> @_
|
||||
,@_ -> !_
|
||||
++ -> +
|
||||
# 加法的终止条件
|
||||
# Termination cleanup for addition
|
||||
_1 -> 1
|
||||
1+_ -> 1
|
||||
_+_ ->
|
||||
</pre>
|
||||
|
||||
对于这段文本:
|
||||
Sample text of:
|
||||
|
||||
`_1111*11111_`
|
||||
|
||||
应该输出:
|
||||
should generate the output:
|
||||
|
||||
`11111111111111111111`
|
||||
|
||||
**规则集5:**
|
||||
**Ruleset 5:**
|
||||
|
||||
一台简单的[图灵机](http://en.wikipedia.org/wiki/Turing_machine)包含三个状态的["忙碌海狸"](http://en.wikipedia.org/wiki/Busy_beaver)。纸带由 0 和 1 组成,状态为 A、B、C 和代表终止(Halt)的 H。通过在字符前写状态字母来的方式来指示读写头的位置。机器运行时需要的初始纸带必须通过输入在一开始全部给出。
|
||||
A simple [Turing machine](http://en.wikipedia.org/wiki/Turing_machine "link: http://en.wikipedia.org/wiki/Turing_machine"), implementing a three-state [busy beaver](http://en.wikipedia.org/wiki/Busy_beaver "link: http://en.wikipedia.org/wiki/Busy_beaver").
|
||||
|
||||
这一规则集除了可以证明 Markov 算法是图灵完备的,它还帮我找出了使用 C++ 完成此题中的一个错误,而且这个错误没有被前四个规则集抓到。
|
||||
The tape consists of `0`s and `1`s, the states are `A`, `B`, `C` and `H` (for `H`alt), and the head position is indicated by writing the state letter before the character where the head is. All parts of the initial tape the machine operates on have to be given in the input.
|
||||
|
||||
<pre># 图灵机:三个状态的"忙碌海狸"
|
||||
# 状态 A,符号 0 => 写入 1,向右移动,新状态 B
|
||||
Besides demonstrating that the Markov algorithm is Turing-complete, it also made me catch a bug in the C++ implementation which wasn't caught by the first four rulesets.
|
||||
|
||||
<pre># Turing machine: three-state busy beaver
|
||||
#
|
||||
# state A, symbol 0 => write 1, move right, new state B
|
||||
A0 -> 1B
|
||||
# 状态 A,符号 1 => 写入 1,向左移动,新状态 C
|
||||
# state A, symbol 1 => write 1, move left, new state C
|
||||
0A1 -> C01
|
||||
1A1 -> C11
|
||||
# 状态 B,符号 0 => 写入 1,向左移动,新状态 A
|
||||
# state B, symbol 0 => write 1, move left, new state A
|
||||
0B0 -> A01
|
||||
1B0 -> A11
|
||||
# 状态 B,符号 1 => 写入 1,向右移动,新状态 B
|
||||
B1 - > 1B
|
||||
# 状态 C,符号 0 => 写入 1,向左移动,新状态 B
|
||||
0C0 - > B01
|
||||
1C0 - > B11
|
||||
# 状态 C,符号 1 => 写入 1,向左移动,停止
|
||||
0C1 - > H01
|
||||
1C1 - > H11
|
||||
# state B, symbol 1 => write 1, move right, new state B
|
||||
B1 -> 1B
|
||||
# state C, symbol 0 => write 1, move left, new state B
|
||||
0C0 -> B01
|
||||
1C0 -> B11
|
||||
# state C, symbol 1 => write 1, move left, halt
|
||||
0C1 -> H01
|
||||
1C1 -> H11
|
||||
</pre>
|
||||
|
||||
这个规则集应将这段输入:
|
||||
This ruleset should turn
|
||||
|
||||
`000000A000000`
|
||||
`000000A000000`
|
||||
|
||||
转换成:
|
||||
into
|
||||
|
||||
`00011H1111000`
|
||||
|
||||
# --hints--
|
||||
|
||||
`markov` 应是一个函数。
|
||||
`markov` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof markov === 'function');
|
||||
```
|
||||
|
||||
`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.")` 应返回 "I bought a bag of apples from my brother."。
|
||||
`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.")` should return "I bought a bag of apples from my brother.".
|
||||
|
||||
```js
|
||||
assert.deepEqual(markov(rules[0], tests[0]), outputs[0]);
|
||||
```
|
||||
|
||||
`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.")` 应返回 "I bought a bag of apples from T shop."。
|
||||
`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.")` should return "I bought a bag of apples from T shop.".
|
||||
|
||||
```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.")` 应返回 "I bought a bag of apples with my money from T shop."。
|
||||
`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.")` should return "I bought a bag of apples with my money from T shop.".
|
||||
|
||||
```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"。
|
||||
`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_")` should return "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"。
|
||||
`markov(["A0 -> 1B","0A1 -> C01","1A1 -> C11","0B0 -> A01","1B0 -> A11","B1 -> 1B","0C0 -> B01","1C0 -> B11","0C1 -> H01","1C1 -> H11"],"")` should return "00011H1111000".
|
||||
|
||||
```js
|
||||
assert.deepEqual(markov(rules[4], tests[4]), outputs[4]);
|
||||
@ -210,6 +212,29 @@ assert.deepEqual(markov(rules[4], tests[4]), outputs[4]);
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
|
||||
let rules=[["A -> apple","B -> bag","S -> shop","T -> the","the shop -> my brother","a never used -> .terminating rule"],
|
||||
["A -> apple","B -> bag","S -> .shop","T -> the","the shop -> my brother","a never used -> .terminating rule"],
|
||||
["A -> apple","WWWW -> with","Bgage -> ->.*","B -> bag","->.* -> money","W -> WW","S -> .shop","T -> the","the shop -> my brother","a never used -> .terminating rule"],
|
||||
["_+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","_+_ -> "],
|
||||
["A0 -> 1B","0A1 -> C01","1A1 -> C11","0B0 -> A01","1B0 -> A11","B1 -> 1B","0C0 -> B01","1C0 -> B11","0C1 -> H01","1C1 -> H11"]];
|
||||
let tests=["I bought a B of As from T S.",
|
||||
"I bought a B of As from T S.",
|
||||
"I bought a B of As W my Bgage from T S.",
|
||||
"_1111*11111_",
|
||||
"000000A000000"];
|
||||
let outputs=["I bought a bag of apples from my brother.",
|
||||
"I bought a bag of apples from T shop.",
|
||||
"I bought a bag of apples with my money from T shop.",
|
||||
"11111111111111111111",
|
||||
"00011H1111000"]
|
||||
|
||||
```
|
||||
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
@ -248,21 +273,4 @@ function markov(rules,test) {
|
||||
}
|
||||
return test;
|
||||
}
|
||||
|
||||
// tail:
|
||||
let rules=[["A -> apple","B -> bag","S -> shop","T -> the","the shop -> my brother","a never used -> .terminating rule"],
|
||||
["A -> apple","B -> bag","S -> .shop","T -> the","the shop -> my brother","a never used -> .terminating rule"],
|
||||
["A -> apple","WWWW -> with","Bgage -> ->.*","B -> bag","->.* -> money","W -> WW","S -> .shop","T -> the","the shop -> my brother","a never used -> .terminating rule"],
|
||||
["_+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","_+_ -> "],
|
||||
["A0 -> 1B","0A1 -> C01","1A1 -> C11","0B0 -> A01","1B0 -> A11","B1 -> 1B","0C0 -> B01","1C0 -> B11","0C1 -> H01","1C1 -> H11"]];
|
||||
let tests=["I bought a B of As from T S.",
|
||||
"I bought a B of As from T S.",
|
||||
"I bought a B of As W my Bgage from T S.",
|
||||
"_1111*11111_",
|
||||
"000000A000000"];
|
||||
let outputs=["I bought a bag of apples from my brother.",
|
||||
"I bought a bag of apples from T shop.",
|
||||
"I bought a bag of apples with my money from T shop.",
|
||||
"11111111111111111111",
|
||||
"00011H1111000"];
|
||||
```
|
||||
|
@ -1,42 +1,61 @@
|
||||
---
|
||||
id: 59e0a8df964e4540d5abe599
|
||||
title: 执行大脑****
|
||||
title: Execute Brain****
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302261
|
||||
dashedName: execute-brain
|
||||
---
|
||||
|
||||
# --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>
|
||||
Write a function to implement a Brain\*\*\*\* interpreter. The function will take a string as a parameter and should return a string as the output. More details are given below:
|
||||
|
||||
RCBF is a set of [Brainf\*\*\*](https://rosettacode.org/wiki/Brainf*** "Brainf\*\*\*") compilers and interpreters written for Rosetta Code in a variety of languages.
|
||||
|
||||
Below are links to each of the versions of RCBF.
|
||||
|
||||
An implementation need only properly implement the following instructions:
|
||||
|
||||
| Command | Description |
|
||||
| ------------------------- | ------------------------------------------------------------------------------------------ |
|
||||
| <code>></code> | Move the pointer to the right |
|
||||
| <code><</code> | Move the pointer to the left |
|
||||
| <code>+</code> | Increment the memory cell under the pointer |
|
||||
| <code>-</code> | Decrement the memory cell under the pointer |
|
||||
| <code>.</code> | Output the character signified by the cell at the pointer |
|
||||
| <code>,</code> | Input a character and store it in the cell at the pointer |
|
||||
| <code>\[</code> | Jump past the matching <code>]</code> if the cell under the pointer is 0 |
|
||||
| <code>]</code> | Jump back to the matching <code>\[</code> if the cell under the pointer is nonzero |
|
||||
|
||||
Any cell size is allowed, EOF (*E*nd-*O*-*F*ile) support is optional, as is whether you have bounded or unbounded memory.
|
||||
|
||||
# --hints--
|
||||
|
||||
`brain(bye)`应该重新调整一个字符串
|
||||
`brain(bye)` should return a string
|
||||
|
||||
```js
|
||||
assert(typeof brain(bye) === 'string');
|
||||
```
|
||||
|
||||
<code>brain("++++++[>++++++++++<-]>+++++.")</code should return "A"
|
||||
`brain("++++++[>++++++++++<-]>+++++.")` should return "A"
|
||||
|
||||
```js
|
||||
assert.equal(brain('++++++[>++++++++++<-]>+++++.'), 'A');
|
||||
```
|
||||
|
||||
`brain(bye)`应该回归`Goodbye, World!\\r\\n`
|
||||
`brain(bye)` should return `Goodbye, World!\r\n`
|
||||
|
||||
```js
|
||||
assert.equal(brain(bye), 'Goodbye, World!\r\n');
|
||||
```
|
||||
|
||||
`brain(hello)`应该回归`Hello World!\\n`
|
||||
`brain(hello)` should return `Hello World!\n`
|
||||
|
||||
```js
|
||||
assert.equal(brain(hello), 'Hello World!\n');
|
||||
```
|
||||
|
||||
`brain(fib)`应该返回`1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89`
|
||||
`brain(fib)` should return `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');
|
||||
|
@ -1,24 +1,35 @@
|
||||
---
|
||||
id: 598ee8b91b410510ae82efef
|
||||
title: 可扩展的素发生器
|
||||
title: Extensible prime generator
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302262
|
||||
dashedName: extensible-prime-generator
|
||||
---
|
||||
|
||||
# --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>
|
||||
Write a generator of prime numbers, in order, that will automatically adjust to accommodate the generation of any reasonably high prime.
|
||||
|
||||
The generator should be able to:
|
||||
|
||||
<ul>
|
||||
<li>Show the first <code>n</code> prime numbers</li>
|
||||
<li>Show the prime numbers in a range</li>
|
||||
<li>Show the number of primes in a range</li>
|
||||
<li>Show the <code>n<sup>th</sup></code> prime number</li>
|
||||
</ul>
|
||||
|
||||
The function should have two parameters. The first will receive `n` or the range as an array. The second will receive a boolean, that specifies if the function returns the prime numbers as an array or a single number(the number of primes in the range or the <code>n<sup>th</sup></code> prime). According to the parameters the function should return an array.
|
||||
|
||||
# --hints--
|
||||
|
||||
`primeGenerator`是一个函数。
|
||||
`primeGenerator` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof primeGenerator === 'function');
|
||||
```
|
||||
|
||||
`primeGenerator`是一个函数。
|
||||
`primeGenerator(20, true)` should return `[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(primeGenerator(20, true), [
|
||||
@ -45,7 +56,7 @@ assert.deepEqual(primeGenerator(20, true), [
|
||||
]);
|
||||
```
|
||||
|
||||
`primeGenerator`是一个函数。
|
||||
`primeGenerator([100, 150], true)` should return `[101, 103, 107, 109, 113, 127, 131, 137, 139, 149]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(primeGenerator([100, 150], true), [
|
||||
@ -62,13 +73,13 @@ assert.deepEqual(primeGenerator([100, 150], true), [
|
||||
]);
|
||||
```
|
||||
|
||||
`primeGenerator`是一个函数。
|
||||
`primeGenerator([7700, 8000], false)` should return `30`.
|
||||
|
||||
```js
|
||||
assert.equal(primeGenerator([7700, 8000], false), 30);
|
||||
```
|
||||
|
||||
`primeGenerator`是一个函数。
|
||||
`primeGenerator(10000, false)` should return `104729`.
|
||||
|
||||
```js
|
||||
assert.equal(primeGenerator(10000, false), 104729);
|
||||
|
@ -1,42 +1,56 @@
|
||||
---
|
||||
id: 597b2b2a2702b44414742771
|
||||
title: 阶乘
|
||||
title: Factorial
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302263
|
||||
dashedName: factorial
|
||||
---
|
||||
|
||||
# --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>
|
||||
Write a function to return the factorial of a number.
|
||||
|
||||
Factorial of a number is given by:
|
||||
|
||||
<pre><big>n! = n * (n-1) * (n-2) * ..... * 1</big>
|
||||
</pre>
|
||||
|
||||
For example:
|
||||
|
||||
<ul>
|
||||
<li><code>3! = 3 * 2 * 1 = 6</code></li>
|
||||
<li><code>4! = 4 * 3 * 2 * 1 = 24</code></li>
|
||||
</ul>
|
||||
|
||||
**Note:** `0! = 1`
|
||||
|
||||
# --hints--
|
||||
|
||||
`factorial`是一种功能。
|
||||
`factorial` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof factorial === 'function');
|
||||
```
|
||||
|
||||
`factorial(2)`应该返回一个数字。
|
||||
`factorial(2)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof factorial(2) === 'number');
|
||||
```
|
||||
|
||||
`factorial(3)`应该返回6.“)
|
||||
`factorial(3)` should return 6.
|
||||
|
||||
```js
|
||||
assert.equal(factorial(3), 6);
|
||||
```
|
||||
|
||||
`factorial(3)`应返回120.“)
|
||||
`factorial(5)` should return 120.
|
||||
|
||||
```js
|
||||
assert.equal(factorial(5), 120);
|
||||
```
|
||||
|
||||
`factorial(3)`应返回3,628,800。“)
|
||||
`factorial(10)` should return 3,628,800.
|
||||
|
||||
```js
|
||||
assert.equal(factorial(10), 3628800);
|
||||
|
@ -1,42 +1,88 @@
|
||||
---
|
||||
id: 598eea87e5cf4b116c3ff81a
|
||||
title: 梅森数的因素
|
||||
title: Factors of a Mersenne number
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302264
|
||||
dashedName: factors-of-a-mersenne-number
|
||||
---
|
||||
|
||||
# --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>
|
||||
A Mersenne number is a number in the form of <code>2<sup>P</sup>-1</code>.
|
||||
|
||||
If `P` is prime, the Mersenne number may be a Mersenne prime. (If `P` is not prime, the Mersenne number is also not prime.)
|
||||
|
||||
In the search for Mersenne prime numbers it is advantageous to eliminate exponents by finding a small factor before starting a, potentially lengthy, [Lucas-Lehmer test](https://rosettacode.org/wiki/Lucas-Lehmer test "Lucas-Lehmer test").
|
||||
|
||||
There are very efficient algorithms for determining if a number divides <code>2<sup>P</sup>-1</code> (or equivalently, if <code>2<sup>P</sup> mod (the number) = 1</code>).
|
||||
|
||||
Some languages already have built-in implementations of this exponent-and-mod operation (called modPow or similar).
|
||||
|
||||
The following is how to implement this modPow yourself:
|
||||
|
||||
For example, let's compute <code>2<sup>23</sup> mod 47</code>.
|
||||
|
||||
Convert the exponent 23 to binary, you get 10111. Starting with <code><tt>square</tt> = 1</code>, repeatedly square it.
|
||||
|
||||
Remove the top bit of the exponent, and if it's 1 multiply `square` by the base of the exponentiation (2), then compute <code><tt>square</tt> modulo 47</code>.
|
||||
|
||||
Use the result of the modulo from the last step as the initial value of `square` in the next step:
|
||||
|
||||
<pre>Remove Optional
|
||||
square top bit multiply by 2 mod 47
|
||||
------------ ------- ------------- ------
|
||||
1*1 = 1 1 0111 1*2 = 2 2
|
||||
2*2 = 4 0 111 no 4
|
||||
4*4 = 16 1 11 16*2 = 32 32
|
||||
32*32 = 1024 1 1 1024*2 = 2048 27
|
||||
27*27 = 729 1 729*2 = 1458 1
|
||||
</pre>
|
||||
|
||||
Since <code>2<sup>23</sup> mod 47 = 1</code>, 47 is a factor of <code>2<sup>P</sup>-1</code>.
|
||||
|
||||
(To see this, subtract 1 from both sides: <code>2<sup>23</sup>-1 = 0 mod 47</code>.)
|
||||
|
||||
Since we've shown that 47 is a factor, <code>2<sup>23</sup>-1</code> is not prime.
|
||||
|
||||
Further properties of Mersenne numbers allow us to refine the process even more.
|
||||
|
||||
Any factor `q` of <code>2<sup>P</sup>-1</code> must be of the form `2kP+1`, `k` being a positive integer or zero. Furthermore, `q` must be `1` or `7 mod 8`.
|
||||
|
||||
Finally any potential factor `q` must be [prime](https://rosettacode.org/wiki/Primality by Trial Division "Primality by Trial Division").
|
||||
|
||||
As in other trial division algorithms, the algorithm stops when `2kP+1 > sqrt(N)`.These primarily tests only work on Mersenne numbers where `P` is prime. For example, <code>M<sub>4</sub>=15</code> yields no factors using these techniques, but factors into 3 and 5, neither of which fit `2kP+1`.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Using the above method find a factor of <code>2<sup>929</sup>-1</code> (aka M929)
|
||||
|
||||
# --hints--
|
||||
|
||||
`check_mersenne`是一个函数。
|
||||
`check_mersenne` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof check_mersenne === 'function');
|
||||
```
|
||||
|
||||
`check_mersenne(3)`应该返回一个字符串。
|
||||
`check_mersenne(3)` should return a string.
|
||||
|
||||
```js
|
||||
assert(typeof check_mersenne(3) == 'string');
|
||||
```
|
||||
|
||||
`check_mersenne(3)`应该返回“M3 = 2 ^ 3-1是素数”。
|
||||
`check_mersenne(3)` should return "M3 = 2^3-1 is prime".
|
||||
|
||||
```js
|
||||
assert.equal(check_mersenne(3), 'M3 = 2^3-1 is prime');
|
||||
```
|
||||
|
||||
`check_mersenne(23)`应返回“M23 = 2 ^ 23-1与因子47复合”。
|
||||
`check_mersenne(23)` should return "M23 = 2^23-1 is composite with factor 47".
|
||||
|
||||
```js
|
||||
assert.equal(check_mersenne(23), 'M23 = 2^23-1 is composite with factor 47');
|
||||
```
|
||||
|
||||
`check_mersenne(929)`应返回“M929 = 2 ^ 929-1与因子13007复合
|
||||
`check_mersenne(929)` should return "M929 = 2^929-1 is composite with factor 13007
|
||||
|
||||
```js
|
||||
assert.equal(
|
||||
|
@ -1,36 +1,38 @@
|
||||
---
|
||||
id: 597f1e7fbc206f0e9ba95dc4
|
||||
title: 整数因子
|
||||
title: Factors of an integer
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302265
|
||||
dashedName: factors-of-an-integer
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
<p>编写一个返回正整数因子的函数。 </p><p>这些因子是正整数,通过该正整数可以将被分解的数量除以产生正整数结果。 </p> ///
|
||||
Write a function that returns the factors of a positive integer as an array.
|
||||
|
||||
These factors are the positive integers by which the number being factored can be divided to yield a positive integer result.
|
||||
|
||||
# --hints--
|
||||
|
||||
`factors`是一种功能。
|
||||
`factors` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof factors === 'function');
|
||||
```
|
||||
|
||||
`factors(45)`应该返回`[1,3,5,9,15,45]` 。
|
||||
`factors(45)` should return `[1,3,5,9,15,45]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(factors(45), ans[0]);
|
||||
```
|
||||
|
||||
`factors(53)`应该返回`[1,53]` 。
|
||||
`factors(53)` should return `[1,53]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(factors(53), ans[1]);
|
||||
```
|
||||
|
||||
`factors(64)`应该返回`[1,2,4,8,16,32,64]` 。
|
||||
`factors(64)` should return `[1,2,4,8,16,32,64]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(factors(64), ans[2]);
|
||||
|
@ -1,42 +1,65 @@
|
||||
---
|
||||
id: 59c3ec9f15068017c96eb8a3
|
||||
title: Farey序列
|
||||
title: Farey sequence
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302266
|
||||
dashedName: farey-sequence
|
||||
---
|
||||
|
||||
# --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>
|
||||
The [Farey sequence](https://en.wikipedia.org/wiki/Farey sequence "wp: Farey sequence") <code>F<sub>n</sub></code> of order `n` is the sequence of completely reduced fractions between `0` and `1` which, when in lowest terms, have denominators less than or equal to `n`, arranged in order of increasing size.
|
||||
|
||||
The *Farey sequence* is sometimes incorrectly called a *Farey series*.
|
||||
|
||||
Each Farey sequence:
|
||||
|
||||
<ul>
|
||||
<li>starts with the value 0, denoted by the fraction $ \frac{0}{1} $</li>
|
||||
<li>ends with the value 1, denoted by the fraction $ \frac{1}{1}$.</li>
|
||||
</ul>
|
||||
|
||||
The Farey sequences of orders `1` to `5` are:
|
||||
|
||||
<ul>
|
||||
<li style='list-style: none;'>${\bf\it{F}}_1 = \frac{0}{1}, \frac{1}{1}$</li>
|
||||
<li style='list-style: none;'>${\bf\it{F}}_2 = \frac{0}{1}, \frac{1}{2}, \frac{1}{1}$</li>
|
||||
<li style='list-style: none;'>${\bf\it{F}}_3 = \frac{0}{1}, \frac{1}{3}, \frac{1}{2}, \frac{2}{3}, \frac{1}{1}$</li>
|
||||
<li style='list-style: none;'>${\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}$</li>
|
||||
<li style='list-style: none;'>${\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}$</li>
|
||||
</ul>
|
||||
|
||||
# --instructions--
|
||||
|
||||
Write a function that returns the Farey sequence of order `n`. The function should have one parameter that is `n`. It should return the sequence as an array.
|
||||
|
||||
# --hints--
|
||||
|
||||
`farey`是一种功能。
|
||||
`farey` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof farey === 'function');
|
||||
```
|
||||
|
||||
`farey(3)`应该返回一个数组
|
||||
`farey(3)` should return an array
|
||||
|
||||
```js
|
||||
assert(Array.isArray(farey(3)));
|
||||
```
|
||||
|
||||
`farey(3)`应该返回`["1/3","1/2","2/3"]`
|
||||
`farey(3)` should return `["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"]`
|
||||
`farey(4)` should return `["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"]`
|
||||
`farey(5)` should return `["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,60 +1,89 @@
|
||||
---
|
||||
id: 598eef80ba501f1268170e1e
|
||||
title: 斐波那契n步数序列
|
||||
title: Fibonacci n-step number sequences
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302267
|
||||
dashedName: fibonacci-n-step-number-sequences
|
||||
---
|
||||
|
||||
# --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>
|
||||
These number series are an expansion of the ordinary [Fibonacci sequence](https://rosettacode.org/wiki/Fibonacci sequence "Fibonacci sequence") where:
|
||||
|
||||
<ol>
|
||||
<li>For $n = 2$ we have the Fibonacci sequence; with initial values $[1, 1]$ and $F_k^2 = F_{k-1}^2 + F_{k-2}^2$</li>
|
||||
<li>For $n = 3$ we have the tribonacci sequence; with initial values $[1, 1, 2]$ and $F_k^3 = F_{k-1}^3 + F_{k-2}^3 + F_{k-3}^3$</li>
|
||||
<li>For $n = 4$ we have the tetranacci sequence; with initial values $[1, 1, 2, 4]$ and $F_k^4 = F_{k-1}^4 + F_{k-2}^4 + F_{k-3}^4 + F_{k-4}^4$...</li>
|
||||
<li>For general $n>2$ we have the Fibonacci $n$-step sequence - $F_k^n$; with initial values of the first $n$ values of the $(n-1)$'th Fibonacci $n$-step sequence $F_k^{n-1}$; and $k$'th value of this $n$'th sequence being $F_k^n = \sum_{i=1}^{(n)} {F_{k-i}^{(n)}}$</li>
|
||||
</ol>
|
||||
|
||||
For small values of $n$, [Greek numeric prefixes](https://en.wikipedia.org/wiki/Number prefix#Greek_series "wp: Number prefix#Greek_series") are sometimes used to individually name each series.
|
||||
|
||||
Fibonacci $n$-step sequences:
|
||||
|
||||
| $n$ | Series name | Values |
|
||||
| --- | ----------- | ------------------------------------------------------ |
|
||||
| 2 | fibonacci | 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 ... |
|
||||
| 3 | tribonacci | 1 1 2 4 7 13 24 44 81 149 274 504 927 1705 3136 ... |
|
||||
| 4 | tetranacci | 1 1 2 4 8 15 29 56 108 208 401 773 1490 2872 5536 ... |
|
||||
| 5 | pentanacci | 1 1 2 4 8 16 31 61 120 236 464 912 1793 3525 6930 ... |
|
||||
| 6 | hexanacci | 1 1 2 4 8 16 32 63 125 248 492 976 1936 3840 7617 ... |
|
||||
| 7 | heptanacci | 1 1 2 4 8 16 32 64 127 253 504 1004 2000 3984 7936 ... |
|
||||
| 8 | octonacci | 1 1 2 4 8 16 32 64 128 255 509 1016 2028 4048 8080 ... |
|
||||
| 9 | nonanacci | 1 1 2 4 8 16 32 64 128 256 511 1021 2040 4076 8144 ... |
|
||||
| 10 | decanacci | 1 1 2 4 8 16 32 64 128 256 512 1023 2045 4088 8172 ... |
|
||||
|
||||
Allied sequences can be generated where the initial values are changed: The [Lucas series](https://en.wikipedia.org/wiki/Lucas number "wp: Lucas number") sums the two preceding values like the fibonacci series for $n=2$ but uses $\[2, 1]$ as its initial values.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Write a function to generate Fibonacci $n$-step number sequences and Lucas sequences. The first parameter will be $n$. The second parameter will be the number of elements to be returned. The third parameter will specify whether to output the Fibonacci sequence or the Lucas sequence. If the parameter is `"f"` then return the Fibonacci sequence and if it is `"l"`, then return the Lucas sequence. The sequences must be returned as an array.
|
||||
|
||||
# --hints--
|
||||
|
||||
`fib_luc`是一个功能。
|
||||
`fib_luc` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof fib_luc === 'function');
|
||||
```
|
||||
|
||||
`fib_luc(2,10,"f")`应返回`[1,1,2,3,5,8,13,21,34,55]` 。
|
||||
`fib_luc(2,10,"f")` should return `[1,1,2,3,5,8,13,21,34,55]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(fib_luc(2, 10, 'f'), ans[0]);
|
||||
```
|
||||
|
||||
`fib_luc(3,15,"f")`应返回`[1,1,2,4,7,13,24,44,81,149,274,504,927,1705,3136]` 。
|
||||
`fib_luc(3,15,"f")` should return `[1,1,2,4,7,13,24,44,81,149,274,504,927,1705,3136]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(fib_luc(3, 15, 'f'), ans[1]);
|
||||
```
|
||||
|
||||
`fib_luc(4,15,"f")`应返回`[1,1,2,4,8,15,29,56,108,208,401,773,1490,2872,5536]` 。
|
||||
`fib_luc(4,15,"f")` should return `[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]` 。
|
||||
`fib_luc(2,10,"l")` should return `[ 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 ]` 。
|
||||
`fib_luc(3,15,"l")` should return `[ 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 ]` 。
|
||||
`fib_luc(4,15,"l")` should return `[ 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 ]` 。
|
||||
`fib_luc(5,15,"l")` should return `[ 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]);
|
||||
|
@ -1,45 +1,53 @@
|
||||
---
|
||||
id: 597f24c1dda4e70f53c79c81
|
||||
title: 斐波那契序列
|
||||
title: Fibonacci sequence
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302268
|
||||
dashedName: fibonacci-sequence
|
||||
---
|
||||
|
||||
# --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> ///
|
||||
Write a function to generate the <code>n<sup>th</sup></code> Fibonacci number.
|
||||
|
||||
The <code>n<sup>th</sup></code> Fibonacci number is given by:
|
||||
|
||||
<code>F<sub>n</sub> = F<sub>n-1</sub> + F<sub>n-2</sub></code>
|
||||
|
||||
The first two terms of the series are 0 and 1.
|
||||
|
||||
Hence, the series is: 0, 1, 1, 2, 3, 5, 8, 13...
|
||||
|
||||
# --hints--
|
||||
|
||||
`fibonacci`是一种功能。
|
||||
`fibonacci` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof fibonacci === 'function');
|
||||
```
|
||||
|
||||
`fibonacci(2)`应该返回一个数字。
|
||||
`fibonacci(2)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof fibonacci(2) == 'number');
|
||||
```
|
||||
|
||||
`fibonacci(3)`应该返回1.“)
|
||||
`fibonacci(3)` should return 2.
|
||||
|
||||
```js
|
||||
assert.equal(fibonacci(3), 1);
|
||||
assert.equal(fibonacci(3), 2);
|
||||
```
|
||||
|
||||
`fibonacci(5)`应该返回3.“)
|
||||
`fibonacci(5)` should return 5.
|
||||
|
||||
```js
|
||||
assert.equal(fibonacci(5), 3);
|
||||
assert.equal(fibonacci(5), 5);
|
||||
```
|
||||
|
||||
`fibonacci(10)`应该返回34.“)
|
||||
`fibonacci(10)` should return 55.
|
||||
|
||||
```js
|
||||
assert.equal(fibonacci(10), 34);
|
||||
assert.equal(fibonacci(10), 55);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
@ -1,30 +1,40 @@
|
||||
---
|
||||
id: 5992e222d397f00d21122931
|
||||
title: 斐波那契字
|
||||
title: Fibonacci word
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302269
|
||||
dashedName: fibonacci-word
|
||||
---
|
||||
|
||||
# --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>
|
||||
The Fibonacci Word may be created in a manner analogous to the Fibonacci Sequence [as described here](https://hal.archives-ouvertes.fr/docs/00/36/79/72/PDF/The_Fibonacci_word_fractal.pdf):
|
||||
|
||||
<pre>Define F_Word<sub>1</sub> as <strong>1</strong>
|
||||
Define F_Word<sub>2</sub> as <strong>0</strong>
|
||||
Form F_Word<sub>3</sub> as F_Word<sub>2</sub> concatenated with F_Word<sub>1</sub> i.e.: <strong>01</strong>
|
||||
Form F_Word<sub>n</sub> as F_Word<sub>n-1</sub> concatenated with F_word<sub>n-2</sub>
|
||||
</pre>
|
||||
|
||||
# --instructions--
|
||||
|
||||
Write a function to return the Fibonacci Words up to `n`. `n` will be provided as a parameter to the function. The function should return an array of objects. The objects should be of the form: `{ N: 1, Length: 1, Entropy: 0, Word: '1' }`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`fibWord`是一个功能。
|
||||
`fibWord` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof fibWord === 'function');
|
||||
```
|
||||
|
||||
`fibWord(5)`应该返回一个数组。
|
||||
`fibWord(5)` should return an array.
|
||||
|
||||
```js
|
||||
assert(Array.isArray(fibWord(5)));
|
||||
```
|
||||
|
||||
`fibWord(5)`应该返回`'+JSON.stringify(ans)+'` 。
|
||||
`fibWord(5)` should return `[{ N:1, Length:1, Entropy:0, Word:"1" },{ N:2, Length:1, Entropy:0, Word:"0" },{ N:3, Length:2, Entropy:1, Word:"01" },{ N:4, Length:3, Entropy:0.9182958340544896, Word:"010" },{ N:5, Length:5, Entropy:0.9709505944546688, Word:"01001" }]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(fibWord(5), ans);
|
||||
|
@ -1,54 +1,81 @@
|
||||
---
|
||||
id: 5a7dad05be01840e1778a0d1
|
||||
title: Fractran
|
||||
challengeType: 3
|
||||
videoUrl: ''
|
||||
challengeType: 5
|
||||
forumTopicId: 302270
|
||||
dashedName: fractran
|
||||
---
|
||||
|
||||
# --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>
|
||||
[FRACTRAN](https://en.wikipedia.org/wiki/FRACTRAN "wp: FRACTRAN") is a Turing-complete esoteric programming language invented by the mathematician [John Horton Conway](https://en.wikipedia.org/wiki/John Horton Conway "wp: John Horton Conway").
|
||||
|
||||
A FRACTRAN program is an ordered list of positive fractions $P = (f_1, f_2, \\ldots, f_m)$, together with an initial positive integer input $n$.
|
||||
|
||||
The program is run by updating the integer $n$ as follows:
|
||||
|
||||
<ul>
|
||||
<li>for the first fraction, $f_i$, in the list for which $nf_i$ is an integer, replace $n$ with $nf_i$ ;</li>
|
||||
<li>repeat this rule until no fraction in the list produces an integer when multiplied by $n$, then halt.</li>
|
||||
</ul>
|
||||
|
||||
Conway gave a program for primes in FRACTRAN:
|
||||
|
||||
$\\dfrac{17}{91}$, $\\dfrac{78}{85}$, $\\dfrac{19}{51}$, $\\dfrac{23}{38}$, $\\dfrac{29}{33}$, $\\dfrac{77}{29}$, $\\dfrac{95}{23}$, $\\dfrac{77}{19}$, $\\dfrac{1}{17}$, $\\dfrac{11}{13}$, $\\dfrac{13}{11}$, $\\dfrac{15}{14}$, $\\dfrac{15}{2}$, $\\dfrac{55}{1}$
|
||||
|
||||
Starting with $n=2$, this FRACTRAN program will change $n$ to $15=2\\times (\\frac{15}{2})$, then $825=15\\times (\\frac{55}{1})$, generating the following sequence of integers:
|
||||
|
||||
$2$, $15$, $825$, $725$, $1925$, $2275$, $425$, $390$, $330$, $290$, $770$, $\\ldots$
|
||||
|
||||
After 2, this sequence contains the following powers of 2:
|
||||
|
||||
$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$
|
||||
|
||||
which are the prime powers of 2.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Write a function that takes a fractran program as a string parameter and returns the first 10 numbers of the program as an array. If the result does not have 10 numbers then return the numbers as is.
|
||||
|
||||
# --hints--
|
||||
|
||||
`fractran`应该是一个功能。
|
||||
`fractran` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof fractran == 'function');
|
||||
```
|
||||
|
||||
`fractran(""+tests[0]+"")`应该返回一个数组。
|
||||
`fractran("3/2, 1/3")` should return an array.
|
||||
|
||||
```js
|
||||
assert(Array.isArray(fractran('3/2, 1/3')));
|
||||
```
|
||||
|
||||
`fractran(""+tests[0]+"")`应返回`"+JSON.stringify(results[0])+"` 。
|
||||
`fractran("3/2, 1/3")` should return `[ 2, 3, 1 ]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(fractran('3/2, 1/3'), [2, 3, 1]);
|
||||
```
|
||||
|
||||
`fractran(""+tests[1]+"")`应返回`"+JSON.stringify(results[1])+"` 。
|
||||
`fractran("3/2, 5/3, 1/5")` should return `[ 2, 3, 5, 1 ]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(fractran('3/2, 5/3, 1/5'), [2, 3, 5, 1]);
|
||||
```
|
||||
|
||||
`fractran(""+tests[2]+"")`应返回`"+JSON.stringify(results[2])+"` 。
|
||||
`fractran("3/2, 6/3")` should return `[ 2, 3, 6, 9, 18, 27, 54, 81, 162, 243 ]`.
|
||||
|
||||
```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])+"` 。
|
||||
`fractran("2/7, 7/2")` should return `[ 2, 7, 2, 7, 2, 7, 2, 7, 2, 7 ]`.
|
||||
|
||||
```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])+"` 。
|
||||
`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")` should return `[ 2, 15, 825, 725, 1925, 2275, 425, 390, 330, 290 ]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
|
@ -1,56 +1,58 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc7e76
|
||||
title: 伽玛功能
|
||||
title: Gamma function
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302271
|
||||
dashedName: gamma-function
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
实现一个算法(或更多)来计算[Gamma](<https://en.wikipedia.org/wiki/Gamma function>) ($ \\ Gamma $)函数(仅在实际字段中)。 Gamma功能可以定义为:
|
||||
Implement one algorithm (or more) to compute the [Gamma](https://en.wikipedia.org/wiki/Gamma function) ($\\Gamma$) function (in the real field only).
|
||||
|
||||
$ \\ Gamma(x)= \\ displaystyle \\ int_0 ^ \\ infty t ^ {x-1} e ^ { - t} dt $
|
||||
The Gamma function can be defined as:
|
||||
|
||||
<div style='padding-left: 4em;'><big><big>$\Gamma(x) = \displaystyle\int_0^\infty t^{x-1}e^{-t} dt$</big></big></div>
|
||||
|
||||
# --hints--
|
||||
|
||||
`gamma`应该是一个功能。
|
||||
`gamma` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof gamma == 'function');
|
||||
```
|
||||
|
||||
`gamma("+tests[0]+")`应该返回一个数字。
|
||||
`gamma(.1)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof gamma(0.1) == 'number');
|
||||
```
|
||||
|
||||
`gamma("+tests[0]+")`应该返回`"+results[0]+"` 。
|
||||
`gamma(.1)` should return `9.513507698668736`.
|
||||
|
||||
```js
|
||||
assert.equal(round(gamma(0.1)), round(9.513507698668736));
|
||||
```
|
||||
|
||||
`gamma("+tests[1]+")`应该返回`"+results[1]+"` 。
|
||||
`gamma(.2)` should return `4.590843711998803`.
|
||||
|
||||
```js
|
||||
assert.equal(round(gamma(0.2)), round(4.590843711998803));
|
||||
```
|
||||
|
||||
`gamma("+tests[2]+")`应该返回`"+results[2]+"` 。
|
||||
`gamma(.3)` should return `2.9915689876875904`.
|
||||
|
||||
```js
|
||||
assert.equal(round(gamma(0.3)), round(2.9915689876875904));
|
||||
```
|
||||
|
||||
`gamma("+tests[3]+")`应该返回`"+results[3]+"` 。
|
||||
`gamma(.4)` should return `2.218159543757687`.
|
||||
|
||||
```js
|
||||
assert.equal(round(gamma(0.4)), round(2.218159543757687));
|
||||
```
|
||||
|
||||
`gamma("+tests[4]+")`应返回`"+results[4]+"` 。
|
||||
`gamma(.5)` should return `1.7724538509055159`.
|
||||
|
||||
```js
|
||||
assert.equal(round(gamma(0.5)), round(1.7724538509055159));
|
||||
|
@ -1,24 +1,28 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc7e77
|
||||
title: 高斯消除
|
||||
title: Gaussian elimination
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302272
|
||||
dashedName: gaussian-elimination
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
编写一个函数来解决\\(Ax = b \\)使用高斯消除然后向后替换。 \\(A \\)是\\(n \\次n \\)矩阵。此外,\\(x \\)和\\(b \\)是\\(n \\)乘以1个向量。要提高准确性,请使用部分旋转和缩放。
|
||||
Write a function to solve \\(Ax = b\\) using Gaussian elimination then backwards substitution.
|
||||
|
||||
\\(A\\) being an \\(n \\times n\\) matrix. Also, \\(x\\) and \\(b\\) are \\(n\\) by 1 vectors.
|
||||
|
||||
To improve accuracy, please use partial pivoting and scaling.
|
||||
|
||||
# --hints--
|
||||
|
||||
`gaussianElimination`应该是一个函数。
|
||||
`gaussianElimination` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof gaussianElimination == 'function');
|
||||
```
|
||||
|
||||
`gaussianElimination("+JSON.stringify(tests[0][0])+","+JSON.stringify(tests[0][1])+")`应该返回一个数组。
|
||||
`gaussianElimination([[1,1],[1,-1]], [5,1])` should return an array.
|
||||
|
||||
```js
|
||||
assert(
|
||||
@ -34,7 +38,7 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
`gaussianElimination("+JSON.stringify(tests[0][0])+","+JSON.stringify(tests[0][1])+")`应返回`"+JSON.stringify(results[0])+"` 。
|
||||
`gaussianElimination([[1,1],[1,-1]], [5,1])` should return `[ 3, 2 ]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
@ -49,7 +53,7 @@ assert.deepEqual(
|
||||
);
|
||||
```
|
||||
|
||||
`gaussianElimination("+JSON.stringify(tests[1][0])+","+JSON.stringify(tests[1][1])+")`应返回`"+JSON.stringify(results[1])+"` 。
|
||||
`gaussianElimination([[2,3],[2,1]] , [8,4])` should return `[ 1, 2 ]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
@ -64,7 +68,7 @@ assert.deepEqual(
|
||||
);
|
||||
```
|
||||
|
||||
`gaussianElimination("+JSON.stringify(tests[2][0])+","+JSON.stringify(tests[2][1])+")`应返回`"+JSON.stringify(results[2])+"` 。
|
||||
`gaussianElimination([[1,3],[5,-2]], [14,19])` should return `[ 5, 3 ]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
@ -79,7 +83,7 @@ assert.deepEqual(
|
||||
);
|
||||
```
|
||||
|
||||
`gaussianElimination("+JSON.stringify(tests[3][0])+","+JSON.stringify(tests[3][1])+")`应返回`"+JSON.stringify(results[3])+"` 。
|
||||
`gaussianElimination([[1,1],[5,-1]] , [10,14])` should return `[ 4, 6 ]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
@ -94,7 +98,7 @@ assert.deepEqual(
|
||||
);
|
||||
```
|
||||
|
||||
`gaussianElimination("+JSON.stringify(tests[4][0])+","+JSON.stringify(tests[4][1])+")`应返回`"+JSON.stringify(results[4])+"` 。
|
||||
`gaussianElimination([[1,2,3],[4,5,6],[7,8,8]] , [6,15,23])` should return `[ 1, 1, 1 ]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
|
@ -1,24 +1,32 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc7e78
|
||||
title: 一般的FizzBuzz
|
||||
title: General FizzBuzz
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302273
|
||||
dashedName: general-fizzbuzz
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
编写[FizzBuzz](http://rosettacode.org/wiki/FizzBuzz)的通用版本,适用于任何因子列表及其单词。这基本上是一种“fizzbuzz”实现,其中游戏规则被提供给用户。创建一个实现此功能的函数。该函数应该有两个参数。第一个是带有FizzBuzz规则的数组。例如: `[ [3,"Fizz"] , [5,"Buzz"] ]` 。此indcates该`Fizz` ,如果数量是3的倍数,并应被打印`Buzz`如果是5的倍数。如果它是两则字符串应该在阵列中指定的顺序被连结的倍数。在这种情况下,如果数字是3和5的倍数,则为`FizzBuzz` 。第二个参数是函数应返回如上所述的字符串的数字。
|
||||
Write a generalized version of [FizzBuzz](https://rosettacode.org/wiki/FizzBuzz) that works for any list of factors, along with their words.
|
||||
|
||||
This is basically a "fizzbuzz" implementation where the rules of the game are supplied to the user. Create a function to implement this. The function should take two parameters.
|
||||
|
||||
The first will be an array with the FizzBuzz rules. For example: `[ [3, "Fizz"] , [5, "Buzz"] ]`.
|
||||
|
||||
This indicates that `Fizz` should be printed if the number is a multiple of 3 and `Buzz` if it is a multiple of 5. If it is a multiple of both then the strings should be concatenated in the order specified in the array. In this case, `FizzBuzz` if the number is a multiple of 3 and 5.
|
||||
|
||||
The second parameter is the number for which the function should return a string as stated above.
|
||||
|
||||
# --hints--
|
||||
|
||||
`genFizzBuzz`应该是一个功能。
|
||||
`genFizzBuzz` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof genFizzBuzz == 'function');
|
||||
```
|
||||
|
||||
`genFizzBuzz("+JSON.stringify(tests[0][0])+","+tests[0][1]+")`应该返回一个类型。
|
||||
`genFizzBuzz([[3, "Fizz"],[5, "Buzz"]], 6)` should return a string.
|
||||
|
||||
```js
|
||||
assert(
|
||||
@ -32,7 +40,7 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
`genFizzBuzz("+JSON.stringify(tests[0][0])+","+tests[0][1]+")`应返回`""+results[0]+""` 。
|
||||
`genFizzBuzz([[3, "Fizz"],[5, "Buzz"]], 6)` should return `"Fizz"`.
|
||||
|
||||
```js
|
||||
assert.equal(
|
||||
@ -47,7 +55,7 @@ assert.equal(
|
||||
);
|
||||
```
|
||||
|
||||
`genFizzBuzz("+JSON.stringify(tests[1][0])+","+tests[1][1]+")`应返回`""+results[1]+""` 。
|
||||
`genFizzBuzz([[3, "Fizz"],[5, "Buzz"]], 10)` should return `"Buzz"`.
|
||||
|
||||
```js
|
||||
assert.equal(
|
||||
@ -62,7 +70,7 @@ assert.equal(
|
||||
);
|
||||
```
|
||||
|
||||
`genFizzBuzz("+JSON.stringify(tests[2][0])+","+tests[2][1]+")`应返回`""+results[2]+""` 。
|
||||
`genFizzBuzz([[3, "Buzz"],[5, "Fizz"]], 12)` should return `"Buzz"`.
|
||||
|
||||
```js
|
||||
assert.equal(
|
||||
@ -77,7 +85,7 @@ assert.equal(
|
||||
);
|
||||
```
|
||||
|
||||
`genFizzBuzz("+JSON.stringify(tests[3][0])+","+tests[3][1]+")`应返回`""+results[3]+""` 。
|
||||
`genFizzBuzz([[3, "Buzz"],[5, "Fizz"]], 13)` should return `"13"`.
|
||||
|
||||
```js
|
||||
assert.equal(
|
||||
@ -92,7 +100,7 @@ assert.equal(
|
||||
);
|
||||
```
|
||||
|
||||
`genFizzBuzz("+JSON.stringify(tests[4][0])+","+tests[4][1]+")`应该返回`""+results[4]+""` 。
|
||||
`genFizzBuzz([[3, "Buzz"],[5, "Fizz"]], 15)` should return `"BuzzFizz"`.
|
||||
|
||||
```js
|
||||
assert.equal(
|
||||
@ -107,7 +115,7 @@ assert.equal(
|
||||
);
|
||||
```
|
||||
|
||||
`genFizzBuzz("+JSON.stringify(tests[5][0])+","+tests[5][1]+")`应返回`""+results[5]+""` 。
|
||||
`genFizzBuzz([[3, "Fizz"],[5, "Buzz"]], 15)` should return `"FizzBuzz"`.
|
||||
|
||||
```js
|
||||
assert.equal(
|
||||
@ -122,7 +130,7 @@ assert.equal(
|
||||
);
|
||||
```
|
||||
|
||||
`genFizzBuzz("+JSON.stringify(tests[6][0])+","+tests[6][1]+")`应该返回`""+results[6]+""` 。
|
||||
`genFizzBuzz([[3, "Fizz"],[5, "Buzz"],[7, "Baxx"]], 105)` should return `"FizzBuzzBaxx"`.
|
||||
|
||||
```js
|
||||
assert.equal(
|
||||
|
@ -1,54 +1,54 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc7e7a
|
||||
title: 生成小写ASCII字母表
|
||||
title: Generate lower case ASCII alphabet
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302274
|
||||
dashedName: generate-lower-case-ascii-alphabet
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
编写一个函数以生成给定范围的小写ASCII字符数组。例如:对于范围1到4,函数应返回`['a','b','c','d']` 。
|
||||
Write a function to generate an array of lower case ASCII characters for a given range. For example, given the range `['a', 'd']`, the function should return `['a', 'b', 'c', 'd']`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`lascii`应该是一个功能。
|
||||
`lascii` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof lascii == 'function');
|
||||
```
|
||||
|
||||
`lascii("a","d")`应该返回一个数组。
|
||||
`lascii("a","d")` should return an array.
|
||||
|
||||
```js
|
||||
assert(Array.isArray(lascii('a', 'd')));
|
||||
```
|
||||
|
||||
`lascii("a","d")`应该返回`[ "a", "b", "c", "d" ]` 。
|
||||
`lascii('a','d')` should return `[ 'a', 'b', 'c', 'd' ]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(lascii('a', 'd'), results[0]);
|
||||
```
|
||||
|
||||
`lascii("c","i")`应该返回`[ "c", "d", "e", "f", "g", "h", "i" ]` 。
|
||||
`lascii('c','i')` should return `[ 'c', 'd', 'e', 'f', 'g', 'h', 'i' ]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(lascii('c', 'i'), results[1]);
|
||||
```
|
||||
|
||||
`lascii("m","q")`应该返回`[ "m", "n", "o", "p", "q" ]` 。
|
||||
`lascii('m','q')` should return `[ 'm', 'n', 'o', 'p', 'q' ]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(lascii('m', 'q'), results[2]);
|
||||
```
|
||||
|
||||
`lascii("k","n")`应返回`[ "k", "l", "m", "n" ]` 。
|
||||
`lascii('k','n')` should return `[ 'k', 'l', 'm', 'n' ]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(lascii('k', 'n'), results[3]);
|
||||
```
|
||||
|
||||
`lascii("t","z")`应该返回`[ "t", "u", "v", "w", "x", "y", "z" ]` 。
|
||||
`lascii('t','z')` should return `[ 't', 'u', 'v', 'w', 'x', 'y', 'z' ]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(lascii('t', 'z'), results[4]);
|
||||
|
@ -1,64 +1,94 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc7e80
|
||||
title: 格雷码
|
||||
title: Gray code
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302276
|
||||
dashedName: gray-code
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
[格雷码](<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>
|
||||
[Gray code](https://en.wikipedia.org/wiki/Gray code) is a form of binary encoding where transitions between consecutive numbers differ by only one bit.
|
||||
|
||||
This is a useful encoding for reducing hardware data hazards with values that change rapidly and/or connect to slower hardware as inputs.
|
||||
|
||||
It is also useful for generating inputs for [Karnaugh maps](https://en.wikipedia.org/wiki/Karnaugh map) in order from left to right or top to bottom.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Create a function to encode a number to and decode a number from Gray code. The function should will have 2 parameters.
|
||||
|
||||
The first would be a boolean. The function should encode for true and decode for false. The second parameter would be the number to be encoded/decoded.
|
||||
|
||||
Display the normal binary representations, Gray code representations, and decoded Gray code values for all 5-bit binary numbers (0-31 inclusive, leading 0's not necessary).
|
||||
|
||||
There are many possible Gray codes. The following encodes what is called "binary reflected Gray code."
|
||||
|
||||
Encoding (MSB is bit 0, b is binary, g is Gray code):
|
||||
|
||||
<pre>if b[i-1] = 1
|
||||
g[i] = not b[i]
|
||||
else
|
||||
g[i] = b[i]
|
||||
</pre>
|
||||
|
||||
Or:
|
||||
|
||||
<pre>g = b xor (b logically right shifted 1 time)
|
||||
</pre>
|
||||
|
||||
Decoding (MSB is bit 0, b is binary, g is Gray code):
|
||||
|
||||
<pre>b[0] = g[0]<br>
|
||||
for other bits:
|
||||
b[i] = g[i] xor b[i-1]
|
||||
</pre>
|
||||
|
||||
# --hints--
|
||||
|
||||
`gray`应该是一个功能。
|
||||
`gray` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof gray == 'function');
|
||||
```
|
||||
|
||||
`gray(true,177)`应该返回一个数字。
|
||||
`gray(true,177)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof gray(true, 177) == 'number');
|
||||
```
|
||||
|
||||
`gray(true,177)`应该返回`233` 。
|
||||
`gray(true,177)` should return `233`.
|
||||
|
||||
```js
|
||||
assert.equal(gray(true, 177), 233);
|
||||
```
|
||||
|
||||
`gray(true,425)`应该返回`381` 。
|
||||
`gray(true,425)` should return `381`.
|
||||
|
||||
```js
|
||||
assert.equal(gray(true, 425), 381);
|
||||
```
|
||||
|
||||
`gray(true,870)`应该返回`725` 。
|
||||
`gray(true,870)` should return `725`.
|
||||
|
||||
```js
|
||||
assert.equal(gray(true, 870), 725);
|
||||
```
|
||||
|
||||
`gray(false,233)`应该返回`177` 。
|
||||
`gray(false,233)` should return `177`.
|
||||
|
||||
```js
|
||||
assert.equal(gray(false, 233), 177);
|
||||
```
|
||||
|
||||
`gray(false,381)`应该返回`425` 。
|
||||
`gray(false,381)` should return `425`.
|
||||
|
||||
```js
|
||||
assert.equal(gray(false, 381), 425);
|
||||
```
|
||||
|
||||
`gray(false,725)`应该返回`870` 。
|
||||
`gray(false,725)` should return `870`.
|
||||
|
||||
```js
|
||||
assert.equal(gray(false, 725), 870);
|
||||
|
@ -1,60 +1,60 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc7e82
|
||||
title: 最大公约数
|
||||
title: Greatest common divisor
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302277
|
||||
dashedName: greatest-common-divisor
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
编写一个函数,返回两个整数的最大公约数。
|
||||
Write a function that returns the greatest common divisor of two integers.
|
||||
|
||||
# --hints--
|
||||
|
||||
`gcd`应该是一个功能。
|
||||
`gcd` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof gcd == 'function');
|
||||
```
|
||||
|
||||
`gcd(24,36)`应该返回一个数字。
|
||||
`gcd(24,36)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof gcd(24, 36) == 'number');
|
||||
```
|
||||
|
||||
`gcd(24,36)`应该返回`12` 。
|
||||
`gcd(24,36)` should return `12`.
|
||||
|
||||
```js
|
||||
assert.equal(gcd(24, 36), 12);
|
||||
```
|
||||
|
||||
`gcd(30,48)`应该返回`6` 。
|
||||
`gcd(30,48)` should return `6`.
|
||||
|
||||
```js
|
||||
assert.equal(gcd(30, 48), 6);
|
||||
```
|
||||
|
||||
`gcd(10,15)`应该返回`5` 。
|
||||
`gcd(10,15)` should return `5`.
|
||||
|
||||
```js
|
||||
assert.equal(gcd(10, 15), 5);
|
||||
```
|
||||
|
||||
`gcd(100,25)`应该返回`25` 。
|
||||
`gcd(100,25)` should return `25`.
|
||||
|
||||
```js
|
||||
assert.equal(gcd(100, 25), 25);
|
||||
```
|
||||
|
||||
`gcd(13,250)`应该返回`1` 。
|
||||
`gcd(13,250)` should return `1`.
|
||||
|
||||
```js
|
||||
assert.equal(gcd(13, 250), 1);
|
||||
```
|
||||
|
||||
`gcd(1300,250)`应该返回`50` 。
|
||||
`gcd(1300,250)` should return `50`.
|
||||
|
||||
```js
|
||||
assert.equal(gcd(1300, 250), 50);
|
||||
|
@ -1,36 +1,38 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc7e84
|
||||
title: 最重要的后续总和
|
||||
title: Greatest subsequential sum
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302278
|
||||
dashedName: greatest-subsequential-sum
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
给定一个整数序列,找到一个连续的子序列,它最大化其元素的总和,也就是说,没有其他单个子序列的元素加起来大于这一个的值。空子序列被认为具有\\(0 \\)的总和;因此,如果所有元素都是负数,则结果必须是空序列。
|
||||
Given a sequence of integers, find a continuous subsequence which maximizes the sum of its elements, that is, the elements of no other single subsequence add up to a value larger than this one.
|
||||
|
||||
An empty subsequence is considered to have the sum of \\( 0 \\); thus if all elements are negative, the result must be the empty sequence.
|
||||
|
||||
# --hints--
|
||||
|
||||
`maximumSubsequence`应该是一个函数。
|
||||
`maximumSubsequence` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof maximumSubsequence == 'function');
|
||||
```
|
||||
|
||||
`maximumSubsequence("+JSON.stringify(tests[0])+")`应该返回一个数组。
|
||||
`maximumSubsequence([ 1, 2, -1, 3, 10, -10 ])` should return an array.
|
||||
|
||||
```js
|
||||
assert(Array.isArray(maximumSubsequence([1, 2, -1, 3, 10, -10])));
|
||||
```
|
||||
|
||||
`maximumSubsequence("+JSON.stringify(tests[0])+")`应返回`"+JSON.stringify(results[0])+"` 。
|
||||
`maximumSubsequence([ 1, 2, -1, 3, 10, -10 ])` should return `[ 1, 2, -1, 3, 10 ]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(maximumSubsequence([1, 2, -1, 3, 10, -10]), [1, 2, -1, 3, 10]);
|
||||
```
|
||||
|
||||
`maximumSubsequence("+JSON.stringify(tests[1])+")`应返回`"+JSON.stringify(results[1])+"` 。
|
||||
`maximumSubsequence([ 0, 8, 10, -2, -4, -1, -5, -3 ])` should return `[ 0, 8, 10 ]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(maximumSubsequence([0, 8, 10, -2, -4, -1, -5, -3]), [
|
||||
@ -40,25 +42,25 @@ assert.deepEqual(maximumSubsequence([0, 8, 10, -2, -4, -1, -5, -3]), [
|
||||
]);
|
||||
```
|
||||
|
||||
`maximumSubsequence("+JSON.stringify(tests[2])+")`应返回`"+JSON.stringify(results[2])+"` 。
|
||||
`maximumSubsequence([ 9, 9, -10, 1 ])` should return `[ 9, 9 ]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(maximumSubsequence([9, 9, -10, 1]), [9, 9]);
|
||||
```
|
||||
|
||||
`maximumSubsequence("+JSON.stringify(tests[3])+")`应返回`"+JSON.stringify(results[3])+"` 。
|
||||
`maximumSubsequence([ 7, 1, -5, -3, -8, 1 ])` should return `[ 7, 1 ]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(maximumSubsequence([7, 1, -5, -3, -8, 1]), [7, 1]);
|
||||
```
|
||||
|
||||
`maximumSubsequence("+JSON.stringify(tests[4])+")`应返回`"+JSON.stringify(results[4])+"` 。
|
||||
`maximumSubsequence([ -3, 6, -1, 4, -4, -6 ])` should return `[ 6, -1, 4 ]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(maximumSubsequence([-3, 6, -1, 4, -4, -6]), [6, -1, 4]);
|
||||
```
|
||||
|
||||
`maximumSubsequence("+JSON.stringify(tests[5])+")`应返回`"+JSON.stringify(results[5])+"` 。
|
||||
`maximumSubsequence([ -1, -2, 3, 5, 6, -2, -1, 4, -4, 2, -1 ])` should return `[ 3, 5, 6, -2, -1, 4 ]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(maximumSubsequence([-1, -2, 3, 5, 6, -2, -1, 4, -4, 2, -1]), [
|
||||
|
@ -1,24 +1,48 @@
|
||||
---
|
||||
id: 595608ff8bcd7a50bd490181
|
||||
title: 冰雹序列
|
||||
title: Hailstone sequence
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302279
|
||||
dashedName: hailstone-sequence
|
||||
---
|
||||
|
||||
# --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> (幽默)。
|
||||
The Hailstone sequence of numbers can be generated from a starting positive integer, `n` by:
|
||||
|
||||
<ul>
|
||||
<li>If <code>n</code> is <code>1</code> then the sequence ends</li>
|
||||
<li>If <code>n</code> is <code>even</code> then the next <code>n</code> of the sequence <code>= n/2</code></li>
|
||||
<li>If <code>n</code> is <code>odd</code> then the next <code>n</code> of the sequence <code>= (3 * n) + 1</code></li>
|
||||
</ul>
|
||||
|
||||
The (unproven) [Collatz conjecture](https://en.wikipedia.org/wiki/Collatz conjecture "wp: Collatz conjecture") is that the hailstone sequence for any starting number always terminates.
|
||||
|
||||
The hailstone sequence is also known as hailstone numbers (because the values are usually subject to multiple descents and ascents like hailstones in a cloud), or as the Collatz sequence.
|
||||
|
||||
# --instructions--
|
||||
|
||||
<ol>
|
||||
<li>Create a routine to generate the hailstone sequence for a number</li>
|
||||
<li>Use the routine to show that the hailstone sequence for the number 27 has 112 elements starting with <code>27, 82, 41, 124</code> and ending with <code>8, 4, 2, 1</code></li>
|
||||
<li>Show the number less than 100,000 which has the longest hailstone sequence together with that sequence's length. (But don't show the actual sequence!)</li>
|
||||
</ol>
|
||||
|
||||
**See also:**
|
||||
|
||||
<ul>
|
||||
<li><a href='https://xkcd.com/710' target='_blank'>xkcd</a> (humourous).</li>
|
||||
</ul>
|
||||
|
||||
# --hints--
|
||||
|
||||
`hailstoneSequence`是一个函数。
|
||||
`hailstoneSequence` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof hailstoneSequence === 'function');
|
||||
```
|
||||
|
||||
`[[27,82,41,124,8,4,2,1], [351, 77031]]` `hailstoneSequence()`应返回`[[27,82,41,124,8,4,2,1], [351, 77031]]`
|
||||
`hailstoneSequence()` should return `[[27,82,41,124,8,4,2,1], [351, 77031]]`
|
||||
|
||||
```js
|
||||
assert.deepEqual(hailstoneSequence(), res);
|
||||
|
@ -1,90 +1,96 @@
|
||||
---
|
||||
id: 594810f028c0303b75339ad1
|
||||
title: 快乐的数字
|
||||
title: Happy numbers
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302280
|
||||
dashedName: happy-numbers
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
<p>幸福的数字由以下过程定义: </p><p>从任何正整数开始,将数字替换为其数字的平方和,并重复该过程直到数字等于1(它将保持不变),或者它在一个不包括1的循环中无休止地循环。这些数字这个过程在1结束的是幸福的数字,而那些不以1结尾的数字是不愉快的数字。 </p><p>实现一个函数,如果数字是满意的,则返回true,否则返回false。 </p>
|
||||
A [happy number](https://en.wikipedia.org/wiki/Happy_number) is defined by the following process:
|
||||
|
||||
Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals `1` (where it will stay), or it loops endlessly in a cycle which does not include `1`. Those numbers for which this process ends in `1` are happy numbers, while those that do not end in `1` are unhappy numbers.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Implement a function that returns true if the number is happy, or false if not.
|
||||
|
||||
# --hints--
|
||||
|
||||
`happy`是一种功能。
|
||||
`happy` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof happy === 'function');
|
||||
```
|
||||
|
||||
`happy(1)`应该返回一个布尔值。
|
||||
`happy(1)` should return a boolean.
|
||||
|
||||
```js
|
||||
assert(typeof happy(1) === 'boolean');
|
||||
```
|
||||
|
||||
`happy(1)`应该回归真实。
|
||||
`happy(1)` should return true.
|
||||
|
||||
```js
|
||||
assert(happy(1));
|
||||
```
|
||||
|
||||
`happy(2)`应该返回虚假。
|
||||
`happy(2)` should return false.
|
||||
|
||||
```js
|
||||
assert(!happy(2));
|
||||
```
|
||||
|
||||
`happy(7)`应该回归真实。
|
||||
`happy(7)` should return true.
|
||||
|
||||
```js
|
||||
assert(happy(7));
|
||||
```
|
||||
|
||||
`happy(10)`应该回归真实。
|
||||
`happy(10)` should return true.
|
||||
|
||||
```js
|
||||
assert(happy(10));
|
||||
```
|
||||
|
||||
`happy(13)`应该回归真实。
|
||||
`happy(13)` should return true.
|
||||
|
||||
```js
|
||||
assert(happy(13));
|
||||
```
|
||||
|
||||
`happy(19)`应该回归真实。
|
||||
`happy(19)` should return true.
|
||||
|
||||
```js
|
||||
assert(happy(19));
|
||||
```
|
||||
|
||||
`happy(23)`应该回归真实。
|
||||
`happy(23)` should return true.
|
||||
|
||||
```js
|
||||
assert(happy(23));
|
||||
```
|
||||
|
||||
`happy(28)`应该回归真实。
|
||||
`happy(28)` should return true.
|
||||
|
||||
```js
|
||||
assert(happy(28));
|
||||
```
|
||||
|
||||
`happy(31)`应该回归真实。
|
||||
`happy(31)` should return true.
|
||||
|
||||
```js
|
||||
assert(happy(31));
|
||||
```
|
||||
|
||||
`happy(32)`应该回归真实:
|
||||
`happy(32)` should return true:.
|
||||
|
||||
```js
|
||||
assert(happy(32));
|
||||
```
|
||||
|
||||
`happy(33)`应该返回虚假。
|
||||
`happy(33)` should return false.
|
||||
|
||||
```js
|
||||
assert(!happy(33));
|
||||
|
@ -1,24 +1,34 @@
|
||||
---
|
||||
id: 595668ca4cfe1af2fb9818d4
|
||||
title: Harshad或Niven系列
|
||||
title: Harshad or Niven series
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302281
|
||||
dashedName: harshad-or-niven-series
|
||||
---
|
||||
|
||||
# --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>
|
||||
The Harshad or Niven numbers are positive integers ≥ 1 that are divisible by the sum of their digits.
|
||||
|
||||
For example, `42` is a [Harshad number](https://rosettacode.org/wiki/Harshad_or_Niven_series "Harshad or Niven series") as `42` is divisible by `(4 + 2)` without remainder.
|
||||
|
||||
Assume that the series is defined as the numbers in increasing order.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Implement a function to generate successive members of the Harshad sequence.
|
||||
|
||||
Use it to list the first twenty members of the sequence and list the first Harshad number greater than 1000.
|
||||
|
||||
# --hints--
|
||||
|
||||
`isHarshadOrNiven`是一个函数。
|
||||
`isHarshadOrNiven` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof isHarshadOrNiven === 'function');
|
||||
```
|
||||
|
||||
`isHarshadOrNiven()`应该返回`{"firstTwenty": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 18, 20, 21, 24, 27, 30, 36, 40, 42],"firstOver1000": 1002}`
|
||||
`isHarshadOrNiven()` should return `{"firstTwenty": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 18, 20, 21, 24, 27, 30, 36, 40, 42],"firstOver1000": 1002}`
|
||||
|
||||
```js
|
||||
assert.deepEqual(isHarshadOrNiven(), res);
|
||||
|
@ -1,58 +1,60 @@
|
||||
---
|
||||
id: 595671d4d2cdc305f0d5b36f
|
||||
title: 来自两个数组的哈希
|
||||
title: Hash from two arrays
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302283
|
||||
dashedName: hash-from-two-arrays
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
任务:
|
||||
Using two Arrays of equal length, create a Hash object where the elements from one array (the keys) are linked to the elements of the other (the values).
|
||||
|
||||
使用两个相等长度的数组,创建一个Hash对象,其中一个数组中的元素(键)链接到另一个数组(值)
|
||||
**Related task:**
|
||||
|
||||
相关任务: [关联数组/创建](<http://rosettacode.org/wiki/Associative arrays/Creation> "关联数组/创建")
|
||||
<ul>
|
||||
<li><a href='https://rosettacode.org/wiki/Associative arrays/Creation' title='Associative arrays/Creation' target='_blank'>Associative arrays/Creation</a></li>
|
||||
</ul>
|
||||
|
||||
# --hints--
|
||||
|
||||
`arrToObj`是一个功能。
|
||||
`arrToObj` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof arrToObj === 'function');
|
||||
```
|
||||
|
||||
`arrToObj([1, 2, 3, 4, 5], ["a", "b", "c", "d", "e"])`应返回`{ 1: "a", 2: "b", 3: "c", 4: "d", 5: "e" }`
|
||||
`arrToObj([1, 2, 3, 4, 5], ["a", "b", "c", "d", "e"])` should return `{ 1: "a", 2: "b", 3: "c", 4: "d", 5: "e" }`
|
||||
|
||||
```js
|
||||
assert.deepEqual(arrToObj(...testCases[0]), res[0]);
|
||||
```
|
||||
|
||||
`arrToObj([1, 2, 3, 4, 5], ["a", "b", "c", "d"])`应返回`{ 1: "a", 2: "b", 3: "c", 4: "d", 5: undefined }`
|
||||
`arrToObj([1, 2, 3, 4, 5], ["a", "b", "c", "d"])` should return `{ 1: "a", 2: "b", 3: "c", 4: "d", 5: undefined }`
|
||||
|
||||
```js
|
||||
assert.deepEqual(arrToObj(...testCases[1]), res[1]);
|
||||
```
|
||||
|
||||
`arrToObj([1, 2, 3], ["a", "b", "c", "d", "e"])`应返回`{ 1: "a", 2: "b", 3: "c" }`
|
||||
`arrToObj([1, 2, 3], ["a", "b", "c", "d", "e"])` should return `{ 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 }`
|
||||
`arrToObj(["a", "b", "c", "d", "e"], [1, 2, 3, 4, 5])` should return `{ "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 }`
|
||||
`arrToObj(["a", "b", "c", "d", "e"], [1, 2, 3, 4])` should return `{ "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 }`
|
||||
`arrToObj(["a", "b", "c"], [1, 2, 3, 4, 5])` should return `{ "a": 1, "b": 2, "c": 3 }`
|
||||
|
||||
```js
|
||||
assert.deepEqual(arrToObj(...testCases[5]), res[5]);
|
||||
|
@ -1,36 +1,155 @@
|
||||
---
|
||||
id: 5956795bc9e2c415eb244de1
|
||||
title: 哈希加入
|
||||
title: Hash join
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302284
|
||||
dashedName: hash-join
|
||||
---
|
||||
|
||||
# --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
|
||||
让M <sub>B</sub> =一个多图,用于从单个值映射到表B的多行(从空白开始)
|
||||
让C =输出表(从空开始)
|
||||
对于表B中的每一行b:
|
||||
将b放在密钥b(j <sub>B</sub> )下的多映射M <sub>B中</sub>
|
||||
对于表A中的每一行a:
|
||||
对于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>
|
||||
An [inner join](https://en.wikipedia.org/wiki/Join_(SQL)#Inner_join "wp: Join\_(SQL)#Inner_join") is an operation that combines two data tables into one table, based on matching column values. The simplest way of implementing this operation is the [nested loop join](https://en.wikipedia.org/wiki/Nested loop join "wp: Nested loop join") algorithm, but a more scalable alternative is the [hash join](https://en.wikipedia.org/wiki/hash join "wp: hash join") algorithm.
|
||||
|
||||
The "hash join" algorithm consists of two steps:
|
||||
|
||||
<ol>
|
||||
<li><strong>Hash phase:</strong> Create a <a href='https://en.wikipedia.org/wiki/Multimap' title='wp: Multimap' target='_blank'>multimap</a> from one of the two tables, mapping from each join column value to all the rows that contain it.</li>
|
||||
<ul>
|
||||
<li>The multimap must support hash-based lookup which scales better than a simple linear search, because that's the whole point of this algorithm.</li>
|
||||
<li>Ideally we should create the multimap for the smaller table, thus minimizing its creation time and memory size.</li>
|
||||
</ul>
|
||||
<li><strong>Join phase:</strong> Scan the other table, and find matching rows by looking in the multimap created before.</li>
|
||||
</ol>
|
||||
|
||||
In pseudo-code, the algorithm could be expressed as follows:
|
||||
|
||||
<pre><strong>let</strong> <i>A</i> = the first input table (or ideally, the larger one)
|
||||
<strong>let</strong> <i>B</i> = the second input table (or ideally, the smaller one)
|
||||
<strong>let</strong> <i>j<sub>A</sub></i> = the join column ID of table <i>A</i>
|
||||
<strong>let</strong> <i>j<sub>B</sub></i> = the join column ID of table <i>B</i>
|
||||
<strong>let</strong> <i>M<sub>B</sub></i> = a multimap for mapping from single values to multiple rows of table <i>B</i> (starts out empty)
|
||||
<strong>let</strong> <i>C</i> = the output table (starts out empty)
|
||||
<strong>for each</strong> row <i>b</i> in table <i>B</i>:
|
||||
<strong>place</strong> <i>b</i> in multimap <i>M<sub>B</sub></i> under key <i>b(j<sub>B</sub>)</i>
|
||||
<strong>for each</strong> row <i>a</i> in table <i>A</i>:
|
||||
<strong>for each</strong> row <i>b</i> in multimap <i>M<sub>B</sub></i> under key <i>a(j<sub>A</sub>)</i>:
|
||||
<strong>let</strong> <i>c</i> = the concatenation of row <i>a</i> and row <i>b</i>
|
||||
<strong>place</strong> row <i>c</i> in table <i>C</i>
|
||||
</pre>
|
||||
|
||||
# --instructions--
|
||||
|
||||
Implement the "hash join" algorithm as a function and demonstrate that it passes the test-case listed below. The function should accept two arrays of objects and return an array of combined objects.
|
||||
|
||||
**Input**
|
||||
|
||||
<table>
|
||||
<tr>
|
||||
<td style="padding: 4px; margin: 5px;">
|
||||
<table style="border:none; border-collapse:collapse;">
|
||||
<tr>
|
||||
<td style="border:none"><i>A =</i></td>
|
||||
<td style="border:none">
|
||||
<table>
|
||||
<tr>
|
||||
<th style="padding: 4px; margin: 5px;">Age</th>
|
||||
<th style="padding: 4px; margin: 5px;">Name</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="padding: 4px; margin: 5px;">27</td>
|
||||
<td style="padding: 4px; margin: 5px;">Jonah</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="padding: 4px; margin: 5px;">18</td>
|
||||
<td style="padding: 4px; margin: 5px;">Alan</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="padding: 4px; margin: 5px;">28</td>
|
||||
<td style="padding: 4px; margin: 5px;">Glory</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="padding: 4px; margin: 5px;">18</td>
|
||||
<td style="padding: 4px; margin: 5px;">Popeye</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="padding: 4px; margin: 5px;">28</td>
|
||||
<td style="padding: 4px; margin: 5px;">Alan</td>
|
||||
</tr>
|
||||
</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>
|
||||
<tr>
|
||||
<th style="padding: 4px; margin: 5px;">Character</th>
|
||||
<th style="padding: 4px; margin: 5px;">Nemesis</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="padding: 4px; margin: 5px;">Jonah</td>
|
||||
<td style="padding: 4px; margin: 5px;">Whales</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="padding: 4px; margin: 5px;">Jonah</td>
|
||||
<td style="padding: 4px; margin: 5px;">Spiders</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="padding: 4px; margin: 5px;">Alan</td>
|
||||
<td style="padding: 4px; margin: 5px;">Ghosts</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="padding: 4px; margin: 5px;">Alan</td>
|
||||
<td style="padding: 4px; margin: 5px;">Zombies</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="padding: 4px; margin: 5px;">Glory</td>
|
||||
<td style="padding: 4px; margin: 5px;">Buffy</td>
|
||||
</tr>
|
||||
</table>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="border:none">
|
||||
<i>j<sub>A</sub> =</i>
|
||||
</td>
|
||||
<td style="border:none">
|
||||
<i><code>Name</code> (i.e. column 1)</i>
|
||||
</td>
|
||||
<td style="border:none">
|
||||
<i>j<sub>B</sub> =</i>
|
||||
</td>
|
||||
<td style="border:none">
|
||||
<i><code>Character</code> (i.e. column 0)</i>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
**Output**
|
||||
|
||||
| A_age | A_name | B_character | B_nemesis |
|
||||
| ----- | ------ | ----------- | --------- |
|
||||
| 27 | Jonah | Jonah | Whales |
|
||||
| 27 | Jonah | Jonah | Spiders |
|
||||
| 18 | Alan | Alan | Ghosts |
|
||||
| 18 | Alan | Alan | Zombies |
|
||||
| 28 | Glory | Glory | Buffy |
|
||||
| 28 | Alan | Alan | Ghosts |
|
||||
| 28 | Alan | Alan | Zombies |
|
||||
|
||||
The order of the rows in the output table is not significant.
|
||||
|
||||
# --hints--
|
||||
|
||||
`hashJoin`是一个函数。
|
||||
`hashJoin` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof hashJoin === 'function');
|
||||
```
|
||||
|
||||
`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"}]`
|
||||
`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" }])` should return `[{"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
|
||||
assert.deepEqual(hashJoin(hash1, hash2), res);
|
||||
|
@ -1,42 +1,64 @@
|
||||
---
|
||||
id: 595b98f8b5a2245e243aa831
|
||||
title: 苍鹭三角形
|
||||
title: Heronian triangles
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302285
|
||||
dashedName: heronian-triangles
|
||||
---
|
||||
|
||||
# --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>
|
||||
[Hero's formula](https://en.wikipedia.org/wiki/Heron's formula "wp: Heron's formula") for the area of a triangle given the length of its three sides `a`, `b`, and `c` is given by:
|
||||
|
||||
$A = \\sqrt{s(s-a)(s-b)(s-c)},$
|
||||
|
||||
where `s` is half the perimeter of the triangle; that is,
|
||||
|
||||
$s=\\frac{a+b+c}{2}.$
|
||||
|
||||
Heronian triangles are triangles whose sides and area are all integers.
|
||||
|
||||
An example is the triangle with sides `3, 4, 5` whose area is `6` (and whose perimeter is `12`).
|
||||
|
||||
Note that any triangle whose sides are all an integer multiple of `3, 4, 5`; such as `6, 8, 10,` will also be a Heronian triangle.
|
||||
|
||||
Define a Primitive Heronian triangle as a Heronian triangle where the greatest common divisor
|
||||
|
||||
of all three sides is `1` (unity).
|
||||
|
||||
This will exclude, for example, triangle `6, 8, 10.`
|
||||
|
||||
# --instructions--
|
||||
|
||||
Implement a function based on Hero's formula that returns the first <code>n<sub>th</sub></code> ordered triangles in an array of arrays.
|
||||
|
||||
# --hints--
|
||||
|
||||
`heronianTriangle`是一个函数。
|
||||
`heronianTriangle` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof heronianTriangle === 'function');
|
||||
```
|
||||
|
||||
`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]]`
|
||||
`heronianTriangle(10)` should return `[[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
|
||||
assert.deepEqual(heronianTriangle(testCases[0]), res[0]);
|
||||
```
|
||||
|
||||
`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]],`
|
||||
`heronianTriangle(15)` should return `[[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
|
||||
assert.deepEqual(heronianTriangle(testCases[1]), res[1]);
|
||||
```
|
||||
|
||||
`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]],`
|
||||
`heronianTriangle(20)` should return `[[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]]`
|
||||
`heronianTriangle(25)` should return `[[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]);
|
||||
|
@ -1,84 +1,113 @@
|
||||
---
|
||||
id: 59622f89e4e137560018a40e
|
||||
title: Hofstadter图 - 图序列
|
||||
title: Hofstadter Figure-Figure sequences
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302286
|
||||
dashedName: hofstadter-figure-figure-sequences
|
||||
---
|
||||
|
||||
# --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> 。
|
||||
These two sequences of positive integers are defined as:
|
||||
|
||||
$R(1)=1\\ ;\\ S(1)=2 \\\\R(n)=R(n-1)+S(n-1), \\quad n>1.$
|
||||
|
||||
The sequence $S(n)$ is further defined as the sequence of positive integers not present in $R(n)$.
|
||||
|
||||
Sequence $R$ starts:
|
||||
|
||||
<pre>1, 3, 7, 12, 18, ...</pre>
|
||||
|
||||
Sequence $S$ starts:
|
||||
|
||||
<pre>2, 4, 5, 6, 8, ...</pre>
|
||||
|
||||
# --instructions--
|
||||
|
||||
Create two functions named `ffr` and `ffs` that when given `n` return `R(n)` or `S(n)` respectively. (Note that R(1) = 1 and S(1) = 2 to avoid off-by-one errors).
|
||||
|
||||
No maximum value for `n` should be assumed.
|
||||
|
||||
**References**
|
||||
|
||||
<ul>
|
||||
<li>
|
||||
Sloane's <a href='https://oeis.org/A005228' target='_blank'>A005228</a> and <a href='https://oeis.org/A030124' target='_blank'>A030124</a>.
|
||||
</li>
|
||||
<li>
|
||||
Wikipedia: <a href='https://en.wikipedia.org/wiki/Hofstadter_sequence#Hofstadter_Figure-Figure_sequences' title='wp: Hofstadter_sequence#Hofstadter_Figure-Figure_sequences' target='_blank'>Hofstadter Figure-Figure sequences</a>.
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
# --hints--
|
||||
|
||||
`ffr`是一个功能。
|
||||
`ffr` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof ffr === 'function');
|
||||
```
|
||||
|
||||
`ffs`是一个函数。
|
||||
`ffs` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof ffs === 'function');
|
||||
```
|
||||
|
||||
`ffr`应该返回整数。
|
||||
`ffr` should return integer.
|
||||
|
||||
```js
|
||||
assert(Number.isInteger(ffr(1)));
|
||||
```
|
||||
|
||||
`ffs`应该返回整数。
|
||||
`ffs` should return integer.
|
||||
|
||||
```js
|
||||
assert(Number.isInteger(ffs(1)));
|
||||
```
|
||||
|
||||
`ffr()`应该返回`69`
|
||||
`ffr(10)` should return `69`
|
||||
|
||||
```js
|
||||
assert.equal(ffr(ffrParamRes[0][0]), ffrParamRes[0][1]);
|
||||
```
|
||||
|
||||
`ffr()`应返回`1509`
|
||||
`ffr(50)` should return `1509`
|
||||
|
||||
```js
|
||||
assert.equal(ffr(ffrParamRes[1][0]), ffrParamRes[1][1]);
|
||||
```
|
||||
|
||||
`ffr()`应返回`5764`
|
||||
`ffr(100)` should return `5764`
|
||||
|
||||
```js
|
||||
assert.equal(ffr(ffrParamRes[2][0]), ffrParamRes[2][1]);
|
||||
```
|
||||
|
||||
`ffr()`应返回`526334`
|
||||
`ffr(1000)` should return `526334`
|
||||
|
||||
```js
|
||||
assert.equal(ffr(ffrParamRes[3][0]), ffrParamRes[3][1]);
|
||||
```
|
||||
|
||||
`ffs()`应该返回`14`
|
||||
`ffs(10)` should return `14`
|
||||
|
||||
```js
|
||||
assert.equal(ffs(ffsParamRes[0][0]), ffsParamRes[0][1]);
|
||||
```
|
||||
|
||||
`ffs()`应该返回`59`
|
||||
`ffs(50)` should return `59`
|
||||
|
||||
```js
|
||||
assert.equal(ffs(ffsParamRes[1][0]), ffsParamRes[1][1]);
|
||||
```
|
||||
|
||||
`ffs()`应该返回`112`
|
||||
`ffs(100)` should return `112`
|
||||
|
||||
```js
|
||||
assert.equal(ffs(ffsParamRes[2][0]), ffsParamRes[2][1]);
|
||||
```
|
||||
|
||||
`ffs()`应该返回`1041`
|
||||
`ffs(1000)` should return `1041`
|
||||
|
||||
```js
|
||||
assert.equal(ffs(ffsParamRes[3][0]), ffsParamRes[3][1]);
|
||||
|
@ -1,48 +1,56 @@
|
||||
---
|
||||
id: 59637c4d89f6786115efd814
|
||||
title: Hofstadter Q序列
|
||||
title: Hofstadter Q sequence
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302287
|
||||
dashedName: hofstadter-q-sequence
|
||||
---
|
||||
|
||||
# --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
|
||||
The [Hofstadter Q sequence](https://en.wikipedia.org/wiki/Hofstadter_sequence#Hofstadter_Q_sequence "wp: Hofstadter_sequence#Hofstadter_Q_sequence") is defined as:
|
||||
|
||||
$Q(1)=Q(2)=1, \\\\ Q(n)=Q\\big(n-Q(n-1)\\big)+Q\\big(n-Q(n-2)), \\quad n>2.$
|
||||
|
||||
It is defined like the [Fibonacci sequence](https://rosettacode.org/wiki/Fibonacci sequence "Fibonacci sequence"), but whereas the next term in the Fibonacci sequence is the sum of the previous two terms, in the Q sequence the previous two terms tell you how far to go back in the Q sequence to find the two numbers to sum to make the next term of the sequence.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Implement the Hofstadter Q Sequence equation as a function. The function should accept number, `n`, and return an integer.
|
||||
|
||||
# --hints--
|
||||
|
||||
`hofstadterQ`是一个函数。
|
||||
`hofstadterQ` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof hofstadterQ === 'function');
|
||||
```
|
||||
|
||||
`hofstadterQ()`应该返回`integer`
|
||||
`hofstadterQ()` should return `integer`
|
||||
|
||||
```js
|
||||
assert(Number.isInteger(hofstadterQ(1000)));
|
||||
```
|
||||
|
||||
`hofstadterQ(1000)`应该返回`502`
|
||||
`hofstadterQ(1000)` should return `502`
|
||||
|
||||
```js
|
||||
assert.equal(hofstadterQ(testCase[0]), res[0]);
|
||||
```
|
||||
|
||||
`hofstadterQ(1500)`应该返回`755`
|
||||
`hofstadterQ(1500)` should return `755`
|
||||
|
||||
```js
|
||||
assert.equal(hofstadterQ(testCase[1]), res[1]);
|
||||
```
|
||||
|
||||
`hofstadterQ(2000)`应该返回`1005`
|
||||
`hofstadterQ(2000)` should return `1005`
|
||||
|
||||
```js
|
||||
assert.equal(hofstadterQ(testCase[2]), res[2]);
|
||||
```
|
||||
|
||||
`hofstadterQ(2500)`应该返回`1261`
|
||||
`hofstadterQ(2500)` should return `1261`
|
||||
|
||||
```js
|
||||
assert.equal(hofstadterQ(testCase[3]), res[3]);
|
||||
|
@ -1,65 +1,77 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc7eb0
|
||||
title: 我在E之前除了C之后
|
||||
title: I before E except after C
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302288
|
||||
dashedName: i-before-e-except-after-c
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
短语[“我在E之前,除了C之后”](<https://en.wikipedia.org/wiki/I before E except after C>)是一个广为人知的助记符,它应该有助于拼写英语单词。使用提供的单词,检查短语的两个子句是否合理:
|
||||
The phrase ["I before E, except after C"](https://en.wikipedia.org/wiki/I before E except after C) is a widely known mnemonic which is supposed to help when spelling English words.
|
||||
|
||||
1. *“我在E之前没有前面的C”。*
|
||||
2. *“在我之前的C之前是C”。*
|
||||
Using the words provided, check if the two sub-clauses of the phrase are plausible individually:
|
||||
|
||||
如果两个子短语都是合理的,则原始短语可以说是合理的。编写一个接受单词的函数,并检查单词是否遵循此规则。如果该函数遵循规则,则该函数应返回true,否则返回false。
|
||||
<ol>
|
||||
<li>
|
||||
<i>"I before E when not preceded by C".</i>
|
||||
</li>
|
||||
<li>
|
||||
<i>"E before I when preceded by C".</i>
|
||||
</li>
|
||||
</ol>
|
||||
|
||||
If both sub-phrases are plausible then the original phrase can be said to be plausible.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Write a function that accepts a word and check if the word follows this rule. The function should return true if the word follows the rule and false if it does not.
|
||||
|
||||
# --hints--
|
||||
|
||||
`IBeforeExceptC`应该是一个函数。
|
||||
`IBeforeExceptC` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof IBeforeExceptC == 'function');
|
||||
```
|
||||
|
||||
`IBeforeExceptC("receive")`应该返回一个布尔值。
|
||||
`IBeforeExceptC("receive")` should return a boolean.
|
||||
|
||||
```js
|
||||
assert(typeof IBeforeExceptC('receive') == 'boolean');
|
||||
```
|
||||
|
||||
`IBeforeExceptC("receive")`应该返回`true` 。
|
||||
`IBeforeExceptC("receive")` should return `true`.
|
||||
|
||||
```js
|
||||
assert.equal(IBeforeExceptC('receive'), true);
|
||||
```
|
||||
|
||||
`IBeforeExceptC("science")`应该返回`false` 。
|
||||
`IBeforeExceptC("science")` should return `false`.
|
||||
|
||||
```js
|
||||
assert.equal(IBeforeExceptC('science'), false);
|
||||
```
|
||||
|
||||
`IBeforeExceptC("imperceivable")`应该返回`true` 。
|
||||
`IBeforeExceptC("imperceivable")` should return `true`.
|
||||
|
||||
```js
|
||||
assert.equal(IBeforeExceptC('imperceivable'), true);
|
||||
```
|
||||
|
||||
`IBeforeExceptC("inconceivable")`应该返回`true` 。
|
||||
`IBeforeExceptC("inconceivable")` should return `true`.
|
||||
|
||||
```js
|
||||
assert.equal(IBeforeExceptC('inconceivable'), true);
|
||||
```
|
||||
|
||||
`IBeforeExceptC("insufficient")`应返回`false` 。
|
||||
`IBeforeExceptC("insufficient")` should return `false`.
|
||||
|
||||
```js
|
||||
assert.equal(IBeforeExceptC('insufficient'), false);
|
||||
```
|
||||
|
||||
`IBeforeExceptC("omniscient")`应该返回`false` 。
|
||||
`IBeforeExceptC("omniscient")` should return `false`.
|
||||
|
||||
```js
|
||||
assert.equal(IBeforeExceptC('omniscient'), false);
|
||||
|
@ -1,60 +1,68 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc7eaf
|
||||
title: 他们
|
||||
title: IBAN
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302289
|
||||
dashedName: iban
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
[国际银行账号(IBAN)](https://en.wikipedia.org/wiki/International_Bank_Account_Number)是一种国际商定的识别跨国界银行账户的方法,降低了传播[抄写错误的](https://en.wikipedia.org/wiki/Transcription_error)风险。 IBAN最多包含34个字母数字字符:
|
||||
The [International Bank Account Number (IBAN)](https://en.wikipedia.org/wiki/International_Bank_Account_Number) is an internationally agreed means of identifying bank accounts across national borders with a reduced risk of propagating [transcription errors](https://en.wikipedia.org/wiki/Transcription_error).
|
||||
|
||||
- 首先是两个字母的ISO 3166-1 alpha-2国家代码
|
||||
- 然后是两个校验位,和
|
||||
- 最后是国家特定的基本银行帐号(BBAN)。
|
||||
The IBAN consists of up to 34 alphanumeric characters:
|
||||
|
||||
通过检查数字,即使在提交交易之前,也可以对银行帐号进行健全性检查以确认其完整性。编写一个以IBAN字符串作为参数的函数。如果有效则返回true。否则,返回false。
|
||||
<ul>
|
||||
<li>first the two-letter ISO 3166-1 alpha-2 country code</li>
|
||||
<li>then two check digits, and</li>
|
||||
<li>finally a country-specific Basic Bank Account Number (BBAN).</li>
|
||||
</ul>
|
||||
|
||||
The check digits enable a sanity check of the bank account number to confirm its integrity even before submitting a transaction.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Write a function that takes IBAN string as parameter. If it is valid return true. Otherwise, return false.
|
||||
|
||||
# --hints--
|
||||
|
||||
`isValid`应该是一个函数。
|
||||
`isValid` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof isValid == 'function');
|
||||
```
|
||||
|
||||
`isValid(""+tests[0]+"")`应该返回一个布尔值。
|
||||
`isValid("GB82 WEST 1234 5698 7654 32")` should return a boolean.
|
||||
|
||||
```js
|
||||
assert(typeof isValid('GB82 WEST 1234 5698 7654 32') == 'boolean');
|
||||
```
|
||||
|
||||
`isValid(""+tests[0]+"")`应该返回`true` 。
|
||||
`isValid("GB82 WEST 1234 5698 7654 32")` should return `true`.
|
||||
|
||||
```js
|
||||
assert.equal(isValid('GB82 WEST 1234 5698 7654 32'), true);
|
||||
```
|
||||
|
||||
`isValid(""+tests[1]+"")`应返回`false` 。
|
||||
`isValid("GB82 WEST 1.34 5698 7654 32")` should return `false`.
|
||||
|
||||
```js
|
||||
assert.equal(isValid('GB82 WEST 1.34 5698 7654 32'), false);
|
||||
```
|
||||
|
||||
`isValid(""+tests[2]+"")`应返回`false` 。
|
||||
`isValid("GB82 WEST 1234 5698 7654 325")` should return `false`.
|
||||
|
||||
```js
|
||||
assert.equal(isValid('GB82 WEST 1234 5698 7654 325'), false);
|
||||
```
|
||||
|
||||
`isValid(""+tests[3]+"")`应该返回`false` 。
|
||||
`isValid("GB82 TEST 1234 5698 7654 32")` should return `false`.
|
||||
|
||||
```js
|
||||
assert.equal(isValid('GB82 TEST 1234 5698 7654 32'), false);
|
||||
```
|
||||
|
||||
`isValid(""+tests[4]+"")`应该返回`true` 。
|
||||
`isValid("SA03 8000 0000 6080 1016 7519")` should return `true`.
|
||||
|
||||
```js
|
||||
assert.equal(isValid('SA03 8000 0000 6080 1016 7519'), true);
|
||||
|
@ -1,48 +1,56 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc7eb1
|
||||
title: 身份矩阵
|
||||
title: Identity matrix
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302290
|
||||
dashedName: identity-matrix
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
*单位矩阵*是大小为\\(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。
|
||||
An *identity matrix* is a square matrix of size \\( n \\times n \\), where the diagonal elements are all `1`s (ones), and all the other elements are all `0`s (zeroes).
|
||||
|
||||
<ul>
|
||||
<li style='list-style: none;'>\(\displaystyle I_{n}=\begin{bmatrix} 1 & 0 & 0 \cr 0 & 1 & 0 \cr 0 & 0 & 1 \cr \end{bmatrix}\)</li>
|
||||
</ul>
|
||||
|
||||
# --instructions--
|
||||
|
||||
Write a function that takes a number `n` as a parameter and returns the identity matrix of order \\( n \\times n \\).
|
||||
|
||||
# --hints--
|
||||
|
||||
`idMatrix`应该是一个功能。
|
||||
`idMatrix` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof idMatrix == 'function');
|
||||
```
|
||||
|
||||
`idMatrix(1)`应该返回一个数组。
|
||||
`idMatrix(1)` should return an array.
|
||||
|
||||
```js
|
||||
assert(Array.isArray(idMatrix(1)));
|
||||
```
|
||||
|
||||
`idMatrix(1)`应返回`"+JSON.stringify(results[0])+"` 。
|
||||
`idMatrix(1)` should return `[ [ 1 ] ]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(idMatrix(1), results[0]);
|
||||
```
|
||||
|
||||
`idMatrix(2)`应返回`"+JSON.stringify(results[1])+"` 。
|
||||
`idMatrix(2)` should return `[ [ 1, 0 ], [ 0, 1 ] ]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(idMatrix(2), results[1]);
|
||||
```
|
||||
|
||||
`idMatrix(3)`应返回`"+JSON.stringify(results[2])+"` 。
|
||||
`idMatrix(3)` should return `[ [ 1, 0, 0 ], [ 0, 1, 0 ], [ 0, 0, 1 ] ]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(idMatrix(3), results[2]);
|
||||
```
|
||||
|
||||
`idMatrix(4)`应返回`"+JSON.stringify(results[3])+"` 。
|
||||
`idMatrix(4)` should return `[ [ 1, 0, 0, 0 ], [ 0, 1, 0, 0 ], [ 0, 0, 1, 0 ], [ 0, 0, 0, 1 ] ]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(idMatrix(4), results[3]);
|
||||
|
@ -1,67 +1,68 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc7ec1
|
||||
title: 迭代的数字平方
|
||||
title: Iterated digits squaring
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302291
|
||||
dashedName: iterated-digits-squaring
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
如果添加自然数(大于零的整数)的数字的平方,则始终以1或89结尾:
|
||||
If you add the square of the digits of a Natural number (an integer bigger than zero), you always end with either 1 or 89:
|
||||
|
||||
```
|
||||
15 - > 26 - > 40 - > 16 - > 37 - > 58 - > 89
|
||||
7 - > 49 - > 97 - > 130 - > 10 - > 1
|
||||
```
|
||||
<pre>15 -> 26 -> 40 -> 16 -> 37 -> 58 -> 89
|
||||
7 -> 49 -> 97 -> 130 -> 10 -> 1
|
||||
</pre>
|
||||
|
||||
编写一个函数,该函数将数字作为参数,并在执行上述过程后返回1或89。
|
||||
# --instructions--
|
||||
|
||||
Write a function that takes a number as a parameter and returns 1 or 89 after performing the mentioned process.
|
||||
|
||||
# --hints--
|
||||
|
||||
`iteratedSquare`应该是一个函数。
|
||||
`iteratedSquare` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof iteratedSquare == 'function');
|
||||
```
|
||||
|
||||
`iteratedSquare(4)`应该返回一个数字。
|
||||
`iteratedSquare(4)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof iteratedSquare(4) == 'number');
|
||||
```
|
||||
|
||||
`iteratedSquare(4)`应该返回`89` 。
|
||||
`iteratedSquare(4)` should return `89`.
|
||||
|
||||
```js
|
||||
assert.equal(iteratedSquare(4), 89);
|
||||
```
|
||||
|
||||
`iteratedSquare(7)`应该返回`1` 。
|
||||
`iteratedSquare(7)` should return `1`.
|
||||
|
||||
```js
|
||||
assert.equal(iteratedSquare(7), 1);
|
||||
```
|
||||
|
||||
`iteratedSquare(15)`应该返回`89` 。
|
||||
`iteratedSquare(15)` should return `89`.
|
||||
|
||||
```js
|
||||
assert.equal(iteratedSquare(15), 89);
|
||||
```
|
||||
|
||||
`iteratedSquare(20)`应该返回`89` 。
|
||||
`iteratedSquare(20)` should return `89`.
|
||||
|
||||
```js
|
||||
assert.equal(iteratedSquare(20), 89);
|
||||
```
|
||||
|
||||
`iteratedSquare(70)`应该返回`1` 。
|
||||
`iteratedSquare(70)` should return `1`.
|
||||
|
||||
```js
|
||||
assert.equal(iteratedSquare(70), 1);
|
||||
```
|
||||
|
||||
`iteratedSquare(100)`应该返回`1` 。
|
||||
`iteratedSquare(100)` should return `1`.
|
||||
|
||||
```js
|
||||
assert.equal(iteratedSquare(100), 1);
|
||||
|
@ -1,66 +1,88 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc7ec2
|
||||
title: Jaro距离
|
||||
title: Jaro distance
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302292
|
||||
dashedName: jaro-distance
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
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}其中:
|
||||
The Jaro distance is a measure of similarity between two strings. The higher the Jaro distance for two strings is, the more similar the strings are. The score is normalized such that `0` equates to no similarity and `1` is an exact match.
|
||||
|
||||
- \\(m \\)是*匹配字符*的数量;
|
||||
- \\(t \\)是*换位*次数的一半。
|
||||
**Definition**
|
||||
|
||||
分别来自\\(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*我们发现:
|
||||
The Jaro distance \\( d_j \\) of two given strings \\(s_1\\) and \\(s_2\\) is
|
||||
|
||||
- \\(m = 4 \\)
|
||||
- \\(| s_1 | = 6 \\)
|
||||
- \\(| s_2 | = 5 \\)
|
||||
- \\(t = 0 \\)
|
||||
\\begin{align}d_j = \\begin{cases}0& & \\text{if }m=0 \\\\\\\\{\\frac {1}{3}}\\left({\\frac {m}{|s\_{1}|}}+{\\frac {m}{|s\_{2}|}}+{\\frac {m-t}{m}}\\right)& & \\text{otherwise}\\end{cases}\\end{align}
|
||||
|
||||
我们发现Jaro得分为:\\(d_j = \\ frac {1} {3} \\ left(\\ frac {4} {6} + \\ frac {4} {5} + \\ frac {4-0} {4} \\ right)= 0.822 \\)。编写一个函数a,它接受两个字符串作为参数并返回相关的Jaro距离。
|
||||
Where:
|
||||
|
||||
<ul>
|
||||
<li>\(m\) is the number of <i>matching characters</i>;</li>
|
||||
<li> \(t\) is half the number of <i>transpositions</i>.</li>
|
||||
</ul>
|
||||
|
||||
Two characters from \\(s_1\\) and \\(s_2\\) respectively, are considered *matching* only if they are the same and not farther than \\(\\left\\lfloor\\frac{\\max(|s_1|,|s_2|)}{2}\\right\\rfloor-1\\).
|
||||
|
||||
Each character of \\(s_1\\) is compared with all its matching characters in \\(s_2\\) . The number of matching (but different sequence order) characters divided by 2 defines the number of *transpositions*.
|
||||
|
||||
**Example**
|
||||
|
||||
Given the strings \\(s_1\\) *DWAYNE* and \\(s_2\\) *DUANE* we find:
|
||||
|
||||
<ul>
|
||||
<li>\(m = 4\)</li>
|
||||
<li>\(|s_1| = 6\)</li>
|
||||
<li>\(|s_2| = 5\)</li>
|
||||
<li>\(t = 0\)</li>
|
||||
</ul>
|
||||
|
||||
We find a Jaro score of: \\(d_j = \\frac{1}{3}\\left(\\frac{4}{6} + \\frac{4}{5} + \\frac{4-0}{4}\\right) = 0.822\\).
|
||||
|
||||
# --instructions--
|
||||
|
||||
Write a function a that takes two strings as parameters and returns the associated Jaro distance.
|
||||
|
||||
# --hints--
|
||||
|
||||
`jaro`应该是一个功能。
|
||||
`jaro` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof jaro == 'function');
|
||||
```
|
||||
|
||||
`jaro(""+tests[0][0]+"",""+tests[0][1]+"")`应返回一个数字。
|
||||
`jaro("MARTHA", "MARHTA")` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof jaro('MARTHA', 'MARHTA') == 'number');
|
||||
```
|
||||
|
||||
`jaro(""+tests[0][0]+"",""+tests[0][1]+"")`应该返回`"+results[0]+"` 。
|
||||
`jaro("MARTHA", "MARHTA")` should return `0.9444444444444445`.
|
||||
|
||||
```js
|
||||
assert.equal(jaro('MARTHA', 'MARHTA'), 0.9444444444444445);
|
||||
```
|
||||
|
||||
`jaro(""+tests[1][0]+"",""+tests[1][1]+"")`应返回`"+results[1]+"` 。
|
||||
`jaro("DIXON", "DICKSONX")` should return `0.7666666666666666`.
|
||||
|
||||
```js
|
||||
assert.equal(jaro('DIXON', 'DICKSONX'), 0.7666666666666666);
|
||||
```
|
||||
|
||||
`jaro(""+tests[2][0]+"",""+tests[2][1]+"")`应返回`"+results[2]+"` 。
|
||||
`jaro("JELLYFISH", "SMELLYFISH")` should return `0.8962962962962964`.
|
||||
|
||||
```js
|
||||
assert.equal(jaro('JELLYFISH', 'SMELLYFISH'), 0.8962962962962964);
|
||||
```
|
||||
|
||||
`jaro(""+tests[3][0]+"",""+tests[3][1]+"")`应返回`"+results[3]+"` 。
|
||||
`jaro("HELLOS", "CHELLO")` should return `0.888888888888889`.
|
||||
|
||||
```js
|
||||
assert.equal(jaro('HELLOS', 'CHELLO'), 0.888888888888889);
|
||||
```
|
||||
|
||||
`jaro(""+tests[4][0]+"",""+tests[4][1]+"")`应该返回`"+results[4]+"` 。
|
||||
`jaro("ABCD", "BCDA")` should return `0.8333333333333334`.
|
||||
|
||||
```js
|
||||
assert.equal(jaro('ABCD', 'BCDA'), 0.8333333333333334);
|
||||
|
@ -2,59 +2,61 @@
|
||||
id: 5a23c84252665b21eecc7ec4
|
||||
title: JortSort
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302293
|
||||
dashedName: jortsort
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
jortSort是一个排序工具集,它使用户可以完成工作并保证效率,因为您不必再次排序。它最初是由着名的[JSConf的](https://www.youtube.com/watch?v=pj4U_W0OFoE) Jenn“Moneydollars”Schiffer [提出的](https://www.youtube.com/watch?v=pj4U_W0OFoE) 。 jortSort是一个函数,它将一个可比较对象数组作为其参数。然后它按升序对数组进行排序,并将排序后的数组与最初提供的数组进行比较。如果数组匹配(即原始数组已经排序),则该函数返回true。如果数组不匹配(即原始数组未排序),则该函数返回false。
|
||||
jortSort is a sorting toolset that makes the user do the work and guarantees efficiency because you don't have to sort ever again. It was originally presented by Jenn "Moneydollars" Schiffer at the prestigious [JSConf](https://www.youtube.com/watch?v=pj4U_W0OFoE).
|
||||
|
||||
jortSort should be a function that takes a single array of comparable objects as its argument. It then sorts the array in ascending order and compares the sorted array to the originally provided array. If the arrays match (i.e. the original array was already sorted), the function returns true. If the arrays do not match (i.e. the original array was not sorted), the function returns false.
|
||||
|
||||
# --hints--
|
||||
|
||||
`jortsort`应该是一个功能。
|
||||
`jortsort` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof jortsort == 'function');
|
||||
```
|
||||
|
||||
`jortsort("+JSON.stringify(tests[0])+")`应该返回一个布尔值。
|
||||
`jortsort([1,2,3,4,5])` should return a boolean.
|
||||
|
||||
```js
|
||||
assert(typeof jortsort([1, 2, 3, 4, 5]) == 'boolean');
|
||||
```
|
||||
|
||||
`jortsort("+JSON.stringify(tests[0])+")`应该返回`true` 。
|
||||
`jortsort([1,2,3,4,5])` should return `true`.
|
||||
|
||||
```js
|
||||
assert.equal(jortsort([1, 2, 3, 4, 5]), true);
|
||||
```
|
||||
|
||||
`jortsort("+JSON.stringify(tests[1])+")`应该返回`false` 。
|
||||
`jortsort([1,2,13,4,5])` should return `false`.
|
||||
|
||||
```js
|
||||
assert.equal(jortsort([1, 2, 13, 4, 5]), false);
|
||||
```
|
||||
|
||||
`jortsort("+JSON.stringify(tests[2])+")`应该返回`false` 。
|
||||
`jortsort([12,4,51,2,4])` should return `false`.
|
||||
|
||||
```js
|
||||
assert.equal(jortsort([12, 4, 51, 2, 4]), false);
|
||||
```
|
||||
|
||||
`jortsort("+JSON.stringify(tests[3])+")`应该返回`true` 。
|
||||
`jortsort([1,2])` should return `true`.
|
||||
|
||||
```js
|
||||
assert.equal(jortsort([1, 2]), true);
|
||||
```
|
||||
|
||||
`jortsort("+JSON.stringify(tests[4])+")`应该返回`false` 。
|
||||
`jortsort([5,4,3,2,1])` should return `false`.
|
||||
|
||||
```js
|
||||
assert.equal(jortsort([5, 4, 3, 2, 1]), false);
|
||||
```
|
||||
|
||||
`jortsort("+JSON.stringify(tests[5])+")`应该返回`true` 。
|
||||
`jortsort([1,1,1,1,1])` should return `true`.
|
||||
|
||||
```js
|
||||
assert.equal(jortsort([1, 1, 1, 1, 1]), true);
|
||||
|
@ -1,54 +1,72 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc7ec5
|
||||
title: 约瑟夫斯问题
|
||||
title: Josephus problem
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302294
|
||||
dashedName: josephus-problem
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
[约瑟夫斯问题](<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'作为参数,并返回幸存的囚犯的数量。
|
||||
[Josephus problem](https://en.wikipedia.org/wiki/Josephus problem) is a math puzzle with a grim description: $n$ prisoners are standing on a circle, sequentially numbered from $0$ to $n-1$.
|
||||
|
||||
An executioner walks along the circle, starting from prisoner $0$, removing every $k$-th prisoner and killing him.
|
||||
|
||||
As the process goes on, the circle becomes smaller and smaller, until only one prisoner remains, who is then freed.
|
||||
|
||||
For example, if there are $n=5$ prisoners and $k=2$, the order the prisoners are killed in (let's call it the "killing sequence") will be 1, 3, 0, and 4, and the survivor will be #2.
|
||||
|
||||
Given any $n, k > 0$, find out which prisoner will be the final survivor.
|
||||
|
||||
In one such incident, there were 41 prisoners and every 3<sup>rd</sup> prisoner was being killed ($k=3$).
|
||||
|
||||
Among them was a clever chap name Josephus who worked out the problem, stood at the surviving position, and lived on to tell the tale.
|
||||
|
||||
Which number was he?
|
||||
|
||||
# --instructions--
|
||||
|
||||
Write a function that takes the initial number of prisoners and 'k' as parameter and returns the number of the prisoner that survives.
|
||||
|
||||
# --hints--
|
||||
|
||||
`josephus`应该是一个功能。
|
||||
`josephus` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof josephus == 'function');
|
||||
```
|
||||
|
||||
`josephus(30,3)`应该返回一个数字。
|
||||
`josephus(30,3)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof josephus(30, 3) == 'number');
|
||||
```
|
||||
|
||||
`josephus(30,3)`应该回`29` 。
|
||||
`josephus(30,3)` should return `29`.
|
||||
|
||||
```js
|
||||
assert.equal(josephus(30, 3), 29);
|
||||
```
|
||||
|
||||
`josephus(30,5)`应该返回`3` 。
|
||||
`josephus(30,5)` should return `3`.
|
||||
|
||||
```js
|
||||
assert.equal(josephus(30, 5), 3);
|
||||
```
|
||||
|
||||
`josephus(20,2)`应该返回`9` 。
|
||||
`josephus(20,2)` should return `9`.
|
||||
|
||||
```js
|
||||
assert.equal(josephus(20, 2), 9);
|
||||
```
|
||||
|
||||
`josephus(17,6)`应该回归`2` 。
|
||||
`josephus(17,6)` should return `2`.
|
||||
|
||||
```js
|
||||
assert.equal(josephus(17, 6), 2);
|
||||
```
|
||||
|
||||
`josephus(29,4)`应该返回`2` 。
|
||||
`josephus(29,4)` should return `2`.
|
||||
|
||||
```js
|
||||
assert.equal(josephus(29, 4), 2);
|
||||
|
@ -8,7 +8,7 @@ dashedName: kaprekar-numbers
|
||||
|
||||
# --description--
|
||||
|
||||
A positive integer is a [Kaprekar number](<https://en.wikipedia.org/wiki/Kaprekar number>) if:
|
||||
A positive integer is a [Kaprekar number](https://en.wikipedia.org/wiki/Kaprekar number) if:
|
||||
|
||||
<ul>
|
||||
<li>It is 1, or,</li>
|
||||
|
@ -8,7 +8,7 @@ dashedName: least-common-multiple
|
||||
|
||||
# --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)} )
|
||||
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--
|
||||
|
||||
|
@ -8,7 +8,7 @@ dashedName: levenshtein-distance
|
||||
|
||||
# --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.
|
||||
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:
|
||||
|
||||
|
@ -8,9 +8,9 @@ dashedName: linear-congruential-generator
|
||||
|
||||
# --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:
|
||||
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$$
|
||||
$$r_{n + 1} = (a \times r_n + c) \bmod m$$
|
||||
|
||||
Where:
|
||||
|
||||
@ -22,7 +22,7 @@ Where:
|
||||
|
||||
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.
|
||||
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--
|
||||
|
||||
|
@ -8,7 +8,7 @@ dashedName: long-multiplication
|
||||
|
||||
# --description--
|
||||
|
||||
Explicitly implement [long multiplication](<https://en.wikipedia.org/wiki/long multiplication>).
|
||||
Explicitly implement [long multiplication](https://en.wikipedia.org/wiki/long multiplication).
|
||||
|
||||
This is one possible approach to arbitrary-precision integer algebra.
|
||||
|
||||
|
@ -18,7 +18,7 @@ Longest increasing sequence is:
|
||||
|
||||
$\\{3, 10, 20\\}$
|
||||
|
||||
For more information on this problem please see [Wikipedia](<https://en.wikipedia.org/wiki/Longest increasing subsequence>).
|
||||
For more information on this problem please see [Wikipedia](https://en.wikipedia.org/wiki/Longest increasing subsequence).
|
||||
|
||||
# --instructions--
|
||||
|
||||
|
@ -8,7 +8,7 @@ dashedName: look-and-say-sequence
|
||||
|
||||
# --description--
|
||||
|
||||
The [Look and say sequence](<https://en.wikipedia.org/wiki/Look and say sequence>) is a recursively defined sequence of numbers.
|
||||
The [Look and say sequence](https://en.wikipedia.org/wiki/Look and say sequence) is a recursively defined sequence of numbers.
|
||||
|
||||
Sequence Definition
|
||||
|
||||
|
@ -8,13 +8,13 @@ dashedName: lu-decomposition
|
||||
|
||||
# --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>).
|
||||
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.
|
||||
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$.
|
||||
|
||||
|
@ -8,7 +8,7 @@ dashedName: luhn-test-of-credit-card-numbers
|
||||
|
||||
# --description--
|
||||
|
||||
The [Luhn test](<https://en.wikipedia.org/wiki/Luhn algorithm>) is used by some credit card companies to distinguish valid credit card numbers from what could be a random selection of digits.
|
||||
The [Luhn test](https://en.wikipedia.org/wiki/Luhn algorithm) is used by some credit card companies to distinguish valid credit card numbers from what could be a random selection of digits.
|
||||
|
||||
Those companies using credit card numbers that can be validated by the Luhn test have numbers that pass the following test:
|
||||
|
||||
|
@ -14,7 +14,7 @@ You can read a complete description of it in the [Wikipedia article](https://en.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Write a function that takes two parameters. The first parameter is a boolean where \`true\` indicates compress and \`false\` indicates decompress. The second parameter is either a string or an array to be processed. If it is a string to be compressed, return an array of numbers. If it's an array of numbers to be decompressed, return a string.
|
||||
Write a function that takes two parameters. The first parameter is a boolean where `true` indicates compress and `false` indicates decompress. The second parameter is either a string or an array to be processed. If it is a string to be compressed, return an array of numbers. If it's an array of numbers to be decompressed, return a string.
|
||||
|
||||
# --hints--
|
||||
|
||||
|
@ -1,32 +1,54 @@
|
||||
---
|
||||
id: 59667989bf71cf555dd5d2ff
|
||||
title: S-表达式
|
||||
title: S-Expressions
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302303
|
||||
dashedName: s-expressions
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
<p> <a href='https://en.wikipedia.org/wiki/S-Expression' title='wp:S表达式'>S-Expressions</a>是一种解析和存储数据的便捷方式。 </p>任务: <p>为S-Expressions编写一个简单的读取器/解析器,处理引用的和不带引号的字符串,整数和浮点数。 </p><p>该函数应从字符串中读取单个但嵌套的S-Expression,并将其作为(嵌套)数组返回。 </p><p>除非包含在带引号的字符串中,否则可以忽略换行符和其他空格。 </p><p> “ <tt>()</tt> ”内部引用的字符串不会被解释,但会被视为字符串的一部分。 </p><p>处理字符串中的转义引号是可选的;因此“ <tt>(foo”bar)</tt> “可能被视为字符串” <tt>foo“bar</tt> ”,或作为错误。 </p><p>为此,读者无需识别“ <tt>\</tt> ”以进行转义,但如果语言具有适当的数据类型,则还应识别数字。 </p><p>请注意,除了“ <tt>()”</tt> “(” <tt>\</tt> “,如果支持转义)和空格,没有特殊字符。其他任何内容都是允许的,不带引号。 </p><p>读者应该能够阅读以下输入</p><p></p><pre> ((数据“引用数据”123 4.5)
|
||||
(数据(!@#(4.5)“(更多”“数据”))))
|
||||
</pre><p></p><p>并将其转换为本机数据结构。 (有关本机数据结构的示例,请参阅<a href='http://rosettacode.org/wiki/#Pike' title='#Pike'>Pike</a> , <a href='http://rosettacode.org/wiki/#Python' title='#蟒蛇'>Python</a>和<a href='http://rosettacode.org/wiki/#Ruby' title='#红宝石'>Ruby</a>实现。) </p>
|
||||
[S-Expressions](https://en.wikipedia.org/wiki/S-Expression "wp: S-Expression") are one convenient way to parse and store data.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Write a simple reader/parser for S-Expressions that handles quoted and unquoted strings, integers and floats.
|
||||
|
||||
The function should read a single but nested S-Expression from a string and return it as a (nested) array.
|
||||
|
||||
Newlines and other whitespace may be ignored unless contained within a quoted string.
|
||||
|
||||
"`()`" inside quoted strings are not interpreted, but treated as part of the string.
|
||||
|
||||
Handling escaped quotes inside a string is optional; thus "`(foo"bar)`" may be treated as a string "`foo"bar`", or as an error.
|
||||
|
||||
For this, the reader need not recognize "<code>\\</code>" for escaping, but should, in addition, recognize numbers if the language has appropriate data types.
|
||||
|
||||
Note that with the exception of "`()"`" ("<code>\\</code>" if escaping is supported) and whitespace there are no special characters. Anything else is allowed without quotes.
|
||||
|
||||
The reader should be able to read the following input
|
||||
|
||||
<pre>((data "quoted data" 123 4.5)
|
||||
(data (!@# (4.5) "(more" "data)")))
|
||||
</pre>
|
||||
|
||||
and turn it into a native data structure. (See the [Pike](https://rosettacode.org/wiki/S-Expressions#Pike "\#Pike"), [Python](https://rosettacode.org/wiki/S-Expressions#Python "\#Python") and [Ruby](https://rosettacode.org/wiki/S-Expressions#Ruby "\#Ruby") implementations for examples of native data structures.)
|
||||
|
||||
# --hints--
|
||||
|
||||
`parseSexpr`是一个函数。
|
||||
`parseSexpr` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof parseSexpr === 'function');
|
||||
```
|
||||
|
||||
`parseSexpr("(data1 data2 data3)")`应返回[“data1”,“data2”,“data3”]“)
|
||||
`parseSexpr('(data1 data2 data3)')` should return `['data1', 'data2', 'data3']`
|
||||
|
||||
```js
|
||||
assert.deepEqual(parseSexpr(simpleSExpr), simpleSolution);
|
||||
```
|
||||
|
||||
`parseSexpr("(data1 data2 data3)")`应该返回一个包含3个元素的数组“)'
|
||||
`parseSexpr('(data1 data2 data3)')` should return an array with 3 elements.
|
||||
|
||||
```js
|
||||
assert.deepEqual(parseSexpr(basicSExpr), basicSolution);
|
||||
|
@ -1,36 +1,45 @@
|
||||
---
|
||||
id: 59da22823d04c95919d46269
|
||||
title: 水手,椰子和猴子问题
|
||||
title: 'Sailors, coconuts and a monkey problem'
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302304
|
||||
dashedName: sailors-coconuts-and-a-monkey-problem
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
<p>五名水手在岛上遭遇海难,并在白天收集了一大堆椰子。 </p><p>那天晚上,第一个水手醒来并决定早点拿走他的第一份,所以试图将一堆椰子平分成五堆,但发现剩下一个椰子,所以他把它扔到一只猴子然后隐藏“他的”五个同样大小的椰子堆中的一个,并将其他四个桩推到一起,再次形成一堆可见的椰子并上床睡觉。 </p><p>长话短说,每个水手轮流在夜间起床,并执行将椰子堆分成五个的相同动作,发现剩下一个椰子并将剩下的椰子留给猴子。 </p><p>在早上(在夜间五个水手的暗中和分开行动之后),剩下的椰子被分成五个相等的堆,每个水手,然后发现一堆椰子在水手之间平分没有余数。 (早上猴子没什么。) </p><p>任务: </p><pre> <code> Create a function that returns the the minimum possible size of the initial pile of coconuts collected during the day for N sailors.</code> </pre><p>注意: </p><pre> <code> Of course the tale is told in a world where the collection of any amount of coconuts in a day and multiple divisions of the pile, etc can occur in time fitting the story line, so as not to affect the mathematics.</code> </pre><p> CF卡: </p><p> <a href='https://www.youtube.com/watch?v=U9qU20VmvaU' title='链接:https://www.youtube.com/watch?v = U9qU20VmvaU'>猴子和椰子 - Numberphile</a> (视频)分析解决方案。 </p><pre> <code> <a href="http://oeis.org/A002021" title="link: http://oeis.org/A002021">A002021 Pile of coconuts problem</a> The On-Line Encyclopedia of Integer Sequences. (Although some of its references may use the alternate form of the tale).</code> </pre>
|
||||
Five sailors are shipwrecked on an island and collect a large pile of coconuts during the day. That night the first sailor wakes up and decides to take his first share early so tries to divide the pile of coconuts equally into five piles but finds that there is one coconut left over, so he tosses it to a monkey and then hides "his" one of the five equally sized piles of coconuts and pushes the other four piles together to form a single visible pile of coconuts again and goes to bed. To cut a long story short, each of the sailors in turn gets up once during the night and performs the same actions of dividing the coconut pile into five, finding that one coconut is left over and giving that single remainder coconut to the monkey. In the morning (after the surreptitious and separate action of each of the five sailors during the night), the remaining coconuts are divided into five equal piles for each of the sailors, whereupon it is found that the pile of coconuts divides equally amongst the sailors with no remainder. (Nothing for the monkey in the morning.)
|
||||
|
||||
# --instructions--
|
||||
|
||||
Create a function that returns the minimum possible size of the initial pile of coconuts collected during the day for `N` sailors. **Note:** Of course the tale is told in a world where the collection of any amount of coconuts in a day and multiple divisions of the pile, etc. can occur in time fitting the story line, so as not to affect the mathematics. **C.f:**
|
||||
|
||||
<ul>
|
||||
<li><a href="https://www.youtube.com/watch?v=U9qU20VmvaU" target="_blank"> Monkeys and Coconuts - Numberphile</a> (Video) Analytical solution.</li>
|
||||
<li><a href="https://oeis.org/A002021" target="_blank">A002021 Pile of coconuts problem</a> The On-Line Encyclopedia of Integer Sequences. (Although some of its references may use the alternate form of the tale).</li>
|
||||
</ul>
|
||||
|
||||
# --hints--
|
||||
|
||||
`splitCoconuts`是一个函数。
|
||||
`splitCoconuts` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof splitCoconuts === 'function');
|
||||
```
|
||||
|
||||
`splitCoconuts(5)`应该返回3121。
|
||||
`splitCoconuts(5)` should return 3121.
|
||||
|
||||
```js
|
||||
assert(splitCoconuts(5) === 3121);
|
||||
```
|
||||
|
||||
`splitCoconuts(6)`应返回233275。
|
||||
`splitCoconuts(6)` should return 233275.
|
||||
|
||||
```js
|
||||
assert(splitCoconuts(6) === 233275);
|
||||
```
|
||||
|
||||
`splitCoconuts(7)`应该返回823537。
|
||||
`splitCoconuts(7)` should return 823537.
|
||||
|
||||
```js
|
||||
assert(splitCoconuts(7) === 823537);
|
||||
|
@ -2,51 +2,57 @@
|
||||
id: 59d9c6bc214c613ba73ff012
|
||||
title: SEDOLs
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
forumTopicId: 302305
|
||||
dashedName: sedols
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
任务:
|
||||
For each number list of 6-digit [SEDOL](https://en.wikipedia.org/wiki/SEDOL "wp: SEDOL")s, calculate and append the checksum digit. That is, given the input string on the left, your function should return the corresponding string on the right:
|
||||
|
||||
对于6位[SEDOL](https://en.wikipedia.org/wiki/SEDOL "wp:SEDOL")的每个数字列表,计算并附加校验和数字。
|
||||
<pre>
|
||||
710889 => 7108899
|
||||
B0YBKJ => B0YBKJ7
|
||||
406566 => 4065663
|
||||
B0YBLH => B0YBLH2
|
||||
228276 => 2282765
|
||||
B0YBKL => B0YBKL9
|
||||
557910 => 5579107
|
||||
B0YBKR => B0YBKR5
|
||||
585284 => 5852842
|
||||
B0YBKT => B0YBKT7
|
||||
B00030 => B000300
|
||||
</pre>
|
||||
|
||||
也就是说,给定左侧的输入字符串,您的函数应返回右侧的相应字符串:
|
||||
|
||||
```
|
||||
<pre> 710889 => 7108899 B0YBKJ => B0YBKJ7 406566 => 4065663 B0YBLH => B0YBLH2 228276 => 2282765 B0YBKL => B0YBKL9 557910 => 5579107 B0YBKR => B0YBKR5 585284 => 5852842 B0YBKT => B0YBKT7 B00030 => B000300 </pre>
|
||||
```
|
||||
|
||||
还要检查每个输入是否正确形成,尤其是对于SEDOL字符串中允许的有效字符。您的函数应在无效输入时返回`null` 。
|
||||
Check that each input is correctly formed, especially with respect to valid characters allowed in a SEDOL string. Your function should return `null` on an invalid input.
|
||||
|
||||
# --hints--
|
||||
|
||||
`sedol`是一个功能。
|
||||
`sedol` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof sedol === 'function');
|
||||
```
|
||||
|
||||
`sedol('a')`应该返回null。
|
||||
`sedol('a')` should return null.
|
||||
|
||||
```js
|
||||
assert(sedol('a') === null);
|
||||
```
|
||||
|
||||
`sedol('710889')`应返回'7108899'。
|
||||
`sedol('710889')` should return '7108899'.
|
||||
|
||||
```js
|
||||
assert(sedol('710889') === '7108899');
|
||||
```
|
||||
|
||||
`sedol('BOATER')`应该返回null。
|
||||
`sedol('BOATER')` should return null.
|
||||
|
||||
```js
|
||||
assert(sedol('BOATER') === null);
|
||||
```
|
||||
|
||||
`sedol('228276')`应该返回'228276'。
|
||||
`sedol('228276')` should return '2282765'.
|
||||
|
||||
```js
|
||||
assert(sedol('228276') === '2282765');
|
||||
|
@ -89,7 +89,7 @@ function isSelfDescribing(n) {
|
||||
if (digits.length != count.length) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
for (let i=0; i< digits.length; i++){
|
||||
if (digits[i] !== count[i]) {
|
||||
return false;
|
||||
|
@ -8,7 +8,7 @@ dashedName: self-referential-sequence
|
||||
|
||||
# --description--
|
||||
|
||||
There are several ways to generate a self-referential sequence. One very common one (the [Look-and-say sequence](<https://rosettacode.org/wiki/Look-and-say sequence>)) is to start with a positive integer, then generate the next term by concatenating enumerated groups of adjacent alike digits:
|
||||
There are several ways to generate a self-referential sequence. One very common one (the [Look-and-say sequence](https://rosettacode.org/wiki/Look-and-say sequence)) is to start with a positive integer, then generate the next term by concatenating enumerated groups of adjacent alike digits:
|
||||
|
||||
<pre>0, 10, 1110, 3110, 132110, 1113122110, 311311222110 ...</pre>
|
||||
|
||||
|
@ -8,7 +8,7 @@ dashedName: sorting-algorithmscocktail-sort
|
||||
|
||||
# --description--
|
||||
|
||||
The cocktail shaker sort is an improvement on the [Bubble Sort](<https://rosettacode.org/wiki/Bubble Sort>). The improvement is basically that values "bubble" both directions through the array, because on each iteration the cocktail shaker sort bubble sorts once forwards and once backwards. Pseudocode for the algorithm (from [wikipedia](<https://en.wikipedia.org/wiki/Cocktail sort>)):
|
||||
The cocktail shaker sort is an improvement on the [Bubble Sort](https://rosettacode.org/wiki/Bubble Sort). The improvement is basically that values "bubble" both directions through the array, because on each iteration the cocktail shaker sort bubble sorts once forwards and once backwards. Pseudocode for the algorithm (from [wikipedia](https://en.wikipedia.org/wiki/Cocktail sort)):
|
||||
|
||||
<pre><b>function</b> <i>cocktailSort</i>( A : list of sortable items )
|
||||
<b>do</b>
|
||||
|
@ -10,9 +10,9 @@ dashedName: sorting-algorithmscomb-sort
|
||||
|
||||
Implement a *comb sort*.
|
||||
|
||||
The **Comb Sort** is a variant of the [Bubble Sort](<https://rosettacode.org/wiki/Bubble Sort>).
|
||||
The **Comb Sort** is a variant of the [Bubble Sort](https://rosettacode.org/wiki/Bubble Sort).
|
||||
|
||||
Like the [Shell sort](<https://rosettacode.org/wiki/Shell sort>), the Comb Sort increases the gap used in comparisons and exchanges.
|
||||
Like the [Shell sort](https://rosettacode.org/wiki/Shell sort), the Comb Sort increases the gap used in comparisons and exchanges.
|
||||
|
||||
Dividing the gap by $(1-e^{-\\varphi})^{-1} \\approx 1.247330950103979$ works best, but 1.3 may be more practical.
|
||||
|
||||
|
@ -8,7 +8,7 @@ dashedName: sorting-algorithmsgnome-sort
|
||||
|
||||
# --description--
|
||||
|
||||
Gnome sort is a sorting algorithm which is similar to [Insertion sort](<https://rosettacode.org/wiki/Insertion sort>), except that moving an element to its proper place is accomplished by a series of swaps, as in [Bubble Sort](<https://rosettacode.org/wiki/Bubble Sort>).
|
||||
Gnome sort is a sorting algorithm which is similar to [Insertion sort](https://rosettacode.org/wiki/Insertion sort), except that moving an element to its proper place is accomplished by a series of swaps, as in [Bubble Sort](https://rosettacode.org/wiki/Bubble Sort).
|
||||
|
||||
The pseudocode for the algorithm is:
|
||||
|
||||
|
@ -8,7 +8,7 @@ dashedName: sorting-algorithmspancake-sort
|
||||
|
||||
# --description--
|
||||
|
||||
Write a function to sort an array of integers (of any convenient size) into ascending order using [Pancake sorting](<https://en.wikipedia.org/wiki/Pancake sorting>). The function should return the sorted array.
|
||||
Write a function to sort an array of integers (of any convenient size) into ascending order using [Pancake sorting](https://en.wikipedia.org/wiki/Pancake sorting). The function should return the sorted array.
|
||||
|
||||
In short, instead of individual elements being sorted, the only operation allowed is to "flip" one end of the list, like so:
|
||||
|
||||
|
@ -8,7 +8,7 @@ dashedName: sorting-algorithmsshell-sort
|
||||
|
||||
# --description--
|
||||
|
||||
Write a function to sort an array of elements using the [Shell sort](<https://en.wikipedia.org/wiki/Shell sort>) algorithm, a diminishing increment sort. The function should return the sorted array.
|
||||
Write a function to sort an array of elements using the [Shell sort](https://en.wikipedia.org/wiki/Shell sort) algorithm, a diminishing increment sort. The function should return the sorted array.
|
||||
|
||||
The Shell sort (also known as Shellsort or Shell's method) is named after its inventor, Donald Shell, who published the algorithm in 1959.
|
||||
|
||||
|
@ -8,7 +8,7 @@ dashedName: sorting-algorithmsstooge-sort
|
||||
|
||||
# --description--
|
||||
|
||||
Write a function to perform [Stooge Sort](<https://en.wikipedia.org/wiki/Stooge sort>) on an array of integers. The function should return a sorted array.
|
||||
Write a function to perform [Stooge Sort](https://en.wikipedia.org/wiki/Stooge sort) on an array of integers. The function should return a sorted array.
|
||||
|
||||
The Stooge Sort algorithm is as follows:
|
||||
|
||||
|
@ -8,7 +8,7 @@ dashedName: sorting-algorithmsstrand-sort
|
||||
|
||||
# --description--
|
||||
|
||||
Write a function to sort an array using the [Strand sort](<https://en.wikipedia.org/wiki/Strand sort>). The function should return the sorted array.
|
||||
Write a function to sort an array using the [Strand sort](https://en.wikipedia.org/wiki/Strand sort). The function should return the sorted array.
|
||||
|
||||
This is a way of sorting numbers by extracting shorter sequences of already sorted numbers from an unsorted list.
|
||||
|
||||
|
@ -8,7 +8,7 @@ dashedName: stern-brocot-sequence
|
||||
|
||||
# --description--
|
||||
|
||||
For this task, the Stern-Brocot sequence is to be generated by an algorithm similar to that employed in generating the [Fibonacci sequence](<https://rosettacode.org/wiki/Fibonacci sequence>).
|
||||
For this task, the Stern-Brocot sequence is to be generated by an algorithm similar to that employed in generating the [Fibonacci sequence](https://rosettacode.org/wiki/Fibonacci sequence).
|
||||
|
||||
<ol>
|
||||
<li>The first and second members of the sequence are both 1:</li>
|
||||
@ -36,7 +36,7 @@ For this task, the Stern-Brocot sequence is to be generated by an algorithm simi
|
||||
|
||||
# --instructions--
|
||||
|
||||
Create a function that returns the $ n^{th} $ member of the sequence using the method outlined above.
|
||||
Create a function that returns the position in the Stern-Brocot sequence at which $ n $ is first encountered, where the sequence is generated with the method outlined above. Note that this sequence uses 1 based indexing.
|
||||
|
||||
# --hints--
|
||||
|
||||
|
@ -8,7 +8,7 @@ dashedName: sum-of-a-series
|
||||
|
||||
# --description--
|
||||
|
||||
Compute the **n**<sup>th</sup> term of a [series](<https://en.wikipedia.org/wiki/Series (mathematics)>), i.e. the sum of the **n** first terms of the corresponding [sequence](https://en.wikipedia.org/wiki/sequence). Informally this value, or its limit when **n** tends to infinity, is also called the *sum of the series*, thus the title of this task. For this task, use: $S*n = \\sum*{k=1}^n \\frac{1}{k^2}$ and compute $S\_{1000}$ This approximates the [zeta function](<https://en.wikipedia.org/wiki/Riemann zeta function>) for S=2, whose exact value $\\zeta(2) = {\\pi^2\\over 6}$ is the solution of the [Basel problem](<https://en.wikipedia.org/wiki/Basel problem>).
|
||||
Compute the **n**<sup>th</sup> term of a [series](https://en.wikipedia.org/wiki/Series (mathematics)), i.e. the sum of the **n** first terms of the corresponding [sequence](https://en.wikipedia.org/wiki/sequence). Informally this value, or its limit when **n** tends to infinity, is also called the *sum of the series*, thus the title of this task. For this task, use: $S*n = \\sum*{k=1}^n \\frac{1}{k^2}$ and compute $S\_{1000}$ This approximates the [zeta function](https://en.wikipedia.org/wiki/Riemann zeta function) for S=2, whose exact value $\\zeta(2) = {\\pi^2\\over 6}$ is the solution of the [Basel problem](https://en.wikipedia.org/wiki/Basel problem).
|
||||
|
||||
# --instructions--
|
||||
|
||||
|
@ -8,7 +8,7 @@ dashedName: sutherland-hodgman-polygon-clipping
|
||||
|
||||
# --description--
|
||||
|
||||
The [Sutherland-Hodgman clipping algorithm](<https://en.wikipedia.org/wiki/Sutherland-Hodgman clipping algorithm>) finds the polygon that is the intersection between an arbitrary polygon (the "subject polygon") and a convex polygon (the "clip polygon"). It is used in computer graphics (especially 2D graphics) to reduce the complexity of a scene being displayed by eliminating parts of a polygon that do not need to be displayed. Take the closed polygon defined by the points:
|
||||
The [Sutherland-Hodgman clipping algorithm](https://en.wikipedia.org/wiki/Sutherland-Hodgman clipping algorithm) finds the polygon that is the intersection between an arbitrary polygon (the "subject polygon") and a convex polygon (the "clip polygon"). It is used in computer graphics (especially 2D graphics) to reduce the complexity of a scene being displayed by eliminating parts of a polygon that do not need to be displayed. Take the closed polygon defined by the points:
|
||||
|
||||
<pre>[(50, 150), (200, 50), (350, 150), (350, 300), (250, 300), (200, 250), (150, 350), (100, 250), (100, 200)]</pre>
|
||||
|
||||
|
@ -8,7 +8,7 @@ dashedName: symmetric-difference
|
||||
|
||||
# --description--
|
||||
|
||||
Given two [set](https://rosettacode.org/wiki/set)s *A* and *B*, compute $(A \\setminus B) \\cup (B \\setminus A).$ That is, enumerate the items that are in *A* or *B* but not both. This set is called the [symmetric difference](<https://en.wikipedia.org/wiki/Symmetric difference>) of *A* and *B*. In other words: $(A \\cup B) \\setminus (A \\cap B)$ (the set of items that are in at least one of *A* or *B* minus the set of items that are in both *A* and *B*).
|
||||
Given two [set](https://rosettacode.org/wiki/set)s *A* and *B*, compute $(A \\setminus B) \\cup (B \\setminus A).$ That is, enumerate the items that are in *A* or *B* but not both. This set is called the [symmetric difference](https://en.wikipedia.org/wiki/Symmetric difference) of *A* and *B*. In other words: $(A \\cup B) \\setminus (A \\cap B)$ (the set of items that are in at least one of *A* or *B* minus the set of items that are in both *A* and *B*).
|
||||
|
||||
# --instructions--
|
||||
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user