fix(challenges): Minor changes to D challenges. Add E challenges
This commit is contained in:
@ -60,7 +60,7 @@ tests:
|
|||||||
<div id='js-seed'>
|
<div id='js-seed'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function combinations (possibleNumbers, total) {
|
function combinations(possibleNumbers, total) {
|
||||||
// Good luck!
|
// Good luck!
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -103,7 +103,7 @@ const result = [
|
|||||||
|
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function combinations (possibleNumbers, total) {
|
function combinations(possibleNumbers, total) {
|
||||||
let firstNumber;
|
let firstNumber;
|
||||||
let secondNumber;
|
let secondNumber;
|
||||||
let thridNumber;
|
let thridNumber;
|
||||||
|
@ -46,7 +46,7 @@ tests:
|
|||||||
<div id='js-seed'>
|
<div id='js-seed'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function discordianDate (date) {
|
function discordianDate(date) {
|
||||||
// Good luck!
|
// Good luck!
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -42,7 +42,7 @@ tests:
|
|||||||
<div id='js-seed'>
|
<div id='js-seed'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function dotProduct (ary1, ary2) {
|
function dotProduct(ary1, ary2) {
|
||||||
// Good luck!
|
// Good luck!
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@ -54,7 +54,7 @@ function dotProduct (ary1, ary2) {
|
|||||||
<section id='solution'>
|
<section id='solution'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function dotProduct (ary1, ary2) {
|
function dotProduct(ary1, ary2) {
|
||||||
var dotprod = 0;
|
var dotprod = 0;
|
||||||
for (var i = 0; i < ary1.length; i++)
|
for (var i = 0; i < ary1.length; i++)
|
||||||
dotprod += ary1[i] * ary2[i];
|
dotprod += ary1[i] * ary2[i];
|
||||||
|
@ -6,13 +6,16 @@ challengeType: 5
|
|||||||
|
|
||||||
## Description
|
## Description
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
<p>Implement basic element-wise matrix-matrix and scalar-matrix operations.</p><p>Implement:</p>
|
Implement basic element-wise matrix-matrix and scalar-matrix operations.
|
||||||
<p>::* addition</p>
|
<b>Implement:</b>
|
||||||
<p>::* subtraction</p>
|
<ul>
|
||||||
<p>::* multiplication</p>
|
<li>addition</li>
|
||||||
<p>::* division</p>
|
<li>subtraction</li>
|
||||||
<p>::* exponentiation</p>
|
<li>multiplication</li>
|
||||||
<p>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.
|
<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.
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
@ -52,7 +55,7 @@ tests:
|
|||||||
<div id='js-seed'>
|
<div id='js-seed'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function operation (op, arr1, arr2) {
|
function operation(op, arr1, arr2) {
|
||||||
// Good luck!
|
// Good luck!
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@ -6,13 +6,19 @@ challengeType: 5
|
|||||||
|
|
||||||
## Description
|
## Description
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
<p>An emirp (prime spelled backwards) are primes that when reversed (in their decimal representation) are a different prime.</p>
|
An emirp (<b>prime</b> spelled backwards) are primes that when reversed (in their decimal representation) are a different prime.
|
||||||
<p>Write a function that should be able to : Show the first <b>n</b> eprimes numbers.Show the eprimes numbers in a range.Show the number of eprimes in a range.Show the <b>n<sup>th</sup></b> eprimes number.<p>The function should have two parameters. The first will receive <b>n</b> or the range as an array. The second will receive a boolean, that specifies if the function returns the eprimes as an array or a single number(the number of primes in the range or the <b>n<sup>th</sup></b> prime). According to the parameters the function should return an array or a number.
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
<section id='instructions'>
|
<section id='instructions'>
|
||||||
|
Write a function that:
|
||||||
|
<ul>
|
||||||
|
<li>Shows the first <b>n</b> 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 <b>n<sup>th</sup></b> emirp number.</li>
|
||||||
|
</ul>
|
||||||
|
The function should accept two parameters. The first will receive <b>n</b> 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 <b>n<sup>th</sup></b> prime). According to the parameters the function should return an array or a number.
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
|
@ -6,9 +6,10 @@ challengeType: 5
|
|||||||
|
|
||||||
## Description
|
## Description
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
Task:
|
Calculate the Shannon entropy H of a given input string.
|
||||||
<p>Calculate the Shannon entropy H of a given input string.</p><p>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 :</p>
|
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:
|
||||||
<p>$H_2(X) = -\sum_{i=1}^n \frac{count_i}{N} \log_2 \left(\frac{count_i}{N}\right)$</p><p>where $count_i$ is the count of character $n_i$.</p>
|
$H_2(X) = -\sum_{i=1}^n \frac{count_i}{N} \log_2 \left(\frac{count_i}{N}\right)$
|
||||||
|
where $count_i$ is the count of character $n_i$.
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
@ -46,7 +47,7 @@ tests:
|
|||||||
<div id='js-seed'>
|
<div id='js-seed'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function entropy (s) {
|
function entropy(s) {
|
||||||
// Good luck!
|
// Good luck!
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@ -6,20 +6,33 @@ challengeType: 5
|
|||||||
|
|
||||||
## Description
|
## Description
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
<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.</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.
|
||||||
<p>For example, in a sequence <big>$A$</big>:</p><p>:::: <big>$A_0 = -7$</big></p>
|
For example, in a sequence <big>$A$</big>:
|
||||||
<p>:::: <big>$A_1 = 1$</big></p>
|
<ul style="list-style: none;">
|
||||||
<p>:::: <big>$A_2 = 5$</big></p>
|
<li><big>$A_0 = -7$</big></li>
|
||||||
<p>:::: <big>$A_3 = 2$</big></p>
|
<li><big>$A_1 = 1$</big></li>
|
||||||
<p>:::: <big>$A_4 = -4$</big></p>
|
<li><big>$A_2 = 5$</big></li>
|
||||||
<p>:::: <big>$A_5 = 3$</big></p>
|
<li><big>$A_3 = 2$</big></li>
|
||||||
<p>:::: <big>$A_6 = 0$</big></p><p>3 is an equilibrium index, because:</p><p>:::: <big>$A_0 + A_1 + A_2 = A_4 + A_5 + A_6$</big></p><p>6 is also an equilibrium index, because:</p><p>:::: <big>$A_0 + A_1 + A_2 + A_3 + A_4 + A_5 = 0$</big></p><p>(sum of zero elements is zero)</p><p>7 is not an equilibrium index, because it is not a valid index of sequence <big>$A$</big>.</p>
|
<li><big>$A_4 = -4$</big></li>
|
||||||
<p>Write a function that, given a sequence, returns its equilibrium indices (if any).</p><p>Assume that the sequence may be very long.</p>
|
<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 <big>$A$</big>.
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
<section id='instructions'>
|
<section id='instructions'>
|
||||||
|
Write a function that, given a sequence, returns its equilibrium indices (if any).
|
||||||
|
Assume that the sequence may be very long.
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
@ -52,7 +65,7 @@ tests:
|
|||||||
<div id='js-seed'>
|
<div id='js-seed'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function equilibrium (a) {
|
function equilibrium(a) {
|
||||||
// Good luck!
|
// Good luck!
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@ -6,53 +6,65 @@ challengeType: 5
|
|||||||
|
|
||||||
## Description
|
## Description
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
<p>Ethiopian multiplication is a method of multiplying integers using only addition, doubling, and halving.</p>
|
Ethiopian multiplication is a method of multiplying integers using only addition, doubling, and halving.
|
||||||
<p>Method: </p>
|
<b>Method:</b>
|
||||||
Take two numbers to be multiplied and write them down at the top of two columns.
|
<ol>
|
||||||
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 1.
|
<li>Take two numbers to be multiplied and write them down at the top of two columns.</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 1.
|
<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 1.</li>
|
||||||
Examine the table produced and discard any row where the value in the left column is even.
|
<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 1.</li>
|
||||||
Sum the values in the right-hand column that remain to produce the result of multiplying the original two numbers together
|
<li>Examine the table produced and discard any row where the value in the left column is even.</li>
|
||||||
<p>For example: 17 × 34</p>
|
<li>Sum the values in the right-hand column that remain to produce the result of multiplying the original two numbers together</li>
|
||||||
<p>17 34</p>
|
</ol>
|
||||||
<p>Halving the first column:</p>
|
<b>For example:</b> 17 × 34
|
||||||
<p>17 34</p>
|
<pre>
|
||||||
<p>8</p>
|
17 34
|
||||||
<p>4</p>
|
</pre>
|
||||||
<p>2</p>
|
Halving the first column:
|
||||||
<p>1</p>
|
<pre>
|
||||||
<p>Doubling the second column:</p>
|
17 34
|
||||||
<p>17 34</p>
|
8
|
||||||
<p>8 68</p>
|
4
|
||||||
<p>4 136</p>
|
2
|
||||||
<p>2 272</p>
|
1
|
||||||
<p>1 544</p>
|
</pre>
|
||||||
<p>Strike-out rows whose first cell is even:</p>
|
Doubling the second column:
|
||||||
<p>17 34</p>
|
<pre>
|
||||||
<p>8 <strike>68</strike></p>
|
17 34
|
||||||
<p>4 <strike>136</strike></p>
|
8 68
|
||||||
<p>2 <strike>272</strike></p>
|
4 136
|
||||||
<p>1 544</p>
|
2 272
|
||||||
<p>Sum the remaining numbers in the right-hand column:</p>
|
1 544
|
||||||
<p>17 34</p>
|
</pre>
|
||||||
<p>8 --</p>
|
Strike-out rows whose first cell is even:
|
||||||
<p>4 ---</p>
|
<pre>
|
||||||
<p>2 ---</p>
|
17 34
|
||||||
<p>1 544</p>
|
8 <strike>68</strike>
|
||||||
<p>====</p>
|
4 <strike>136</strike>
|
||||||
<p>578</p>
|
2 <strike>272</strike>
|
||||||
<p>So 17 multiplied by 34, by the Ethiopian method is 578.</p>
|
1 544
|
||||||
Task:
|
</pre>
|
||||||
<p>The task is to define three named functions/methods/procedures/subroutines:</p>
|
Sum the remaining numbers in the right-hand column:
|
||||||
one to halve an integer,
|
<pre>
|
||||||
one to double an integer, and
|
17 34
|
||||||
one to state if an integer is even.
|
8 --
|
||||||
<p>Use these functions to create a function that does Ethiopian multiplication.</p>
|
4 ---
|
||||||
|
2 ---
|
||||||
|
1 544
|
||||||
|
====
|
||||||
|
578
|
||||||
|
</pre>
|
||||||
|
So 17 multiplied by 34, by the Ethiopian method is 578.
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
<section id='instructions'>
|
<section id='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.
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
@ -83,7 +95,7 @@ tests:
|
|||||||
<div id='js-seed'>
|
<div id='js-seed'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function eth_mult (a, b) {
|
function eth_mult(a, b) {
|
||||||
// Good luck!
|
// Good luck!
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@ -6,28 +6,64 @@ challengeType: 5
|
|||||||
|
|
||||||
## Description
|
## Description
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
<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 <a href="https://en.wikipedia.org/wiki/Euler method" title="wp: Euler method">the wikipedia page</a>.</p><p>The ODE has to be provided in the following form:</p><p>:: <big>$\frac{dy(t)}{dt} = f(t,y(t))$</big></p><p>with an initial value</p><p>:: <big>$y(t_0) = y_0$</big></p><p>To get a numeric solution, we replace the derivative on the LHS with a finite difference approximation:</p><p>:: <big>$\frac{dy(t)}{dt} \approx \frac{y(t+h)-y(t)}{h}$</big></p><p>then solve for $y(t+h)$:</p><p>:: <big>$y(t+h) \approx y(t) + h \, \frac{dy(t)}{dt}$</big></p><p>which is the same as</p><p>:: <big>$y(t+h) \approx y(t) + h \, f(t,y(t))$</big></p><p>The iterative solution rule is then:</p><p>:: <big>$y_{n+1} = y_n + h \, f(t_n, y_n)$</big></p><p>where <big>$h$</big> 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.</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 <a href="https://en.wikipedia.org/wiki/Euler method" title="wp: Euler method" target="_blank">the wikipedia page</a>.
|
||||||
<p>Example: Newton's Cooling Law</p><p>Newton's cooling law describes how an object of initial temperature <big>$T(t_0) = T_0$</big> cools down in an environment of temperature <big>$T_R$</big>:</p><p>:: <big>$\frac{dT(t)}{dt} = -k \, \Delta T$</big></p>
|
The ODE has to be provided in the following form:
|
||||||
<p>or</p>
|
<ul style="list-style: none;">
|
||||||
<p>:: <big>$\frac{dT(t)}{dt} = -k \, (T(t) - T_R)$</big></p>
|
<li><big>$\frac{dy(t)}{dt} = f(t,y(t))$</big></li>
|
||||||
<p>It says that the cooling rate <big>$\frac{dT(t)}{dt}$</big> of the object is proportional to the current temperature difference <big>$\Delta T = (T(t) - T_R)$</big> to the surrounding environment.</p><p>The analytical solution, which we will compare to the numerical approximation, is</p>
|
</ul>
|
||||||
<p>:: <big>$T(t) = T_R + (T_0 - T_R) \; e^{-k t}$</big></p>
|
with an initial value
|
||||||
Task:
|
<ul style="list-style: none;">
|
||||||
<p>Implement a routine of Euler's method and then to use it to solve the given example of Newton's cooling law with it for three different step sizes of:</p>
|
<li><big>$y(t_0) = y_0$</big></li>
|
||||||
<p>::* 2 s</p>
|
</ul>
|
||||||
<p>::* 5 s and </p>
|
To get a numeric solution, we replace the derivative on the LHS with a finite difference approximation:
|
||||||
<p>::* 10 s </p>
|
<ul style="list-style: none;">
|
||||||
<p>and to compare with the analytical solution.</p>
|
<li><big>$\frac{dy(t)}{dt} \approx \frac{y(t+h)-y(t)}{h}$</big></li>
|
||||||
Initial values:
|
</ul>
|
||||||
<p>::* initial temperature <big>$T_0$</big> shall be 100 °C</p>
|
then solve for $y(t+h)$:
|
||||||
<p>::* room temperature <big>$T_R$</big> shall be 20 °C</p>
|
<ul style="list-style: none;">
|
||||||
<p>::* cooling constant <big>$k$</big> shall be 0.07 </p>
|
<li><big>$y(t+h) \approx y(t) + h \, \frac{dy(t)}{dt}$</big></li>
|
||||||
<p>::* time interval to calculate shall be from 0 s ──► 100 s</p>
|
</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 <big>$h$</big> 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.
|
||||||
|
<b>Example: Newton's Cooling Law</b>
|
||||||
|
Newton's cooling law describes how an object of initial temperature <big>$T(t_0) = T_0$</big> cools down in an environment of temperature <big>$T_R$</big>:
|
||||||
|
<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 <big>$\frac{dT(t)}{dt}$</big> of the object is proportional to the current temperature difference <big>$\Delta T = (T(t) - T_R)$</big> 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>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
<section id='instructions'>
|
<section id='instructions'>
|
||||||
|
Implement a routine of Euler's method and then to use it to solve the given example of Newton's cooling law with it for three different step sizes of:
|
||||||
|
<ul>
|
||||||
|
<li>2 s</li>
|
||||||
|
<li>5 s and</li>
|
||||||
|
<li>10 s</li>
|
||||||
|
</ul>
|
||||||
|
and to compare with the analytical solution.
|
||||||
|
<b>Initial values:</b>
|
||||||
|
<ul>
|
||||||
|
<li>initial temperature <big>$T_0$</big> shall be 100 °C</li>
|
||||||
|
<li>room temperature <big>$T_R$</big> shall be 20 °C</li>
|
||||||
|
<li>cooling constant <big>$k$</big> shall be 0.07</li>
|
||||||
|
<li>time interval to calculate shall be from 0s to 100 s</li>
|
||||||
|
</ul>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
@ -56,7 +92,7 @@ tests:
|
|||||||
<div id='js-seed'>
|
<div id='js-seed'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function eulersMethod (x1, y1, x2, h) {
|
function eulersMethod(x1, y1, x2, h) {
|
||||||
// Good luck!
|
// Good luck!
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@ -6,7 +6,8 @@ challengeType: 5
|
|||||||
|
|
||||||
## Description
|
## Description
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
<p>Write a function to calculate the binomial coefficient for the given value of n and k.</p><p>This formula is recommended:</p>
|
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}$
|
$\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}$
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
@ -43,7 +44,7 @@ tests:
|
|||||||
<div id='js-seed'>
|
<div id='js-seed'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function binom (n, k) {
|
function binom(n, k) {
|
||||||
// Good luck!
|
// Good luck!
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@ -6,45 +6,54 @@ challengeType: 5
|
|||||||
|
|
||||||
## Description
|
## Description
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
Task:
|
Create an interpreter for a <a href="https://en.wikipedia.org/wiki/Markov algorithm" title="wp: Markov algorithm" target="_blank">Markov Algorithm</a>.
|
||||||
<p>Create an interpreter for a <a href="https://en.wikipedia.org/wiki/Markov algorithm" title="wp: Markov algorithm">Markov Algorithm</a>.</p><p>Rules have the syntax:</p>
|
Rules have the syntax:
|
||||||
<p><ruleset> ::= ((<comment> | <rule>) <newline>+)*</p>
|
|
||||||
<p><comment> ::= # {<any character>}</p>
|
|
||||||
<p><rule> ::= <pattern> <whitespace> -> <whitespace> [.] <replacement></p>
|
|
||||||
<p><whitespace> ::= (<tab> | <space>) [<whitespace>]</p>
|
|
||||||
<p>There is one rule per line.</p><p>If there is a <b>.</b> (period) present before the <replacement>, then this is a terminating rule in which case the interpreter must halt execution.</p><p>A ruleset consists of a sequence of rules, with optional comments.</p>
|
|
||||||
<p><big><big> Rulesets </big></big></p><p>Use the following tests on entries:</p>
|
|
||||||
Ruleset 1:
|
|
||||||
<pre>
|
<pre>
|
||||||
This rules file is extracted from Wikipedia:
|
[ruleset] ::= (([comment] | [rule]) [newline]+)*
|
||||||
http://en.wikipedia.org/wiki/Markov_AlgorithmA -> apple
|
[comment] ::= # {[any character]}
|
||||||
|
[rule] ::= [pattern] [whitespace] -> [whitespace] [.] [replacement]
|
||||||
|
[whitespace] ::= ([tab] | [space]) [[whitespace]]
|
||||||
|
</pre>
|
||||||
|
There is one rule per line.
|
||||||
|
If there is a <b>.</b> (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.
|
||||||
|
<big><big>Rulesets</big></big>
|
||||||
|
Use the following tests on entries:
|
||||||
|
<b>Ruleset 1:</b>
|
||||||
|
<pre>
|
||||||
|
# This rules file is extracted from Wikipedia:
|
||||||
|
# http://en.wikipedia.org/wiki/Markov_Algorithm
|
||||||
|
A -> apple
|
||||||
B -> bag
|
B -> bag
|
||||||
S -> shop
|
S -> shop
|
||||||
T -> the
|
T -> the
|
||||||
the shop -> my brother
|
the shop -> my brother
|
||||||
a never used -> .terminating rule
|
a never used -> .terminating rule
|
||||||
</pre>
|
</pre>
|
||||||
<p>Sample text of:</p>
|
Sample text of:
|
||||||
<p> <code> I bought a B of As from T S. </code></p>
|
<code>I bought a B of As from T S.</code>
|
||||||
<p>Should generate the output:</p>
|
Should generate the output:
|
||||||
<p> <code> I bought a bag of apples from my brother. </code></p>
|
<code>I bought a bag of apples from my brother.</code>
|
||||||
Ruleset 2:
|
<b>Ruleset 2:</b>
|
||||||
<p>A test of the terminating rule</p>
|
A test of the terminating rule
|
||||||
<pre>
|
<pre>
|
||||||
Slightly modified from the rules on WikipediaA -> apple
|
# Slightly modified from the rules on Wikipedia
|
||||||
|
A -> apple
|
||||||
B -> bag
|
B -> bag
|
||||||
S -> .shop
|
S -> .shop
|
||||||
T -> the
|
T -> the
|
||||||
the shop -> my brother
|
the shop -> my brother
|
||||||
a never used -> .terminating rule</pre>
|
a never used -> .terminating rule
|
||||||
<p>Sample text of:</p>
|
</pre>
|
||||||
<p> <code>I bought a B of As from T S.</code></p>
|
Sample text of:
|
||||||
<p>Should generate:</p>
|
<code>I bought a B of As from T S.</code>
|
||||||
<p> <code>I bought a bag of apples from T shop.</code></p>
|
Should generate:
|
||||||
Ruleset 3:
|
<code>I bought a bag of apples from T shop.</code>
|
||||||
<p>This tests for correct substitution order and may trap simple regexp based replacement routines if special regexp characters are not escaped.</p>
|
<b>Ruleset 3:</b>
|
||||||
|
This tests for correct substitution order and may trap simple regexp based replacement routines if special regexp characters are not escaped.
|
||||||
<pre>
|
<pre>
|
||||||
BNF Syntax testing rulesA -> apple
|
#BNF Syntax testing rules
|
||||||
|
A -> apple
|
||||||
WWWW -> with
|
WWWW -> with
|
||||||
Bgage -> ->.*
|
Bgage -> ->.*
|
||||||
B -> bag
|
B -> bag
|
||||||
@ -55,22 +64,24 @@ T -> the
|
|||||||
the shop -> my brother
|
the shop -> my brother
|
||||||
a never used -> .terminating rule
|
a never used -> .terminating rule
|
||||||
</pre>
|
</pre>
|
||||||
<p>Sample text of:</p>
|
Sample text of:
|
||||||
<p> <code>I bought a B of As W my Bgage from T S.</code></p>
|
<code>I bought a B of As W my Bgage from T S.</code>
|
||||||
<p>Should generate:</p>
|
Should generate:
|
||||||
<p> <code>I bought a bag of apples with my money from T shop.</code></p>
|
<code>I bought a bag of apples with my money from T shop.</code>
|
||||||
Ruleset 4:
|
<b>Ruleset 4:</b>
|
||||||
<p>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.)</p>
|
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>
|
<pre>
|
||||||
## Unary Multiplication Engine, for testing Markov Algorithm implementations
|
### Unary Multiplication Engine, for testing Markov Algorithm implementations
|
||||||
## By Donal Fellows.
|
### By Donal Fellows.
|
||||||
Unary addition engine_+1 -> _1+
|
# Unary addition engine
|
||||||
|
_+1 -> _1+
|
||||||
1+1 -> 11+
|
1+1 -> 11+
|
||||||
Pass for converting from the splitting of multiplication into ordinary
|
# Pass for converting from the splitting of multiplication into ordinary
|
||||||
addition1! -> !1
|
# addition
|
||||||
|
1! -> !1
|
||||||
,! -> !+
|
,! -> !+
|
||||||
_! -> _
|
_! -> _
|
||||||
Unary multiplication by duplicating left side, right side times1*1 -> x,@y
|
# Unary multiplication by duplicating left side, right side times1*1 -> x,@y
|
||||||
1x -> xX
|
1x -> xX
|
||||||
X, -> 1,1
|
X, -> 1,1
|
||||||
X1 -> 1X
|
X1 -> 1X
|
||||||
@ -78,39 +89,46 @@ _x -> _X
|
|||||||
,x -> ,X
|
,x -> ,X
|
||||||
y1 -> 1y
|
y1 -> 1y
|
||||||
y_ -> _
|
y_ -> _
|
||||||
Next phase of applying1@1 -> x,@y
|
# Next phase of applying1@1 -> x,@y
|
||||||
1@_ -> @_
|
1@_ -> @_
|
||||||
,@_ -> !_
|
,@_ -> !_
|
||||||
++ -> +
|
++ -> +
|
||||||
Termination cleanup for addition_1 -> 1
|
# Termination cleanup for addition_1 -> 1
|
||||||
1+_ -> 1
|
1+_ -> 1
|
||||||
_+_ ->
|
_+_ ->
|
||||||
</pre>
|
</pre>
|
||||||
<p>Sample text of:</p>
|
Sample text of:
|
||||||
<p> <code> _1111*11111_ </code></p>
|
<code>_1111*11111_</code>
|
||||||
<p>should generate the output:</p>
|
should generate the output:
|
||||||
<p> <code> 11111111111111111111 </code></p>
|
<code>11111111111111111111</code>
|
||||||
Ruleset 5:
|
<b>Ruleset 5:</b>
|
||||||
<p>A simple <a href="http://en.wikipedia.org/wiki/Turing_machine" title="link: http://en.wikipedia.org/wiki/Turing_machine">Turing machine</a>,</p>
|
A simple <a href="http://en.wikipedia.org/wiki/Turing_machine" title="link: http://en.wikipedia.org/wiki/Turing_machine" target="_blank">Turing machine</a>,
|
||||||
<p>implementing a three-state <a href="http://en.wikipedia.org/wiki/Busy_beaver" title="link: http://en.wikipedia.org/wiki/Busy_beaver">busy beaver</a>.</p><p>The tape consists of 0s and 1s, the states are A, B, C and H (for Halt), and the head position is indicated by writing the state letter before the character where the head is.</p>
|
implementing a three-state <a href="http://en.wikipedia.org/wiki/Busy_beaver" title="link: http://en.wikipedia.org/wiki/Busy_beaver" target="_blank">busy beaver</a>.The tape consists of <b>0</b>s and <b>1</b>s, the states are <b>A</b>, <b>B</b>, <b>C</b> and <b>H</b> (for <b>H</b>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.
|
||||||
<p>All parts of the initial tape the machine operates on have to be given in the input.</p><p>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.</p>
|
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>
|
<pre>
|
||||||
Turing machine: three-state busy beaver
|
#Turing machine: three-state busy beaver
|
||||||
# state A, symbol 0 => write 1, move right, new state BA0 -> 1B
|
#
|
||||||
state A, symbol 1 => write 1, move left, new state C0A1 -> C01
|
# state A, symbol 0 => write 1, move right, new state B
|
||||||
|
A0 -> 1B
|
||||||
|
# state A, symbol 1 => write 1, move left, new state C
|
||||||
|
0A1 -> C01
|
||||||
1A1 -> C11
|
1A1 -> C11
|
||||||
state B, symbol 0 => write 1, move left, new state A0B0 -> A01
|
# state B, symbol 0 => write 1, move left, new state A
|
||||||
|
0B0 -> A01
|
||||||
1B0 -> A11
|
1B0 -> A11
|
||||||
state B, symbol 1 => write 1, move right, new state BB1 -> 1B
|
# state B, symbol 1 => write 1, move right, new state B
|
||||||
state C, symbol 0 => write 1, move left, new state B0C0 -> B01
|
B1 -> 1B
|
||||||
|
# state C, symbol 0 => write 1, move left, new state B
|
||||||
|
0C0 -> B01
|
||||||
1C0 -> B11
|
1C0 -> B11
|
||||||
state C, symbol 1 => write 1, move left, halt0C1 -> H01
|
# state C, symbol 1 => write 1, move left, halt
|
||||||
|
0C1 -> H01
|
||||||
1C1 -> H11
|
1C1 -> H11
|
||||||
</pre>
|
</pre>
|
||||||
<p>This ruleset should turn</p>
|
This ruleset should turn
|
||||||
<p> <code> 000000A000000 </code></p>
|
<code>000000A000000</code>
|
||||||
<p>into</p>
|
into
|
||||||
<p> <code> 00011H1111000 </code></p>
|
<code>00011H1111000</code>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
@ -146,7 +164,7 @@ tests:
|
|||||||
<div id='js-seed'>
|
<div id='js-seed'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function markov (rules,test) {
|
function markov(rules,test) {
|
||||||
// Good luck!
|
// Good luck!
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@ -6,30 +6,22 @@ challengeType: 5
|
|||||||
|
|
||||||
## Description
|
## Description
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
<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 : </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:
|
||||||
<p>RCBF is a set of <a href="http://rosettacode.org/wiki/Brainf***" title="Brainf***">Brainf***</a> compilers and interpreters written for Rosetta Code in a variety of languages.</p><p>Below are links to each of the versions of RCBF.</p><p>An implementation need only properly implement the following instructions:</p>
|
RCBF is a set of <a href="http://rosettacode.org/wiki/Brainf***" title="Brainf***">Brainf***</a> compilers and interpreters written for Rosetta Code in a variety of languages.
|
||||||
<p>{|</p>
|
Below are links to each of the versions of RCBF.
|
||||||
<p>!Command</p>
|
An implementation need only properly implement the following instructions:
|
||||||
<p>!Description</p>
|
|
||||||
<p>|-</p>
|
|
||||||
<p>| style="text-align:center"| <code>></code> || Move the pointer to the right</p>
|
|
||||||
<p>|-</p>
|
|
||||||
<p>| style="text-align:center"| <code><</code> || Move the pointer to the left</p>
|
|
||||||
<p>|-</p>
|
|
||||||
<p>| style="text-align:center"| <code>+</code> || Increment the memory cell under the pointer</p>
|
|
||||||
<p>|-</p>
|
|
||||||
<p>| style="text-align:center"| <code>-</code> || Decrement the memory cell under the pointer</p>
|
|
||||||
<p>|-</p>
|
|
||||||
<p>| style="text-align:center"| <code>.</code> || Output the character signified by the cell at the pointer</p>
|
|
||||||
<p>|-</p>
|
|
||||||
<p>| style="text-align:center"| <code>,</code> || Input a character and store it in the cell at the pointer</p>
|
|
||||||
<p>|-</p>
|
|
||||||
<p>| style="text-align:center"| <code>[</code> || Jump past the matching <code>]</code> if the cell under the pointer is 0</p>
|
|
||||||
<p>|-</p>
|
|
||||||
<p>| style="text-align:center"| <code>]</code> || Jump back to the matching <code>[</code> if the cell under the pointer is nonzero</p>
|
|
||||||
<p>|}</p>
|
|
||||||
<p>Any cell size is allowed, EOF (<u>E</u>nd-<u>O</u>-<u>F</u>ile) support is optional, as is whether you have bounded or unbounded memory.</p>
|
|
||||||
|
|
||||||
|
| 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 (<u>E</u>nd-<u>O</u>-<u>F</u>ile) support is optional, as is whether you have bounded or unbounded memory.
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
@ -63,7 +55,7 @@ tests:
|
|||||||
<div id='js-seed'>
|
<div id='js-seed'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function brain (prog) {
|
function brain(prog) {
|
||||||
// Good luck!
|
// Good luck!
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@ -6,7 +6,15 @@ challengeType: 5
|
|||||||
|
|
||||||
## Description
|
## Description
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
<p>Write a generator of prime numbers, in order, that will automatically adjust to accommodate the generation of any reasonably high prime.</p> The generator should be able to : Show the first <b>n</b> prime numbers.Show the prime numbers in a range.Show the number of primes in a range.Show the <b>n<sup>th</sup></b> prime number.<p>The function should have two parameters. The first will receive <b>n</b> 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 <b>n<sup>th</sup></b> prime). According to the parameters the function should return an array.
|
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 <b>n</b> 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 <b>n<sup>th</sup></b> prime number.</li>
|
||||||
|
</ul>
|
||||||
|
The function should have two parameters. The first will receive <b>n</b> 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 <b>n<sup>th</sup></b> prime). According to the parameters the function should return an array.
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
@ -40,7 +48,7 @@ tests:
|
|||||||
<div id='js-seed'>
|
<div id='js-seed'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function primeGenerator (num, showPrimes) {
|
function primeGenerator(num, showPrimes) {
|
||||||
// Good luck!
|
// Good luck!
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
Reference in New Issue
Block a user