fix: remove unneeded assert text (#38453)
This commit is contained in:
@ -6,40 +6,45 @@ forumTopicId: 302240
|
|||||||
---
|
---
|
||||||
|
|
||||||
## Description
|
## Description
|
||||||
|
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
|
|
||||||
Write a function that takes an array of numbers as parameter and returns the <a href="https://en.wikipedia.org/wiki/Standard Deviation">standard deviation</a> of the series.
|
Write a function that takes an array of numbers as parameter and returns the <a href="https://en.wikipedia.org/wiki/Standard Deviation">standard deviation</a> of the series.
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
|
|
||||||
<section id='instructions'>
|
<section id='instructions'>
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
|
|
||||||
<section id='tests'>
|
<section id='tests'>
|
||||||
|
|
||||||
``` yml
|
```yml
|
||||||
tests:
|
tests:
|
||||||
- text: <code>standardDeviation</code> should be a function.
|
- text: <code>standardDeviation</code> should be a function.
|
||||||
testString: assert(typeof standardDeviation == 'function', '<code>standardDeviation</code> should be a function.');
|
testString: assert(typeof standardDeviation == 'function');
|
||||||
- text: <code>standardDeviation([2, 4, 4, 4, 5, 5, 7, 9])</code> should return a number.
|
- text: <code>standardDeviation([2, 4, 4, 4, 5, 5, 7, 9])</code> should return a number.
|
||||||
testString: assert(typeof standardDeviation([2, 4, 4, 4, 5, 5, 7, 9]) == 'number', '<code>standardDeviation([2, 4, 4, 4, 5, 5, 7, 9])</code> should return a number.');
|
testString: assert(typeof standardDeviation([2, 4, 4, 4, 5, 5, 7, 9]) == 'number');
|
||||||
- text: <code>standardDeviation([2, 4, 4, 4, 5, 5, 7, 9])</code> should return <code>2</code>.
|
- text: <code>standardDeviation([2, 4, 4, 4, 5, 5, 7, 9])</code> should return <code>2</code>.
|
||||||
testString: assert.equal(standardDeviation([2, 4, 4, 4, 5, 5, 7, 9]), 2, '<code>standardDeviation([2, 4, 4, 4, 5, 5, 7, 9])</code> should return <code>2</code>.');
|
testString: assert.equal(standardDeviation([2, 4, 4, 4, 5, 5, 7, 9]), 2);
|
||||||
- text: <code>standardDeviation([600, 470, 170, 430, 300])</code> should return <code>147.323</code>.
|
- text: <code>standardDeviation([600, 470, 170, 430, 300])</code> should return <code>147.323</code>.
|
||||||
testString: assert.equal(standardDeviation([600, 470, 170, 430, 300]), 147.323, '<code>standardDeviation([600, 470, 170, 430, 300])</code> should return <code>147.323</code>.');
|
testString: assert.equal(standardDeviation([600, 470, 170, 430, 300]), 147.323);
|
||||||
- text: <code>standardDeviation([75, 83, 96, 100, 121, 125])</code> should return <code>18.239</code>.
|
- text: <code>standardDeviation([75, 83, 96, 100, 121, 125])</code> should return <code>18.239</code>.
|
||||||
testString: assert.equal(standardDeviation([75, 83, 96, 100, 121, 125]), 18.239, '<code>standardDeviation([75, 83, 96, 100, 121, 125])</code> should return <code>18.239</code>.');
|
testString: assert.equal(standardDeviation([75, 83, 96, 100, 121, 125]), 18.239);
|
||||||
- text: <code>standardDeviation([23, 37, 45, 49, 56, 63, 63, 70, 72, 82])</code> should return <code>16.87</code>.
|
- text: <code>standardDeviation([23, 37, 45, 49, 56, 63, 63, 70, 72, 82])</code> should return <code>16.87</code>.
|
||||||
testString: assert.equal(standardDeviation([23, 37, 45, 49, 56, 63, 63, 70, 72, 82]), 16.87, '<code>standardDeviation([23, 37, 45, 49, 56, 63, 63, 70, 72, 82])</code> should return <code>16.87</code>.');
|
testString: assert.equal(standardDeviation([23, 37, 45, 49, 56, 63, 63, 70, 72, 82]), 16.87);
|
||||||
- text: <code>standardDeviation([271, 354, 296, 301, 333, 326, 285, 298, 327, 316, 287, 314])</code> should return <code>22.631</code>.
|
- text: <code>standardDeviation([271, 354, 296, 301, 333, 326, 285, 298, 327, 316, 287, 314])</code> should return <code>22.631</code>.
|
||||||
testString: assert.equal(standardDeviation([271, 354, 296, 301, 333, 326, 285, 298, 327, 316, 287, 314]), 22.631, '<code>standardDeviation([271, 354, 296, 301, 333, 326, 285, 298, 327, 316, 287, 314])</code> should return <code>22.631</code>.');
|
testString: assert.equal(standardDeviation([271, 354, 296, 301, 333, 326, 285, 298, 327, 316, 287, 314]), 22.631);
|
||||||
```
|
```
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Challenge Seed
|
## Challenge Seed
|
||||||
|
|
||||||
<section id='challengeSeed'>
|
<section id='challengeSeed'>
|
||||||
|
|
||||||
<div id='js-seed'>
|
<div id='js-seed'>
|
||||||
@ -54,6 +59,7 @@ function standardDeviation(arr) {
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Solution
|
## Solution
|
||||||
|
|
||||||
<section id='solution'>
|
<section id='solution'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
@ -64,10 +70,10 @@ function standardDeviation(arr) {
|
|||||||
arr.forEach(function(e) {
|
arr.forEach(function(e) {
|
||||||
sum += e;
|
sum += e;
|
||||||
sum_sq += e * e;
|
sum_sq += e * e;
|
||||||
})
|
});
|
||||||
|
|
||||||
var std_dev=Math.sqrt((sum_sq / n) - Math.pow(sum / n, 2))
|
var std_dev = Math.sqrt(sum_sq / n - Math.pow(sum / n, 2));
|
||||||
return Math.round(std_dev*1000)/1000;
|
return Math.round(std_dev * 1000) / 1000;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -6,46 +6,51 @@ forumTopicId: 302241
|
|||||||
---
|
---
|
||||||
|
|
||||||
## Description
|
## Description
|
||||||
|
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
|
|
||||||
A <b>CUSIP</b> is a nine-character alphanumeric code that identifies a North American financial security for the purposes of facilitating clearing and settlement of trades. The CUSIP was adopted as an American National Standard under Accredited Standards X9.6.
|
A <b>CUSIP</b> is a nine-character alphanumeric code that identifies a North American financial security for the purposes of facilitating clearing and settlement of trades. The CUSIP was adopted as an American National Standard under Accredited Standards X9.6.
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
|
|
||||||
<section id='instructions'>
|
<section id='instructions'>
|
||||||
Write a function that takes a string as a parameter and checks if the string is valid CUSIP.
|
Write a function that takes a string as a parameter and checks if the string is valid CUSIP.
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
|
|
||||||
<section id='tests'>
|
<section id='tests'>
|
||||||
|
|
||||||
``` yml
|
```yml
|
||||||
tests:
|
tests:
|
||||||
- text: <code>isCusip</code> should be a function.
|
- text: <code>isCusip</code> should be a function.
|
||||||
testString: assert(typeof isCusip == 'function', '<code>isCusip</code> should be a function.');
|
testString: assert(typeof isCusip == 'function');
|
||||||
- text: <code>isCusip("037833100")</code> should return a boolean.
|
- text: <code>isCusip("037833100")</code> should return a boolean.
|
||||||
testString: assert(typeof isCusip("037833100") == 'boolean', '<code>isCusip("037833100")</code> should return a boolean.');
|
testString: assert(typeof isCusip("037833100") == 'boolean');
|
||||||
- text: <code>isCusip("037833100")</code> should return <code>true</code>.
|
- text: <code>isCusip("037833100")</code> should return <code>true</code>.
|
||||||
testString: assert.equal(isCusip("037833100"), true, '<code>isCusip("037833100")</code> should return <code>true</code>.');
|
testString: assert.equal(isCusip("037833100"), true);
|
||||||
- text: <code>isCusip("17275R102")</code> should return <code>true</code>.
|
- text: <code>isCusip("17275R102")</code> should return <code>true</code>.
|
||||||
testString: assert.equal(isCusip("17275R102"), true, '<code>isCusip("17275R102")</code> should return <code>true</code>.');
|
testString: assert.equal(isCusip("17275R102"), true);
|
||||||
- text: <code>isCusip("38259P50a")</code> should return <code>false</code>.
|
- text: <code>isCusip("38259P50a")</code> should return <code>false</code>.
|
||||||
testString: assert.equal(isCusip("38259P50a"), false, '<code>isCusip("38259P50a")</code> should return <code>false</code>.');
|
testString: assert.equal(isCusip("38259P50a"), false);
|
||||||
- text: <code>isCusip("38259P508")</code> should return <code>true</code>.
|
- text: <code>isCusip("38259P508")</code> should return <code>true</code>.
|
||||||
testString: assert.equal(isCusip("38259P508"), true, '<code>isCusip("38259P508")</code> should return <code>true</code>.');
|
testString: assert.equal(isCusip("38259P508"), true);
|
||||||
- text: <code>isCusip("38259P50#")</code> should return <code>false</code>.
|
- text: <code>isCusip("38259P50#")</code> should return <code>false</code>.
|
||||||
testString: assert.equal(isCusip("38259P50#"), false, '<code>isCusip("38259P50#")</code> should return <code>false</code>.');
|
testString: assert.equal(isCusip("38259P50#"), false);
|
||||||
- text: <code>isCusip("68389X105")</code> should return <code>true</code>.
|
- text: <code>isCusip("68389X105")</code> should return <code>true</code>.
|
||||||
testString: assert.equal(isCusip("68389X105"), true, '<code>isCusip("68389X105")</code> should return <code>true</code>.');
|
testString: assert.equal(isCusip("68389X105"), true);
|
||||||
- text: <code>isCusip("68389X106")</code> should return <code>false</code>.
|
- text: <code>isCusip("68389X106")</code> should return <code>false</code>.
|
||||||
testString: assert.equal(isCusip("68389X106"), false, '<code>isCusip("68389X106")</code> should return <code>false</code>.');
|
testString: assert.equal(isCusip("68389X106"), false);
|
||||||
- text: <code>isCusip("5949181")</code> should return <code>false</code>.
|
- text: <code>isCusip("5949181")</code> should return <code>false</code>.
|
||||||
testString: assert.equal(isCusip("5949181"), false, '<code>isCusip("5949181")</code> should return <code>false</code>.');
|
testString: assert.equal(isCusip("5949181"), false);
|
||||||
```
|
```
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Challenge Seed
|
## Challenge Seed
|
||||||
|
|
||||||
<section id='challengeSeed'>
|
<section id='challengeSeed'>
|
||||||
|
|
||||||
<div id='js-seed'>
|
<div id='js-seed'>
|
||||||
@ -60,6 +65,7 @@ function isCusip(s) {
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Solution
|
## Solution
|
||||||
|
|
||||||
<section id='solution'>
|
<section id='solution'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
@ -85,7 +91,7 @@ function isCusip(s) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (i % 2 == 1) v *= 2; // check if odd as using 0-based indexing
|
if (i % 2 == 1) v *= 2; // check if odd as using 0-based indexing
|
||||||
sum += Math.floor(v / 10) + v % 10;
|
sum += Math.floor(v / 10) + (v % 10);
|
||||||
}
|
}
|
||||||
return s.charCodeAt(8) - 48 == (10 - (sum % 10)) % 10;
|
return s.charCodeAt(8) - 48 == (10 - (sum % 10)) % 10;
|
||||||
}
|
}
|
||||||
|
@ -6,9 +6,11 @@ forumTopicId: 302242
|
|||||||
---
|
---
|
||||||
|
|
||||||
## Description
|
## Description
|
||||||
|
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
|
|
||||||
A given rectangle is made from <i>m</i> × <i>n</i> squares. If <i>m</i> and <i>n</i> are not both odd, then it is possible to cut a path through the rectangle along the square edges such that the rectangle splits into two connected pieces with the same shape (after rotating one of the pieces by 180°). All such paths for 2 × 2 and 4 × 3 rectangles are shown below.
|
A given rectangle is made from <i>m</i> × <i>n</i> squares. If <i>m</i> and <i>n</i> are not both odd, then it is possible to cut a path through the rectangle along the square edges such that the rectangle splits into two connected pieces with the same shape (after rotating one of the pieces by 180°). All such paths for 2 × 2 and 4 × 3 rectangles are shown below.
|
||||||
|
|
||||||
<div style="width: 100%; text-align: center;">
|
<div style="width: 100%; text-align: center;">
|
||||||
<svg xmlns="https://www.w3.org/2000/svg" xmlns:xlink="https://www.w3.org/1999/xlink" width="520" height="170" aria-hidden="true" alt="Diagram showing the possible paths for 2 by 2 and 4 by 3 rectangles">
|
<svg xmlns="https://www.w3.org/2000/svg" xmlns:xlink="https://www.w3.org/1999/xlink" width="520" height="170" aria-hidden="true" alt="Diagram showing the possible paths for 2 by 2 and 4 by 3 rectangles">
|
||||||
<style>
|
<style>
|
||||||
@ -60,34 +62,37 @@ A given rectangle is made from <i>m</i> × <i>n</i> squares. If <i>m</i> and <i>
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
|
|
||||||
<section id='instructions'>
|
<section id='instructions'>
|
||||||
Write a function that calculates the number of different ways to cut an <i>m</i> × <i>n</i> rectangle.
|
Write a function that calculates the number of different ways to cut an <i>m</i> × <i>n</i> rectangle.
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
|
|
||||||
<section id='tests'>
|
<section id='tests'>
|
||||||
|
|
||||||
``` yml
|
```yml
|
||||||
tests:
|
tests:
|
||||||
- text: <code>cutRectangle</code> should be a function.
|
- text: <code>cutRectangle</code> should be a function.
|
||||||
testString: assert(typeof cutRectangle == 'function', '<code>cutRectangle</code> should be a function.');
|
testString: assert(typeof cutRectangle == 'function');
|
||||||
- text: <code>cutRectangle(2, 2)</code> should return a number.
|
- text: <code>cutRectangle(2, 2)</code> should return a number.
|
||||||
testString: assert(typeof cutRectangle(2, 2) == 'number', '<code>cutRectangle(2, 2)</code> should return a number.');
|
testString: assert(typeof cutRectangle(2, 2) == 'number');
|
||||||
- text: <code>cutRectangle(2, 2)</code> should return <code>2</code>.
|
- text: <code>cutRectangle(2, 2)</code> should return <code>2</code>.
|
||||||
testString: assert.equal(cutRectangle(2, 2), 2, '<code>cutRectangle(2, 2)</code> should return <code>2</code>.');
|
testString: assert.equal(cutRectangle(2, 2), 2);
|
||||||
- text: <code>cutRectangle(4, 3)</code> should return <code>9</code>.
|
- text: <code>cutRectangle(4, 3)</code> should return <code>9</code>.
|
||||||
testString: assert.equal(cutRectangle(4, 3), 9, '<code>cutRectangle(4, 3)</code> should return <code>9</code>.');
|
testString: assert.equal(cutRectangle(4, 3), 9);
|
||||||
- text: <code>cutRectangle(4, 4)</code> should return <code>22</code>.
|
- text: <code>cutRectangle(4, 4)</code> should return <code>22</code>.
|
||||||
testString: assert.equal(cutRectangle(4, 4), 22, '<code>cutRectangle(4, 4)</code> should return <code>22</code>.');
|
testString: assert.equal(cutRectangle(4, 4), 22);
|
||||||
- text: <code>cutRectangle(8, 3)</code> should return <code>53</code>.
|
- text: <code>cutRectangle(8, 3)</code> should return <code>53</code>.
|
||||||
testString: assert.equal(cutRectangle(8, 3), 53, '<code>cutRectangle(8, 3)</code> should return <code>53</code>.');
|
testString: assert.equal(cutRectangle(8, 3), 53);
|
||||||
- text: <code>cutRectangle(7, 4)</code> should return <code>151</code>.
|
- text: <code>cutRectangle(7, 4)</code> should return <code>151</code>.
|
||||||
testString: assert.equal(cutRectangle(7, 4), 151, '<code>cutRectangle(7, 4)</code> should return <code>151</code>.');
|
testString: assert.equal(cutRectangle(7, 4), 151);
|
||||||
```
|
```
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Challenge Seed
|
## Challenge Seed
|
||||||
|
|
||||||
<section id='challengeSeed'>
|
<section id='challengeSeed'>
|
||||||
|
|
||||||
<div id='js-seed'>
|
<div id='js-seed'>
|
||||||
@ -102,23 +107,23 @@ function cutRectangle(w, h) {
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Solution
|
## Solution
|
||||||
|
|
||||||
<section id='solution'>
|
<section id='solution'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function cutRectangle(w, h) {
|
function cutRectangle(w, h) {
|
||||||
if (w % 2 == 1 && h % 2 == 1)
|
if (w % 2 == 1 && h % 2 == 1) return;
|
||||||
return;
|
|
||||||
|
|
||||||
var dirs = [[0, -1], [-1, 0], [0, 1], [1, 0]];
|
var dirs = [[0, -1], [-1, 0], [0, 1], [1, 0]];
|
||||||
|
|
||||||
var grid = new Array(h); for (var i = 0; i < grid.length; i++) grid[i]=new Array(w);
|
var grid = new Array(h);
|
||||||
|
for (var i = 0; i < grid.length; i++) grid[i] = new Array(w);
|
||||||
var stack = [];
|
var stack = [];
|
||||||
|
|
||||||
var half = Math.floor((w * h) / 2);
|
var half = Math.floor((w * h) / 2);
|
||||||
var bits = Math.pow(2, half) - 1;
|
var bits = Math.pow(2, half) - 1;
|
||||||
var result=0;
|
var result = 0;
|
||||||
for (; bits > 0; bits -= 2) {
|
for (; bits > 0; bits -= 2) {
|
||||||
|
|
||||||
for (var i = 0; i < half; i++) {
|
for (var i = 0; i < half; i++) {
|
||||||
var r = Math.floor(i / w);
|
var r = Math.floor(i / w);
|
||||||
var c = i % w;
|
var c = i % w;
|
||||||
@ -129,8 +134,7 @@ function cutRectangle(w, h) {
|
|||||||
stack.push(0);
|
stack.push(0);
|
||||||
grid[0][0] = 2;
|
grid[0][0] = 2;
|
||||||
var count = 1;
|
var count = 1;
|
||||||
while (stack.length!=0) {
|
while (stack.length != 0) {
|
||||||
|
|
||||||
var pos = stack.pop();
|
var pos = stack.pop();
|
||||||
var r = Math.floor(pos / w);
|
var r = Math.floor(pos / w);
|
||||||
var c = pos % w;
|
var c = pos % w;
|
||||||
@ -140,7 +144,6 @@ function cutRectangle(w, h) {
|
|||||||
var nextC = c + dir[1];
|
var nextC = c + dir[1];
|
||||||
|
|
||||||
if (nextR >= 0 && nextR < h && nextC >= 0 && nextC < w) {
|
if (nextR >= 0 && nextR < h && nextC >= 0 && nextC < w) {
|
||||||
|
|
||||||
if (grid[nextR][nextC] == 1) {
|
if (grid[nextR][nextC] == 1) {
|
||||||
stack.push(nextR * w + nextC);
|
stack.push(nextR * w + nextC);
|
||||||
grid[nextR][nextC] = 2;
|
grid[nextR][nextC] = 2;
|
||||||
|
@ -6,40 +6,45 @@ forumTopicId: 302251
|
|||||||
---
|
---
|
||||||
|
|
||||||
## Description
|
## Description
|
||||||
|
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
|
|
||||||
Create a function, to compute the <b><a href="https://en.wikipedia.org/wiki/Dot product">dot product</a></b>, also known as the <b>scalar product</b> of two vectors.
|
Create a function, to compute the <b><a href="https://en.wikipedia.org/wiki/Dot product">dot product</a></b>, also known as the <b>scalar product</b> of two vectors.
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
|
|
||||||
<section id='instructions'>
|
<section id='instructions'>
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
|
|
||||||
<section id='tests'>
|
<section id='tests'>
|
||||||
|
|
||||||
``` yml
|
```yml
|
||||||
tests:
|
tests:
|
||||||
- text: <code>dotProduct</code> should be a function.
|
- text: <code>dotProduct</code> should be a function.
|
||||||
testString: assert(typeof dotProduct == 'function', '<code>dotProduct</code> should be a function.');
|
testString: assert(typeof dotProduct == 'function');
|
||||||
- text: <code>dotProduct([1, 3, -5], [4, -2, -1])</code> should return a number.
|
- text: <code>dotProduct([1, 3, -5], [4, -2, -1])</code> should return a number.
|
||||||
testString: assert(typeof dotProduct([1, 3, -5], [4, -2, -1]) == 'number', '<code>dotProduct([1, 3, -5], [4, -2, -1])</code> should return a number.');
|
testString: assert(typeof dotProduct([1, 3, -5], [4, -2, -1]) == 'number');
|
||||||
- text: <code>dotProduct([1, 3, -5], [4, -2, -1])</code> should return <code>3</code>.
|
- text: <code>dotProduct([1, 3, -5], [4, -2, -1])</code> should return <code>3</code>.
|
||||||
testString: assert.equal(dotProduct([1, 3, -5], [4, -2, -1]), 3, '<code>dotProduct([1, 3, -5], [4, -2, -1])</code> should return <code>3</code>.');
|
testString: assert.equal(dotProduct([1, 3, -5], [4, -2, -1]), 3);
|
||||||
- text: <code>dotProduct([1, 2, 3, 4, 5], [6, 7, 8, 9, 10])</code> should return <code>130</code>.
|
- text: <code>dotProduct([1, 2, 3, 4, 5], [6, 7, 8, 9, 10])</code> should return <code>130</code>.
|
||||||
testString: assert.equal(dotProduct([1, 2, 3, 4, 5], [6, 7, 8, 9, 10]), 130, '<code>dotProduct([1, 2, 3, 4, 5], [6, 7, 8, 9, 10])</code> should return <code>130</code>.');
|
testString: assert.equal(dotProduct([1, 2, 3, 4, 5], [6, 7, 8, 9, 10]), 130);
|
||||||
- text: <code>dotProduct([5, 4, 3, 2], [7, 8, 9, 6])</code> should return <code>106</code>.
|
- text: <code>dotProduct([5, 4, 3, 2], [7, 8, 9, 6])</code> should return <code>106</code>.
|
||||||
testString: assert.equal(dotProduct([5, 4, 3, 2], [7, 8, 9, 6]), 106, '<code>dotProduct([5, 4, 3, 2], [7, 8, 9, 6])</code> should return <code>106</code>.');
|
testString: assert.equal(dotProduct([5, 4, 3, 2], [7, 8, 9, 6]), 106);
|
||||||
- text: <code>dotProduct([-5, 4, -3, 2], [-7, -8, 9, -6])</code> should return <code>-36</code>.
|
- text: <code>dotProduct([-5, 4, -3, 2], [-7, -8, 9, -6])</code> should return <code>-36</code>.
|
||||||
testString: assert.equal(dotProduct([-5, 4, -3, 2], [-7, -8, 9, -6]), -36, '<code>dotProduct([-5, 4, -3, 2], [-7, -8, 9, -6])</code> should return <code>-36</code>.');
|
testString: assert.equal(dotProduct([-5, 4, -3, 2], [-7, -8, 9, -6]), -36);
|
||||||
- text: <code>dotProduct([17, 27, 34, 43, 15], [62, 73, 48, 95, 110])</code> should return <code>10392</code>.
|
- text: <code>dotProduct([17, 27, 34, 43, 15], [62, 73, 48, 95, 110])</code> should return <code>10392</code>.
|
||||||
testString: assert.equal(dotProduct([17, 27, 34, 43, 15], [62, 73, 48, 95, 110]), 10392, '<code>dotProduct([17, 27, 34, 43, 15], [62, 73, 48, 95, 110])</code> should return <code>10392</code>.');
|
testString: assert.equal(dotProduct([17, 27, 34, 43, 15], [62, 73, 48, 95, 110]), 10392);
|
||||||
```
|
```
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Challenge Seed
|
## Challenge Seed
|
||||||
|
|
||||||
<section id='challengeSeed'>
|
<section id='challengeSeed'>
|
||||||
|
|
||||||
<div id='js-seed'>
|
<div id='js-seed'>
|
||||||
@ -54,14 +59,14 @@ function dotProduct(ary1, ary2) {
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Solution
|
## Solution
|
||||||
|
|
||||||
<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];
|
return dotprod;
|
||||||
return dotprod;
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -6,46 +6,52 @@ forumTopicId: 302295
|
|||||||
---
|
---
|
||||||
|
|
||||||
## Description
|
## Description
|
||||||
|
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
|
|
||||||
A k-d tree (short for <i>k</i>-dimensional tree) is a space-partitioning data structure for organizing points in a k-dimensional space.
|
A k-d tree (short for <i>k</i>-dimensional tree) is a space-partitioning data structure for organizing points in a k-dimensional space.
|
||||||
k-d trees are a useful data structure for several applications, such as searches involving a multidimensional search key (e.g. range searches and nearest neighbor searches).
|
k-d trees are a useful data structure for several applications, such as searches involving a multidimensional search key (e.g. range searches and nearest neighbor searches).
|
||||||
k-d trees are a special case of binary space partitioning trees. k-d trees are not suitable, however, for efficiently finding the nearest neighbor in high dimensional spaces. As a general rule, if the dimensionality is <i>k</i>, the number of points in the data, <i>N</i>, should be <i>N</i> ≫ 2<sup><i>k</i></sup>.
|
k-d trees are a special case of binary space partitioning trees. k-d trees are not suitable, however, for efficiently finding the nearest neighbor in high dimensional spaces. As a general rule, if the dimensionality is <i>k</i>, the number of points in the data, <i>N</i>, should be <i>N</i> ≫ 2<sup><i>k</i></sup>.
|
||||||
Otherwise, when k-d trees are used with high-dimensional data, most of the points in the tree will be evaluated and the efficiency is no better than exhaustive search, and other methods such as approximate nearest-neighbor are used instead.
|
Otherwise, when k-d trees are used with high-dimensional data, most of the points in the tree will be evaluated and the efficiency is no better than exhaustive search, and other methods such as approximate nearest-neighbor are used instead.
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
|
|
||||||
<section id='instructions'>
|
<section id='instructions'>
|
||||||
|
|
||||||
Write a function to perform a nearest neighbour search using k-d tree. The function takes two parameters: an array of k-dimensional points, and a single k-dimensional point whose nearest neighbour should be returned by the function. A k-dimensional point will be given as an array of k elements.
|
Write a function to perform a nearest neighbour search using k-d tree. The function takes two parameters: an array of k-dimensional points, and a single k-dimensional point whose nearest neighbour should be returned by the function. A k-dimensional point will be given as an array of k elements.
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
|
|
||||||
<section id='tests'>
|
<section id='tests'>
|
||||||
|
|
||||||
``` yml
|
```yml
|
||||||
tests:
|
tests:
|
||||||
- text: <code>kdNN</code> should be a function.
|
- text: <code>kdNN</code> should be a function.
|
||||||
testString: assert(typeof kdNN == 'function', '<code>kdNN</code> should be a function.');
|
testString: assert(typeof kdNN == 'function');
|
||||||
- text: <code>kdNN([[[2, 3], [5, 4], [9, 6], [4, 7], [8, 1], [7, 2]], [9, 2])</code> should return an array.
|
- text: <code>kdNN([[[2, 3], [5, 4], [9, 6], [4, 7], [8, 1], [7, 2]], [9, 2])</code> should return an array.
|
||||||
testString: assert(Array.isArray(kdNN([[2, 3], [5, 4], [9, 6], [4, 7], [8, 1], [7, 2]], [9, 2])), '<code>kdNN([[[2, 3], [5, 4], [9, 6], [4, 7], [8, 1], [7, 2]], [9, 2])</code> should return an array.');
|
testString: assert(Array.isArray(kdNN([[2, 3], [5, 4], [9, 6], [4, 7], [8, 1], [7, 2]], [9, 2])));
|
||||||
- text: <code>kdNN([[[2, 3], [5, 4], [9, 6], [4, 7], [8, 1], [7, 2]], [9, 2])</code> should return <code>[ 8, 1 ]</code>.
|
- text: <code>kdNN([[[2, 3], [5, 4], [9, 6], [4, 7], [8, 1], [7, 2]], [9, 2])</code> should return <code>[ 8, 1 ]</code>.
|
||||||
testString: assert.deepEqual(kdNN([[2, 3], [5, 4], [9, 6], [4, 7], [8, 1], [7, 2]], [9, 2]), [8, 1], '<code>kdNN([[[2, 3], [5, 4], [9, 6], [4, 7], [8, 1], [7, 2]], [9, 2])</code> should return <code>[ 8, 1 ]</code>.');
|
testString: assert.deepEqual(kdNN([[2, 3], [5, 4], [9, 6], [4, 7], [8, 1], [7, 2]], [9, 2]), [8, 1]);
|
||||||
- text: <code>kdNN([[[2, 3], [5, 4], [9, 6], [4, 7], [8, 1], [7, 2]], [7, 1])</code> should return <code>[ 8, 1 ]</code>.
|
- text: <code>kdNN([[[2, 3], [5, 4], [9, 6], [4, 7], [8, 1], [7, 2]], [7, 1])</code> should return <code>[ 8, 1 ]</code>.
|
||||||
testString: assert.deepEqual(kdNN([[2, 3], [5, 4], [9, 6], [4, 7], [8, 1], [7, 2]], [7, 1]), [8, 1], '<code>kdNN([[[2, 3], [5, 4], [9, 6], [4, 7], [8, 1], [7, 2]], [7, 1])</code> should return <code>[ 8, 1 ]</code>.');
|
testString: assert.deepEqual(kdNN([[2, 3], [5, 4], [9, 6], [4, 7], [8, 1], [7, 2]], [7, 1]), [8, 1]);
|
||||||
- text: <code>kdNN([[[2, 3], [5, 4], [9, 6], [4, 7], [8, 1], [7, 2]], [3, 2])</code> should return <code>[ 2, 3 ]</code>.
|
- text: <code>kdNN([[[2, 3], [5, 4], [9, 6], [4, 7], [8, 1], [7, 2]], [3, 2])</code> should return <code>[ 2, 3 ]</code>.
|
||||||
testString: assert.deepEqual(kdNN([[2, 3], [5, 4], [9, 6], [4, 7], [8, 1], [7, 2]], [3, 2]), [2, 3], '<code>kdNN([[[2, 3], [5, 4], [9, 6], [4, 7], [8, 1], [7, 2]], [3, 2])</code> should return <code>[ 2, 3 ]</code>.');
|
testString: assert.deepEqual(kdNN([[2, 3], [5, 4], [9, 6], [4, 7], [8, 1], [7, 2]], [3, 2]), [2, 3]);
|
||||||
- text: <code>kdNN([[2, 3, 1], [9, 4, 5], [4, 6, 7], [1, 2, 5], [7, 8, 9], [3, 6, 1]], [1, 2, 3])</code> should return <code>[ 1, 2, 5 ]</code>.
|
- text: <code>kdNN([[2, 3, 1], [9, 4, 5], [4, 6, 7], [1, 2, 5], [7, 8, 9], [3, 6, 1]], [1, 2, 3])</code> should return <code>[ 1, 2, 5 ]</code>.
|
||||||
testString: assert.deepEqual(kdNN([[2, 3, 1], [9, 4, 5], [4, 6, 7], [1, 2, 5], [7, 8, 9], [3, 6, 1]], [1, 2, 3]), [1, 2, 5], '<code>kdNN([[2, 3, 1], [9, 4, 5], [4, 6, 7], [1, 2, 5], [7, 8, 9], [3, 6, 1]], [1, 2, 3])</code> should return <code>[ 1, 2, 5 ]</code>.');
|
testString: assert.deepEqual(kdNN([[2, 3, 1], [9, 4, 5], [4, 6, 7], [1, 2, 5], [7, 8, 9], [3, 6, 1]], [1, 2, 3]), [1, 2, 5]);
|
||||||
- text: <code>kdNN([[2, 3, 1], [9, 4, 5], [4, 6, 7], [1, 2, 5], [7, 8, 9], [3, 6, 1]], [4, 5, 6])</code> should return <code>[ 4, 6, 7 ]</code>.
|
- text: <code>kdNN([[2, 3, 1], [9, 4, 5], [4, 6, 7], [1, 2, 5], [7, 8, 9], [3, 6, 1]], [4, 5, 6])</code> should return <code>[ 4, 6, 7 ]</code>.
|
||||||
testString: assert.deepEqual(kdNN([[2, 3, 1], [9, 4, 5], [4, 6, 7], [1, 2, 5], [7, 8, 9], [3, 6, 1]], [4, 5, 6]), [4, 6, 7], '<code>kdNN([[2, 3, 1], [9, 4, 5], [4, 6, 7], [1, 2, 5], [7, 8, 9], [3, 6, 1]], [4, 5, 6])</code> should return <code>[ 4, 6, 7 ]</code>.');
|
testString: assert.deepEqual(kdNN([[2, 3, 1], [9, 4, 5], [4, 6, 7], [1, 2, 5], [7, 8, 9], [3, 6, 1]], [4, 5, 6]), [4, 6, 7]);
|
||||||
- text: <code>kdNN([[2, 3, 1], [9, 4, 5], [4, 6, 7], [1, 2, 5], [7, 8, 9], [3, 6, 1]], [8, 8, 8])</code> should return <code>[ 7, 8, 9 ]</code>.
|
- text: <code>kdNN([[2, 3, 1], [9, 4, 5], [4, 6, 7], [1, 2, 5], [7, 8, 9], [3, 6, 1]], [8, 8, 8])</code> should return <code>[ 7, 8, 9 ]</code>.
|
||||||
testString: assert.deepEqual(kdNN([[2, 3, 1], [9, 4, 5], [4, 6, 7], [1, 2, 5], [7, 8, 9], [3, 6, 1]], [8, 8, 8]), [7, 8, 9], '<code>kdNN([[2, 3, 1], [9, 4, 5], [4, 6, 7], [1, 2, 5], [7, 8, 9], [3, 6, 1]], [8, 8, 8])</code> should return <code>[ 7, 8, 9 ]</code>.');
|
testString: assert.deepEqual(kdNN([[2, 3, 1], [9, 4, 5], [4, 6, 7], [1, 2, 5], [7, 8, 9], [3, 6, 1]], [8, 8, 8]), [7, 8, 9]);
|
||||||
```
|
```
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Challenge Seed
|
## Challenge Seed
|
||||||
|
|
||||||
<section id='challengeSeed'>
|
<section id='challengeSeed'>
|
||||||
|
|
||||||
<div id='js-seed'>
|
<div id='js-seed'>
|
||||||
@ -60,6 +66,7 @@ function kdNN(fpoints, fpoint) {
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Solution
|
## Solution
|
||||||
|
|
||||||
<section id='solution'>
|
<section id='solution'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
@ -73,7 +80,6 @@ function kdNN(fpoints, fpoint) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function kdTree(points, metric, dimensions) {
|
function kdTree(points, metric, dimensions) {
|
||||||
|
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
function buildTree(points, depth, parent) {
|
function buildTree(points, depth, parent) {
|
||||||
@ -88,7 +94,7 @@ function kdNN(fpoints, fpoint) {
|
|||||||
return new Node(points[0], dim, parent);
|
return new Node(points[0], dim, parent);
|
||||||
}
|
}
|
||||||
|
|
||||||
points.sort(function (a, b) {
|
points.sort(function(a, b) {
|
||||||
return a[dimensions[dim]] - b[dimensions[dim]];
|
return a[dimensions[dim]] - b[dimensions[dim]];
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -102,9 +108,8 @@ function kdNN(fpoints, fpoint) {
|
|||||||
|
|
||||||
this.root = buildTree(points, 0, null);
|
this.root = buildTree(points, 0, null);
|
||||||
|
|
||||||
this.insert = function (point) {
|
this.insert = function(point) {
|
||||||
function innerSearch(node, parent) {
|
function innerSearch(node, parent) {
|
||||||
|
|
||||||
if (node === null) {
|
if (node === null) {
|
||||||
return parent;
|
return parent;
|
||||||
}
|
}
|
||||||
@ -126,7 +131,11 @@ function kdNN(fpoints, fpoint) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
newNode = new Node(point, (insertPosition.dimension + 1) % dimensions.length, insertPosition);
|
newNode = new Node(
|
||||||
|
point,
|
||||||
|
(insertPosition.dimension + 1) % dimensions.length,
|
||||||
|
insertPosition
|
||||||
|
);
|
||||||
dimension = dimensions[insertPosition.dimension];
|
dimension = dimensions[insertPosition.dimension];
|
||||||
|
|
||||||
if (point[dimension] < insertPosition.obj[dimension]) {
|
if (point[dimension] < insertPosition.obj[dimension]) {
|
||||||
@ -136,14 +145,12 @@ function kdNN(fpoints, fpoint) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
this.nearest = function (point, maxNodes, maxDistance) {
|
this.nearest = function(point, maxNodes, maxDistance) {
|
||||||
var i,
|
var i, result, bestNodes;
|
||||||
result,
|
|
||||||
bestNodes;
|
|
||||||
|
|
||||||
bestNodes = new BinaryHeap(
|
bestNodes = new BinaryHeap(function(e) {
|
||||||
function (e) { return -e[1]; }
|
return -e[1];
|
||||||
);
|
});
|
||||||
|
|
||||||
function nearestSearch(node) {
|
function nearestSearch(node) {
|
||||||
var bestChild,
|
var bestChild,
|
||||||
@ -172,7 +179,10 @@ function kdNN(fpoints, fpoint) {
|
|||||||
linearDistance = metric(linearPoint, node.obj);
|
linearDistance = metric(linearPoint, node.obj);
|
||||||
|
|
||||||
if (node.right === null && node.left === null) {
|
if (node.right === null && node.left === null) {
|
||||||
if (bestNodes.size() < maxNodes || ownDistance < bestNodes.peek()[1]) {
|
if (
|
||||||
|
bestNodes.size() < maxNodes ||
|
||||||
|
ownDistance < bestNodes.peek()[1]
|
||||||
|
) {
|
||||||
saveNode(node, ownDistance);
|
saveNode(node, ownDistance);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
@ -196,7 +206,10 @@ function kdNN(fpoints, fpoint) {
|
|||||||
saveNode(node, ownDistance);
|
saveNode(node, ownDistance);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (bestNodes.size() < maxNodes || Math.abs(linearDistance) < bestNodes.peek()[1]) {
|
if (
|
||||||
|
bestNodes.size() < maxNodes ||
|
||||||
|
Math.abs(linearDistance) < bestNodes.peek()[1]
|
||||||
|
) {
|
||||||
if (bestChild === node.left) {
|
if (bestChild === node.left) {
|
||||||
otherChild = node.right;
|
otherChild = node.right;
|
||||||
} else {
|
} else {
|
||||||
@ -214,8 +227,7 @@ function kdNN(fpoints, fpoint) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (self.root)
|
if (self.root) nearestSearch(self.root);
|
||||||
nearestSearch(self.root);
|
|
||||||
|
|
||||||
result = [];
|
result = [];
|
||||||
|
|
||||||
@ -234,14 +246,14 @@ function kdNN(fpoints, fpoint) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
BinaryHeap.prototype = {
|
BinaryHeap.prototype = {
|
||||||
push: function (element) {
|
push: function(element) {
|
||||||
// Add the new element to the end of the array.
|
// Add the new element to the end of the array.
|
||||||
this.content.push(element);
|
this.content.push(element);
|
||||||
// Allow it to bubble up.
|
// Allow it to bubble up.
|
||||||
this.bubbleUp(this.content.length - 1);
|
this.bubbleUp(this.content.length - 1);
|
||||||
},
|
},
|
||||||
|
|
||||||
pop: function () {
|
pop: function() {
|
||||||
// Store the first element so we can return it later.
|
// Store the first element so we can return it later.
|
||||||
var result = this.content[0];
|
var result = this.content[0];
|
||||||
// Get the element at the end of the array.
|
// Get the element at the end of the array.
|
||||||
@ -255,15 +267,15 @@ function kdNN(fpoints, fpoint) {
|
|||||||
return result;
|
return result;
|
||||||
},
|
},
|
||||||
|
|
||||||
peek: function () {
|
peek: function() {
|
||||||
return this.content[0];
|
return this.content[0];
|
||||||
},
|
},
|
||||||
|
|
||||||
size: function () {
|
size: function() {
|
||||||
return this.content.length;
|
return this.content.length;
|
||||||
},
|
},
|
||||||
|
|
||||||
bubbleUp: function (n) {
|
bubbleUp: function(n) {
|
||||||
// Fetch the element that has to be moved.
|
// Fetch the element that has to be moved.
|
||||||
var element = this.content[n];
|
var element = this.content[n];
|
||||||
// When at 0, an element can not go up any further.
|
// When at 0, an element can not go up any further.
|
||||||
@ -285,7 +297,7 @@ function kdNN(fpoints, fpoint) {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
sinkDown: function (n) {
|
sinkDown: function(n) {
|
||||||
// Look up the target element and its score.
|
// Look up the target element and its score.
|
||||||
var length = this.content.length,
|
var length = this.content.length,
|
||||||
element = this.content[n],
|
element = this.content[n],
|
||||||
@ -293,7 +305,8 @@ function kdNN(fpoints, fpoint) {
|
|||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
// Compute the indices of the child elements.
|
// Compute the indices of the child elements.
|
||||||
var child2N = (n + 1) * 2, child1N = child2N - 1;
|
var child2N = (n + 1) * 2,
|
||||||
|
child1N = child2N - 1;
|
||||||
// This is used to store the new position of the element,
|
// This is used to store the new position of the element,
|
||||||
// if any.
|
// if any.
|
||||||
var swap = null;
|
var swap = null;
|
||||||
@ -303,8 +316,7 @@ function kdNN(fpoints, fpoint) {
|
|||||||
var child1 = this.content[child1N],
|
var child1 = this.content[child1N],
|
||||||
child1Score = this.scoreFunction(child1);
|
child1Score = this.scoreFunction(child1);
|
||||||
// If the score is less than our element's, we need to swap.
|
// If the score is less than our element's, we need to swap.
|
||||||
if (child1Score < elemScore)
|
if (child1Score < elemScore) swap = child1N;
|
||||||
swap = child1N;
|
|
||||||
}
|
}
|
||||||
// Do the same checks for the other child.
|
// Do the same checks for the other child.
|
||||||
if (child2N < length) {
|
if (child2N < length) {
|
||||||
@ -329,26 +341,29 @@ function kdNN(fpoints, fpoint) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
var dims = []
|
var dims = [];
|
||||||
|
|
||||||
for (var i = 0; i < fpoint.length; i++) dims.push(i)
|
for (var i = 0; i < fpoint.length; i++) dims.push(i);
|
||||||
|
|
||||||
var tree = new kdTree(fpoints, function (e1, e2) {
|
var tree = new kdTree(
|
||||||
var d = 0;
|
fpoints,
|
||||||
var e3 = e1;
|
function(e1, e2) {
|
||||||
if (!Array.isArray(e1)) {
|
var d = 0;
|
||||||
e3 = []
|
var e3 = e1;
|
||||||
for (var key in e1)
|
if (!Array.isArray(e1)) {
|
||||||
e3.push(e1[key])
|
e3 = [];
|
||||||
|
for (var key in e1) e3.push(e1[key]);
|
||||||
|
|
||||||
e1 = e3
|
e1 = e3;
|
||||||
}
|
}
|
||||||
e1.forEach(function (e, i) {
|
e1.forEach(function(e, i) {
|
||||||
var sqd = (e1[i] - e2[i]);
|
var sqd = e1[i] - e2[i];
|
||||||
d += sqd * sqd;
|
d += sqd * sqd;
|
||||||
})
|
});
|
||||||
return d;
|
return d;
|
||||||
}, dims)
|
},
|
||||||
|
dims
|
||||||
|
);
|
||||||
|
|
||||||
return tree.nearest(fpoint, 1, 1000)[0][0];
|
return tree.nearest(fpoint, 1, 1000)[0][0];
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,7 @@ forumTopicId: 302296
|
|||||||
---
|
---
|
||||||
|
|
||||||
## Description
|
## Description
|
||||||
|
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
A positive integer is a <a href="https://en.wikipedia.org/wiki/Kaprekar number">Kaprekar number</a> if:
|
A positive integer is a <a href="https://en.wikipedia.org/wiki/Kaprekar number">Kaprekar number</a> if:
|
||||||
<ul>
|
<ul>
|
||||||
@ -21,38 +22,41 @@ Kaprekar numbers:
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
|
|
||||||
<section id='instructions'>
|
<section id='instructions'>
|
||||||
Write a function that takes a number $n$, a base $bs$, and returns true if the number is a Kaprekar number for the given base. Otherwise, the function returns false.
|
Write a function that takes a number $n$, a base $bs$, and returns true if the number is a Kaprekar number for the given base. Otherwise, the function returns false.
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
|
|
||||||
<section id='tests'>
|
<section id='tests'>
|
||||||
|
|
||||||
``` yml
|
```yml
|
||||||
tests:
|
tests:
|
||||||
- text: <code>isKaprekar</code> should be a function.
|
- text: <code>isKaprekar</code> should be a function.
|
||||||
testString: assert(typeof isKaprekar == 'function', '<code>isKaprekar</code> should be a function.');
|
testString: assert(typeof isKaprekar == 'function');
|
||||||
- text: <code>isKaprekar(1, 10)</code> should return a boolean.
|
- text: <code>isKaprekar(1, 10)</code> should return a boolean.
|
||||||
testString: assert(typeof isKaprekar(1, 10) == 'boolean', '<code>isKaprekar(1, 10)</code> should return a boolean.');
|
testString: assert(typeof isKaprekar(1, 10) == 'boolean');
|
||||||
- text: <code>isKaprekar(1, 10)</code> should return <code>true</code>.
|
- text: <code>isKaprekar(1, 10)</code> should return <code>true</code>.
|
||||||
testString: assert.equal(isKaprekar(1, 10), true, '<code>isKaprekar(1, 10)</code> should return <code>true</code>.');
|
testString: assert.equal(isKaprekar(1, 10), true);
|
||||||
- text: <code>isKaprekar(9, 10)</code> should return <code>true</code>.
|
- text: <code>isKaprekar(9, 10)</code> should return <code>true</code>.
|
||||||
testString: assert.equal(isKaprekar(9, 10), true, '<code>isKaprekar(9, 10)</code> should return <code>true</code>.');
|
testString: assert.equal(isKaprekar(9, 10), true);
|
||||||
- text: <code>isKaprekar(2223, 10)</code> should return <code>true</code>.
|
- text: <code>isKaprekar(2223, 10)</code> should return <code>true</code>.
|
||||||
testString: assert.equal(isKaprekar(2223, 10), true, '<code>isKaprekar(2223, 10)</code> should return <code>true</code>.');
|
testString: assert.equal(isKaprekar(2223, 10), true);
|
||||||
- text: <code>isKaprekar(22823, 10)</code> should return <code>false</code>.
|
- text: <code>isKaprekar(22823, 10)</code> should return <code>false</code>.
|
||||||
testString: assert.equal(isKaprekar(22823, 10), false, '<code>isKaprekar(22823, 10)</code> should return <code>false</code>.');
|
testString: assert.equal(isKaprekar(22823, 10), false);
|
||||||
- text: <code>isKaprekar(9, 17)</code> should return <code>false</code>.
|
- text: <code>isKaprekar(9, 17)</code> should return <code>false</code>.
|
||||||
testString: assert.equal(isKaprekar(9, 17), false, '<code>isKaprekar(9, 17)</code> should return <code>false</code>.');
|
testString: assert.equal(isKaprekar(9, 17), false);
|
||||||
- text: <code>isKaprekar(225, 17)</code> should return <code>true</code>.
|
- text: <code>isKaprekar(225, 17)</code> should return <code>true</code>.
|
||||||
testString: assert.equal(isKaprekar(225, 17), true, '<code>isKaprekar(225, 17)</code> should return <code>true</code>.');
|
testString: assert.equal(isKaprekar(225, 17), true);
|
||||||
- text: <code>isKaprekar(999, 17)</code> should return <code>false</code>.
|
- text: <code>isKaprekar(999, 17)</code> should return <code>false</code>.
|
||||||
testString: assert.equal(isKaprekar(999, 17), false, '<code>isKaprekar(999, 17)</code> should return <code>false</code>.');
|
testString: assert.equal(isKaprekar(999, 17), false);
|
||||||
```
|
```
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Challenge Seed
|
## Challenge Seed
|
||||||
|
|
||||||
<section id='challengeSeed'>
|
<section id='challengeSeed'>
|
||||||
<div id='js-seed'>
|
<div id='js-seed'>
|
||||||
|
|
||||||
@ -66,6 +70,7 @@ function isKaprekar(n, bs) {
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Solution
|
## Solution
|
||||||
|
|
||||||
<section id='solution'>
|
<section id='solution'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
@ -73,11 +78,12 @@ function isKaprekar(n, bs) {
|
|||||||
if (n < 1) return false;
|
if (n < 1) return false;
|
||||||
if (n == 1) return true;
|
if (n == 1) return true;
|
||||||
for (var a = n * n, b = 0, s = 1; a; s *= bs) {
|
for (var a = n * n, b = 0, s = 1; a; s *= bs) {
|
||||||
b += a % bs * s;
|
b += (a % bs) * s;
|
||||||
a = Math.floor(a / bs);
|
a = Math.floor(a / bs);
|
||||||
if (b && a + b == n) return true;
|
if (b && a + b == n) return true;
|
||||||
} return false;
|
}
|
||||||
}
|
return false;
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
@ -6,38 +6,42 @@ forumTopicId: 323649
|
|||||||
---
|
---
|
||||||
|
|
||||||
## Description
|
## Description
|
||||||
|
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
The 0-1 knapsack problem is defined as follows:
|
The 0-1 knapsack problem is defined as follows:
|
||||||
You are given an array of objects representing items to be put in a knapsack. The objects have 3 attributes: name, weight, and value. The items need to be selected so that the total weight does not exceed the maximum weight and the value is maximized.
|
You are given an array of objects representing items to be put in a knapsack. The objects have 3 attributes: name, weight, and value. The items need to be selected so that the total weight does not exceed the maximum weight and the value is maximized.
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
|
|
||||||
<section id='instructions'>
|
<section id='instructions'>
|
||||||
Write a function to solve the knapsack problem. The function is given the array of objects and the maximum weight as parameters. It should return the maximum total value possible.
|
Write a function to solve the knapsack problem. The function is given the array of objects and the maximum weight as parameters. It should return the maximum total value possible.
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
|
|
||||||
<section id='tests'>
|
<section id='tests'>
|
||||||
|
|
||||||
``` yml
|
```yml
|
||||||
tests:
|
tests:
|
||||||
- text: <code>knapsack([{ name:'map', weight:9, value:150 }, { name:'compass', weight:13, value:35 }, { name:'water', weight:153, value:200 }, { name:'sandwich', weight:50, value:160 }, { name:'glucose', weight:15, value:60 }, { name:'tin', weight:68, value:45 }, { name:'banana', weight:27, value:60 }, { name:'apple', weight:39, value:40 }], 100)</code> should return <code>405</code>.
|
- text: <code>knapsack([{ name:'map', weight:9, value:150 }, { name:'compass', weight:13, value:35 }, { name:'water', weight:153, value:200 }, { name:'sandwich', weight:50, value:160 }, { name:'glucose', weight:15, value:60 }, { name:'tin', weight:68, value:45 }, { name:'banana', weight:27, value:60 }, { name:'apple', weight:39, value:40 }], 100)</code> should return <code>405</code>.
|
||||||
testString: assert.equal(knapsack([{ name:'map', weight:9, value:150 }, { name:'compass', weight:13, value:35 }, { name:'water', weight:153, value:200 }, { name:'sandwich', weight:50, value:160 }, { name:'glucose', weight:15, value:60 }, { name:'tin', weight:68, value:45 }, { name:'banana', weight:27, value:60 }, { name:'apple', weight:39, value:40 }], 100), 405, "<code>knapsack([{ name:'map', weight:9, value:150 }, { name:'compass', weight:13, value:35 }, { name:'water', weight:153, value:200 }, { name:'sandwich', weight:50, value:160 }, { name:'glucose', weight:15, value:60 }, { name:'tin', weight:68, value:45 }, { name:'banana', weight:27, value:60 }, { name:'apple', weight:39, value:40 }], 100)</code> should return <code>405</code>.");
|
testString: assert.equal(knapsack([{ name:'map', weight:9, value:150 }, { name:'compass', weight:13, value:35 }, { name:'water', weight:153, value:200 }, { name:'sandwich', weight:50, value:160 }, { name:'glucose', weight:15, value:60 }, { name:'tin', weight:68, value:45 }, { name:'banana', weight:27, value:60 }, { name:'apple', weight:39, value:40 }], 100), 405);
|
||||||
- text: <code>knapsack([{ name:'map', weight:9, value:150 }, { name:'compass', weight:13, value:35 }, { name:'water', weight:153, value:200 }, { name:'sandwich', weight:50, value:160 }, { name:'glucose', weight:15, value:60 }, { name:'tin', weight:68, value:45 }, { name:'banana', weight:27, value:60 }, { name:'apple', weight:39, value:40 }], 200)</code> should return <code>510</code>.
|
- text: <code>knapsack([{ name:'map', weight:9, value:150 }, { name:'compass', weight:13, value:35 }, { name:'water', weight:153, value:200 }, { name:'sandwich', weight:50, value:160 }, { name:'glucose', weight:15, value:60 }, { name:'tin', weight:68, value:45 }, { name:'banana', weight:27, value:60 }, { name:'apple', weight:39, value:40 }], 200)</code> should return <code>510</code>.
|
||||||
testString: assert.equal(knapsack([{ name:'map', weight:9, value:150 }, { name:'compass', weight:13, value:35 }, { name:'water', weight:153, value:200 }, { name:'sandwich', weight:50, value:160 }, { name:'glucose', weight:15, value:60 }, { name:'tin', weight:68, value:45 }, { name:'banana', weight:27, value:60 }, { name:'apple', weight:39, value:40 }], 200), 510, "<code>knapsack([{ name:'map', weight:9, value:150 }, { name:'compass', weight:13, value:35 }, { name:'water', weight:153, value:200 }, { name:'sandwich', weight:50, value:160 }, { name:'glucose', weight:15, value:60 }, { name:'tin', weight:68, value:45 }, { name:'banana', weight:27, value:60 }, { name:'apple', weight:39, value:40 }], 200)</code> should return <code>510</code>.");
|
testString: assert.equal(knapsack([{ name:'map', weight:9, value:150 }, { name:'compass', weight:13, value:35 }, { name:'water', weight:153, value:200 }, { name:'sandwich', weight:50, value:160 }, { name:'glucose', weight:15, value:60 }, { name:'tin', weight:68, value:45 }, { name:'banana', weight:27, value:60 }, { name:'apple', weight:39, value:40 }], 200), 510);
|
||||||
- text: <code>knapsack([{ name:'cheese', weight:23, value:30 }, { name:'beer', weight:52, value:10 }, { name:'suntan cream', weight:11, value:70 }, { name:'camera', weight:32, value:30 }, { name:'T-shirt', weight:24, value:15 }, { name:'trousers', weight:48, value:10 }, { name:'umbrella', weight:73, value:40 }], 100)</code> should return <code>145</code>.
|
- text: <code>knapsack([{ name:'cheese', weight:23, value:30 }, { name:'beer', weight:52, value:10 }, { name:'suntan cream', weight:11, value:70 }, { name:'camera', weight:32, value:30 }, { name:'T-shirt', weight:24, value:15 }, { name:'trousers', weight:48, value:10 }, { name:'umbrella', weight:73, value:40 }], 100)</code> should return <code>145</code>.
|
||||||
testString: assert.equal(knapsack([{ name:'cheese', weight:23, value:30 }, { name:'beer', weight:52, value:10 }, { name:'suntan cream', weight:11, value:70 }, { name:'camera', weight:32, value:30 }, { name:'T-shirt', weight:24, value:15 }, { name:'trousers', weight:48, value:10 }, { name:'umbrella', weight:73, value:40 }], 100), 145, "<code>knapsack([{ name:'cheese', weight:23, value:30 }, { name:'beer', weight:52, value:10 }, { name:'suntan cream', weight:11, value:70 }, { name:'camera', weight:32, value:30 }, { name:'T-shirt', weight:24, value:15 }, { name:'trousers', weight:48, value:10 }, { name:'umbrella', weight:73, value:40 }], 100)</code> should return <code>145</code>.");
|
testString: assert.equal(knapsack([{ name:'cheese', weight:23, value:30 }, { name:'beer', weight:52, value:10 }, { name:'suntan cream', weight:11, value:70 }, { name:'camera', weight:32, value:30 }, { name:'T-shirt', weight:24, value:15 }, { name:'trousers', weight:48, value:10 }, { name:'umbrella', weight:73, value:40 }], 100), 145);
|
||||||
- text: <code>knapsack([{ name:'cheese', weight:23, value:30 }, { name:'beer', weight:52, value:10 }, { name:'suntan cream', weight:11, value:70 }, { name:'camera', weight:32, value:30 }, { name:'T-shirt', weight:24, value:15 }, { name:'trousers', weight:48, value:10 }, { name:'umbrella', weight:73, value:40 }], 200)</code> should return <code>185</code>.
|
- text: <code>knapsack([{ name:'cheese', weight:23, value:30 }, { name:'beer', weight:52, value:10 }, { name:'suntan cream', weight:11, value:70 }, { name:'camera', weight:32, value:30 }, { name:'T-shirt', weight:24, value:15 }, { name:'trousers', weight:48, value:10 }, { name:'umbrella', weight:73, value:40 }], 200)</code> should return <code>185</code>.
|
||||||
testString: assert.equal(knapsack([{ name:'cheese', weight:23, value:30 }, { name:'beer', weight:52, value:10 }, { name:'suntan cream', weight:11, value:70 }, { name:'camera', weight:32, value:30 }, { name:'T-shirt', weight:24, value:15 }, { name:'trousers', weight:48, value:10 }, { name:'umbrella', weight:73, value:40 }], 200), 185, "<code>knapsack([{ name:'cheese', weight:23, value:30 }, { name:'beer', weight:52, value:10 }, { name:'suntan cream', weight:11, value:70 }, { name:'camera', weight:32, value:30 }, { name:'T-shirt', weight:24, value:15 }, { name:'trousers', weight:48, value:10 }, { name:'umbrella', weight:73, value:40 }], 200)</code> should return <code>185</code>.");
|
testString: assert.equal(knapsack([{ name:'cheese', weight:23, value:30 }, { name:'beer', weight:52, value:10 }, { name:'suntan cream', weight:11, value:70 }, { name:'camera', weight:32, value:30 }, { name:'T-shirt', weight:24, value:15 }, { name:'trousers', weight:48, value:10 }, { name:'umbrella', weight:73, value:40 }], 200), 185);
|
||||||
- text: <code>knapsack([{ name:'waterproof trousers', weight:42, value:70 }, { name:'waterproof overclothes', weight:43, value:75 }, { name:'note-case', weight:22, value:80 }, { name:'sunglasses', weight:7, value:20 }, { name:'towel', weight:18, value:12 }, { name:'socks', weight:4, value:50 }, { name:'book', weight:30, value:10 }], 100)</code> should return <code>237</code>.
|
- text: <code>knapsack([{ name:'waterproof trousers', weight:42, value:70 }, { name:'waterproof overclothes', weight:43, value:75 }, { name:'note-case', weight:22, value:80 }, { name:'sunglasses', weight:7, value:20 }, { name:'towel', weight:18, value:12 }, { name:'socks', weight:4, value:50 }, { name:'book', weight:30, value:10 }], 100)</code> should return <code>237</code>.
|
||||||
testString: assert.equal(knapsack([{ name:'waterproof trousers', weight:42, value:70 }, { name:'waterproof overclothes', weight:43, value:75 }, { name:'note-case', weight:22, value:80 }, { name:'sunglasses', weight:7, value:20 }, { name:'towel', weight:18, value:12 }, { name:'socks', weight:4, value:50 }, { name:'book', weight:30, value:10 }], 100), 237, "<code>knapsack([{ name:'waterproof trousers', weight:42, value:70 }, { name:'waterproof overclothes', weight:43, value:75 }, { name:'note-case', weight:22, value:80 }, { name:'sunglasses', weight:7, value:20 }, { name:'towel', weight:18, value:12 }, { name:'socks', weight:4, value:50 }, { name:'book', weight:30, value:10 }], 100)</code> should return <code>237</code>.");
|
testString: assert.equal(knapsack([{ name:'waterproof trousers', weight:42, value:70 }, { name:'waterproof overclothes', weight:43, value:75 }, { name:'note-case', weight:22, value:80 }, { name:'sunglasses', weight:7, value:20 }, { name:'towel', weight:18, value:12 }, { name:'socks', weight:4, value:50 }, { name:'book', weight:30, value:10 }], 100), 237);
|
||||||
- text: <code>knapsack([{ name:'waterproof trousers', weight:42, value:70 }, { name:'waterproof overclothes', weight:43, value:75 }, { name:'note-case', weight:22, value:80 }, { name:'sunglasses', weight:7, value:20 }, { name:'towel', weight:18, value:12 }, { name:'socks', weight:4, value:50 }, { name:'book', weight:30, value:10 }], 200)</code> should return <code>317</code>.'
|
- text: <code>knapsack([{ name:'waterproof trousers', weight:42, value:70 }, { name:'waterproof overclothes', weight:43, value:75 }, { name:'note-case', weight:22, value:80 }, { name:'sunglasses', weight:7, value:20 }, { name:'towel', weight:18, value:12 }, { name:'socks', weight:4, value:50 }, { name:'book', weight:30, value:10 }], 200)</code> should return <code>317</code>.'
|
||||||
testString: assert.equal(knapsack([{ name:'waterproof trousers', weight:42, value:70 }, { name:'waterproof overclothes', weight:43, value:75 }, { name:'note-case', weight:22, value:80 }, { name:'sunglasses', weight:7, value:20 }, { name:'towel', weight:18, value:12 }, { name:'socks', weight:4, value:50 }, { name:'book', weight:30, value:10 }], 200), 317, "<code>knapsack([{ name:'waterproof trousers', weight:42, value:70 }, { name:'waterproof overclothes', weight:43, value:75 }, { name:'note-case', weight:22, value:80 }, { name:'sunglasses', weight:7, value:20 }, { name:'towel', weight:18, value:12 }, { name:'socks', weight:4, value:50 }, { name:'book', weight:30, value:10 }], 200)</code> should return <code>317</code>.");
|
testString: assert.equal(knapsack([{ name:'waterproof trousers', weight:42, value:70 }, { name:'waterproof overclothes', weight:43, value:75 }, { name:'note-case', weight:22, value:80 }, { name:'sunglasses', weight:7, value:20 }, { name:'towel', weight:18, value:12 }, { name:'socks', weight:4, value:50 }, { name:'book', weight:30, value:10 }], 200), 317);
|
||||||
```
|
```
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Challenge Seed
|
## Challenge Seed
|
||||||
|
|
||||||
<section id='challengeSeed'>
|
<section id='challengeSeed'>
|
||||||
<div id='js-seed'>
|
<div id='js-seed'>
|
||||||
|
|
||||||
@ -51,110 +55,108 @@ function knapsack(items, maxweight) {
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Solution
|
## Solution
|
||||||
|
|
||||||
<section id='solution'>
|
<section id='solution'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function knapsack(items, maxweight) {
|
function knapsack(items, maxweight) {
|
||||||
var _ = {
|
var _ = {
|
||||||
max: function (e) {
|
max: function(e) {
|
||||||
var mx = e[0];
|
var mx = e[0];
|
||||||
e.forEach(function (f) {
|
e.forEach(function(f) {
|
||||||
if (mx < f)
|
if (mx < f) mx = f;
|
||||||
mx = f;
|
});
|
||||||
})
|
return mx;
|
||||||
return mx;
|
},
|
||||||
},
|
map: function(array, func) {
|
||||||
map: function (array, func) {
|
return array.map(func);
|
||||||
return array.map(func)
|
},
|
||||||
},
|
isUndefined: function(a) {
|
||||||
isUndefined: function (a) {
|
if (a) {
|
||||||
if (a) {
|
return false;
|
||||||
return false;
|
}
|
||||||
}
|
return true;
|
||||||
return true;
|
},
|
||||||
},
|
range: function(start, end, step) {
|
||||||
range: function (start, end, step) {
|
var a = [];
|
||||||
var a = []
|
var f = (f = (i, end) => i < end);
|
||||||
var f = f = (i, end) => i < end
|
if (start > end) f = (i, end) => i > end;
|
||||||
if (start > end)
|
|
||||||
f = (i, end) => i > end
|
|
||||||
|
|
||||||
for (var i = start; f(i, end); i += step)
|
for (var i = start; f(i, end); i += step) a.push(i);
|
||||||
a.push(i)
|
return a;
|
||||||
return a
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
|
var valuefn = e => e.value;
|
||||||
|
var weightfn = e => e.weight;
|
||||||
|
var _epsilon = 0.01;
|
||||||
|
var _p = _.max(_.map(items, valuefn));
|
||||||
|
var _k = (_epsilon * _p) / items.length;
|
||||||
|
|
||||||
|
var _memo = (function() {
|
||||||
|
var _mem = {};
|
||||||
|
var _key = function(i, w) {
|
||||||
|
return i + '::' + w;
|
||||||
|
};
|
||||||
|
return {
|
||||||
|
get: function(i, w) {
|
||||||
|
return _mem[_key(i, w)];
|
||||||
|
},
|
||||||
|
put: function(i, w, r) {
|
||||||
|
_mem[_key(i, w)] = r;
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
})();
|
||||||
|
|
||||||
|
var _m = function(i, w) {
|
||||||
|
i = Math.round(i);
|
||||||
|
w = Math.round(w);
|
||||||
|
|
||||||
|
if (i < 0 || w === 0) {
|
||||||
|
// empty base case
|
||||||
|
return { items: [], totalWeight: 0, totalValue: 0 };
|
||||||
}
|
}
|
||||||
|
|
||||||
var valuefn = (e) => e.value
|
var mm = _memo.get(i, w);
|
||||||
var weightfn = (e) => e.weight
|
if (!_.isUndefined(mm)) {
|
||||||
var _epsilon = 0.01;
|
return mm;
|
||||||
var _p = _.max(_.map(items, valuefn));
|
}
|
||||||
var _k = _epsilon * _p / items.length;
|
|
||||||
|
|
||||||
var _memo = (function () {
|
var item = items[i];
|
||||||
var _mem = {};
|
if (weightfn(item) > w) {
|
||||||
var _key = function (i, w) {
|
//item does not fit, try the next item
|
||||||
return i + '::' + w;
|
return _memo.put(i, w, _m(i - 1, w));
|
||||||
};
|
}
|
||||||
return {
|
// this item could fit.
|
||||||
get: function (i, w) {
|
// are we better off excluding it?
|
||||||
return _mem[_key(i, w)];
|
var excluded = _m(i - 1, w);
|
||||||
},
|
// or including it?
|
||||||
put: function (i, w, r) {
|
var included = _m(i - 1, w - weightfn(item));
|
||||||
_mem[_key(i, w)] = r;
|
if (
|
||||||
return r;
|
included.totalValue + Math.floor(valuefn(item) / _k) >
|
||||||
}
|
excluded.totalValue
|
||||||
};
|
) {
|
||||||
})();
|
// better off including it
|
||||||
|
// make a copy of the list
|
||||||
|
var i1 = included.items.slice();
|
||||||
|
i1.push(item);
|
||||||
|
return _memo.put(i, w, {
|
||||||
|
items: i1,
|
||||||
|
totalWeight: included.totalWeight + weightfn(item),
|
||||||
|
totalValue: included.totalValue + Math.floor(valuefn(item) / _k)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
//better off excluding it
|
||||||
|
return _memo.put(i, w, excluded);
|
||||||
|
};
|
||||||
|
var scaled = _m(items.length - 1, maxweight);
|
||||||
|
|
||||||
var _m = function (i, w) {
|
var val = 0;
|
||||||
|
scaled.items.forEach(function(e) {
|
||||||
i = Math.round(i);
|
val += e.value;
|
||||||
w = Math.round(w);
|
});
|
||||||
|
return val;
|
||||||
|
|
||||||
if (i < 0 || w === 0) {
|
|
||||||
// empty base case
|
|
||||||
return { items: [], totalWeight: 0, totalValue: 0 };
|
|
||||||
}
|
|
||||||
|
|
||||||
var mm = _memo.get(i, w);
|
|
||||||
if (!_.isUndefined(mm)) {
|
|
||||||
return mm;
|
|
||||||
}
|
|
||||||
|
|
||||||
var item = items[i];
|
|
||||||
if (weightfn(item) > w) {
|
|
||||||
//item does not fit, try the next item
|
|
||||||
return _memo.put(i, w, _m(i - 1, w));
|
|
||||||
}
|
|
||||||
// this item could fit.
|
|
||||||
// are we better off excluding it?
|
|
||||||
var excluded = _m(i - 1, w);
|
|
||||||
// or including it?
|
|
||||||
var included = _m(i - 1, w - weightfn(item));
|
|
||||||
if (included.totalValue + Math.floor(valuefn(item) / _k) > excluded.totalValue) {
|
|
||||||
// better off including it
|
|
||||||
// make a copy of the list
|
|
||||||
var i1 = included.items.slice();
|
|
||||||
i1.push(item);
|
|
||||||
return _memo.put(i, w,
|
|
||||||
{
|
|
||||||
items: i1,
|
|
||||||
totalWeight: included.totalWeight + weightfn(item),
|
|
||||||
totalValue: included.totalValue + Math.floor(valuefn(item) / _k)
|
|
||||||
});
|
|
||||||
}
|
|
||||||
//better off excluding it
|
|
||||||
return _memo.put(i, w, excluded);
|
|
||||||
};
|
|
||||||
var scaled = _m(items.length - 1, maxweight);
|
|
||||||
|
|
||||||
var val = 0;
|
|
||||||
scaled.items.forEach(function (e) {
|
|
||||||
val += e.value
|
|
||||||
})
|
|
||||||
return val;
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -6,36 +6,40 @@ forumTopicId: 323652
|
|||||||
---
|
---
|
||||||
|
|
||||||
## Description
|
## Description
|
||||||
|
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
The bounded knapsack problem is defined as follows:
|
The bounded knapsack problem is defined as follows:
|
||||||
You are given an array of objects representing items to be put in a knapsack. The objects have 4 attributes: name, pieces (the number of the particular item), weight, and value. The items need to be selected so that the total weight does not exceed the maximum weight and the value is maximized. Keep in mind that each item can appear between 0 and <code>pieces</code> times.
|
You are given an array of objects representing items to be put in a knapsack. The objects have 4 attributes: name, pieces (the number of the particular item), weight, and value. The items need to be selected so that the total weight does not exceed the maximum weight and the value is maximized. Keep in mind that each item can appear between 0 and <code>pieces</code> times.
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
|
|
||||||
<section id='instructions'>
|
<section id='instructions'>
|
||||||
Write a function to solve the knapsack problem. The function is given the array of objects and the maximum weight as parameters. It should return the maximum total value possible.
|
Write a function to solve the knapsack problem. The function is given the array of objects and the maximum weight as parameters. It should return the maximum total value possible.
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
|
|
||||||
<section id='tests'>
|
<section id='tests'>
|
||||||
|
|
||||||
``` yml
|
```yml
|
||||||
tests:
|
tests:
|
||||||
- text: <code>findBestPack([{ name:'map', weight:9, value:150, pieces:1 }, { name:'compass', weight:13, value:35, pieces:1 }, { name:'water', weight:153, value:200, pieces:2 }, { name:'sandwich', weight:50, value:60, pieces:2 }, { name:'glucose', weight:15, value:60, pieces:2 }, { name:'tin', weight:68, value:45, pieces:3 }, { name:'banana', weight:27, value:60, pieces:3 }, { name:'apple', weight:39, value:40, pieces:3 }, { name:'cheese', weight:23, value:30, pieces:1 }, { name:'beer', weight:52, value:10, pieces:3 }, { name:'suntan, cream', weight:11, value:70, pieces:1 }, { name:'camera', weight:32, value:30, pieces:1 }, { name:'T-shirt', weight:24, value:15, pieces:2 }], 300)</code> should return <code>755</code>.
|
- text: <code>findBestPack([{ name:'map', weight:9, value:150, pieces:1 }, { name:'compass', weight:13, value:35, pieces:1 }, { name:'water', weight:153, value:200, pieces:2 }, { name:'sandwich', weight:50, value:60, pieces:2 }, { name:'glucose', weight:15, value:60, pieces:2 }, { name:'tin', weight:68, value:45, pieces:3 }, { name:'banana', weight:27, value:60, pieces:3 }, { name:'apple', weight:39, value:40, pieces:3 }, { name:'cheese', weight:23, value:30, pieces:1 }, { name:'beer', weight:52, value:10, pieces:3 }, { name:'suntan, cream', weight:11, value:70, pieces:1 }, { name:'camera', weight:32, value:30, pieces:1 }, { name:'T-shirt', weight:24, value:15, pieces:2 }], 300)</code> should return <code>755</code>.
|
||||||
testString: assert.equal(findBestPack([{ name:'map', weight:9, value:150, pieces:1 }, { name:'compass', weight:13, value:35, pieces:1 }, { name:'water', weight:153, value:200, pieces:2 }, { name:'sandwich', weight:50, value:60, pieces:2 }, { name:'glucose', weight:15, value:60, pieces:2 }, { name:'tin', weight:68, value:45, pieces:3 }, { name:'banana', weight:27, value:60, pieces:3 }, { name:'apple', weight:39, value:40, pieces:3 }, { name:'cheese', weight:23, value:30, pieces:1 }, { name:'beer', weight:52, value:10, pieces:3 }, { name:'suntan, cream', weight:11, value:70, pieces:1 }, { name:'camera', weight:32, value:30, pieces:1 }, { name:'T-shirt', weight:24, value:15, pieces:2 }], 300), 755, "<code>findBestPack([{ name:'map', weight:9, value:150, pieces:1 }, { name:'compass', weight:13, value:35, pieces:1 }, { name:'water', weight:153, value:200, pieces:2 }, { name:'sandwich', weight:50, value:60, pieces:2 }, { name:'glucose', weight:15, value:60, pieces:2 }, { name:'tin', weight:68, value:45, pieces:3 }, { name:'banana', weight:27, value:60, pieces:3 }, { name:'apple', weight:39, value:40, pieces:3 }, { name:'cheese', weight:23, value:30, pieces:1 }, { name:'beer', weight:52, value:10, pieces:3 }, { name:'suntan, cream', weight:11, value:70, pieces:1 }, { name:'camera', weight:32, value:30, pieces:1 }, { name:'T-shirt', weight:24, value:15, pieces:2 }], 300)</code> should return <code>755</code>.");
|
testString: assert.equal(findBestPack([{ name:'map', weight:9, value:150, pieces:1 }, { name:'compass', weight:13, value:35, pieces:1 }, { name:'water', weight:153, value:200, pieces:2 }, { name:'sandwich', weight:50, value:60, pieces:2 }, { name:'glucose', weight:15, value:60, pieces:2 }, { name:'tin', weight:68, value:45, pieces:3 }, { name:'banana', weight:27, value:60, pieces:3 }, { name:'apple', weight:39, value:40, pieces:3 }, { name:'cheese', weight:23, value:30, pieces:1 }, { name:'beer', weight:52, value:10, pieces:3 }, { name:'suntan, cream', weight:11, value:70, pieces:1 }, { name:'camera', weight:32, value:30, pieces:1 }, { name:'T-shirt', weight:24, value:15, pieces:2 }], 300), 755);
|
||||||
- text: <code>findBestPack([{ name:'map', weight:9, value:150, pieces:1 }, { name:'compass', weight:13, value:35, pieces:1 }, { name:'water', weight:153, value:200, pieces:2 }, { name:'sandwich', weight:50, value:60, pieces:2 }, { name:'glucose', weight:15, value:60, pieces:2 }, { name:'tin', weight:68, value:45, pieces:3 }, { name:'banana', weight:27, value:60, pieces:3 }, { name:'apple', weight:39, value:40, pieces:3 }, { name:'cheese', weight:23, value:30, pieces:1 }, { name:'beer', weight:52, value:10, pieces:3 }, { name:'suntan, cream', weight:11, value:70, pieces:1 }, { name:'camera', weight:32, value:30, pieces:1 }, { name:'T-shirt', weight:24, value:15, pieces:2 }], 400)</code> should return <code>875</code>.
|
- text: <code>findBestPack([{ name:'map', weight:9, value:150, pieces:1 }, { name:'compass', weight:13, value:35, pieces:1 }, { name:'water', weight:153, value:200, pieces:2 }, { name:'sandwich', weight:50, value:60, pieces:2 }, { name:'glucose', weight:15, value:60, pieces:2 }, { name:'tin', weight:68, value:45, pieces:3 }, { name:'banana', weight:27, value:60, pieces:3 }, { name:'apple', weight:39, value:40, pieces:3 }, { name:'cheese', weight:23, value:30, pieces:1 }, { name:'beer', weight:52, value:10, pieces:3 }, { name:'suntan, cream', weight:11, value:70, pieces:1 }, { name:'camera', weight:32, value:30, pieces:1 }, { name:'T-shirt', weight:24, value:15, pieces:2 }], 400)</code> should return <code>875</code>.
|
||||||
testString: assert.equal(findBestPack([{ name:'map', weight:9, value:150, pieces:1 }, { name:'compass', weight:13, value:35, pieces:1 }, { name:'water', weight:153, value:200, pieces:2 }, { name:'sandwich', weight:50, value:60, pieces:2 }, { name:'glucose', weight:15, value:60, pieces:2 }, { name:'tin', weight:68, value:45, pieces:3 }, { name:'banana', weight:27, value:60, pieces:3 }, { name:'apple', weight:39, value:40, pieces:3 }, { name:'cheese', weight:23, value:30, pieces:1 }, { name:'beer', weight:52, value:10, pieces:3 }, { name:'suntan, cream', weight:11, value:70, pieces:1 }, { name:'camera', weight:32, value:30, pieces:1 }, { name:'T-shirt', weight:24, value:15, pieces:2 }], 400), 875, "<code>findBestPack([{ name:'map', weight:9, value:150, pieces:1 }, { name:'compass', weight:13, value:35, pieces:1 }, { name:'water', weight:153, value:200, pieces:2 }, { name:'sandwich', weight:50, value:60, pieces:2 }, { name:'glucose', weight:15, value:60, pieces:2 }, { name:'tin', weight:68, value:45, pieces:3 }, { name:'banana', weight:27, value:60, pieces:3 }, { name:'apple', weight:39, value:40, pieces:3 }, { name:'cheese', weight:23, value:30, pieces:1 }, { name:'beer', weight:52, value:10, pieces:3 }, { name:'suntan, cream', weight:11, value:70, pieces:1 }, { name:'camera', weight:32, value:30, pieces:1 }, { name:'T-shirt', weight:24, value:15, pieces:2 }], 400)</code> should return <code>875</code>.");
|
testString: assert.equal(findBestPack([{ name:'map', weight:9, value:150, pieces:1 }, { name:'compass', weight:13, value:35, pieces:1 }, { name:'water', weight:153, value:200, pieces:2 }, { name:'sandwich', weight:50, value:60, pieces:2 }, { name:'glucose', weight:15, value:60, pieces:2 }, { name:'tin', weight:68, value:45, pieces:3 }, { name:'banana', weight:27, value:60, pieces:3 }, { name:'apple', weight:39, value:40, pieces:3 }, { name:'cheese', weight:23, value:30, pieces:1 }, { name:'beer', weight:52, value:10, pieces:3 }, { name:'suntan, cream', weight:11, value:70, pieces:1 }, { name:'camera', weight:32, value:30, pieces:1 }, { name:'T-shirt', weight:24, value:15, pieces:2 }], 400), 875);
|
||||||
- text: <code>findBestPack([{ name:'map', weight:9, value:150, pieces:1 }, { name:'compass', weight:13, value:35, pieces:1 }, { name:'water', weight:153, value:200, pieces:2 }, { name:'sandwich', weight:50, value:60, pieces:2 }, { name:'glucose', weight:15, value:60, pieces:2 }, { name:'tin', weight:68, value:45, pieces:3 }, { name:'banana', weight:27, value:60, pieces:3 }, { name:'apple', weight:39, value:40, pieces:3 }, { name:'cheese', weight:23, value:30, pieces:1 }, { name:'beer', weight:52, value:10, pieces:3 }, { name:'suntan, cream', weight:11, value:70, pieces:1 }, { name:'camera', weight:32, value:30, pieces:1 }, { name:'T-shirt', weight:24, value:15, pieces:2 }], 500)</code> should return <code>1015</code>.
|
- text: <code>findBestPack([{ name:'map', weight:9, value:150, pieces:1 }, { name:'compass', weight:13, value:35, pieces:1 }, { name:'water', weight:153, value:200, pieces:2 }, { name:'sandwich', weight:50, value:60, pieces:2 }, { name:'glucose', weight:15, value:60, pieces:2 }, { name:'tin', weight:68, value:45, pieces:3 }, { name:'banana', weight:27, value:60, pieces:3 }, { name:'apple', weight:39, value:40, pieces:3 }, { name:'cheese', weight:23, value:30, pieces:1 }, { name:'beer', weight:52, value:10, pieces:3 }, { name:'suntan, cream', weight:11, value:70, pieces:1 }, { name:'camera', weight:32, value:30, pieces:1 }, { name:'T-shirt', weight:24, value:15, pieces:2 }], 500)</code> should return <code>1015</code>.
|
||||||
testString: assert.equal(findBestPack([{ name:'map', weight:9, value:150, pieces:1 }, { name:'compass', weight:13, value:35, pieces:1 }, { name:'water', weight:153, value:200, pieces:2 }, { name:'sandwich', weight:50, value:60, pieces:2 }, { name:'glucose', weight:15, value:60, pieces:2 }, { name:'tin', weight:68, value:45, pieces:3 }, { name:'banana', weight:27, value:60, pieces:3 }, { name:'apple', weight:39, value:40, pieces:3 }, { name:'cheese', weight:23, value:30, pieces:1 }, { name:'beer', weight:52, value:10, pieces:3 }, { name:'suntan, cream', weight:11, value:70, pieces:1 }, { name:'camera', weight:32, value:30, pieces:1 }, { name:'T-shirt', weight:24, value:15, pieces:2 }], 500), 1015, "<code>findBestPack([{ name:'map', weight:9, value:150, pieces:1 }, { name:'compass', weight:13, value:35, pieces:1 }, { name:'water', weight:153, value:200, pieces:2 }, { name:'sandwich', weight:50, value:60, pieces:2 }, { name:'glucose', weight:15, value:60, pieces:2 }, { name:'tin', weight:68, value:45, pieces:3 }, { name:'banana', weight:27, value:60, pieces:3 }, { name:'apple', weight:39, value:40, pieces:3 }, { name:'cheese', weight:23, value:30, pieces:1 }, { name:'beer', weight:52, value:10, pieces:3 }, { name:'suntan, cream', weight:11, value:70, pieces:1 }, { name:'camera', weight:32, value:30, pieces:1 }, { name:'T-shirt', weight:24, value:15, pieces:2 }], 500)</code> should return <code>1015</code>.");
|
testString: assert.equal(findBestPack([{ name:'map', weight:9, value:150, pieces:1 }, { name:'compass', weight:13, value:35, pieces:1 }, { name:'water', weight:153, value:200, pieces:2 }, { name:'sandwich', weight:50, value:60, pieces:2 }, { name:'glucose', weight:15, value:60, pieces:2 }, { name:'tin', weight:68, value:45, pieces:3 }, { name:'banana', weight:27, value:60, pieces:3 }, { name:'apple', weight:39, value:40, pieces:3 }, { name:'cheese', weight:23, value:30, pieces:1 }, { name:'beer', weight:52, value:10, pieces:3 }, { name:'suntan, cream', weight:11, value:70, pieces:1 }, { name:'camera', weight:32, value:30, pieces:1 }, { name:'T-shirt', weight:24, value:15, pieces:2 }], 500), 1015);
|
||||||
- text: <code>findBestPack([{ name:'map', weight:9, value:150, pieces:1 }, { name:'compass', weight:13, value:35, pieces:1 }, { name:'water', weight:153, value:200, pieces:2 }, { name:'sandwich', weight:50, value:60, pieces:2 }, { name:'glucose', weight:15, value:60, pieces:2 }, { name:'tin', weight:68, value:45, pieces:3 }, { name:'banana', weight:27, value:60, pieces:3 }, { name:'apple', weight:39, value:40, pieces:3 }, { name:'cheese', weight:23, value:30, pieces:1 }, { name:'beer', weight:52, value:10, pieces:3 }, { name:'suntan, cream', weight:11, value:70, pieces:1 }, { name:'camera', weight:32, value:30, pieces:1 }, { name:'T-shirt', weight:24, value:15, pieces:2 }], 600)</code> should return <code>1120</code>.
|
- text: <code>findBestPack([{ name:'map', weight:9, value:150, pieces:1 }, { name:'compass', weight:13, value:35, pieces:1 }, { name:'water', weight:153, value:200, pieces:2 }, { name:'sandwich', weight:50, value:60, pieces:2 }, { name:'glucose', weight:15, value:60, pieces:2 }, { name:'tin', weight:68, value:45, pieces:3 }, { name:'banana', weight:27, value:60, pieces:3 }, { name:'apple', weight:39, value:40, pieces:3 }, { name:'cheese', weight:23, value:30, pieces:1 }, { name:'beer', weight:52, value:10, pieces:3 }, { name:'suntan, cream', weight:11, value:70, pieces:1 }, { name:'camera', weight:32, value:30, pieces:1 }, { name:'T-shirt', weight:24, value:15, pieces:2 }], 600)</code> should return <code>1120</code>.
|
||||||
testString: assert.equal(findBestPack([{ name:'map', weight:9, value:150, pieces:1 }, { name:'compass', weight:13, value:35, pieces:1 }, { name:'water', weight:153, value:200, pieces:2 }, { name:'sandwich', weight:50, value:60, pieces:2 }, { name:'glucose', weight:15, value:60, pieces:2 }, { name:'tin', weight:68, value:45, pieces:3 }, { name:'banana', weight:27, value:60, pieces:3 }, { name:'apple', weight:39, value:40, pieces:3 }, { name:'cheese', weight:23, value:30, pieces:1 }, { name:'beer', weight:52, value:10, pieces:3 }, { name:'suntan, cream', weight:11, value:70, pieces:1 }, { name:'camera', weight:32, value:30, pieces:1 }, { name:'T-shirt', weight:24, value:15, pieces:2 }], 600), 1120, "<code>findBestPack([{ name:'map', weight:9, value:150, pieces:1 }, { name:'compass', weight:13, value:35, pieces:1 }, { name:'water', weight:153, value:200, pieces:2 }, { name:'sandwich', weight:50, value:60, pieces:2 }, { name:'glucose', weight:15, value:60, pieces:2 }, { name:'tin', weight:68, value:45, pieces:3 }, { name:'banana', weight:27, value:60, pieces:3 }, { name:'apple', weight:39, value:40, pieces:3 }, { name:'cheese', weight:23, value:30, pieces:1 }, { name:'beer', weight:52, value:10, pieces:3 }, { name:'suntan, cream', weight:11, value:70, pieces:1 }, { name:'camera', weight:32, value:30, pieces:1 }, { name:'T-shirt', weight:24, value:15, pieces:2 }], 600)</code> should return <code>1120</code>.");
|
testString: assert.equal(findBestPack([{ name:'map', weight:9, value:150, pieces:1 }, { name:'compass', weight:13, value:35, pieces:1 }, { name:'water', weight:153, value:200, pieces:2 }, { name:'sandwich', weight:50, value:60, pieces:2 }, { name:'glucose', weight:15, value:60, pieces:2 }, { name:'tin', weight:68, value:45, pieces:3 }, { name:'banana', weight:27, value:60, pieces:3 }, { name:'apple', weight:39, value:40, pieces:3 }, { name:'cheese', weight:23, value:30, pieces:1 }, { name:'beer', weight:52, value:10, pieces:3 }, { name:'suntan, cream', weight:11, value:70, pieces:1 }, { name:'camera', weight:32, value:30, pieces:1 }, { name:'T-shirt', weight:24, value:15, pieces:2 }], 600), 1120);
|
||||||
- text: <code>findBestPack([{ name:'map', weight:9, value:150, pieces:1 }, { name:'compass', weight:13, value:35, pieces:1 }, { name:'water', weight:153, value:200, pieces:2 }, { name:'sandwich', weight:50, value:60, pieces:2 }, { name:'glucose', weight:15, value:60, pieces:2 }, { name:'tin', weight:68, value:45, pieces:3 }, { name:'banana', weight:27, value:60, pieces:3 }, { name:'apple', weight:39, value:40, pieces:3 }, { name:'cheese', weight:23, value:30, pieces:1 }, { name:'beer', weight:52, value:10, pieces:3 }, { name:'suntan, cream', weight:11, value:70, pieces:1 }, { name:'camera', weight:32, value:30, pieces:1 }, { name:'T-shirt', weight:24, value:15, pieces:2 }], 700)</code> should return <code>1225</code>.
|
- text: <code>findBestPack([{ name:'map', weight:9, value:150, pieces:1 }, { name:'compass', weight:13, value:35, pieces:1 }, { name:'water', weight:153, value:200, pieces:2 }, { name:'sandwich', weight:50, value:60, pieces:2 }, { name:'glucose', weight:15, value:60, pieces:2 }, { name:'tin', weight:68, value:45, pieces:3 }, { name:'banana', weight:27, value:60, pieces:3 }, { name:'apple', weight:39, value:40, pieces:3 }, { name:'cheese', weight:23, value:30, pieces:1 }, { name:'beer', weight:52, value:10, pieces:3 }, { name:'suntan, cream', weight:11, value:70, pieces:1 }, { name:'camera', weight:32, value:30, pieces:1 }, { name:'T-shirt', weight:24, value:15, pieces:2 }], 700)</code> should return <code>1225</code>.
|
||||||
testString: assert.equal(findBestPack([{ name:'map', weight:9, value:150, pieces:1 }, { name:'compass', weight:13, value:35, pieces:1 }, { name:'water', weight:153, value:200, pieces:2 }, { name:'sandwich', weight:50, value:60, pieces:2 }, { name:'glucose', weight:15, value:60, pieces:2 }, { name:'tin', weight:68, value:45, pieces:3 }, { name:'banana', weight:27, value:60, pieces:3 }, { name:'apple', weight:39, value:40, pieces:3 }, { name:'cheese', weight:23, value:30, pieces:1 }, { name:'beer', weight:52, value:10, pieces:3 }, { name:'suntan, cream', weight:11, value:70, pieces:1 }, { name:'camera', weight:32, value:30, pieces:1 }, { name:'T-shirt', weight:24, value:15, pieces:2 }], 700), 1225, "<code>findBestPack([{ name:'map', weight:9, value:150, pieces:1 }, { name:'compass', weight:13, value:35, pieces:1 }, { name:'water', weight:153, value:200, pieces:2 }, { name:'sandwich', weight:50, value:60, pieces:2 }, { name:'glucose', weight:15, value:60, pieces:2 }, { name:'tin', weight:68, value:45, pieces:3 }, { name:'banana', weight:27, value:60, pieces:3 }, { name:'apple', weight:39, value:40, pieces:3 }, { name:'cheese', weight:23, value:30, pieces:1 }, { name:'beer', weight:52, value:10, pieces:3 }, { name:'suntan, cream', weight:11, value:70, pieces:1 }, { name:'camera', weight:32, value:30, pieces:1 }, { name:'T-shirt', weight:24, value:15, pieces:2 }], 700)</code> should return <code>1225</code>.");
|
testString: assert.equal(findBestPack([{ name:'map', weight:9, value:150, pieces:1 }, { name:'compass', weight:13, value:35, pieces:1 }, { name:'water', weight:153, value:200, pieces:2 }, { name:'sandwich', weight:50, value:60, pieces:2 }, { name:'glucose', weight:15, value:60, pieces:2 }, { name:'tin', weight:68, value:45, pieces:3 }, { name:'banana', weight:27, value:60, pieces:3 }, { name:'apple', weight:39, value:40, pieces:3 }, { name:'cheese', weight:23, value:30, pieces:1 }, { name:'beer', weight:52, value:10, pieces:3 }, { name:'suntan, cream', weight:11, value:70, pieces:1 }, { name:'camera', weight:32, value:30, pieces:1 }, { name:'T-shirt', weight:24, value:15, pieces:2 }], 700), 1225);
|
||||||
```
|
```
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Challenge Seed
|
## Challenge Seed
|
||||||
|
|
||||||
<section id='challengeSeed'>
|
<section id='challengeSeed'>
|
||||||
<div id='js-seed'>
|
<div id='js-seed'>
|
||||||
|
|
||||||
@ -49,16 +53,13 @@ function findBestPack(data, maxweight) {
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Solution
|
## Solution
|
||||||
|
|
||||||
<section id='solution'>
|
<section id='solution'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function findBestPack(data, maxweight) {
|
function findBestPack(data, maxweight) {
|
||||||
var m = [
|
var m = [[0]]; // maximum pack value found so far
|
||||||
[0]
|
var b = [[0]]; // best combination found so far
|
||||||
]; // maximum pack value found so far
|
|
||||||
var b = [
|
|
||||||
[0]
|
|
||||||
]; // best combination found so far
|
|
||||||
var opts = [0]; // item index for 0 of item 0
|
var opts = [0]; // item index for 0 of item 0
|
||||||
var P = [1]; // item encoding for 0 of item 0
|
var P = [1]; // item encoding for 0 of item 0
|
||||||
var choose = 0;
|
var choose = 0;
|
||||||
|
@ -6,41 +6,45 @@ forumTopicId: 323654
|
|||||||
---
|
---
|
||||||
|
|
||||||
## Description
|
## Description
|
||||||
|
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
A thief burgles a butcher's shop, where he can select from some items.
|
A thief burgles a butcher's shop, where he can select from some items.
|
||||||
The thief knows the weights and prices of each items. Because he has a knapsack with a limit on the maximum weight that it can carry, he wants to select the items such that he would have his profit maximized. He may cut the items; the item has a reduced price after cutting that is proportional to the original price by the ratio of masses. That means: half of an item has half the price of the original.
|
The thief knows the weights and prices of each items. Because he has a knapsack with a limit on the maximum weight that it can carry, he wants to select the items such that he would have his profit maximized. He may cut the items; the item has a reduced price after cutting that is proportional to the original price by the ratio of masses. That means: half of an item has half the price of the original.
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
|
|
||||||
<section id='instructions'>
|
<section id='instructions'>
|
||||||
Write a function that takes an array of objects representing the items available in the shop. Each object has 3 attributes: name, weight, and value. The function also takes the maximum weight as a parameter. The function should return the maximum value possible, and the total weight of the selected items should not exceed the maximum weight.
|
Write a function that takes an array of objects representing the items available in the shop. Each object has 3 attributes: name, weight, and value. The function also takes the maximum weight as a parameter. The function should return the maximum value possible, and the total weight of the selected items should not exceed the maximum weight.
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
|
|
||||||
<section id='tests'>
|
<section id='tests'>
|
||||||
|
|
||||||
``` yml
|
```yml
|
||||||
tests:
|
tests:
|
||||||
- text: <code>knapContinuous([{ "weight":3.8, "value":36, name:"beef" }, { "weight":5.4, "value":43, name:"pork" }, { "weight":3.6, "value":90, name:"ham" }, { "weight":2.4, "value":45, name:"greaves" }, { "weight":4.0, "value":30, name:"flitch" }, { "weight":2.5, "value":56, name:"brawn" }, { "weight":3.7, "value":67, name:"welt" }, { "weight":3.0, "value":95, name:"salami" }, { "weight":5.9, "value":98, name:"sausage" }], 10)</code> should return <code>257.875</code>.
|
- text: <code>knapContinuous([{ "weight":3.8, "value":36, name:"beef" }, { "weight":5.4, "value":43, name:"pork" }, { "weight":3.6, "value":90, name:"ham" }, { "weight":2.4, "value":45, name:"greaves" }, { "weight":4.0, "value":30, name:"flitch" }, { "weight":2.5, "value":56, name:"brawn" }, { "weight":3.7, "value":67, name:"welt" }, { "weight":3.0, "value":95, name:"salami" }, { "weight":5.9, "value":98, name:"sausage" }], 10)</code> should return <code>257.875</code>.
|
||||||
testString: assert.equal(knapContinuous([{ "weight":3.8, "value":36, name:"beef" }, { "weight":5.4, "value":43, name:"pork" }, { "weight":3.6, "value":90, name:"ham" }, { "weight":2.4, "value":45, name:"greaves" }, { "weight":4.0, "value":30, name:"flitch" }, { "weight":2.5, "value":56, name:"brawn" }, { "weight":3.7, "value":67, name:"welt" }, { "weight":3.0, "value":95, name:"salami" }, { "weight":5.9, "value":98, name:"sausage" }], 10), 257.875, '<code>knapContinuous([{ "weight":3.8, "value":36, name:"beef" }, { "weight":5.4, "value":43, name:"pork" }, { "weight":3.6, "value":90, name:"ham" }, { "weight":2.4, "value":45, name:"greaves" }, { "weight":4.0, "value":30, name:"flitch" }, { "weight":2.5, "value":56, name:"brawn" }, { "weight":3.7, "value":67, name:"welt" }, { "weight":3.0, "value":95, name:"salami" }, { "weight":5.9, "value":98, name:"sausage" }], 10)</code> should return <code>257.875</code>.');
|
testString: assert.equal(knapContinuous([{ "weight":3.8, "value":36, name:"beef" }, { "weight":5.4, "value":43, name:"pork" }, { "weight":3.6, "value":90, name:"ham" }, { "weight":2.4, "value":45, name:"greaves" }, { "weight":4.0, "value":30, name:"flitch" }, { "weight":2.5, "value":56, name:"brawn" }, { "weight":3.7, "value":67, name:"welt" }, { "weight":3.0, "value":95, name:"salami" }, { "weight":5.9, "value":98, name:"sausage" }], 10), 257.875);
|
||||||
- text: <code>knapContinuous([{ "weight":3.8, "value":36, name:"beef" }, { "weight":5.4, "value":43, name:"pork" }, { "weight":3.6, "value":90, name:"ham" }, { "weight":2.4, "value":45, name:"greaves" }, { "weight":4.0, "value":30, name:"flitch" }, { "weight":2.5, "value":56, name:"brawn" }, { "weight":3.7, "value":67, name:"welt" }, { "weight":3.0, "value":95, name:"salami" }, { "weight":5.9, "value":98, name:"sausage" }], 12)</code> should return <code>295.05405405405406</code>.
|
- text: <code>knapContinuous([{ "weight":3.8, "value":36, name:"beef" }, { "weight":5.4, "value":43, name:"pork" }, { "weight":3.6, "value":90, name:"ham" }, { "weight":2.4, "value":45, name:"greaves" }, { "weight":4.0, "value":30, name:"flitch" }, { "weight":2.5, "value":56, name:"brawn" }, { "weight":3.7, "value":67, name:"welt" }, { "weight":3.0, "value":95, name:"salami" }, { "weight":5.9, "value":98, name:"sausage" }], 12)</code> should return <code>295.05405405405406</code>.
|
||||||
testString: assert.equal(knapContinuous([{ "weight":3.8, "value":36, name:"beef" }, { "weight":5.4, "value":43, name:"pork" }, { "weight":3.6, "value":90, name:"ham" }, { "weight":2.4, "value":45, name:"greaves" }, { "weight":4.0, "value":30, name:"flitch" }, { "weight":2.5, "value":56, name:"brawn" }, { "weight":3.7, "value":67, name:"welt" }, { "weight":3.0, "value":95, name:"salami" }, { "weight":5.9, "value":98, name:"sausage" }], 12), 295.05405405405406, '<code>knapContinuous([{ "weight":3.8, "value":36, name:"beef" }, { "weight":5.4, "value":43, name:"pork" }, { "weight":3.6, "value":90, name:"ham" }, { "weight":2.4, "value":45, name:"greaves" }, { "weight":4.0, "value":30, name:"flitch" }, { "weight":2.5, "value":56, name:"brawn" }, { "weight":3.7, "value":67, name:"welt" }, { "weight":3.0, "value":95, name:"salami" }, { "weight":5.9, "value":98, name:"sausage" }], 12)</code> should return <code>295.05405405405406</code>.');
|
testString: assert.equal(knapContinuous([{ "weight":3.8, "value":36, name:"beef" }, { "weight":5.4, "value":43, name:"pork" }, { "weight":3.6, "value":90, name:"ham" }, { "weight":2.4, "value":45, name:"greaves" }, { "weight":4.0, "value":30, name:"flitch" }, { "weight":2.5, "value":56, name:"brawn" }, { "weight":3.7, "value":67, name:"welt" }, { "weight":3.0, "value":95, name:"salami" }, { "weight":5.9, "value":98, name:"sausage" }], 12), 295.05405405405406);
|
||||||
- text: <code>knapContinuous([{ "weight":3.8, "value":36, name:"beef" }, { "weight":5.4, "value":43, name:"pork" }, { "weight":3.6, "value":90, name:"ham" }, { "weight":2.4, "value":45, name:"greaves" }, { "weight":4.0, "value":30, name:"flitch" }, { "weight":2.5, "value":56, name:"brawn" }, { "weight":3.7, "value":67, name:"welt" }, { "weight":3.0, "value":95, name:"salami" }, { "weight":5.9, "value":98, name:"sausage" }], 15)</code> should return <code>349.3783783783784</code>.
|
- text: <code>knapContinuous([{ "weight":3.8, "value":36, name:"beef" }, { "weight":5.4, "value":43, name:"pork" }, { "weight":3.6, "value":90, name:"ham" }, { "weight":2.4, "value":45, name:"greaves" }, { "weight":4.0, "value":30, name:"flitch" }, { "weight":2.5, "value":56, name:"brawn" }, { "weight":3.7, "value":67, name:"welt" }, { "weight":3.0, "value":95, name:"salami" }, { "weight":5.9, "value":98, name:"sausage" }], 15)</code> should return <code>349.3783783783784</code>.
|
||||||
testString: assert.equal(knapContinuous([{ "weight":3.8, "value":36, name:"beef" }, { "weight":5.4, "value":43, name:"pork" }, { "weight":3.6, "value":90, name:"ham" }, { "weight":2.4, "value":45, name:"greaves" }, { "weight":4.0, "value":30, name:"flitch" }, { "weight":2.5, "value":56, name:"brawn" }, { "weight":3.7, "value":67, name:"welt" }, { "weight":3.0, "value":95, name:"salami" }, { "weight":5.9, "value":98, name:"sausage" }], 15), 349.3783783783784, '<code>knapContinuous([{ "weight":3.8, "value":36, name:"beef" }, { "weight":5.4, "value":43, name:"pork" }, { "weight":3.6, "value":90, name:"ham" }, { "weight":2.4, "value":45, name:"greaves" }, { "weight":4.0, "value":30, name:"flitch" }, { "weight":2.5, "value":56, name:"brawn" }, { "weight":3.7, "value":67, name:"welt" }, { "weight":3.0, "value":95, name:"salami" }, { "weight":5.9, "value":98, name:"sausage" }], 15)</code> should return <code>349.3783783783784</code>.');
|
testString: assert.equal(knapContinuous([{ "weight":3.8, "value":36, name:"beef" }, { "weight":5.4, "value":43, name:"pork" }, { "weight":3.6, "value":90, name:"ham" }, { "weight":2.4, "value":45, name:"greaves" }, { "weight":4.0, "value":30, name:"flitch" }, { "weight":2.5, "value":56, name:"brawn" }, { "weight":3.7, "value":67, name:"welt" }, { "weight":3.0, "value":95, name:"salami" }, { "weight":5.9, "value":98, name:"sausage" }], 15), 349.3783783783784);
|
||||||
- text: <code>knapContinuous([{ "weight":3.8, "value":36, name:"beef" }, { "weight":5.4, "value":43, name:"pork" }, { "weight":3.6, "value":90, name:"ham" }, { "weight":2.4, "value":45, name:"greaves" }, { "weight":4.0, "value":30, name:"flitch" }, { "weight":2.5, "value":56, name:"brawn" }, { "weight":3.7, "value":67, name:"welt" }, { "weight":3.0, "value":95, name:"salami" }, { "weight":5.9, "value":98, name:"sausage" }], 22)</code> should return <code>459.5263157894737</code>.
|
- text: <code>knapContinuous([{ "weight":3.8, "value":36, name:"beef" }, { "weight":5.4, "value":43, name:"pork" }, { "weight":3.6, "value":90, name:"ham" }, { "weight":2.4, "value":45, name:"greaves" }, { "weight":4.0, "value":30, name:"flitch" }, { "weight":2.5, "value":56, name:"brawn" }, { "weight":3.7, "value":67, name:"welt" }, { "weight":3.0, "value":95, name:"salami" }, { "weight":5.9, "value":98, name:"sausage" }], 22)</code> should return <code>459.5263157894737</code>.
|
||||||
testString: assert.equal(knapContinuous([{ "weight":3.8, "value":36, name:"beef" }, { "weight":5.4, "value":43, name:"pork" }, { "weight":3.6, "value":90, name:"ham" }, { "weight":2.4, "value":45, name:"greaves" }, { "weight":4.0, "value":30, name:"flitch" }, { "weight":2.5, "value":56, name:"brawn" }, { "weight":3.7, "value":67, name:"welt" }, { "weight":3.0, "value":95, name:"salami" }, { "weight":5.9, "value":98, name:"sausage" }], 22), 459.5263157894737, '<code>knapContinuous([{ "weight":3.8, "value":36, name:"beef" }, { "weight":5.4, "value":43, name:"pork" }, { "weight":3.6, "value":90, name:"ham" }, { "weight":2.4, "value":45, name:"greaves" }, { "weight":4.0, "value":30, name:"flitch" }, { "weight":2.5, "value":56, name:"brawn" }, { "weight":3.7, "value":67, name:"welt" }, { "weight":3.0, "value":95, name:"salami" }, { "weight":5.9, "value":98, name:"sausage" }], 22)</code> should return <code>459.5263157894737</code>.');
|
testString: assert.equal(knapContinuous([{ "weight":3.8, "value":36, name:"beef" }, { "weight":5.4, "value":43, name:"pork" }, { "weight":3.6, "value":90, name:"ham" }, { "weight":2.4, "value":45, name:"greaves" }, { "weight":4.0, "value":30, name:"flitch" }, { "weight":2.5, "value":56, name:"brawn" }, { "weight":3.7, "value":67, name:"welt" }, { "weight":3.0, "value":95, name:"salami" }, { "weight":5.9, "value":98, name:"sausage" }], 22), 459.5263157894737);
|
||||||
- text: <code>knapContinuous([{ "weight":3.8, "value":36, name:"beef" }, { "weight":5.4, "value":43, name:"pork" }, { "weight":3.6, "value":90, name:"ham" }, { "weight":2.4, "value":45, name:"greaves" }, { "weight":4.0, "value":30, name:"flitch" }, { "weight":2.5, "value":56, name:"brawn" }, { "weight":3.7, "value":67, name:"welt" }, { "weight":3.0, "value":95, name:"salami" }, { "weight":5.9, "value":98, name:"sausage" }], 24)</code> should return <code>478.4736842105263</code>.
|
- text: <code>knapContinuous([{ "weight":3.8, "value":36, name:"beef" }, { "weight":5.4, "value":43, name:"pork" }, { "weight":3.6, "value":90, name:"ham" }, { "weight":2.4, "value":45, name:"greaves" }, { "weight":4.0, "value":30, name:"flitch" }, { "weight":2.5, "value":56, name:"brawn" }, { "weight":3.7, "value":67, name:"welt" }, { "weight":3.0, "value":95, name:"salami" }, { "weight":5.9, "value":98, name:"sausage" }], 24)</code> should return <code>478.4736842105263</code>.
|
||||||
testString: assert.equal(knapContinuous([{ "weight":3.8, "value":36, name:"beef" }, { "weight":5.4, "value":43, name:"pork" }, { "weight":3.6, "value":90, name:"ham" }, { "weight":2.4, "value":45, name:"greaves" }, { "weight":4.0, "value":30, name:"flitch" }, { "weight":2.5, "value":56, name:"brawn" }, { "weight":3.7, "value":67, name:"welt" }, { "weight":3.0, "value":95, name:"salami" }, { "weight":5.9, "value":98, name:"sausage" }], 24), 478.4736842105263, '<code>knapContinuous([{ "weight":3.8, "value":36, name:"beef" }, { "weight":5.4, "value":43, name:"pork" }, { "weight":3.6, "value":90, name:"ham" }, { "weight":2.4, "value":45, name:"greaves" }, { "weight":4.0, "value":30, name:"flitch" }, { "weight":2.5, "value":56, name:"brawn" }, { "weight":3.7, "value":67, name:"welt" }, { "weight":3.0, "value":95, name:"salami" }, { "weight":5.9, "value":98, name:"sausage" }], 24)</code> should return <code>478.4736842105263</code>.');
|
testString: assert.equal(knapContinuous([{ "weight":3.8, "value":36, name:"beef" }, { "weight":5.4, "value":43, name:"pork" }, { "weight":3.6, "value":90, name:"ham" }, { "weight":2.4, "value":45, name:"greaves" }, { "weight":4.0, "value":30, name:"flitch" }, { "weight":2.5, "value":56, name:"brawn" }, { "weight":3.7, "value":67, name:"welt" }, { "weight":3.0, "value":95, name:"salami" }, { "weight":5.9, "value":98, name:"sausage" }], 24), 478.4736842105263);
|
||||||
```
|
```
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Challenge Seed
|
## Challenge Seed
|
||||||
|
|
||||||
<section id='challengeSeed'>
|
<section id='challengeSeed'>
|
||||||
<div id='js-seed'>
|
<div id='js-seed'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function knapContinuous (items, maxweight) {
|
function knapContinuous(items, maxweight) {
|
||||||
// Good luck!
|
// Good luck!
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@ -49,16 +53,21 @@ function knapContinuous (items, maxweight) {
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Solution
|
## Solution
|
||||||
|
|
||||||
<section id='solution'>
|
<section id='solution'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function knapContinuous(items, maxweight) {
|
function knapContinuous(items, maxweight) {
|
||||||
function item_cmp(a, b) {
|
function item_cmp(a, b) {
|
||||||
const ua = a.unitVal, ub = b.unitVal;
|
const ua = a.unitVal,
|
||||||
|
ub = b.unitVal;
|
||||||
return ua < ub ? 1 : ua > ub ? -1 : 0;
|
return ua < ub ? 1 : ua > ub ? -1 : 0;
|
||||||
}
|
}
|
||||||
items = items.map(({ value, weight }) => ({ unitVal: value / weight, weight }));
|
items = items.map(({ value, weight }) => ({
|
||||||
items.sort(item_cmp)
|
unitVal: value / weight,
|
||||||
|
weight
|
||||||
|
}));
|
||||||
|
items.sort(item_cmp);
|
||||||
|
|
||||||
let val = 0;
|
let val = 0;
|
||||||
let wt = 0;
|
let wt = 0;
|
||||||
|
@ -6,6 +6,7 @@ forumTopicId: 323655
|
|||||||
---
|
---
|
||||||
|
|
||||||
## Description
|
## Description
|
||||||
|
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
A traveler gets diverted and has to make an unscheduled stop in what turns out to be Shangri-La. Opting to leave, he is allowed to take as much as he likes of the items available there, so long as it will fit in his knapsack, and he can carry it.
|
A traveler gets diverted and has to make an unscheduled stop in what turns out to be Shangri-La. Opting to leave, he is allowed to take as much as he likes of the items available there, so long as it will fit in his knapsack, and he can carry it.
|
||||||
He knows that he can carry no more than a particular value of maximum weight in total; and that the capacity of his knapsack has a limited volume.
|
He knows that he can carry no more than a particular value of maximum weight in total; and that the capacity of his knapsack has a limited volume.
|
||||||
@ -14,30 +15,33 @@ He can only take whole units of any item, but there is much more of any item tha
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
|
|
||||||
<section id='instructions'>
|
<section id='instructions'>
|
||||||
Write a function that takes an array of objects, maximum weight, and maximum volume as parameters. Each object has 4 attributes: name, value, weight, and volume. The function should return the maximum value of items the traveller can take with him.
|
Write a function that takes an array of objects, maximum weight, and maximum volume as parameters. Each object has 4 attributes: name, value, weight, and volume. The function should return the maximum value of items the traveller can take with him.
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
|
|
||||||
<section id='tests'>
|
<section id='tests'>
|
||||||
|
|
||||||
``` yml
|
```yml
|
||||||
tests:
|
tests:
|
||||||
- text: <code>knapsackUnbounded([{ name:"panacea", value:3000, weight:0.3, volume:0.025 }, { name:"ichor", value:1800, weight:0.2, volume:0.015 }, { name:"gold", value:2500, weight:2, volume:0.002 }], 25, 0.25)</code> should return <code>54500</code>.
|
- text: <code>knapsackUnbounded([{ name:"panacea", value:3000, weight:0.3, volume:0.025 }, { name:"ichor", value:1800, weight:0.2, volume:0.015 }, { name:"gold", value:2500, weight:2, volume:0.002 }], 25, 0.25)</code> should return <code>54500</code>.
|
||||||
testString: assert.equal(knapsackUnbounded([{ name:"panacea", value:3000, weight:0.3, volume:0.025 }, { name:"ichor", value:1800, weight:0.2, volume:0.015 }, { name:"gold", value:2500, weight:2, volume:0.002 }], 25, 0.25), 54500, '<code>knapsackUnbounded([{ name:"panacea", value:3000, weight:0.3, volume:0.025 }, { name:"ichor", value:1800, weight:0.2, volume:0.015 }, { name:"gold", value:2500, weight:2, volume:0.002 }], 25, 0.25)</code> should return <code>54500</code>.');
|
testString: assert.equal(knapsackUnbounded([{ name:"panacea", value:3000, weight:0.3, volume:0.025 }, { name:"ichor", value:1800, weight:0.2, volume:0.015 }, { name:"gold", value:2500, weight:2, volume:0.002 }], 25, 0.25), 54500);
|
||||||
- text: <code>knapsackUnbounded([{ name:"panacea", value:3000, weight:0.3, volume:0.025 }, { name:"ichor", value:1800, weight:0.2, volume:0.015 }, { name:"gold", value:2500, weight:2, volume:0.002 }], 55, 0.25)</code> should return <code>88400</code>.
|
- text: <code>knapsackUnbounded([{ name:"panacea", value:3000, weight:0.3, volume:0.025 }, { name:"ichor", value:1800, weight:0.2, volume:0.015 }, { name:"gold", value:2500, weight:2, volume:0.002 }], 55, 0.25)</code> should return <code>88400</code>.
|
||||||
testString: assert.equal(knapsackUnbounded([{ name:"panacea", value:3000, weight:0.3, volume:0.025 }, { name:"ichor", value:1800, weight:0.2, volume:0.015 }, { name:"gold", value:2500, weight:2, volume:0.002 }], 55, 0.25), 88400, '<code>knapsackUnbounded([{ name:"panacea", value:3000, weight:0.3, volume:0.025 }, { name:"ichor", value:1800, weight:0.2, volume:0.015 }, { name:"gold", value:2500, weight:2, volume:0.002 }], 55, 0.25)</code> should return <code>88400</code>.');
|
testString: assert.equal(knapsackUnbounded([{ name:"panacea", value:3000, weight:0.3, volume:0.025 }, { name:"ichor", value:1800, weight:0.2, volume:0.015 }, { name:"gold", value:2500, weight:2, volume:0.002 }], 55, 0.25), 88400);
|
||||||
- text: <code>knapsackUnbounded([{ name:"panacea", value:3000, weight:0.3, volume:0.025 }, { name:"ichor", value:1800, weight:0.2, volume:0.015 }, { name:"gold", value:2500, weight:2, volume:0.002 }], 25, 0.15)</code> should return <code>42500</code>.
|
- text: <code>knapsackUnbounded([{ name:"panacea", value:3000, weight:0.3, volume:0.025 }, { name:"ichor", value:1800, weight:0.2, volume:0.015 }, { name:"gold", value:2500, weight:2, volume:0.002 }], 25, 0.15)</code> should return <code>42500</code>.
|
||||||
testString: assert.equal(knapsackUnbounded([{ name:"panacea", value:3000, weight:0.3, volume:0.025 }, { name:"ichor", value:1800, weight:0.2, volume:0.015 }, { name:"gold", value:2500, weight:2, volume:0.002 }], 25, 0.15), 42500, '<code>knapsackUnbounded([{ name:"panacea", value:3000, weight:0.3, volume:0.025 }, { name:"ichor", value:1800, weight:0.2, volume:0.015 }, { name:"gold", value:2500, weight:2, volume:0.002 }], 25, 0.15)</code> should return <code>42500</code>.');
|
testString: assert.equal(knapsackUnbounded([{ name:"panacea", value:3000, weight:0.3, volume:0.025 }, { name:"ichor", value:1800, weight:0.2, volume:0.015 }, { name:"gold", value:2500, weight:2, volume:0.002 }], 25, 0.15), 42500);
|
||||||
- text: <code>knapsackUnbounded([{ name:"panacea", value:3000, weight:0.3, volume:0.025 }, { name:"ichor", value:1800, weight:0.2, volume:0.015 }, { name:"gold", value:2500, weight:2, volume:0.002 }], 35, 0.35)</code> should return <code>75300</code>.
|
- text: <code>knapsackUnbounded([{ name:"panacea", value:3000, weight:0.3, volume:0.025 }, { name:"ichor", value:1800, weight:0.2, volume:0.015 }, { name:"gold", value:2500, weight:2, volume:0.002 }], 35, 0.35)</code> should return <code>75300</code>.
|
||||||
testString: assert.equal(knapsackUnbounded([{ name:"panacea", value:3000, weight:0.3, volume:0.025 }, { name:"ichor", value:1800, weight:0.2, volume:0.015 }, { name:"gold", value:2500, weight:2, volume:0.002 }], 35, 0.35), 75300, '<code>knapsackUnbounded([{ name:"panacea", value:3000, weight:0.3, volume:0.025 }, { name:"ichor", value:1800, weight:0.2, volume:0.015 }, { name:"gold", value:2500, weight:2, volume:0.002 }], 35, 0.35)</code> should return <code>75300</code>.');
|
testString: assert.equal(knapsackUnbounded([{ name:"panacea", value:3000, weight:0.3, volume:0.025 }, { name:"ichor", value:1800, weight:0.2, volume:0.015 }, { name:"gold", value:2500, weight:2, volume:0.002 }], 35, 0.35), 75300);
|
||||||
- text: <code>knapsackUnbounded([{ name:"panacea", value:3000, weight:0.3, volume:0.025 }, { name:"ichor", value:1800, weight:0.2, volume:0.015 }, { name:"gold", value:2500, weight:2, volume:0.002 }], 15, 0.25)</code> should return <code>43200</code>.
|
- text: <code>knapsackUnbounded([{ name:"panacea", value:3000, weight:0.3, volume:0.025 }, { name:"ichor", value:1800, weight:0.2, volume:0.015 }, { name:"gold", value:2500, weight:2, volume:0.002 }], 15, 0.25)</code> should return <code>43200</code>.
|
||||||
testString: assert.equal(knapsackUnbounded([{ name:"panacea", value:3000, weight:0.3, volume:0.025 }, { name:"ichor", value:1800, weight:0.2, volume:0.015 }, { name:"gold", value:2500, weight:2, volume:0.002 }], 15, 0.25), 43200, '<code>knapsackUnbounded([{ name:"panacea", value:3000, weight:0.3, volume:0.025 }, { name:"ichor", value:1800, weight:0.2, volume:0.015 }, { name:"gold", value:2500, weight:2, volume:0.002 }], 15, 0.25)</code> should return <code>43200</code>.');
|
testString: assert.equal(knapsackUnbounded([{ name:"panacea", value:3000, weight:0.3, volume:0.025 }, { name:"ichor", value:1800, weight:0.2, volume:0.015 }, { name:"gold", value:2500, weight:2, volume:0.002 }], 15, 0.25), 43200);
|
||||||
```
|
```
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Challenge Seed
|
## Challenge Seed
|
||||||
|
|
||||||
<section id='challengeSeed'>
|
<section id='challengeSeed'>
|
||||||
<div id='js-seed'>
|
<div id='js-seed'>
|
||||||
|
|
||||||
@ -51,40 +55,41 @@ function knapsackUnbounded(items, maxweight, maxvolume) {
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Solution
|
## Solution
|
||||||
|
|
||||||
<section id='solution'>
|
<section id='solution'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function knapsackUnbounded(items, maxweight, maxvolume) {
|
function knapsackUnbounded(items, maxweight, maxvolume) {
|
||||||
var n = items.length;
|
var n = items.length;
|
||||||
var best_value = 0;
|
var best_value = 0;
|
||||||
var count = new Array(n)
|
var count = new Array(n);
|
||||||
var best = new Array(n)
|
var best = new Array(n);
|
||||||
function recurseKnapsack(i, value, weight, volume) {
|
function recurseKnapsack(i, value, weight, volume) {
|
||||||
var j, m1, m2, m;
|
var j, m1, m2, m;
|
||||||
if (i == n) {
|
if (i == n) {
|
||||||
if (value > best_value) {
|
if (value > best_value) {
|
||||||
best_value = value;
|
best_value = value;
|
||||||
for (j = 0; j < n; j++) {
|
for (j = 0; j < n; j++) {
|
||||||
best[j] = count[j];
|
best[j] = count[j];
|
||||||
}
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
m1 = Math.floor(weight / items[i].weight);
|
|
||||||
m2 = Math.floor(volume / items[i].volume);
|
|
||||||
m = m1 < m2 ? m1 : m2;
|
|
||||||
for (count[i] = m; count[i] >= 0; count[i]--) {
|
|
||||||
recurseKnapsack(
|
|
||||||
i + 1,
|
|
||||||
value + count[i] * items[i].value,
|
|
||||||
weight - count[i] * items[i].weight,
|
|
||||||
volume - count[i] * items[i].volume
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
m1 = Math.floor(weight / items[i].weight);
|
||||||
|
m2 = Math.floor(volume / items[i].volume);
|
||||||
|
m = m1 < m2 ? m1 : m2;
|
||||||
|
for (count[i] = m; count[i] >= 0; count[i]--) {
|
||||||
|
recurseKnapsack(
|
||||||
|
i + 1,
|
||||||
|
value + count[i] * items[i].value,
|
||||||
|
weight - count[i] * items[i].weight,
|
||||||
|
volume - count[i] * items[i].volume
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
recurseKnapsack(0, 0, maxweight, maxvolume);
|
recurseKnapsack(0, 0, maxweight, maxvolume);
|
||||||
return best_value;
|
return best_value;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -6,39 +6,43 @@ forumTopicId: 302297
|
|||||||
---
|
---
|
||||||
|
|
||||||
## Description
|
## Description
|
||||||
|
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
<a href="https://en.wikipedia.org/wiki/Knight%27s_tour">Knight's Tour</a>Problem: You have an empty <code>w</code> * <code>h</code> chessboard, but for a single knight on some square. The knight must perform a sequence of legal moves that result in the knight visiting every square on the chessboard exactly once. Note that it is <i>not</i> a requirement that the tour be "closed"; that is, the knight need not end within a single move of its start position.
|
<a href="https://en.wikipedia.org/wiki/Knight%27s_tour">Knight's Tour</a>Problem: You have an empty <code>w</code> * <code>h</code> chessboard, but for a single knight on some square. The knight must perform a sequence of legal moves that result in the knight visiting every square on the chessboard exactly once. Note that it is <i>not</i> a requirement that the tour be "closed"; that is, the knight need not end within a single move of its start position.
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
|
|
||||||
<section id='instructions'>
|
<section id='instructions'>
|
||||||
Write a function that takes <code>w</code> and <code>h</code> as parameters and returns the number of initial positions from where it is possible to achieve the task stated above.
|
Write a function that takes <code>w</code> and <code>h</code> as parameters and returns the number of initial positions from where it is possible to achieve the task stated above.
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
|
|
||||||
<section id='tests'>
|
<section id='tests'>
|
||||||
|
|
||||||
``` yml
|
```yml
|
||||||
tests:
|
tests:
|
||||||
- text: <code>knightTour</code> should be a function.
|
- text: <code>knightTour</code> should be a function.
|
||||||
testString: assert(typeof knightTour == 'function', '<code>knightTour</code> should be a function.');
|
testString: assert(typeof knightTour == 'function');
|
||||||
- text: <code>knightTour(6, 6)</code> should return a number.
|
- text: <code>knightTour(6, 6)</code> should return a number.
|
||||||
testString: assert(typeof knightTour(6, 6) == 'number', '<code>knightTour(6, 6)</code> should return a number.');
|
testString: assert(typeof knightTour(6, 6) == 'number');
|
||||||
- text: <code>knightTour(6, 6)</code> should return <code>35</code>.
|
- text: <code>knightTour(6, 6)</code> should return <code>35</code>.
|
||||||
testString: assert.equal(knightTour(6, 6), 35, '<code>knightTour(6, 6)</code> should return <code>35</code>.');
|
testString: assert.equal(knightTour(6, 6), 35);
|
||||||
- text: <code>knightTour(5, 6)</code> should return <code>20</code>.
|
- text: <code>knightTour(5, 6)</code> should return <code>20</code>.
|
||||||
testString: assert.equal(knightTour(5, 6), 20, '<code>knightTour(5, 6)</code> should return <code>20</code>.');
|
testString: assert.equal(knightTour(5, 6), 20);
|
||||||
- text: <code>knightTour(4, 6)</code> should return <code>10</code>.
|
- text: <code>knightTour(4, 6)</code> should return <code>10</code>.
|
||||||
testString: assert.equal(knightTour(4, 6), 10, '<code>knightTour(4, 6)</code> should return <code>10</code>.');
|
testString: assert.equal(knightTour(4, 6), 10);
|
||||||
- text: <code>knightTour(7, 3)</code> should return <code>4</code>.
|
- text: <code>knightTour(7, 3)</code> should return <code>4</code>.
|
||||||
testString: assert.equal(knightTour(7, 3), 4, '<code>knightTour(7, 3)</code> should return <code>4</code>.');
|
testString: assert.equal(knightTour(7, 3), 4);
|
||||||
- text: <code>knightTour(8, 6)</code> should return <code>47</code>.
|
- text: <code>knightTour(8, 6)</code> should return <code>47</code>.
|
||||||
testString: assert.equal(knightTour(8, 6), 47, '<code>knightTour(8, 6)</code> should return <code>47</code>.');
|
testString: assert.equal(knightTour(8, 6), 47);
|
||||||
```
|
```
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Challenge Seed
|
## Challenge Seed
|
||||||
|
|
||||||
<section id='challengeSeed'>
|
<section id='challengeSeed'>
|
||||||
<div id='js-seed'>
|
<div id='js-seed'>
|
||||||
|
|
||||||
@ -52,85 +56,84 @@ function knightTour(w, h) {
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Solution
|
## Solution
|
||||||
|
|
||||||
<section id='solution'>
|
<section id='solution'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function knightTour (w, h) {
|
function knightTour(w, h) {
|
||||||
var b, cnt=0;
|
var b,
|
||||||
|
cnt = 0;
|
||||||
|
|
||||||
var dx = [ -2, -2, -1, 1, 2, 2, 1, -1 ];
|
var dx = [-2, -2, -1, 1, 2, 2, 1, -1];
|
||||||
var dy = [ -1, 1, 2, 2, 1, -1, -2, -2 ];
|
var dy = [-1, 1, 2, 2, 1, -1, -2, -2];
|
||||||
|
|
||||||
function init_board()
|
function init_board() {
|
||||||
{
|
var i, j, k, x, y;
|
||||||
var i, j, k, x, y;
|
// * b is board; a is board with 2 rows padded at each side
|
||||||
// * b is board; a is board with 2 rows padded at each side
|
|
||||||
|
|
||||||
for(i=0;i<h;i++){
|
for (i = 0; i < h; i++) {
|
||||||
for(j=0;j<w;j++){
|
for (j = 0; j < w; j++) {
|
||||||
b[i][j]=255
|
b[i][j] = 255;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i < h; i++) {
|
for (i = 0; i < h; i++) {
|
||||||
for (j = 0; j < w; j++) {
|
for (j = 0; j < w; j++) {
|
||||||
for (k = 0; k < 8; k++) {
|
for (k = 0; k < 8; k++) {
|
||||||
x = j + dx[k], y = i + dy[k];
|
(x = j + dx[k]), (y = i + dy[k]);
|
||||||
if (b[i][j] == 255) b[i][j] = 0;
|
if (b[i][j] == 255) b[i][j] = 0;
|
||||||
if(x >= 0 && x < w && y >= 0 && y < h) b[i][j]++;
|
if (x >= 0 && x < w && y >= 0 && y < h) b[i][j]++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function walk_board(x, y)
|
function walk_board(x, y) {
|
||||||
{
|
var i, nx, ny, least;
|
||||||
var i, nx, ny, least;
|
var steps = 0;
|
||||||
var steps = 0;
|
// printf(E"H"E"J"E"%d;%dH"E"32m[]"E"m", y + 1, 1 + 2 * x);
|
||||||
// printf(E"H"E"J"E"%d;%dH"E"32m[]"E"m", y + 1, 1 + 2 * x);
|
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
// * occupy cell
|
// * occupy cell
|
||||||
b[y][x] = 255;
|
b[y][x] = 255;
|
||||||
|
|
||||||
// * reduce all neighbors' neighbor count
|
// * reduce all neighbors' neighbor count
|
||||||
for (i = 0; i < 8; i++)
|
for (i = 0; i < 8; i++)
|
||||||
if(y+dy[i] >= 0 && x+dx[i] >= 0 && y+dy[i] < h && x+dx[i] < w)
|
if (y + dy[i] >= 0 && x + dx[i] >= 0 && y + dy[i] < h && x + dx[i] < w)
|
||||||
b[ y + dy[i] ][ x + dx[i] ]--;
|
b[y + dy[i]][x + dx[i]]--;
|
||||||
|
|
||||||
// find neighbor with lowest neighbor count
|
// find neighbor with lowest neighbor count
|
||||||
least = 255;
|
least = 255;
|
||||||
for (i = 0; i < 8; i++) {
|
for (i = 0; i < 8; i++) {
|
||||||
if(y+dy[i] >= 0 && x+dx[i] >= 0 && y+dy[i] < h && x+dx[i] < w)
|
if (y + dy[i] >= 0 && x + dx[i] >= 0 && y + dy[i] < h && x + dx[i] < w)
|
||||||
if (b[ y + dy[i] ][ x + dx[i] ] < least) {
|
if (b[y + dy[i]][x + dx[i]] < least) {
|
||||||
nx = x + dx[i];
|
nx = x + dx[i];
|
||||||
ny = y + dy[i];
|
ny = y + dy[i];
|
||||||
least = b[ny][nx];
|
least = b[ny][nx];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (least > 7) {
|
if (least > 7) {
|
||||||
return steps == w * h - 1;
|
return steps == w * h - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
steps++;
|
steps++;
|
||||||
x = nx, y = ny;
|
(x = nx), (y = ny);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function solve (x, y) {
|
function solve(x, y) {
|
||||||
b=new Array(h);
|
b = new Array(h);
|
||||||
for(var i=0;i<h;i++)
|
for (var i = 0; i < h; i++) b[i] = new Array(w);
|
||||||
b[i]=new Array(w)
|
|
||||||
|
|
||||||
init_board();
|
init_board();
|
||||||
if (walk_board(x, y)) {
|
if (walk_board(x, y)) {
|
||||||
cnt++;
|
cnt++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for(var i=0;i<h;i++){
|
for (var i = 0; i < h; i++) {
|
||||||
for(var j=0;j<w;j++){
|
for (var j = 0; j < w; j++) {
|
||||||
solve(j, i);
|
solve(j, i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,40 +6,45 @@ forumTopicId: 302298
|
|||||||
---
|
---
|
||||||
|
|
||||||
## Description
|
## Description
|
||||||
|
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
|
|
||||||
Given a set of positive integers, write a function to order the integers in such a way that the concatenation of the numbers forms the largest possible integer and return this integer.
|
Given a set of positive integers, write a function to order the integers in such a way that the concatenation of the numbers forms the largest possible integer and return this integer.
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
|
|
||||||
<section id='instructions'>
|
<section id='instructions'>
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
|
|
||||||
<section id='tests'>
|
<section id='tests'>
|
||||||
|
|
||||||
``` yml
|
```yml
|
||||||
tests:
|
tests:
|
||||||
- text: <code>maxCombine</code> should be a function.
|
- text: <code>maxCombine</code> should be a function.
|
||||||
testString: assert(typeof maxCombine == 'function', '<code>maxCombine</code> should be a function.');
|
testString: assert(typeof maxCombine == 'function');
|
||||||
- text: <code>maxCombine([1, 3, 3, 4, 55])</code> should return a number.
|
- text: <code>maxCombine([1, 3, 3, 4, 55])</code> should return a number.
|
||||||
testString: assert(typeof maxCombine([1, 3, 3, 4, 55]) == 'number', '<code>maxCombine([1, 3, 3, 4, 55])</code> should return a number.');
|
testString: assert(typeof maxCombine([1, 3, 3, 4, 55]) == 'number');
|
||||||
- text: <code>maxCombine([1, 3, 3, 4, 55])</code> should return <code>554331</code>.
|
- text: <code>maxCombine([1, 3, 3, 4, 55])</code> should return <code>554331</code>.
|
||||||
testString: assert.equal(maxCombine([1, 3, 3, 4, 55]), 554331, '<code>maxCombine([1, 3, 3, 4, 55])</code> should return <code>554331</code>.');
|
testString: assert.equal(maxCombine([1, 3, 3, 4, 55]), 554331);
|
||||||
- text: <code>maxCombine([71, 45, 23, 4, 5])</code> should return <code>71545423</code>.
|
- text: <code>maxCombine([71, 45, 23, 4, 5])</code> should return <code>71545423</code>.
|
||||||
testString: assert.equal(maxCombine([71, 45, 23, 4, 5]), 71545423, '<code>maxCombine([71, 45, 23, 4, 5])</code> should return <code>71545423</code>.');
|
testString: assert.equal(maxCombine([71, 45, 23, 4, 5]), 71545423);
|
||||||
- text: <code>maxCombine([14, 43, 53, 114, 55])</code> should return <code>55534314114</code>.
|
- text: <code>maxCombine([14, 43, 53, 114, 55])</code> should return <code>55534314114</code>.
|
||||||
testString: assert.equal(maxCombine([14, 43, 53, 114, 55]), 55534314114, '<code>maxCombine([14, 43, 53, 114, 55])</code> should return <code>55534314114</code>.');
|
testString: assert.equal(maxCombine([14, 43, 53, 114, 55]), 55534314114);
|
||||||
- text: <code>maxCombine([1, 34, 3, 98, 9, 76, 45, 4])</code> should return <code>998764543431</code>.
|
- text: <code>maxCombine([1, 34, 3, 98, 9, 76, 45, 4])</code> should return <code>998764543431</code>.
|
||||||
testString: assert.equal(maxCombine([1, 34, 3, 98, 9, 76, 45, 4]), 998764543431, '<code>maxCombine([1, 34, 3, 98, 9, 76, 45, 4])</code> should return <code>998764543431</code>.');
|
testString: assert.equal(maxCombine([1, 34, 3, 98, 9, 76, 45, 4]), 998764543431);
|
||||||
- text: <code>maxCombine([54, 546, 548, 60])</code> should return <code>6054854654</code>.
|
- text: <code>maxCombine([54, 546, 548, 60])</code> should return <code>6054854654</code>.
|
||||||
testString: assert.equal(maxCombine([54, 546, 548, 60]), 6054854654, '<code>maxCombine([54, 546, 548, 60])</code> should return <code>6054854654</code>.');
|
testString: assert.equal(maxCombine([54, 546, 548, 60]), 6054854654);
|
||||||
```
|
```
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Challenge Seed
|
## Challenge Seed
|
||||||
|
|
||||||
<section id='challengeSeed'>
|
<section id='challengeSeed'>
|
||||||
|
|
||||||
<div id='js-seed'>
|
<div id='js-seed'>
|
||||||
@ -54,23 +59,24 @@ function maxCombine(xs) {
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Solution
|
## Solution
|
||||||
|
|
||||||
<section id='solution'>
|
<section id='solution'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function maxCombine (xs) {
|
function maxCombine(xs) {
|
||||||
return parseInt(
|
return parseInt(
|
||||||
xs.sort(
|
xs
|
||||||
function (x, y) {
|
.sort(function(x, y) {
|
||||||
var a = x.toString(),
|
var a = x.toString(),
|
||||||
b = y.toString(),
|
b = y.toString(),
|
||||||
ab = parseInt(a + b),
|
ab = parseInt(a + b),
|
||||||
ba = parseInt(b + a);
|
ba = parseInt(b + a);
|
||||||
|
|
||||||
return ab > ba ? -1 : (ab < ba ? 1 : 0);
|
return ab > ba ? -1 : ab < ba ? 1 : 0;
|
||||||
}
|
})
|
||||||
)
|
.join(''),
|
||||||
.join(''), 10
|
10
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -6,46 +6,51 @@ forumTopicId: 302299
|
|||||||
---
|
---
|
||||||
|
|
||||||
## Description
|
## Description
|
||||||
|
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
|
|
||||||
Write a function that returns the date of the last Friday of a given month for a given year.
|
Write a function that returns the date of the last Friday of a given month for a given year.
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
|
|
||||||
<section id='instructions'>
|
<section id='instructions'>
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
|
|
||||||
<section id='tests'>
|
<section id='tests'>
|
||||||
|
|
||||||
``` yml
|
```yml
|
||||||
tests:
|
tests:
|
||||||
- text: <code>lastFriday</code> should be a function.
|
- text: <code>lastFriday</code> should be a function.
|
||||||
testString: assert(typeof lastFriday == 'function', '<code>lastFriday</code> should be a function.');
|
testString: assert(typeof lastFriday == 'function');
|
||||||
- text: <code>lastFriday(2018, 1)</code> should return a number.
|
- text: <code>lastFriday(2018, 1)</code> should return a number.
|
||||||
testString: assert(typeof lastFriday(2018, 1) == 'number', '<code>lastFriday(2018, 1)</code> should return a number.');
|
testString: assert(typeof lastFriday(2018, 1) == 'number');
|
||||||
- text: <code>lastFriday(2018, 1)</code> should return <code>26</code>.
|
- text: <code>lastFriday(2018, 1)</code> should return <code>26</code>.
|
||||||
testString: assert.equal(lastFriday(2018, 1), 26, '<code>lastFriday(2018, 1)</code> should return <code>26</code>.');
|
testString: assert.equal(lastFriday(2018, 1), 26);
|
||||||
- text: <code>lastFriday(2017, 2)</code> should return <code>24</code>.
|
- text: <code>lastFriday(2017, 2)</code> should return <code>24</code>.
|
||||||
testString: assert.equal(lastFriday(2017, 2), 24, '<code>lastFriday(2017, 2)</code> should return <code>24</code>.');
|
testString: assert.equal(lastFriday(2017, 2), 24);
|
||||||
- text: <code>lastFriday(2012, 3)</code> should return <code>30</code>.
|
- text: <code>lastFriday(2012, 3)</code> should return <code>30</code>.
|
||||||
testString: assert.equal(lastFriday(2012, 3), 30, '<code>lastFriday(2012, 3)</code> should return <code>30</code>.');
|
testString: assert.equal(lastFriday(2012, 3), 30);
|
||||||
- text: <code>lastFriday(1900, 4)</code> should return <code>27</code>.
|
- text: <code>lastFriday(1900, 4)</code> should return <code>27</code>.
|
||||||
testString: assert.equal(lastFriday(1900, 4), 27, '<code>lastFriday(1900, 4)</code> should return <code>27</code>.');
|
testString: assert.equal(lastFriday(1900, 4), 27);
|
||||||
- text: <code>lastFriday(2000, 5)</code> should return <code>26</code>.
|
- text: <code>lastFriday(2000, 5)</code> should return <code>26</code>.
|
||||||
testString: assert.equal(lastFriday(2000, 5), 26, '<code>lastFriday(2000, 5)</code> should return <code>26</code>.');
|
testString: assert.equal(lastFriday(2000, 5), 26);
|
||||||
- text: <code>lastFriday(2006, 6)</code> should return <code>30</code>.
|
- text: <code>lastFriday(2006, 6)</code> should return <code>30</code>.
|
||||||
testString: assert.equal(lastFriday(2006, 6), 30, '<code>lastFriday(2006, 6)</code> should return <code>30</code>.');
|
testString: assert.equal(lastFriday(2006, 6), 30);
|
||||||
- text: <code>lastFriday(2010, 7)</code> should return <code>30</code>.
|
- text: <code>lastFriday(2010, 7)</code> should return <code>30</code>.
|
||||||
testString: assert.equal(lastFriday(2010, 7), 30, '<code>lastFriday(2010, 7)</code> should return <code>30</code>.');
|
testString: assert.equal(lastFriday(2010, 7), 30);
|
||||||
- text: <code>lastFriday(2005, 8)</code> should return <code>26</code>.
|
- text: <code>lastFriday(2005, 8)</code> should return <code>26</code>.
|
||||||
testString: assert.equal(lastFriday(2005, 8), 26, '<code>lastFriday(2005, 8)</code> should return <code>26</code>.');
|
testString: assert.equal(lastFriday(2005, 8), 26);
|
||||||
```
|
```
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Challenge Seed
|
## Challenge Seed
|
||||||
|
|
||||||
<section id='challengeSeed'>
|
<section id='challengeSeed'>
|
||||||
|
|
||||||
<div id='js-seed'>
|
<div id='js-seed'>
|
||||||
@ -60,10 +65,11 @@ function lastFriday(year, month) {
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Solution
|
## Solution
|
||||||
|
|
||||||
<section id='solution'>
|
<section id='solution'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function lastFriday (year, month) {
|
function lastFriday(year, month) {
|
||||||
var i, last_day;
|
var i, last_day;
|
||||||
i = 0;
|
i = 0;
|
||||||
while (true) {
|
while (true) {
|
||||||
@ -73,7 +79,7 @@ function lastFriday (year, month) {
|
|||||||
}
|
}
|
||||||
i -= 1;
|
i -= 1;
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
@ -6,42 +6,47 @@ forumTopicId: 302300
|
|||||||
---
|
---
|
||||||
|
|
||||||
## Description
|
## Description
|
||||||
|
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
|
|
||||||
Determine whether a given year is a leap year in the Gregorian calendar.
|
Determine whether a given year is a leap year in the Gregorian calendar.
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
|
|
||||||
<section id='instructions'>
|
<section id='instructions'>
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
|
|
||||||
<section id='tests'>
|
<section id='tests'>
|
||||||
|
|
||||||
``` yml
|
```yml
|
||||||
tests:
|
tests:
|
||||||
- text: <code>isLeapYear</code> should be a function.
|
- text: <code>isLeapYear</code> should be a function.
|
||||||
testString: assert(typeof isLeapYear == 'function', '<code>isLeapYear</code> should be a function.');
|
testString: assert(typeof isLeapYear == 'function');
|
||||||
- text: <code>isLeapYear()</code> should return a boolean.
|
- text: <code>isLeapYear()</code> should return a boolean.
|
||||||
testString: assert(typeof isLeapYear(2018) == 'boolean', '<code>isLeapYear()</code> should return a boolean.');
|
testString: assert(typeof isLeapYear(2018) == 'boolean');
|
||||||
- text: <code>isLeapYear(2018)</code> should return <code>false</code>.
|
- text: <code>isLeapYear(2018)</code> should return <code>false</code>.
|
||||||
testString: assert.equal(isLeapYear(2018), false, '<code>isLeapYear(2018)</code> should return <code>false</code>.');
|
testString: assert.equal(isLeapYear(2018), false);
|
||||||
- text: <code>isLeapYear(2016)</code> should return <code>true</code>.
|
- text: <code>isLeapYear(2016)</code> should return <code>true</code>.
|
||||||
testString: assert.equal(isLeapYear(2016), true, '<code>isLeapYear(2016)</code> should return <code>true</code>.');
|
testString: assert.equal(isLeapYear(2016), true);
|
||||||
- text: <code>isLeapYear(2000)</code> should return <code>true</code>.
|
- text: <code>isLeapYear(2000)</code> should return <code>true</code>.
|
||||||
testString: assert.equal(isLeapYear(2000), true, '<code>isLeapYear(2000)</code> should return <code>true</code>.');
|
testString: assert.equal(isLeapYear(2000), true);
|
||||||
- text: <code>isLeapYear(1900)</code> should return <code>false</code>.
|
- text: <code>isLeapYear(1900)</code> should return <code>false</code>.
|
||||||
testString: assert.equal(isLeapYear(1900), false, '<code>isLeapYear(1900)</code> should return <code>false</code>.');
|
testString: assert.equal(isLeapYear(1900), false);
|
||||||
- text: <code>isLeapYear(1996)</code> should return <code>true</code>.
|
- text: <code>isLeapYear(1996)</code> should return <code>true</code>.
|
||||||
testString: assert.equal(isLeapYear(1996), true, '<code>isLeapYear(1996)</code> should return <code>true</code>.');
|
testString: assert.equal(isLeapYear(1996), true);
|
||||||
- text: <code>isLeapYear(1800)</code> should return <code>false</code>.
|
- text: <code>isLeapYear(1800)</code> should return <code>false</code>.
|
||||||
testString: assert.equal(isLeapYear(1800), false, '<code>isLeapYear(1800)</code> should return <code>false</code>.');
|
testString: assert.equal(isLeapYear(1800), false);
|
||||||
```
|
```
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Challenge Seed
|
## Challenge Seed
|
||||||
|
|
||||||
<section id='challengeSeed'>
|
<section id='challengeSeed'>
|
||||||
|
|
||||||
<div id='js-seed'>
|
<div id='js-seed'>
|
||||||
@ -56,12 +61,13 @@ function isLeapYear(year) {
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Solution
|
## Solution
|
||||||
|
|
||||||
<section id='solution'>
|
<section id='solution'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function isLeapYear (year) {
|
function isLeapYear(year) {
|
||||||
return (year % 100 === 0) ? (year % 400 === 0) : (year % 4 === 0);
|
return year % 100 === 0 ? year % 400 === 0 : year % 4 === 0;
|
||||||
};
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
@ -6,45 +6,51 @@ forumTopicId: 302301
|
|||||||
---
|
---
|
||||||
|
|
||||||
## Description
|
## Description
|
||||||
|
|
||||||
<section id='description'>
|
<section id='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 <i>m</i> or <i>n</i> is zero, then the least common multiple is zero.
|
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 <i>m</i> or <i>n</i> is zero, then the least common multiple is zero.
|
||||||
One way to calculate the least common multiple is to iterate all the multiples of <i>m</i>, until you find one that is also a multiple of <i>n</i>.
|
One way to calculate the least common multiple is to iterate all the multiples of <i>m</i>, until you find one that is also a multiple of <i>n</i>.
|
||||||
If you already have <i>gcd</i> for <a href="https://rosettacode.org/wiki/greatest common divisor" target="_blank">greatest common divisor</a>, then this formula calculates <i>lcm</i>.
|
If you already have <i>gcd</i> for <a href="https://rosettacode.org/wiki/greatest common divisor" target="_blank">greatest common divisor</a>, then this formula calculates <i>lcm</i>.
|
||||||
\( \operatorname{lcm}(m, n) = \frac{|m \times n|}{\operatorname{gcd}(m, n)} \)
|
\( \operatorname{lcm}(m, n) = \frac{|m \times n|}{\operatorname{gcd}(m, n)} \)
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
|
|
||||||
<section id='instructions'>
|
<section id='instructions'>
|
||||||
|
|
||||||
Compute the least common multiple of an array of integers.
|
Compute the least common multiple of an array of integers.
|
||||||
Given <i>m</i> and <i>n</i>, the least common multiple is the smallest positive integer that has both <i>m</i> and <i>n</i> as factors.
|
Given <i>m</i> and <i>n</i>, the least common multiple is the smallest positive integer that has both <i>m</i> and <i>n</i> as factors.
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
|
|
||||||
<section id='tests'>
|
<section id='tests'>
|
||||||
|
|
||||||
``` yml
|
```yml
|
||||||
tests:
|
tests:
|
||||||
- text: <code>LCM</code> should be a function.
|
- text: <code>LCM</code> should be a function.
|
||||||
testString: assert(typeof LCM == 'function', '<code>LCM</code> should be a function.');
|
testString: assert(typeof LCM == 'function');
|
||||||
- text: <code>LCM([2, 4, 8])</code> should return a number.
|
- text: <code>LCM([2, 4, 8])</code> should return a number.
|
||||||
testString: assert(typeof LCM([2, 4, 8]) == 'number', '<code>LCM([2, 4, 8])</code> should return a number.');
|
testString: assert(typeof LCM([2, 4, 8]) == 'number');
|
||||||
- text: <code>LCM([2, 4, 8])</code> should return <code>8</code>.
|
- text: <code>LCM([2, 4, 8])</code> should return <code>8</code>.
|
||||||
testString: assert.equal(LCM([2, 4, 8]), 8, '<code>LCM([2, 4, 8])</code> should return <code>8</code>.');
|
testString: assert.equal(LCM([2, 4, 8]), 8);
|
||||||
- text: <code>LCM([4, 8, 12])</code> should return <code>24</code>.
|
- text: <code>LCM([4, 8, 12])</code> should return <code>24</code>.
|
||||||
testString: assert.equal(LCM([4, 8, 12]), 24, '<code>LCM([4, 8, 12])</code> should return <code>24</code>.');
|
testString: assert.equal(LCM([4, 8, 12]), 24);
|
||||||
- text: <code>LCM([3, 4, 5, 12, 40])</code> should return <code>120</code>.
|
- text: <code>LCM([3, 4, 5, 12, 40])</code> should return <code>120</code>.
|
||||||
testString: assert.equal(LCM([3, 4, 5, 12, 40]), 120, '<code>LCM([3, 4, 5, 12, 40])</code> should return <code>120</code>.');
|
testString: assert.equal(LCM([3, 4, 5, 12, 40]), 120);
|
||||||
- text: <code>LCM([11, 33, 90])</code> should return <code>990</code>.
|
- text: <code>LCM([11, 33, 90])</code> should return <code>990</code>.
|
||||||
testString: assert.equal(LCM([11, 33, 90]), 990, '<code>LCM([11, 33, 90])</code> should return <code>990</code>.');
|
testString: assert.equal(LCM([11, 33, 90]), 990);
|
||||||
- text: <code>LCM([-50, 25, -45, -18, 90, 447])</code> should return <code>67050</code>.
|
- text: <code>LCM([-50, 25, -45, -18, 90, 447])</code> should return <code>67050</code>.
|
||||||
testString: assert.equal(LCM([-50, 25, -45, -18, 90, 447]), 67050, '<code>LCM([-50, 25, -45, -18, 90, 447])</code> should return <code>67050</code>.');
|
testString: assert.equal(LCM([-50, 25, -45, -18, 90, 447]), 67050);
|
||||||
```
|
```
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Challenge Seed
|
## Challenge Seed
|
||||||
|
|
||||||
<section id='challengeSeed'>
|
<section id='challengeSeed'>
|
||||||
|
|
||||||
<div id='js-seed'>
|
<div id='js-seed'>
|
||||||
@ -59,17 +65,22 @@ function LCM(A) {
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Solution
|
## Solution
|
||||||
|
|
||||||
<section id='solution'>
|
<section id='solution'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function LCM (A) {
|
function LCM(A) {
|
||||||
var n = A.length, a = Math.abs(A[0]);
|
var n = A.length,
|
||||||
for (var i = 1; i < n; i++)
|
a = Math.abs(A[0]);
|
||||||
{ var b = Math.abs(A[i]), c = a;
|
for (var i = 1; i < n; i++) {
|
||||||
while (a && b){ a > b ? a %= b : b %= a; }
|
var b = Math.abs(A[i]),
|
||||||
a = Math.abs(c*A[i])/(a+b);
|
c = a;
|
||||||
}
|
while (a && b) {
|
||||||
return a;
|
a > b ? (a %= b) : (b %= a);
|
||||||
|
}
|
||||||
|
a = Math.abs(c * A[i]) / (a + b);
|
||||||
|
}
|
||||||
|
return a;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -6,6 +6,7 @@ forumTopicId: 302302
|
|||||||
---
|
---
|
||||||
|
|
||||||
## Description
|
## Description
|
||||||
|
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
<b>Left factorials</b>, $ !n $, may refer to either <i>subfactorials</i> or to <i>factorial sums</i>. The same notation can be confusingly seen used for the two different definitions. Sometimes, <i>subfactorials</i> (also known as <i>derangements</i>) may use any of the notations:
|
<b>Left factorials</b>, $ !n $, may refer to either <i>subfactorials</i> or to <i>factorial sums</i>. The same notation can be confusingly seen used for the two different definitions. Sometimes, <i>subfactorials</i> (also known as <i>derangements</i>) may use any of the notations:
|
||||||
<ul>
|
<ul>
|
||||||
@ -19,38 +20,41 @@ where $!0 = 0$
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
|
|
||||||
<section id='instructions'>
|
<section id='instructions'>
|
||||||
Write a function to calculate the left factorial of a given number.
|
Write a function to calculate the left factorial of a given number.
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
|
|
||||||
<section id='tests'>
|
<section id='tests'>
|
||||||
|
|
||||||
``` yml
|
```yml
|
||||||
tests:
|
tests:
|
||||||
- text: <code>leftFactorial</code> should be a function.
|
- text: <code>leftFactorial</code> should be a function.
|
||||||
testString: assert(typeof leftFactorial == 'function', '<code>leftFactorial</code> should be a function.');
|
testString: assert(typeof leftFactorial == 'function');
|
||||||
- text: <code>leftFactorial(0)</code> should return a number.
|
- text: <code>leftFactorial(0)</code> should return a number.
|
||||||
testString: assert(typeof leftFactorial(0) == 'number', '<code>leftFactorial(0)</code> should return a number.');
|
testString: assert(typeof leftFactorial(0) == 'number');
|
||||||
- text: <code>leftFactorial(0)</code> should return <code>0</code>.
|
- text: <code>leftFactorial(0)</code> should return <code>0</code>.
|
||||||
testString: assert.equal(leftFactorial(0), 0, '<code>leftFactorial(0)</code> should return <code>0</code>.');
|
testString: assert.equal(leftFactorial(0), 0);
|
||||||
- text: <code>leftFactorial(1)</code> should return <code>1</code>.
|
- text: <code>leftFactorial(1)</code> should return <code>1</code>.
|
||||||
testString: assert.equal(leftFactorial(1), 1, '<code>leftFactorial(1)</code> should return <code>1</code>.');
|
testString: assert.equal(leftFactorial(1), 1);
|
||||||
- text: <code>leftFactorial(2)</code> should return <code>2</code>.
|
- text: <code>leftFactorial(2)</code> should return <code>2</code>.
|
||||||
testString: assert.equal(leftFactorial(2), 2, '<code>leftFactorial(2)</code> should return <code>2</code>.');
|
testString: assert.equal(leftFactorial(2), 2);
|
||||||
- text: <code>leftFactorial(3)</code> should return <code>4</code>.
|
- text: <code>leftFactorial(3)</code> should return <code>4</code>.
|
||||||
testString: assert.equal(leftFactorial(3), 4, '<code>leftFactorial(3)</code> should return <code>4</code>.');
|
testString: assert.equal(leftFactorial(3), 4);
|
||||||
- text: <code>leftFactorial(10)</code> should return <code>409114</code>.
|
- text: <code>leftFactorial(10)</code> should return <code>409114</code>.
|
||||||
testString: assert.equal(leftFactorial(10), 409114, '<code>leftFactorial(10)</code> should return <code>409114</code>.');
|
testString: assert.equal(leftFactorial(10), 409114);
|
||||||
- text: <code>leftFactorial(17)</code> should return <code>22324392524314</code>.
|
- text: <code>leftFactorial(17)</code> should return <code>22324392524314</code>.
|
||||||
testString: assert.equal(leftFactorial(17), 22324392524314, '<code>leftFactorial(17)</code> should return <code>22324392524314</code>.');
|
testString: assert.equal(leftFactorial(17), 22324392524314);
|
||||||
- text: <code>leftFactorial(19)</code> should return <code>6780385526348314</code>.
|
- text: <code>leftFactorial(19)</code> should return <code>6780385526348314</code>.
|
||||||
testString: assert.equal(leftFactorial(19), 6780385526348314, '<code>leftFactorial(19)</code> should return <code>6780385526348314</code>.');
|
testString: assert.equal(leftFactorial(19), 6780385526348314);
|
||||||
```
|
```
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Challenge Seed
|
## Challenge Seed
|
||||||
|
|
||||||
<section id='challengeSeed'>
|
<section id='challengeSeed'>
|
||||||
<div id='js-seed'>
|
<div id='js-seed'>
|
||||||
|
|
||||||
@ -64,26 +68,26 @@ function leftFactorial(n) {
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Solution
|
## Solution
|
||||||
|
|
||||||
<section id='solution'>
|
<section id='solution'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function leftFactorial(n) {
|
function leftFactorial(n) {
|
||||||
if (n == 0)
|
if (n == 0) return 0;
|
||||||
return 0
|
if (n == 1) return 1;
|
||||||
if (n == 1)
|
|
||||||
return 1;
|
|
||||||
|
|
||||||
// Note: for n>=20, the result may not be correct.
|
// Note: for n>=20, the result may not be correct.
|
||||||
// This is because JavaScript uses 53 bit integers and
|
// This is because JavaScript uses 53 bit integers and
|
||||||
// for n>=20 result becomes too large.
|
// for n>=20 result becomes too large.
|
||||||
|
|
||||||
let res = 2, fact = 2;
|
let res = 2,
|
||||||
for (var i = 2; i < n; i++) {
|
fact = 2;
|
||||||
res += fact;
|
for (var i = 2; i < n; i++) {
|
||||||
fact *= (i + 1);
|
res += fact;
|
||||||
}
|
fact *= i + 1;
|
||||||
|
}
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -6,6 +6,7 @@ forumTopicId: 302307
|
|||||||
---
|
---
|
||||||
|
|
||||||
## Description
|
## Description
|
||||||
|
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
Given a list of values and a set of integer indices into that value list, the task is to sort the values at the given indices, but preserving the values at indices outside the set of those to be sorted.
|
Given a list of values and a set of integer indices into that value list, the task is to sort the values at the given indices, but preserving the values at indices outside the set of those to be sorted.
|
||||||
Make your function work with the following list of values and set of indices:
|
Make your function work with the following list of values and set of indices:
|
||||||
@ -16,34 +17,37 @@ Where the correct result would be:
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
|
|
||||||
<section id='instructions'>
|
<section id='instructions'>
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
|
|
||||||
<section id='tests'>
|
<section id='tests'>
|
||||||
|
|
||||||
``` yml
|
```yml
|
||||||
tests:
|
tests:
|
||||||
- text: <code>sortDisjoint</code> should be a function.
|
- text: <code>sortDisjoint</code> should be a function.
|
||||||
testString: assert(typeof sortDisjoint == 'function', '<code>sortDisjoint</code> should be a function.');
|
testString: assert(typeof sortDisjoint == 'function');
|
||||||
- text: <code>sortDisjoint([7, 6, 5, 4, 3, 2, 1, 0], [6, 1, 7])</code> should return an array.
|
- text: <code>sortDisjoint([7, 6, 5, 4, 3, 2, 1, 0], [6, 1, 7])</code> should return an array.
|
||||||
testString: assert(Array.isArray(sortDisjoint([7, 6, 5, 4, 3, 2, 1, 0], [6, 1, 7])), '<code>sortDisjoint([7, 6, 5, 4, 3, 2, 1, 0], [6, 1, 7])</code> should return an array.');
|
testString: assert(Array.isArray(sortDisjoint([7, 6, 5, 4, 3, 2, 1, 0], [6, 1, 7])));
|
||||||
- text: <code>sortDisjoint([7, 6, 5, 4, 3, 2, 1, 0], [6, 1, 7])</code> should return <code>[7, 0, 5, 4, 3, 2, 1, 6]</code>.
|
- text: <code>sortDisjoint([7, 6, 5, 4, 3, 2, 1, 0], [6, 1, 7])</code> should return <code>[7, 0, 5, 4, 3, 2, 1, 6]</code>.
|
||||||
testString: assert.deepEqual(sortDisjoint([7, 6, 5, 4, 3, 2, 1, 0], [6, 1, 7]), [7, 0, 5, 4, 3, 2, 1, 6], '<code>sortDisjoint([7, 6, 5, 4, 3, 2, 1, 0], [6, 1, 7])</code> should return <code>[7, 0, 5, 4, 3, 2, 1, 6]</code>.');
|
testString: assert.deepEqual(sortDisjoint([7, 6, 5, 4, 3, 2, 1, 0], [6, 1, 7]), [7, 0, 5, 4, 3, 2, 1, 6]);
|
||||||
- text: <code>sortDisjoint([7, 6, 5, 4, 3, 2, 1, 0], [1, 2, 5, 6])</code> should return <code>[7, 1, 2, 4, 3, 5, 6, 0]</code>.
|
- text: <code>sortDisjoint([7, 6, 5, 4, 3, 2, 1, 0], [1, 2, 5, 6])</code> should return <code>[7, 1, 2, 4, 3, 5, 6, 0]</code>.
|
||||||
testString: assert.deepEqual(sortDisjoint([7, 6, 5, 4, 3, 2, 1, 0], [1, 2, 5, 6]), [7, 1, 2, 4, 3, 5, 6, 0], '<code>sortDisjoint([7, 6, 5, 4, 3, 2, 1, 0], [1, 2, 5, 6])</code> should return <code>[7, 1, 2, 4, 3, 5, 6, 0]</code>.');
|
testString: assert.deepEqual(sortDisjoint([7, 6, 5, 4, 3, 2, 1, 0], [1, 2, 5, 6]), [7, 1, 2, 4, 3, 5, 6, 0]);
|
||||||
- text: <code>sortDisjoint([8, 7, 6, 5, 4, 3, 2, 1], [6, 1, 7])</code> should return <code>[8, 1, 6, 5, 4, 3, 2, 7]</code>.
|
- text: <code>sortDisjoint([8, 7, 6, 5, 4, 3, 2, 1], [6, 1, 7])</code> should return <code>[8, 1, 6, 5, 4, 3, 2, 7]</code>.
|
||||||
testString: assert.deepEqual(sortDisjoint([8, 7, 6, 5, 4, 3, 2, 1], [6, 1, 7]), [8, 1, 6, 5, 4, 3, 2, 7], '<code>sortDisjoint([8, 7, 6, 5, 4, 3, 2, 1], [6, 1, 7])</code> should return <code>[8, 1, 6, 5, 4, 3, 2, 7]</code>.');
|
testString: assert.deepEqual(sortDisjoint([8, 7, 6, 5, 4, 3, 2, 1], [6, 1, 7]), [8, 1, 6, 5, 4, 3, 2, 7]);
|
||||||
- text: <code>sortDisjoint([8, 7, 6, 5, 4, 3, 2, 1], [1, 3, 5, 6])</code> should return <code>[8, 2, 6, 3, 4, 5, 7, 1]</code>.
|
- text: <code>sortDisjoint([8, 7, 6, 5, 4, 3, 2, 1], [1, 3, 5, 6])</code> should return <code>[8, 2, 6, 3, 4, 5, 7, 1]</code>.
|
||||||
testString: assert.deepEqual(sortDisjoint([8, 7, 6, 5, 4, 3, 2, 1], [1, 3, 5, 6]), [8, 2, 6, 3, 4, 5, 7, 1], '<code>sortDisjoint([8, 7, 6, 5, 4, 3, 2, 1], [1, 3, 5, 6])</code> should return <code>[8, 2, 6, 3, 4, 5, 7, 1]</code>.');
|
testString: assert.deepEqual(sortDisjoint([8, 7, 6, 5, 4, 3, 2, 1], [1, 3, 5, 6]), [8, 2, 6, 3, 4, 5, 7, 1]);
|
||||||
- text: <code>sortDisjoint([6, 1, 7, 1, 3, 5, 6], [6, 1, 5, 4])</code> should return <code>[6, 1, 7, 1, 3, 5, 6]</code>.
|
- text: <code>sortDisjoint([6, 1, 7, 1, 3, 5, 6], [6, 1, 5, 4])</code> should return <code>[6, 1, 7, 1, 3, 5, 6]</code>.
|
||||||
testString: assert.deepEqual(sortDisjoint([6, 1, 7, 1, 3, 5, 6], [6, 1, 5, 4]), [6, 1, 7, 1, 3, 5, 6],'<code>sortDisjoint([6, 1, 7, 1, 3, 5, 6], [6, 1, 5, 4])</code> should return <code>[6, 1, 7, 1, 3, 5, 6]</code>.');
|
testString: assert.deepEqual(sortDisjoint([6, 1, 7, 1, 3, 5, 6], [6, 1, 5, 4]), [6, 1, 7, 1, 3, 5, 6]);
|
||||||
```
|
```
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Challenge Seed
|
## Challenge Seed
|
||||||
|
|
||||||
<section id='challengeSeed'>
|
<section id='challengeSeed'>
|
||||||
<div id='js-seed'>
|
<div id='js-seed'>
|
||||||
|
|
||||||
@ -58,20 +62,25 @@ function sortDisjoint(values, indices) {
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Solution
|
## Solution
|
||||||
|
|
||||||
<section id='solution'>
|
<section id='solution'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function sortDisjoint(values, indices) {
|
function sortDisjoint(values, indices) {
|
||||||
let sublist = [];
|
let sublist = [];
|
||||||
|
|
||||||
indices.sort(function (a, b) { return a - b; });
|
indices.sort(function(a, b) {
|
||||||
|
return a - b;
|
||||||
|
});
|
||||||
|
|
||||||
for (let i = 0; i < indices.length; i++) {
|
for (let i = 0; i < indices.length; i++) {
|
||||||
sublist.push(values[indices[i]]);
|
sublist.push(values[indices[i]]);
|
||||||
}
|
}
|
||||||
|
|
||||||
sublist.sort((a, b) => { return a - b; });
|
sublist.sort((a, b) => {
|
||||||
|
return a - b;
|
||||||
|
});
|
||||||
|
|
||||||
for (let i = 0; i < indices.length; i++) {
|
for (let i = 0; i < indices.length; i++) {
|
||||||
values[indices[i]] = sublist[i];
|
values[indices[i]] = sublist[i];
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,7 @@ forumTopicId: 302308
|
|||||||
---
|
---
|
||||||
|
|
||||||
## Description
|
## Description
|
||||||
|
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
When sorting records in a table by a particular column or field, a <a href="https://en.wikipedia.org/wiki/Stable_sort#Stability" target="_blank">stable sort</a> will always retain the relative order of records that have the same key.
|
When sorting records in a table by a particular column or field, a <a href="https://en.wikipedia.org/wiki/Stable_sort#Stability" target="_blank">stable sort</a> will always retain the relative order of records that have the same key.
|
||||||
For example, in this table of countries and cities, a stable sort on the <b>second</b> column, the cities, would keep the US Birmingham above the UK Birmingham. (Although an unstable sort <i>might</i>, in this case, place the US Birmingham above the UK Birmingham, a stable sort routine would <i>guarantee</i> it).
|
For example, in this table of countries and cities, a stable sort on the <b>second</b> column, the cities, would keep the US Birmingham above the UK Birmingham. (Although an unstable sort <i>might</i>, in this case, place the US Birmingham above the UK Birmingham, a stable sort routine would <i>guarantee</i> it).
|
||||||
@ -19,34 +20,37 @@ Similarly, stable sorting on just the first column would generate "UK London" as
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
|
|
||||||
<section id='instructions'>
|
<section id='instructions'>
|
||||||
Write a function that takes a 2D array as a parameter. Each element has 2 elements similar to the above example. The function should sort the array as mentioned previously and return the sorted array.
|
Write a function that takes a 2D array as a parameter. Each element has 2 elements similar to the above example. The function should sort the array as mentioned previously and return the sorted array.
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
|
|
||||||
<section id='tests'>
|
<section id='tests'>
|
||||||
|
|
||||||
``` yml
|
```yml
|
||||||
tests:
|
tests:
|
||||||
- text: <code>stableSort</code> should be a function.
|
- text: <code>stableSort</code> should be a function.
|
||||||
testString: assert(typeof stableSort == 'function', '<code>stableSort</code> should be a function.');
|
testString: assert(typeof stableSort == 'function');
|
||||||
- text: <code>stableSort([["UK", "London"], ["US", "New York"], ["US", "Birmingham"], ["UK", "Birmingham"]])</code> should return an array.
|
- text: <code>stableSort([["UK", "London"], ["US", "New York"], ["US", "Birmingham"], ["UK", "Birmingham"]])</code> should return an array.
|
||||||
testString: assert(Array.isArray(stableSort([["UK", "London"], ["US", "New York"], ["US", "Birmingham"], ["UK", "Birmingham"]])), '<code>stableSort([["UK", "London"], ["US", "New York"], ["US", "Birmingham"], ["UK", "Birmingham"]])</code> should return an array.');
|
testString: assert(Array.isArray(stableSort([["UK", "London"], ["US", "New York"], ["US", "Birmingham"], ["UK", "Birmingham"]])));
|
||||||
- text: <code>stableSort([["UK", "London"], ["US", "New York"], ["US", "Birmingham"], ["UK", "Birmingham"]])</code> should return <code>[["US", "Birmingham"], ["UK", "Birmingham"], ["UK", "London"], ["US", "New York"]]</code>.
|
- text: <code>stableSort([["UK", "London"], ["US", "New York"], ["US", "Birmingham"], ["UK", "Birmingham"]])</code> should return <code>[["US", "Birmingham"], ["UK", "Birmingham"], ["UK", "London"], ["US", "New York"]]</code>.
|
||||||
testString: assert.deepEqual(stableSort([["UK", "London"], ["US", "New York"], ["US", "Birmingham"], ["UK", "Birmingham"]]), [["US", "Birmingham"], ["UK", "Birmingham"], ["UK", "London"], ["US", "New York"]], '<code>stableSort([["UK", "London"], ["US", "New York"], ["US", "Birmingham"], ["UK", "Birmingham"]])</code> should return <code>[["US", "Birmingham"], ["UK", "Birmingham"], ["UK", "London"], ["US", "New York"]]</code>.');
|
testString: assert.deepEqual(stableSort([["UK", "London"], ["US", "New York"], ["US", "Birmingham"], ["UK", "Birmingham"]]), [["US", "Birmingham"], ["UK", "Birmingham"], ["UK", "London"], ["US", "New York"]]);
|
||||||
- text: <code>stableSort([[2, 2], [1, 2], [1, 4], [1, 5]])</code> should return <code>[[2, 2], [1, 2], [1, 4], [1, 5]]</code>.
|
- text: <code>stableSort([[2, 2], [1, 2], [1, 4], [1, 5]])</code> should return <code>[[2, 2], [1, 2], [1, 4], [1, 5]]</code>.
|
||||||
testString: assert.deepEqual(stableSort([[2, 2], [1, 2], [1, 4], [1, 5]]), [[2, 2], [1, 2], [1, 4], [1, 5]], '<code>stableSort([[2, 2], [1, 2], [1, 4], [1, 5]])</code> should return <code>[[2, 2], [1, 2], [1, 4], [1, 5]]</code>.');
|
testString: assert.deepEqual(stableSort([[2, 2], [1, 2], [1, 4], [1, 5]]), [[2, 2], [1, 2], [1, 4], [1, 5]]);
|
||||||
- text: <code>stableSort([[11, 55], [12, 45], [11, 45], [32, 45]])</code> should return <code>[[12, 45], [11, 45], [32, 45], [11, 55]]</code>.
|
- text: <code>stableSort([[11, 55], [12, 45], [11, 45], [32, 45]])</code> should return <code>[[12, 45], [11, 45], [32, 45], [11, 55]]</code>.
|
||||||
testString: assert.deepEqual(stableSort([[11, 55], [12, 45], [11, 45], [32, 45]]), [[12, 45], [11, 45], [32, 45], [11, 55]], '<code>stableSort([[11, 55], [12, 45], [11, 45], [32, 45]])</code> should return <code>[[12, 45], [11, 45], [32, 45], [11, 55]]</code>.');
|
testString: assert.deepEqual(stableSort([[11, 55], [12, 45], [11, 45], [32, 45]]), [[12, 45], [11, 45], [32, 45], [11, 55]]);
|
||||||
- text: <code>stableSort([[10, 22], [1, 2], [1, 4], [1, 5], [10, 9]])</code> should return <code>[[1, 2], [1, 4], [1, 5], [10, 9], [10, 22]]</code>.
|
- text: <code>stableSort([[10, 22], [1, 2], [1, 4], [1, 5], [10, 9]])</code> should return <code>[[1, 2], [1, 4], [1, 5], [10, 9], [10, 22]]</code>.
|
||||||
testString: assert.deepEqual(stableSort([[10, 22], [1, 2], [1, 4], [1, 5], [10, 9]]), [[1, 2], [1, 4], [1, 5], [10, 9], [10, 22]], '<code>stableSort([[10, 22], [1, 2], [1, 4], [1, 5], [10, 9]])</code> should return <code>[[1, 2], [1, 4], [1, 5], [10, 9], [10, 22]]</code>.');
|
testString: assert.deepEqual(stableSort([[10, 22], [1, 2], [1, 4], [1, 5], [10, 9]]), [[1, 2], [1, 4], [1, 5], [10, 9], [10, 22]]);
|
||||||
- text: <code>stableSort([[55, 54], [12, 22], [31, 43], [31, 54], [10, 49]])</code> should return <code>[[12, 22], [31, 43], [10, 49], [55, 54], [31, 54]]</code>.
|
- text: <code>stableSort([[55, 54], [12, 22], [31, 43], [31, 54], [10, 49]])</code> should return <code>[[12, 22], [31, 43], [10, 49], [55, 54], [31, 54]]</code>.
|
||||||
testString: assert.deepEqual(stableSort([[55, 54], [12, 22], [31, 43], [31, 54], [10, 49]]), [[12, 22], [31, 43], [10, 49], [55, 54], [31, 54]], '<code>stableSort([[55, 54], [12, 22], [31, 43], [31, 54], [10, 49]])</code> should return <code>[[12, 22], [31, 43], [10, 49], [55, 54], [31, 54]]</code>.');
|
testString: assert.deepEqual(stableSort([[55, 54], [12, 22], [31, 43], [31, 54], [10, 49]]), [[12, 22], [31, 43], [10, 49], [55, 54], [31, 54]]);
|
||||||
```
|
```
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Challenge Seed
|
## Challenge Seed
|
||||||
|
|
||||||
<section id='challengeSeed'>
|
<section id='challengeSeed'>
|
||||||
<div id='js-seed'>
|
<div id='js-seed'>
|
||||||
|
|
||||||
@ -61,12 +65,15 @@ function stableSort(arr) {
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Solution
|
## Solution
|
||||||
|
|
||||||
<section id='solution'>
|
<section id='solution'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function stableSort(arr) {
|
function stableSort(arr) {
|
||||||
arr.sort(function (a, b) { return (a[1] < b[1] ? -1 : (a[1] > b[1] ? 1 : 0)) });
|
arr.sort(function(a, b) {
|
||||||
return arr;
|
return a[1] < b[1] ? -1 : a[1] > b[1] ? 1 : 0;
|
||||||
|
});
|
||||||
|
return arr;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -6,39 +6,43 @@ forumTopicId: 302309
|
|||||||
---
|
---
|
||||||
|
|
||||||
## Description
|
## Description
|
||||||
|
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
Write a function to sort an array (or list) of strings in order of descending length, and in ascending lexicographic order for strings of equal length.
|
Write a function to sort an array (or list) of strings in order of descending length, and in ascending lexicographic order for strings of equal length.
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
|
|
||||||
<section id='instructions'>
|
<section id='instructions'>
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
|
|
||||||
<section id='tests'>
|
<section id='tests'>
|
||||||
|
|
||||||
``` yml
|
```yml
|
||||||
tests:
|
tests:
|
||||||
- text: <code>lengthSorter</code> should be a function.
|
- text: <code>lengthSorter</code> should be a function.
|
||||||
testString: assert(typeof lengthSorter == 'function', '<code>lengthSorter</code> should be a function.');
|
testString: assert(typeof lengthSorter == 'function');
|
||||||
- text: <code>lengthSorter(["Here", "are", "some", "sample", "strings", "to", "be", "sorted"])</code> should return an array.
|
- text: <code>lengthSorter(["Here", "are", "some", "sample", "strings", "to", "be", "sorted"])</code> should return an array.
|
||||||
testString: assert(Array.isArray(lengthSorter(["Here", "are", "some", "sample", "strings", "to", "be", "sorted"])), '<code>lengthSorter(["Here", "are", "some", "sample", "strings", "to", "be", "sorted"])</code> should return an array.');
|
testString: assert(Array.isArray(lengthSorter(["Here", "are", "some", "sample", "strings", "to", "be", "sorted"])));
|
||||||
- text: <code>lengthSorter(["Here", "are", "some", "sample", "strings", "to", "be", "sorted"])</code> should return <code>["strings", "sample", "sorted", "Here", "some", "are", "be", "to"]</code>.
|
- text: <code>lengthSorter(["Here", "are", "some", "sample", "strings", "to", "be", "sorted"])</code> should return <code>["strings", "sample", "sorted", "Here", "some", "are", "be", "to"]</code>.
|
||||||
testString: assert.deepEqual(lengthSorter(["Here", "are", "some", "sample", "strings", "to", "be", "sorted"]), ["strings", "sample", "sorted", "Here", "some", "are", "be", "to"], '<code>lengthSorter(["Here", "are", "some", "sample", "strings", "to", "be", "sorted"])</code> should return <code>["strings", "sample", "sorted", "Here", "some", "are", "be", "to"]</code>.');
|
testString: assert.deepEqual(lengthSorter(["Here", "are", "some", "sample", "strings", "to", "be", "sorted"]), ["strings", "sample", "sorted", "Here", "some", "are", "be", "to"]);
|
||||||
- text: <code>lengthSorter(["I", "hope", "your", "day", "is", "going", "good", "?"])</code> should return <code>["going", "good", "hope", "your", "day", "is", "?","I"]</code>.
|
- text: <code>lengthSorter(["I", "hope", "your", "day", "is", "going", "good", "?"])</code> should return <code>["going", "good", "hope", "your", "day", "is", "?","I"]</code>.
|
||||||
testString: assert.deepEqual(lengthSorter(["I", "hope", "your", "day", "is", "going", "good", "?"]), ["going", "good", "hope", "your", "day", "is", "?","I"], '<code>lengthSorter(["I", "hope", "your", "day", "is", "going", "good", "?"])</code> should return <code>["going", "good", "hope", "your", "day", "is", "?","I"]</code>.');
|
testString: assert.deepEqual(lengthSorter(["I", "hope", "your", "day", "is", "going", "good", "?"]), ["going", "good", "hope", "your", "day", "is", "?","I"]);
|
||||||
- text: <code>lengthSorter(["Mine", "is", "going", "great"])</code> should return <code>["going", "great", "Mine", "is"]</code>.
|
- text: <code>lengthSorter(["Mine", "is", "going", "great"])</code> should return <code>["going", "great", "Mine", "is"]</code>.
|
||||||
testString: assert.deepEqual(lengthSorter(["Mine", "is", "going", "great"]), ["going", "great", "Mine", "is"], '<code>lengthSorter(["Mine", "is", "going", "great"])</code> should return <code>["going", "great", "Mine", "is"]</code>.');
|
testString: assert.deepEqual(lengthSorter(["Mine", "is", "going", "great"]), ["going", "great", "Mine", "is"]);
|
||||||
- text: <code>lengthSorter(["Have", "fun", "sorting", "!!"])</code> should return <code>["sorting", "Have", "fun", "!!"]</code>.
|
- text: <code>lengthSorter(["Have", "fun", "sorting", "!!"])</code> should return <code>["sorting", "Have", "fun", "!!"]</code>.
|
||||||
testString: assert.deepEqual(lengthSorter(["Have", "fun", "sorting", "!!"]), ["sorting", "Have", "fun", "!!"], '<code>lengthSorter(["Have", "fun", "sorting", "!!"])</code> should return <code>["sorting", "Have", "fun", "!!"]</code>.');
|
testString: assert.deepEqual(lengthSorter(["Have", "fun", "sorting", "!!"]), ["sorting", "Have", "fun", "!!"]);
|
||||||
- text: <code>lengthSorter(["Everything", "is", "good", "!!"])</code> should return <code>["Everything", "good", "!!", "is"]</code>.
|
- text: <code>lengthSorter(["Everything", "is", "good", "!!"])</code> should return <code>["Everything", "good", "!!", "is"]</code>.
|
||||||
testString: assert.deepEqual(lengthSorter(["Everything", "is", "good", "!!"]), ["Everything", "good", "!!", "is"], '<code>lengthSorter(["Everything", "is", "good", "!!"])</code> should return <code>["Everything", "good", "!!", "is"]</code>.');
|
testString: assert.deepEqual(lengthSorter(["Everything", "is", "good", "!!"]), ["Everything", "good", "!!", "is"]);
|
||||||
```
|
```
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Challenge Seed
|
## Challenge Seed
|
||||||
|
|
||||||
<section id='challengeSeed'>
|
<section id='challengeSeed'>
|
||||||
<div id='js-seed'>
|
<div id='js-seed'>
|
||||||
|
|
||||||
@ -52,17 +56,17 @@ function lengthSorter(arr) {
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Solution
|
## Solution
|
||||||
|
|
||||||
<section id='solution'>
|
<section id='solution'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function lengthSorter(arr) {
|
function lengthSorter(arr) {
|
||||||
arr.sort(function (a, b) {
|
arr.sort(function(a, b) {
|
||||||
var result = b.length - a.length;
|
var result = b.length - a.length;
|
||||||
if (result == 0)
|
if (result == 0) result = a.localeCompare(b);
|
||||||
result = a.localeCompare(b);
|
return result;
|
||||||
return result;
|
});
|
||||||
})
|
return arr;
|
||||||
return arr;
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -6,6 +6,7 @@ forumTopicId: 302310
|
|||||||
---
|
---
|
||||||
|
|
||||||
## Description
|
## Description
|
||||||
|
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
Sort an array of positive integers using the <a href="https://en.wikipedia.org/wiki/Bead_sort" target="_blank">Bead Sort Algorithm</a>.
|
Sort an array of positive integers using the <a href="https://en.wikipedia.org/wiki/Bead_sort" target="_blank">Bead Sort Algorithm</a>.
|
||||||
A <i>bead sort</i> is also known as a <i>gravity sort</i>.
|
A <i>bead sort</i> is also known as a <i>gravity sort</i>.
|
||||||
@ -14,34 +15,37 @@ This is the case when bead sort is implemented without a mechanism to assist in
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
|
|
||||||
<section id='instructions'>
|
<section id='instructions'>
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
|
|
||||||
<section id='tests'>
|
<section id='tests'>
|
||||||
|
|
||||||
``` yml
|
```yml
|
||||||
tests:
|
tests:
|
||||||
- text: <code>beadSort</code> should be a function.
|
- text: <code>beadSort</code> should be a function.
|
||||||
testString: assert(typeof beadSort == 'function', '<code>beadSort</code> should be a function.');
|
testString: assert(typeof beadSort == 'function');
|
||||||
- text: <code>beadSort([25, 32, 12, 7, 20])</code> should return an array.
|
- text: <code>beadSort([25, 32, 12, 7, 20])</code> should return an array.
|
||||||
testString: assert(Array.isArray(beadSort([25, 32, 12, 7, 20])), '<code>beadSort([25, 32, 12, 7, 20])</code> should return an array.');
|
testString: assert(Array.isArray(beadSort([25, 32, 12, 7, 20])));
|
||||||
- text: <code>beadSort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.
|
- text: <code>beadSort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.
|
||||||
testString: assert.deepEqual(beadSort([25, 32, 12, 7, 20]), [7, 12, 20, 25, 32], '<code>beadSort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.');
|
testString: assert.deepEqual(beadSort([25, 32, 12, 7, 20]), [7, 12, 20, 25, 32]);
|
||||||
- text: <code>beadSort([38, 45, 35, 8, 13])</code> should return <code>[8, 13, 35, 38, 45]</code>.
|
- text: <code>beadSort([38, 45, 35, 8, 13])</code> should return <code>[8, 13, 35, 38, 45]</code>.
|
||||||
testString: assert.deepEqual(beadSort([38, 45, 35, 8, 13]), [8, 13, 35, 38, 45], '<code>beadSort([38, 45, 35, 8, 13])</code> should return <code>[8, 13, 35, 38, 45]</code>.');
|
testString: assert.deepEqual(beadSort([38, 45, 35, 8, 13]), [8, 13, 35, 38, 45]);
|
||||||
- text: <code>beadSort([43, 36, 20, 34, 24])</code> should return <code>[20, 24, 34, 36, 43]</code>.
|
- text: <code>beadSort([43, 36, 20, 34, 24])</code> should return <code>[20, 24, 34, 36, 43]</code>.
|
||||||
testString: assert.deepEqual(beadSort([43, 36, 20, 34, 24]), [20, 24, 34, 36, 43], '<code>beadSort([43, 36, 20, 34, 24])</code> should return <code>[20, 24, 34, 36, 43]</code>.');
|
testString: assert.deepEqual(beadSort([43, 36, 20, 34, 24]), [20, 24, 34, 36, 43]);
|
||||||
- text: <code>beadSort([12, 33, 26, 18, 1, 16, 38])</code> should return <code>[1, 12, 16, 18, 26, 33, 38]</code>.
|
- text: <code>beadSort([12, 33, 26, 18, 1, 16, 38])</code> should return <code>[1, 12, 16, 18, 26, 33, 38]</code>.
|
||||||
testString: assert.deepEqual(beadSort([12, 33, 26, 18, 1, 16, 38]), [1, 12, 16, 18, 26, 33, 38], '<code>beadSort([12, 33, 26, 18, 1, 16, 38])</code> should return <code>[1, 12, 16, 18, 26, 33, 38]</code>.');
|
testString: assert.deepEqual(beadSort([12, 33, 26, 18, 1, 16, 38]), [1, 12, 16, 18, 26, 33, 38]);
|
||||||
- text: <code>beadSort([3, 39, 48, 16, 1, 4, 29])</code> should return <code>[1, 3, 4, 16, 29, 39, 48]</code>.
|
- text: <code>beadSort([3, 39, 48, 16, 1, 4, 29])</code> should return <code>[1, 3, 4, 16, 29, 39, 48]</code>.
|
||||||
testString: assert.deepEqual(beadSort([3, 39, 48, 16, 1, 4, 29]), [1, 3, 4, 16, 29, 39, 48], '<code>beadSort([3, 39, 48, 16, 1, 4, 29])</code> should return <code>[1, 3, 4, 16, 29, 39, 48]</code>.');
|
testString: assert.deepEqual(beadSort([3, 39, 48, 16, 1, 4, 29]), [1, 3, 4, 16, 29, 39, 48]);
|
||||||
```
|
```
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Challenge Seed
|
## Challenge Seed
|
||||||
|
|
||||||
<section id='challengeSeed'>
|
<section id='challengeSeed'>
|
||||||
<div id='js-seed'>
|
<div id='js-seed'>
|
||||||
|
|
||||||
@ -55,43 +59,46 @@ function beadSort(arr) {
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Solution
|
## Solution
|
||||||
|
|
||||||
<section id='solution'>
|
<section id='solution'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function beadSort(arr) {
|
function beadSort(arr) {
|
||||||
var max = 0;
|
var max = 0;
|
||||||
for (var i = 0; i < arr.length; i++)
|
for (var i = 0; i < arr.length; i++) if (arr[i] > max) max = arr[i];
|
||||||
if (arr[i] > max)
|
var grid = new Array(arr.length);
|
||||||
max = arr[i];;
|
for (var i = 0; i < grid.length; i++) {
|
||||||
var grid = new Array(arr.length);
|
grid[i] = new Array(max);
|
||||||
for (var i = 0; i < grid.length; i++) {
|
}
|
||||||
grid[i] = new Array(max);
|
var levelcount = new Array(max);
|
||||||
|
levelcount.fill(0);
|
||||||
|
for (var i = 0; i < max; i++) {
|
||||||
|
levelcount[i] = 0;
|
||||||
|
for (var j = 0; j < arr.length; j++) grid[j][i] = '_';
|
||||||
|
}
|
||||||
|
for (var i = 0; i < arr.length; i++) {
|
||||||
|
var num = arr[i];
|
||||||
|
for (var j = 0; num > 0; j++) {
|
||||||
|
grid[levelcount[j]++][j] = '*';
|
||||||
|
num--;
|
||||||
}
|
}
|
||||||
var levelcount = new Array(max);
|
}
|
||||||
levelcount.fill(0)
|
var sorted = new Array(arr.length);
|
||||||
for (var i = 0; i < max; i++) {
|
sorted.fill(0);
|
||||||
levelcount[i] = 0;
|
for (var i = 0; i < arr.length; i++) {
|
||||||
for (var j = 0; j < arr.length; j++)
|
var putt = 0;
|
||||||
grid[j][i] = '_';
|
for (
|
||||||
};
|
var j = 0;
|
||||||
for (var i = 0; i < arr.length; i++) {
|
j < max &&
|
||||||
var num = arr[i];
|
(function(c) {
|
||||||
for (var j = 0; num > 0; j++) {
|
return c.charCodeAt == null ? c : c.charCodeAt(0);
|
||||||
grid[levelcount[j]++][j] = '*';
|
})(grid[arr.length - 1 - i][j]) == '*'.charCodeAt(0);
|
||||||
num--;
|
j++
|
||||||
};
|
)
|
||||||
};
|
putt++;
|
||||||
var sorted = new Array(arr.length)
|
sorted[i] = putt;
|
||||||
sorted.fill(0)
|
}
|
||||||
for (var i = 0; i < arr.length; i++) {
|
return sorted;
|
||||||
var putt = 0;
|
|
||||||
for (var j = 0; j < max && (function (c) {
|
|
||||||
return c.charCodeAt == null ? c : c.charCodeAt(0);
|
|
||||||
})(grid[arr.length - 1 - i][j]) == '*'.charCodeAt(0); j++)
|
|
||||||
putt++;
|
|
||||||
sorted[i] = putt;
|
|
||||||
};
|
|
||||||
return sorted;
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -6,6 +6,7 @@ forumTopicId: 302311
|
|||||||
---
|
---
|
||||||
|
|
||||||
## Description
|
## Description
|
||||||
|
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
<a href="https://en.wikipedia.org/wiki/Bogosort" target="_blank">Bogosort</a> a list of numbers.
|
<a href="https://en.wikipedia.org/wiki/Bogosort" target="_blank">Bogosort</a> a list of numbers.
|
||||||
Bogosort simply shuffles a collection randomly until it is sorted.
|
Bogosort simply shuffles a collection randomly until it is sorted.
|
||||||
@ -21,34 +22,37 @@ Pseudocode:
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
|
|
||||||
<section id='instructions'>
|
<section id='instructions'>
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
|
|
||||||
<section id='tests'>
|
<section id='tests'>
|
||||||
|
|
||||||
``` yml
|
```yml
|
||||||
tests:
|
tests:
|
||||||
- text: <code>bogosort</code> should be a function.
|
- text: <code>bogosort</code> should be a function.
|
||||||
testString: assert(typeof bogosort == 'function', '<code>bogosort</code> should be a function.');
|
testString: assert(typeof bogosort == 'function');
|
||||||
- text: <code>bogosort([25, 32, 12, 7, 20])</code> should return an array.
|
- text: <code>bogosort([25, 32, 12, 7, 20])</code> should return an array.
|
||||||
testString: assert(Array.isArray(bogosort([25, 32, 12, 7, 20])), '<code>bogosort([25, 32, 12, 7, 20])</code> should return an array.');
|
testString: assert(Array.isArray(bogosort([25, 32, 12, 7, 20])));
|
||||||
- text: <code>bogosort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.
|
- text: <code>bogosort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.
|
||||||
testString: assert.deepEqual(bogosort([25, 32, 12, 7, 20]), [7, 12, 20, 25, 32], '<code>bogosort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.');
|
testString: assert.deepEqual(bogosort([25, 32, 12, 7, 20]), [7, 12, 20, 25, 32]);
|
||||||
- text: <code>bogosort([38, 45, 35, 8, 13])</code> should return <code>[8, 13, 35, 38, 45]</code>.
|
- text: <code>bogosort([38, 45, 35, 8, 13])</code> should return <code>[8, 13, 35, 38, 45]</code>.
|
||||||
testString: assert.deepEqual(bogosort([38, 45, 35, 8, 13]), [8, 13, 35, 38, 45], '<code>bogosort([38, 45, 35, 8, 13])</code> should return <code>[8, 13, 35, 38, 45]</code>.');
|
testString: assert.deepEqual(bogosort([38, 45, 35, 8, 13]), [8, 13, 35, 38, 45]);
|
||||||
- text: <code>bogosort([43, 36, 20, 34, 24])</code> should return <code>[20, 24, 34, 36, 43]</code>.
|
- text: <code>bogosort([43, 36, 20, 34, 24])</code> should return <code>[20, 24, 34, 36, 43]</code>.
|
||||||
testString: assert.deepEqual(bogosort([43, 36, 20, 34, 24]), [20, 24, 34, 36, 43], '<code>bogosort([43, 36, 20, 34, 24])</code> should return <code>[20, 24, 34, 36, 43]</code>.');
|
testString: assert.deepEqual(bogosort([43, 36, 20, 34, 24]), [20, 24, 34, 36, 43]);
|
||||||
- text: <code>bogosort([12, 33, 26, 18, 1, 16, 38])</code> should return <code>[1, 12, 16, 18, 26, 33, 38]</code>.
|
- text: <code>bogosort([12, 33, 26, 18, 1, 16, 38])</code> should return <code>[1, 12, 16, 18, 26, 33, 38]</code>.
|
||||||
testString: assert.deepEqual(bogosort([12, 33, 26, 18, 1, 16, 38]), [1, 12, 16, 18, 26, 33, 38], '<code>bogosort([12, 33, 26, 18, 1, 16, 38])</code> should return <code>[1, 12, 16, 18, 26, 33, 38]</code>.');
|
testString: assert.deepEqual(bogosort([12, 33, 26, 18, 1, 16, 38]), [1, 12, 16, 18, 26, 33, 38]);
|
||||||
- text: <code>bogosort([3, 39, 48, 16, 1, 4, 29])</code> should return <code>[1, 3, 4, 16, 29, 39, 48]</code>.
|
- text: <code>bogosort([3, 39, 48, 16, 1, 4, 29])</code> should return <code>[1, 3, 4, 16, 29, 39, 48]</code>.
|
||||||
testString: assert.deepEqual(bogosort([3, 39, 48, 16, 1, 4, 29]), [1, 3, 4, 16, 29, 39, 48], '<code>bogosort([3, 39, 48, 16, 1, 4, 29])</code> should return <code>[1, 3, 4, 16, 29, 39, 48]</code>.');
|
testString: assert.deepEqual(bogosort([3, 39, 48, 16, 1, 4, 29]), [1, 3, 4, 16, 29, 39, 48]);
|
||||||
```
|
```
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Challenge Seed
|
## Challenge Seed
|
||||||
|
|
||||||
<section id='challengeSeed'>
|
<section id='challengeSeed'>
|
||||||
<div id='js-seed'>
|
<div id='js-seed'>
|
||||||
|
|
||||||
@ -62,29 +66,34 @@ function bogosort(v) {
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Solution
|
## Solution
|
||||||
|
|
||||||
<section id='solution'>
|
<section id='solution'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function bogosort(v) {
|
function bogosort(v) {
|
||||||
function shuffle(v) {
|
function shuffle(v) {
|
||||||
for (var j, x, i = v.length; i; j = Math.floor(Math.random() * i), x = v[--i], v[i] = v[j], v[j] = x);
|
for (
|
||||||
return v;
|
var j, x, i = v.length;
|
||||||
};
|
i;
|
||||||
|
j = Math.floor(Math.random() * i), x = v[--i], v[i] = v[j], v[j] = x
|
||||||
function isSorted(v) {
|
);
|
||||||
for (var i = 1; i < v.length; i++) {
|
|
||||||
if (v[i - 1] > v[i]) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
var sorted = false;
|
|
||||||
while (sorted == false) {
|
|
||||||
v = shuffle(v);
|
|
||||||
sorted = isSorted(v);
|
|
||||||
}
|
|
||||||
return v;
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
function isSorted(v) {
|
||||||
|
for (var i = 1; i < v.length; i++) {
|
||||||
|
if (v[i - 1] > v[i]) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
var sorted = false;
|
||||||
|
while (sorted == false) {
|
||||||
|
v = shuffle(v);
|
||||||
|
sorted = isSorted(v);
|
||||||
|
}
|
||||||
|
return v;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -6,6 +6,7 @@ forumTopicId: 302312
|
|||||||
---
|
---
|
||||||
|
|
||||||
## Description
|
## Description
|
||||||
|
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
The cocktail shaker sort is an improvement on the <a href="https://rosettacode.org/wiki/Bubble Sort" target="_blank">Bubble Sort</a>. 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 <a href="https://en.wikipedia.org/wiki/Cocktail sort" target="_blank">wikipedia</a>):</p>
|
The cocktail shaker sort is an improvement on the <a href="https://rosettacode.org/wiki/Bubble Sort" target="_blank">Bubble Sort</a>. 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 <a href="https://en.wikipedia.org/wiki/Cocktail sort" target="_blank">wikipedia</a>):</p>
|
||||||
<pre>
|
<pre>
|
||||||
@ -33,34 +34,37 @@ The cocktail shaker sort is an improvement on the <a href="https://rosettacode.o
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
|
|
||||||
<section id='instructions'>
|
<section id='instructions'>
|
||||||
Write a function that sorts a given array using cocktail sort.
|
Write a function that sorts a given array using cocktail sort.
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
|
|
||||||
<section id='tests'>
|
<section id='tests'>
|
||||||
|
|
||||||
``` yml
|
```yml
|
||||||
tests:
|
tests:
|
||||||
- text: <code>cocktailSort</code> should be a function.
|
- text: <code>cocktailSort</code> should be a function.
|
||||||
testString: assert(typeof cocktailSort == 'function', '<code>cocktailSort</code> should be a function.');
|
testString: assert(typeof cocktailSort == 'function');
|
||||||
- text: <code>cocktailSort([25, 32, 12, 7, 20])</code> should return an array.
|
- text: <code>cocktailSort([25, 32, 12, 7, 20])</code> should return an array.
|
||||||
testString: assert(Array.isArray(cocktailSort([25, 32, 12, 7, 20])), '<code>cocktailSort([25, 32, 12, 7, 20])</code> should return an array.');
|
testString: assert(Array.isArray(cocktailSort([25, 32, 12, 7, 20])));
|
||||||
- text: <code>cocktailSort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.
|
- text: <code>cocktailSort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.
|
||||||
testString: assert.deepEqual(cocktailSort([25, 32, 12, 7, 20]), [7, 12, 20, 25, 32], '<code>cocktailSort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.');
|
testString: assert.deepEqual(cocktailSort([25, 32, 12, 7, 20]), [7, 12, 20, 25, 32]);
|
||||||
- text: <code>cocktailSort([38, 45, 35, 8, 13])</code> should return <code>[8, 13, 35, 38, 45]</code>.
|
- text: <code>cocktailSort([38, 45, 35, 8, 13])</code> should return <code>[8, 13, 35, 38, 45]</code>.
|
||||||
testString: assert.deepEqual(cocktailSort([38, 45, 35, 8, 13]), [8, 13, 35, 38, 45], '<code>cocktailSort([38, 45, 35, 8, 13])</code> should return <code>[8, 13, 35, 38, 45]</code>.');
|
testString: assert.deepEqual(cocktailSort([38, 45, 35, 8, 13]), [8, 13, 35, 38, 45]);
|
||||||
- text: <code>cocktailSort([43, 36, 20, 34, 24])</code> should return <code>[20, 24, 34, 36, 43]</code>.
|
- text: <code>cocktailSort([43, 36, 20, 34, 24])</code> should return <code>[20, 24, 34, 36, 43]</code>.
|
||||||
testString: assert.deepEqual(cocktailSort([43, 36, 20, 34, 24]), [20, 24, 34, 36, 43], '<code>cocktailSort([43, 36, 20, 34, 24])</code> should return <code>[20, 24, 34, 36, 43]</code>.');
|
testString: assert.deepEqual(cocktailSort([43, 36, 20, 34, 24]), [20, 24, 34, 36, 43]);
|
||||||
- text: <code>cocktailSort([12, 33, 26, 18, 1, 16, 38])</code> should return <code>[1, 12, 16, 18, 26, 33, 38]</code>.
|
- text: <code>cocktailSort([12, 33, 26, 18, 1, 16, 38])</code> should return <code>[1, 12, 16, 18, 26, 33, 38]</code>.
|
||||||
testString: assert.deepEqual(cocktailSort([12, 33, 26, 18, 1, 16, 38]), [1, 12, 16, 18, 26, 33, 38], '<code>cocktailSort([12, 33, 26, 18, 1, 16, 38])</code> should return <code>[1, 12, 16, 18, 26, 33, 38]</code>.');
|
testString: assert.deepEqual(cocktailSort([12, 33, 26, 18, 1, 16, 38]), [1, 12, 16, 18, 26, 33, 38]);
|
||||||
- text: <code>cocktailSort([3, 39, 48, 16, 1, 4, 29])</code> should return <code>[1, 3, 4, 16, 29, 39, 48]</code>.
|
- text: <code>cocktailSort([3, 39, 48, 16, 1, 4, 29])</code> should return <code>[1, 3, 4, 16, 29, 39, 48]</code>.
|
||||||
testString: assert.deepEqual(cocktailSort([3, 39, 48, 16, 1, 4, 29]), [1, 3, 4, 16, 29, 39, 48], '<code>cocktailSort([3, 39, 48, 16, 1, 4, 29])</code> should return <code>[1, 3, 4, 16, 29, 39, 48]</code>.');
|
testString: assert.deepEqual(cocktailSort([3, 39, 48, 16, 1, 4, 29]), [1, 3, 4, 16, 29, 39, 48]);
|
||||||
```
|
```
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Challenge Seed
|
## Challenge Seed
|
||||||
|
|
||||||
<section id='challengeSeed'>
|
<section id='challengeSeed'>
|
||||||
<div id='js-seed'>
|
<div id='js-seed'>
|
||||||
|
|
||||||
@ -74,6 +78,7 @@ function cocktailSort(arr) {
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Solution
|
## Solution
|
||||||
|
|
||||||
<section id='solution'>
|
<section id='solution'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
@ -89,8 +94,7 @@ function cocktailSort(arr) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isSorted)
|
if (!isSorted) break;
|
||||||
break;
|
|
||||||
|
|
||||||
isSorted = false;
|
isSorted = false;
|
||||||
|
|
||||||
|
@ -6,6 +6,7 @@ forumTopicId: 302313
|
|||||||
---
|
---
|
||||||
|
|
||||||
## Description
|
## Description
|
||||||
|
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
Implement a <i>comb sort</i>.
|
Implement a <i>comb sort</i>.
|
||||||
The <b>Comb Sort</b> is a variant of the <a href="https://rosettacode.org/wiki/Bubble Sort" target="_blank">Bubble Sort</a>.
|
The <b>Comb Sort</b> is a variant of the <a href="https://rosettacode.org/wiki/Bubble Sort" target="_blank">Bubble Sort</a>.
|
||||||
@ -49,34 +50,37 @@ Pseudocode:
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
|
|
||||||
<section id='instructions'>
|
<section id='instructions'>
|
||||||
Write a function that sorts a given array using Comb sort.
|
Write a function that sorts a given array using Comb sort.
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
|
|
||||||
<section id='tests'>
|
<section id='tests'>
|
||||||
|
|
||||||
``` yml
|
```yml
|
||||||
tests:
|
tests:
|
||||||
- text: <code>combSort</code> should be a function.
|
- text: <code>combSort</code> should be a function.
|
||||||
testString: assert(typeof combSort == 'function', '<code>combSort</code> should be a function.');
|
testString: assert(typeof combSort == 'function');
|
||||||
- text: <code>combSort([25, 32, 12, 7, 20])</code> should return an array.
|
- text: <code>combSort([25, 32, 12, 7, 20])</code> should return an array.
|
||||||
testString: assert(Array.isArray(combSort([25, 32, 12, 7, 20])), '<code>combSort([25, 32, 12, 7, 20])</code> should return an array.');
|
testString: assert(Array.isArray(combSort([25, 32, 12, 7, 20])));
|
||||||
- text: <code>combSort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.
|
- text: <code>combSort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.
|
||||||
testString: assert.deepEqual(combSort([25, 32, 12, 7, 20]), [7, 12, 20, 25, 32], '<code>combSort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.');
|
testString: assert.deepEqual(combSort([25, 32, 12, 7, 20]), [7, 12, 20, 25, 32]);
|
||||||
- text: <code>combSort([38, 45, 35, 8, 13])</code> should return <code>[8, 13, 35, 38, 45]</code>.
|
- text: <code>combSort([38, 45, 35, 8, 13])</code> should return <code>[8, 13, 35, 38, 45]</code>.
|
||||||
testString: assert.deepEqual(combSort([38, 45, 35, 8, 13]), [8, 13, 35, 38, 45], '<code>combSort([38, 45, 35, 8, 13])</code> should return <code>[8, 13, 35, 38, 45]</code>.');
|
testString: assert.deepEqual(combSort([38, 45, 35, 8, 13]), [8, 13, 35, 38, 45]);
|
||||||
- text: <code>combSort([43, 36, 20, 34, 24])</code> should return <code>[20, 24, 34, 36, 43]</code>.
|
- text: <code>combSort([43, 36, 20, 34, 24])</code> should return <code>[20, 24, 34, 36, 43]</code>.
|
||||||
testString: assert.deepEqual(combSort([43, 36, 20, 34, 24]), [20, 24, 34, 36, 43], '<code>combSort([43, 36, 20, 34, 24])</code> should return <code>[20, 24, 34, 36, 43]</code>.');
|
testString: assert.deepEqual(combSort([43, 36, 20, 34, 24]), [20, 24, 34, 36, 43]);
|
||||||
- text: <code>combSort([12, 33, 26, 18, 1, 16, 38])</code> should return <code>[1, 12, 16, 18, 26, 33, 38]</code>.
|
- text: <code>combSort([12, 33, 26, 18, 1, 16, 38])</code> should return <code>[1, 12, 16, 18, 26, 33, 38]</code>.
|
||||||
testString: assert.deepEqual(combSort([12, 33, 26, 18, 1, 16, 38]), [1, 12, 16, 18, 26, 33, 38], '<code>combSort([12, 33, 26, 18, 1, 16, 38])</code> should return <code>[1, 12, 16, 18, 26, 33, 38]</code>.');
|
testString: assert.deepEqual(combSort([12, 33, 26, 18, 1, 16, 38]), [1, 12, 16, 18, 26, 33, 38]);
|
||||||
- text: <code>combSort([3, 39, 48, 16, 1, 4, 29])</code> should return <code>[1, 3, 4, 16, 29, 39, 48]</code>.
|
- text: <code>combSort([3, 39, 48, 16, 1, 4, 29])</code> should return <code>[1, 3, 4, 16, 29, 39, 48]</code>.
|
||||||
testString: assert.deepEqual(combSort([3, 39, 48, 16, 1, 4, 29]), [1, 3, 4, 16, 29, 39, 48], '<code>combSort([3, 39, 48, 16, 1, 4, 29])</code> should return <code>[1, 3, 4, 16, 29, 39, 48]</code>.');
|
testString: assert.deepEqual(combSort([3, 39, 48, 16, 1, 4, 29]), [1, 3, 4, 16, 29, 39, 48]);
|
||||||
```
|
```
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Challenge Seed
|
## Challenge Seed
|
||||||
|
|
||||||
<section id='challengeSeed'>
|
<section id='challengeSeed'>
|
||||||
<div id='js-seed'>
|
<div id='js-seed'>
|
||||||
|
|
||||||
@ -90,6 +94,7 @@ function combSort(arr) {
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Solution
|
## Solution
|
||||||
|
|
||||||
<section id='solution'>
|
<section id='solution'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
@ -113,7 +118,7 @@ function combSort(arr) {
|
|||||||
// If not first gap
|
// If not first gap
|
||||||
if (iteration_count > 0)
|
if (iteration_count > 0)
|
||||||
// Calculate gap
|
// Calculate gap
|
||||||
gap = (gap == 1) ? gap : Math.floor(gap / decrease_factor);
|
gap = gap == 1 ? gap : Math.floor(gap / decrease_factor);
|
||||||
|
|
||||||
// Set front and back elements and increment to a gap
|
// Set front and back elements and increment to a gap
|
||||||
var front = 0;
|
var front = 0;
|
||||||
|
@ -6,6 +6,7 @@ forumTopicId: 302314
|
|||||||
---
|
---
|
||||||
|
|
||||||
## Description
|
## Description
|
||||||
|
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
Gnome sort is a sorting algorithm which is similar to <a href="https://rosettacode.org/wiki/Insertion sort" target="_blank">Insertion sort</a>, except that moving an element to its proper place is accomplished by a series of swaps, as in <a href="https://rosettacode.org/wiki/Bubble Sort" target="_blank">Bubble Sort</a>.
|
Gnome sort is a sorting algorithm which is similar to <a href="https://rosettacode.org/wiki/Insertion sort" target="_blank">Insertion sort</a>, except that moving an element to its proper place is accomplished by a series of swaps, as in <a href="https://rosettacode.org/wiki/Bubble Sort" target="_blank">Bubble Sort</a>.
|
||||||
The pseudocode for the algorithm is:
|
The pseudocode for the algorithm is:
|
||||||
@ -31,34 +32,37 @@ The pseudocode for the algorithm is:
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
|
|
||||||
<section id='instructions'>
|
<section id='instructions'>
|
||||||
Write a function to implement the above pseudo code. The function should return the sorted array.
|
Write a function to implement the above pseudo code. The function should return the sorted array.
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
|
|
||||||
<section id='tests'>
|
<section id='tests'>
|
||||||
|
|
||||||
``` yml
|
```yml
|
||||||
tests:
|
tests:
|
||||||
- text: <code>gnomeSort</code> should be a function.
|
- text: <code>gnomeSort</code> should be a function.
|
||||||
testString: assert(typeof gnomeSort == 'function', '<code>gnomeSort</code> should be a function.');
|
testString: assert(typeof gnomeSort == 'function');
|
||||||
- text: <code>gnomeSort([25, 32, 12, 7, 20])</code> should return an array.
|
- text: <code>gnomeSort([25, 32, 12, 7, 20])</code> should return an array.
|
||||||
testString: assert(Array.isArray(gnomeSort([25, 32, 12, 7, 20])), '<code>gnomeSort([25, 32, 12, 7, 20])</code> should return an array.');
|
testString: assert(Array.isArray(gnomeSort([25, 32, 12, 7, 20])));
|
||||||
- text: <code>gnomeSort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.
|
- text: <code>gnomeSort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.
|
||||||
testString: assert.deepEqual(gnomeSort([25, 32, 12, 7, 20]), [7, 12, 20, 25, 32], '<code>gnomeSort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.');
|
testString: assert.deepEqual(gnomeSort([25, 32, 12, 7, 20]), [7, 12, 20, 25, 32]);
|
||||||
- text: <code>gnomeSort([38, 45, 35, 8, 13])</code> should return <code>[8, 13, 35, 38, 45]</code>.
|
- text: <code>gnomeSort([38, 45, 35, 8, 13])</code> should return <code>[8, 13, 35, 38, 45]</code>.
|
||||||
testString: assert.deepEqual(gnomeSort([38, 45, 35, 8, 13]), [8, 13, 35, 38, 45], '<code>gnomeSort([38, 45, 35, 8, 13])</code> should return <code>[8, 13, 35, 38, 45]</code>.');
|
testString: assert.deepEqual(gnomeSort([38, 45, 35, 8, 13]), [8, 13, 35, 38, 45]);
|
||||||
- text: <code>gnomeSort([43, 36, 20, 34, 24])</code> should return <code>[20, 24, 34, 36, 43]</code>.
|
- text: <code>gnomeSort([43, 36, 20, 34, 24])</code> should return <code>[20, 24, 34, 36, 43]</code>.
|
||||||
testString: assert.deepEqual(gnomeSort([43, 36, 20, 34, 24]), [20, 24, 34, 36, 43], '<code>gnomeSort([43, 36, 20, 34, 24])</code> should return <code>[20, 24, 34, 36, 43]</code>.');
|
testString: assert.deepEqual(gnomeSort([43, 36, 20, 34, 24]), [20, 24, 34, 36, 43]);
|
||||||
- text: <code>gnomeSort([12, 33, 26, 18, 1, 16, 38])</code> should return <code>[1, 12, 16, 18, 26, 33, 38]</code>.
|
- text: <code>gnomeSort([12, 33, 26, 18, 1, 16, 38])</code> should return <code>[1, 12, 16, 18, 26, 33, 38]</code>.
|
||||||
testString: assert.deepEqual(gnomeSort([12, 33, 26, 18, 1, 16, 38]), [1, 12, 16, 18, 26, 33, 38], '<code>gnomeSort([12, 33, 26, 18, 1, 16, 38])</code> should return <code>[1, 12, 16, 18, 26, 33, 38]</code>.');
|
testString: assert.deepEqual(gnomeSort([12, 33, 26, 18, 1, 16, 38]), [1, 12, 16, 18, 26, 33, 38]);
|
||||||
- text: <code>gnomeSort([3, 39, 48, 16, 1, 4, 29])</code> should return <code>[1, 3, 4, 16, 29, 39, 48]</code>.
|
- text: <code>gnomeSort([3, 39, 48, 16, 1, 4, 29])</code> should return <code>[1, 3, 4, 16, 29, 39, 48]</code>.
|
||||||
testString: assert.deepEqual(gnomeSort([3, 39, 48, 16, 1, 4, 29]), [1, 3, 4, 16, 29, 39, 48], '<code>gnomeSort([3, 39, 48, 16, 1, 4, 29])</code> should return <code>[1, 3, 4, 16, 29, 39, 48]</code>.');
|
testString: assert.deepEqual(gnomeSort([3, 39, 48, 16, 1, 4, 29]), [1, 3, 4, 16, 29, 39, 48]);
|
||||||
```
|
```
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Challenge Seed
|
## Challenge Seed
|
||||||
|
|
||||||
<section id='challengeSeed'>
|
<section id='challengeSeed'>
|
||||||
<div id='js-seed'>
|
<div id='js-seed'>
|
||||||
|
|
||||||
@ -72,6 +76,7 @@ function gnomeSort(a) {
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Solution
|
## Solution
|
||||||
|
|
||||||
<section id='solution'>
|
<section id='solution'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
@ -6,6 +6,7 @@ forumTopicId: 302315
|
|||||||
---
|
---
|
||||||
|
|
||||||
## Description
|
## Description
|
||||||
|
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
Write a function to sort an array of integers (of any convenient size) into ascending order using <a href="https://en.wikipedia.org/wiki/Pancake sorting" target="_blank">Pancake sorting</a>. The function should return the sorted array.
|
Write a function to sort an array of integers (of any convenient size) into ascending order using <a href="https://en.wikipedia.org/wiki/Pancake sorting" target="_blank">Pancake sorting</a>. 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:
|
In short, instead of individual elements being sorted, the only operation allowed is to "flip" one end of the list, like so:
|
||||||
@ -19,34 +20,37 @@ Only one end of the list can be flipped; this should be the low end, but the hig
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
|
|
||||||
<section id='instructions'>
|
<section id='instructions'>
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
|
|
||||||
<section id='tests'>
|
<section id='tests'>
|
||||||
|
|
||||||
``` yml
|
```yml
|
||||||
tests:
|
tests:
|
||||||
- text: <code>pancakeSort</code> should be a function.
|
- text: <code>pancakeSort</code> should be a function.
|
||||||
testString: assert(typeof pancakeSort == 'function', '<code>pancakeSort</code> should be a function.');
|
testString: assert(typeof pancakeSort == 'function');
|
||||||
- text: <code>pancakeSort([25, 32, 12, 7, 20])</code> should return an array.
|
- text: <code>pancakeSort([25, 32, 12, 7, 20])</code> should return an array.
|
||||||
testString: assert(Array.isArray(pancakeSort([25, 32, 12, 7, 20])), '<code>pancakeSort([25, 32, 12, 7, 20])</code> should return an array.');
|
testString: assert(Array.isArray(pancakeSort([25, 32, 12, 7, 20])));
|
||||||
- text: <code>pancakeSort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.
|
- text: <code>pancakeSort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.
|
||||||
testString: assert.deepEqual(pancakeSort([25, 32, 12, 7, 20]), [7, 12, 20, 25, 32], '<code>pancakeSort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.');
|
testString: assert.deepEqual(pancakeSort([25, 32, 12, 7, 20]), [7, 12, 20, 25, 32]);
|
||||||
- text: <code>pancakeSort([38, 45, 35, 8, 13])</code> should return <code>[8, 13, 35, 38, 45]</code>.
|
- text: <code>pancakeSort([38, 45, 35, 8, 13])</code> should return <code>[8, 13, 35, 38, 45]</code>.
|
||||||
testString: assert.deepEqual(pancakeSort([38, 45, 35, 8, 13]), [8, 13, 35, 38, 45], '<code>pancakeSort([38, 45, 35, 8, 13])</code> should return <code>[8, 13, 35, 38, 45]</code>.');
|
testString: assert.deepEqual(pancakeSort([38, 45, 35, 8, 13]), [8, 13, 35, 38, 45]);
|
||||||
- text: <code>pancakeSort([43, 36, 20, 34, 24])</code> should return <code>[20, 24, 34, 36, 43]</code>.
|
- text: <code>pancakeSort([43, 36, 20, 34, 24])</code> should return <code>[20, 24, 34, 36, 43]</code>.
|
||||||
testString: assert.deepEqual(pancakeSort([43, 36, 20, 34, 24]), [20, 24, 34, 36, 43], '<code>pancakeSort([43, 36, 20, 34, 24])</code> should return <code>[20, 24, 34, 36, 43]</code>.');
|
testString: assert.deepEqual(pancakeSort([43, 36, 20, 34, 24]), [20, 24, 34, 36, 43]);
|
||||||
- text: <code>pancakeSort([12, 33, 26, 18, 1, 16, 38])</code> should return <code>[1, 12, 16, 18, 26, 33, 38]</code>.
|
- text: <code>pancakeSort([12, 33, 26, 18, 1, 16, 38])</code> should return <code>[1, 12, 16, 18, 26, 33, 38]</code>.
|
||||||
testString: assert.deepEqual(pancakeSort([12, 33, 26, 18, 1, 16, 38]), [1, 12, 16, 18, 26, 33, 38], '<code>pancakeSort([12, 33, 26, 18, 1, 16, 38])</code> should return <code>[1, 12, 16, 18, 26, 33, 38]</code>.');
|
testString: assert.deepEqual(pancakeSort([12, 33, 26, 18, 1, 16, 38]), [1, 12, 16, 18, 26, 33, 38]);
|
||||||
- text: <code>pancakeSort([3, 39, 48, 16, 1, 4, 29])</code> should return <code>[1, 3, 4, 16, 29, 39, 48]</code>.
|
- text: <code>pancakeSort([3, 39, 48, 16, 1, 4, 29])</code> should return <code>[1, 3, 4, 16, 29, 39, 48]</code>.
|
||||||
testString: assert.deepEqual(pancakeSort([3, 39, 48, 16, 1, 4, 29]), [1, 3, 4, 16, 29, 39, 48], '<code>pancakeSort([3, 39, 48, 16, 1, 4, 29])</code> should return <code>[1, 3, 4, 16, 29, 39, 48]</code>.');
|
testString: assert.deepEqual(pancakeSort([3, 39, 48, 16, 1, 4, 29]), [1, 3, 4, 16, 29, 39, 48]);
|
||||||
```
|
```
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Challenge Seed
|
## Challenge Seed
|
||||||
|
|
||||||
<section id='challengeSeed'>
|
<section id='challengeSeed'>
|
||||||
<div id='js-seed'>
|
<div id='js-seed'>
|
||||||
|
|
||||||
@ -60,39 +64,37 @@ function pancakeSort(arr) {
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Solution
|
## Solution
|
||||||
|
|
||||||
<section id='solution'>
|
<section id='solution'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function pancakeSort(arr) {
|
function pancakeSort(arr) {
|
||||||
for (var i = arr.length - 1; i >= 1; i--) {
|
for (var i = arr.length - 1; i >= 1; i--) {
|
||||||
// find the index of the largest element not yet sorted
|
// find the index of the largest element not yet sorted
|
||||||
var max_idx = 0;
|
var max_idx = 0;
|
||||||
var max = arr[0];
|
var max = arr[0];
|
||||||
for (var j = 1; j <= i; j++) {
|
for (var j = 1; j <= i; j++) {
|
||||||
if (arr[j] > max) {
|
if (arr[j] > max) {
|
||||||
max = arr[j];
|
max = arr[j];
|
||||||
max_idx = j;
|
max_idx = j;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (max_idx == i)
|
|
||||||
continue; // element already in place
|
|
||||||
|
|
||||||
var new_slice;
|
|
||||||
|
|
||||||
// flip arr max element to index 0
|
|
||||||
if (max_idx > 0) {
|
|
||||||
new_slice = arr.slice(0, max_idx + 1).reverse();
|
|
||||||
for (var j = 0; j <= max_idx; j++)
|
|
||||||
arr[j] = new_slice[j];
|
|
||||||
}
|
|
||||||
|
|
||||||
// then flip the max element to its place
|
|
||||||
new_slice = arr.slice(0, i + 1).reverse();
|
|
||||||
for (var j = 0; j <= i; j++)
|
|
||||||
arr[j] = new_slice[j];
|
|
||||||
}
|
}
|
||||||
return arr;
|
|
||||||
|
if (max_idx == i) continue; // element already in place
|
||||||
|
|
||||||
|
var new_slice;
|
||||||
|
|
||||||
|
// flip arr max element to index 0
|
||||||
|
if (max_idx > 0) {
|
||||||
|
new_slice = arr.slice(0, max_idx + 1).reverse();
|
||||||
|
for (var j = 0; j <= max_idx; j++) arr[j] = new_slice[j];
|
||||||
|
}
|
||||||
|
|
||||||
|
// then flip the max element to its place
|
||||||
|
new_slice = arr.slice(0, i + 1).reverse();
|
||||||
|
for (var j = 0; j <= i; j++) arr[j] = new_slice[j];
|
||||||
|
}
|
||||||
|
return arr;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -6,6 +6,7 @@ forumTopicId: 302316
|
|||||||
---
|
---
|
||||||
|
|
||||||
## Description
|
## Description
|
||||||
|
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
Write a function to implement a permutation sort, which proceeds by generating the possible permutations of the input array until discovering the sorted one. The function should return the sorted array.
|
Write a function to implement a permutation sort, which proceeds by generating the possible permutations of the input array until discovering the sorted one. The function should return the sorted array.
|
||||||
Pseudocode:
|
Pseudocode:
|
||||||
@ -17,34 +18,37 @@ Pseudocode:
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
|
|
||||||
<section id='instructions'>
|
<section id='instructions'>
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
|
|
||||||
<section id='tests'>
|
<section id='tests'>
|
||||||
|
|
||||||
``` yml
|
```yml
|
||||||
tests:
|
tests:
|
||||||
- text: <code>permutationSort</code> should be a function.
|
- text: <code>permutationSort</code> should be a function.
|
||||||
testString: assert(typeof permutationSort == 'function', '<code>permutationSort</code> should be a function.');
|
testString: assert(typeof permutationSort == 'function');
|
||||||
- text: <code>permutationSort([25, 32, 12, 7, 20])</code> should return an array.
|
- text: <code>permutationSort([25, 32, 12, 7, 20])</code> should return an array.
|
||||||
testString: assert(Array.isArray(permutationSort([25, 32, 12, 7, 20])), '<code>permutationSort([25, 32, 12, 7, 20])</code> should return an array.');
|
testString: assert(Array.isArray(permutationSort([25, 32, 12, 7, 20])));
|
||||||
- text: <code>permutationSort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.
|
- text: <code>permutationSort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.
|
||||||
testString: assert.deepEqual(permutationSort([25, 32, 12, 7, 20]), [7, 12, 20, 25, 32], '<code>permutationSort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.');
|
testString: assert.deepEqual(permutationSort([25, 32, 12, 7, 20]), [7, 12, 20, 25, 32]);
|
||||||
- text: <code>permutationSort([38, 45, 35, 8, 13])</code> should return <code>[8, 13, 35, 38, 45]</code>.
|
- text: <code>permutationSort([38, 45, 35, 8, 13])</code> should return <code>[8, 13, 35, 38, 45]</code>.
|
||||||
testString: assert.deepEqual(permutationSort([38, 45, 35, 8, 13]), [8, 13, 35, 38, 45], '<code>permutationSort([38, 45, 35, 8, 13])</code> should return <code>[8, 13, 35, 38, 45]</code>.');
|
testString: assert.deepEqual(permutationSort([38, 45, 35, 8, 13]), [8, 13, 35, 38, 45]);
|
||||||
- text: <code>permutationSort([43, 36, 20, 34, 24])</code> should return <code>[20, 24, 34, 36, 43]</code>.
|
- text: <code>permutationSort([43, 36, 20, 34, 24])</code> should return <code>[20, 24, 34, 36, 43]</code>.
|
||||||
testString: assert.deepEqual(permutationSort([43, 36, 20, 34, 24]), [20, 24, 34, 36, 43], '<code>permutationSort([43, 36, 20, 34, 24])</code> should return <code>[20, 24, 34, 36, 43]</code>.');
|
testString: assert.deepEqual(permutationSort([43, 36, 20, 34, 24]), [20, 24, 34, 36, 43]);
|
||||||
- text: <code>permutationSort([12, 33, 26, 18, 1, 16, 38])</code> should return <code>[1, 12, 16, 18, 26, 33, 38]</code>.
|
- text: <code>permutationSort([12, 33, 26, 18, 1, 16, 38])</code> should return <code>[1, 12, 16, 18, 26, 33, 38]</code>.
|
||||||
testString: assert.deepEqual(permutationSort([12, 33, 26, 18, 1, 16, 38]), [1, 12, 16, 18, 26, 33, 38], '<code>permutationSort([12, 33, 26, 18, 1, 16, 38])</code> should return <code>[1, 12, 16, 18, 26, 33, 38]</code>.');
|
testString: assert.deepEqual(permutationSort([12, 33, 26, 18, 1, 16, 38]), [1, 12, 16, 18, 26, 33, 38]);
|
||||||
- text: <code>permutationSort([3, 39, 48, 16, 1, 4, 29])</code> should return <code>[1, 3, 4, 16, 29, 39, 48]</code>.
|
- text: <code>permutationSort([3, 39, 48, 16, 1, 4, 29])</code> should return <code>[1, 3, 4, 16, 29, 39, 48]</code>.
|
||||||
testString: assert.deepEqual(permutationSort([3, 39, 48, 16, 1, 4, 29]), [1, 3, 4, 16, 29, 39, 48], '<code>permutationSort([3, 39, 48, 16, 1, 4, 29])</code> should return <code>[1, 3, 4, 16, 29, 39, 48]</code>.');
|
testString: assert.deepEqual(permutationSort([3, 39, 48, 16, 1, 4, 29]), [1, 3, 4, 16, 29, 39, 48]);
|
||||||
```
|
```
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Challenge Seed
|
## Challenge Seed
|
||||||
|
|
||||||
<section id='challengeSeed'>
|
<section id='challengeSeed'>
|
||||||
<div id='js-seed'>
|
<div id='js-seed'>
|
||||||
|
|
||||||
@ -58,6 +62,7 @@ function permutationSort(arr) {
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Solution
|
## Solution
|
||||||
|
|
||||||
<section id='solution'>
|
<section id='solution'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
@ -67,11 +72,10 @@ function permutationSort(arr) {
|
|||||||
permute(a, a.length, list);
|
permute(a, a.length, list);
|
||||||
for (var i = 0; i < list.length; i++) {
|
for (var i = 0; i < list.length; i++) {
|
||||||
var x = list[i];
|
var x = list[i];
|
||||||
if (isSorted(x))
|
if (isSorted(x)) return x;
|
||||||
return x;
|
|
||||||
}
|
}
|
||||||
return a;
|
return a;
|
||||||
};
|
}
|
||||||
|
|
||||||
function permute(a, n, list) {
|
function permute(a, n, list) {
|
||||||
if (n === 1) {
|
if (n === 1) {
|
||||||
@ -83,21 +87,19 @@ function permutationSort(arr) {
|
|||||||
swap(a, i, n - 1);
|
swap(a, i, n - 1);
|
||||||
permute(a, n - 1, list);
|
permute(a, n - 1, list);
|
||||||
swap(a, i, n - 1);
|
swap(a, i, n - 1);
|
||||||
};
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
function isSorted(a) {
|
function isSorted(a) {
|
||||||
for (var i = 1; i < a.length; i++)
|
for (var i = 1; i < a.length; i++) if (a[i - 1] > a[i]) return false;
|
||||||
if (a[i - 1] > a[i])
|
|
||||||
return false;;
|
|
||||||
return true;
|
return true;
|
||||||
};
|
}
|
||||||
|
|
||||||
function swap(arr, i, j) {
|
function swap(arr, i, j) {
|
||||||
var temp = arr[i];
|
var temp = arr[i];
|
||||||
arr[i] = arr[j];
|
arr[i] = arr[j];
|
||||||
arr[j] = temp;
|
arr[j] = temp;
|
||||||
};
|
}
|
||||||
return pSort(arr);
|
return pSort(arr);
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@ -6,6 +6,7 @@ forumTopicId: 302317
|
|||||||
---
|
---
|
||||||
|
|
||||||
## Description
|
## Description
|
||||||
|
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
Write a function to sort an array of elements using the <a href="https://en.wikipedia.org/wiki/Shell sort" target="_blank">Shell sort</a> algorithm, a diminishing increment sort. The function should return the sorted array.
|
Write a function to sort an array of elements using the <a href="https://en.wikipedia.org/wiki/Shell sort" target="_blank">Shell sort</a> 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.
|
The Shell sort (also known as Shellsort or Shell's method) is named after its inventor, Donald Shell, who published the algorithm in 1959.
|
||||||
@ -16,34 +17,37 @@ Empirical studies have shown a geometric increment sequence with a ratio of abou
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
|
|
||||||
<section id='instructions'>
|
<section id='instructions'>
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
|
|
||||||
<section id='tests'>
|
<section id='tests'>
|
||||||
|
|
||||||
``` yml
|
```yml
|
||||||
tests:
|
tests:
|
||||||
- text: <code>shellSort</code> should be a function.
|
- text: <code>shellSort</code> should be a function.
|
||||||
testString: assert(typeof shellSort == 'function', '<code>shellSort</code> should be a function.');
|
testString: assert(typeof shellSort == 'function');
|
||||||
- text: <code>shellSort([25, 32, 12, 7, 20])</code> should return an array.
|
- text: <code>shellSort([25, 32, 12, 7, 20])</code> should return an array.
|
||||||
testString: assert(Array.isArray(shellSort([25, 32, 12, 7, 20])), '<code>shellSort([25, 32, 12, 7, 20])</code> should return an array.');
|
testString: assert(Array.isArray(shellSort([25, 32, 12, 7, 20])));
|
||||||
- text: <code>shellSort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.
|
- text: <code>shellSort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.
|
||||||
testString: assert.deepEqual(shellSort([25, 32, 12, 7, 20]), [7, 12, 20, 25, 32], '<code>shellSort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.');
|
testString: assert.deepEqual(shellSort([25, 32, 12, 7, 20]), [7, 12, 20, 25, 32]);
|
||||||
- text: <code>shellSort([38, 45, 35, 8, 13])</code> should return <code>[8, 13, 35, 38, 45]</code>.
|
- text: <code>shellSort([38, 45, 35, 8, 13])</code> should return <code>[8, 13, 35, 38, 45]</code>.
|
||||||
testString: assert.deepEqual(shellSort([38, 45, 35, 8, 13]), [8, 13, 35, 38, 45], '<code>shellSort([38, 45, 35, 8, 13])</code> should return <code>[8, 13, 35, 38, 45]</code>.');
|
testString: assert.deepEqual(shellSort([38, 45, 35, 8, 13]), [8, 13, 35, 38, 45]);
|
||||||
- text: <code>shellSort([43, 36, 20, 34, 24])</code> should return <code>[20, 24, 34, 36, 43]</code>.
|
- text: <code>shellSort([43, 36, 20, 34, 24])</code> should return <code>[20, 24, 34, 36, 43]</code>.
|
||||||
testString: assert.deepEqual(shellSort([43, 36, 20, 34, 24]), [20, 24, 34, 36, 43], '<code>shellSort([43, 36, 20, 34, 24])</code> should return <code>[20, 24, 34, 36, 43]</code>.');
|
testString: assert.deepEqual(shellSort([43, 36, 20, 34, 24]), [20, 24, 34, 36, 43]);
|
||||||
- text: <code>shellSort([12, 33, 26, 18, 1, 16, 38])</code> should return <code>[1, 12, 16, 18, 26, 33, 38]</code>.
|
- text: <code>shellSort([12, 33, 26, 18, 1, 16, 38])</code> should return <code>[1, 12, 16, 18, 26, 33, 38]</code>.
|
||||||
testString: assert.deepEqual(shellSort([12, 33, 26, 18, 1, 16, 38]), [1, 12, 16, 18, 26, 33, 38], '<code>shellSort([12, 33, 26, 18, 1, 16, 38])</code> should return <code>[1, 12, 16, 18, 26, 33, 38]</code>.');
|
testString: assert.deepEqual(shellSort([12, 33, 26, 18, 1, 16, 38]), [1, 12, 16, 18, 26, 33, 38]);
|
||||||
- text: <code>shellSort([3, 39, 48, 16, 1, 4, 29])</code> should return <code>[1, 3, 4, 16, 29, 39, 48]</code>.
|
- text: <code>shellSort([3, 39, 48, 16, 1, 4, 29])</code> should return <code>[1, 3, 4, 16, 29, 39, 48]</code>.
|
||||||
testString: assert.deepEqual(shellSort([3, 39, 48, 16, 1, 4, 29]), [1, 3, 4, 16, 29, 39, 48], '<code>shellSort([3, 39, 48, 16, 1, 4, 29])</code> should return <code>[1, 3, 4, 16, 29, 39, 48]</code>.');
|
testString: assert.deepEqual(shellSort([3, 39, 48, 16, 1, 4, 29]), [1, 3, 4, 16, 29, 39, 48]);
|
||||||
```
|
```
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Challenge Seed
|
## Challenge Seed
|
||||||
|
|
||||||
<section id='challengeSeed'>
|
<section id='challengeSeed'>
|
||||||
<div id='js-seed'>
|
<div id='js-seed'>
|
||||||
|
|
||||||
@ -57,19 +61,19 @@ function shellSort(a) {
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Solution
|
## Solution
|
||||||
|
|
||||||
<section id='solution'>
|
<section id='solution'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function shellSort(a) {
|
function shellSort(a) {
|
||||||
for (var h = a.length; h > 0; h = parseInt(h / 2)) {
|
for (var h = a.length; h > 0; h = parseInt(h / 2)) {
|
||||||
for (var i = h; i < a.length; i++) {
|
for (var i = h; i < a.length; i++) {
|
||||||
var k = a[i];
|
var k = a[i];
|
||||||
for (var j = i; j >= h && k < a[j - h]; j -= h)
|
for (var j = i; j >= h && k < a[j - h]; j -= h) a[j] = a[j - h];
|
||||||
a[j] = a[j - h];
|
a[j] = k;
|
||||||
a[j] = k;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return a;
|
}
|
||||||
|
return a;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -6,6 +6,7 @@ forumTopicId: 302318
|
|||||||
---
|
---
|
||||||
|
|
||||||
## Description
|
## Description
|
||||||
|
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
Write a function to perform <a href="https://en.wikipedia.org/wiki/Stooge sort" target="_blank">Stooge Sort</a> on an array of integers. The function should return a sorted array.
|
Write a function to perform <a href="https://en.wikipedia.org/wiki/Stooge sort" target="_blank">Stooge Sort</a> on an array of integers. The function should return a sorted array.
|
||||||
The Stooge Sort algorithm is as follows:
|
The Stooge Sort algorithm is as follows:
|
||||||
@ -23,34 +24,37 @@ The Stooge Sort algorithm is as follows:
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
|
|
||||||
<section id='instructions'>
|
<section id='instructions'>
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
|
|
||||||
<section id='tests'>
|
<section id='tests'>
|
||||||
|
|
||||||
``` yml
|
```yml
|
||||||
tests:
|
tests:
|
||||||
- text: <code>stoogeSort</code> should be a function.
|
- text: <code>stoogeSort</code> should be a function.
|
||||||
testString: assert(typeof stoogeSort == 'function', '<code>stoogeSort</code> should be a function.');
|
testString: assert(typeof stoogeSort == 'function');
|
||||||
- text: <code>stoogeSort([25, 32, 12, 7, 20])</code> should return an array.
|
- text: <code>stoogeSort([25, 32, 12, 7, 20])</code> should return an array.
|
||||||
testString: assert(Array.isArray(stoogeSort([25, 32, 12, 7, 20])), '<code>stoogeSort([25, 32, 12, 7, 20])</code> should return an array.');
|
testString: assert(Array.isArray(stoogeSort([25, 32, 12, 7, 20])));
|
||||||
- text: <code>stoogeSort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.
|
- text: <code>stoogeSort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.
|
||||||
testString: assert.deepEqual(stoogeSort([25, 32, 12, 7, 20]), [7, 12, 20, 25, 32], '<code>stoogeSort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.');
|
testString: assert.deepEqual(stoogeSort([25, 32, 12, 7, 20]), [7, 12, 20, 25, 32]);
|
||||||
- text: <code>stoogeSort([38, 45, 35, 8, 13])</code> should return <code>[8, 13, 35, 38, 45]</code>.
|
- text: <code>stoogeSort([38, 45, 35, 8, 13])</code> should return <code>[8, 13, 35, 38, 45]</code>.
|
||||||
testString: assert.deepEqual(stoogeSort([38, 45, 35, 8, 13]), [8, 13, 35, 38, 45], '<code>stoogeSort([38, 45, 35, 8, 13])</code> should return <code>[8, 13, 35, 38, 45]</code>.');
|
testString: assert.deepEqual(stoogeSort([38, 45, 35, 8, 13]), [8, 13, 35, 38, 45]);
|
||||||
- text: <code>stoogeSort([43, 36, 20, 34, 24])</code> should return <code>[20, 24, 34, 36, 43]</code>.
|
- text: <code>stoogeSort([43, 36, 20, 34, 24])</code> should return <code>[20, 24, 34, 36, 43]</code>.
|
||||||
testString: assert.deepEqual(stoogeSort([43, 36, 20, 34, 24]), [20, 24, 34, 36, 43], '<code>stoogeSort([43, 36, 20, 34, 24])</code> should return <code>[20, 24, 34, 36, 43]</code>.');
|
testString: assert.deepEqual(stoogeSort([43, 36, 20, 34, 24]), [20, 24, 34, 36, 43]);
|
||||||
- text: <code>stoogeSort([12, 33, 26, 18, 1, 16, 38])</code> should return <code>[1, 12, 16, 18, 26, 33, 38]</code>.
|
- text: <code>stoogeSort([12, 33, 26, 18, 1, 16, 38])</code> should return <code>[1, 12, 16, 18, 26, 33, 38]</code>.
|
||||||
testString: assert.deepEqual(stoogeSort([12, 33, 26, 18, 1, 16, 38]), [1, 12, 16, 18, 26, 33, 38], '<code>stoogeSort([12, 33, 26, 18, 1, 16, 38])</code> should return <code>[1, 12, 16, 18, 26, 33, 38]</code>.');
|
testString: assert.deepEqual(stoogeSort([12, 33, 26, 18, 1, 16, 38]), [1, 12, 16, 18, 26, 33, 38]);
|
||||||
- text: <code>stoogeSort([3, 39, 48, 16, 1, 4, 29])</code> should return <code>[1, 3, 4, 16, 29, 39, 48]</code>.
|
- text: <code>stoogeSort([3, 39, 48, 16, 1, 4, 29])</code> should return <code>[1, 3, 4, 16, 29, 39, 48]</code>.
|
||||||
testString: assert.deepEqual(stoogeSort([3, 39, 48, 16, 1, 4, 29]), [1, 3, 4, 16, 29, 39, 48], '<code>stoogeSort([3, 39, 48, 16, 1, 4, 29])</code> should return <code>[1, 3, 4, 16, 29, 39, 48]</code>.');
|
testString: assert.deepEqual(stoogeSort([3, 39, 48, 16, 1, 4, 29]), [1, 3, 4, 16, 29, 39, 48]);
|
||||||
```
|
```
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Challenge Seed
|
## Challenge Seed
|
||||||
|
|
||||||
<section id='challengeSeed'>
|
<section id='challengeSeed'>
|
||||||
<div id='js-seed'>
|
<div id='js-seed'>
|
||||||
|
|
||||||
@ -64,6 +68,7 @@ function stoogeSort(arr) {
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Solution
|
## Solution
|
||||||
|
|
||||||
<section id='solution'>
|
<section id='solution'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
@ -6,40 +6,44 @@ forumTopicId: 302319
|
|||||||
---
|
---
|
||||||
|
|
||||||
## Description
|
## Description
|
||||||
|
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
Write a function to sort an array using the <a href="https://en.wikipedia.org/wiki/Strand sort" target="_blank">Strand sort</a>. The function should return the sorted array.
|
Write a function to sort an array using the <a href="https://en.wikipedia.org/wiki/Strand sort" target="_blank">Strand sort</a>. 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.
|
This is a way of sorting numbers by extracting shorter sequences of already sorted numbers from an unsorted list.
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
|
|
||||||
<section id='instructions'>
|
<section id='instructions'>
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
|
|
||||||
<section id='tests'>
|
<section id='tests'>
|
||||||
|
|
||||||
``` yml
|
```yml
|
||||||
tests:
|
tests:
|
||||||
- text: <code>strandSort</code> should be a function.
|
- text: <code>strandSort</code> should be a function.
|
||||||
testString: assert(typeof strandSort == 'function', '<code>strandSort</code> should be a function.');
|
testString: assert(typeof strandSort == 'function');
|
||||||
- text: <code>strandSort([25, 32, 12, 7, 20])</code> should return an array.
|
- text: <code>strandSort([25, 32, 12, 7, 20])</code> should return an array.
|
||||||
testString: assert(Array.isArray(strandSort([25, 32, 12, 7, 20])), '<code>strandSort([25, 32, 12, 7, 20])</code> should return an array.');
|
testString: assert(Array.isArray(strandSort([25, 32, 12, 7, 20])));
|
||||||
- text: <code>strandSort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.
|
- text: <code>strandSort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.
|
||||||
testString: assert.deepEqual(strandSort([25, 32, 12, 7, 20]), [7, 12, 20, 25, 32], '<code>strandSort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.');
|
testString: assert.deepEqual(strandSort([25, 32, 12, 7, 20]), [7, 12, 20, 25, 32]);
|
||||||
- text: <code>strandSort([38, 45, 35, 8, 13])</code> should return <code>[8, 13, 35, 38, 45]</code>.
|
- text: <code>strandSort([38, 45, 35, 8, 13])</code> should return <code>[8, 13, 35, 38, 45]</code>.
|
||||||
testString: assert.deepEqual(strandSort([38, 45, 35, 8, 13]), [8, 13, 35, 38, 45], '<code>strandSort([38, 45, 35, 8, 13])</code> should return <code>[8, 13, 35, 38, 45]</code>.');
|
testString: assert.deepEqual(strandSort([38, 45, 35, 8, 13]), [8, 13, 35, 38, 45]);
|
||||||
- text: <code>strandSort([43, 36, 20, 34, 24])</code> should return <code>[20, 24, 34, 36, 43]</code>.
|
- text: <code>strandSort([43, 36, 20, 34, 24])</code> should return <code>[20, 24, 34, 36, 43]</code>.
|
||||||
testString: assert.deepEqual(strandSort([43, 36, 20, 34, 24]), [20, 24, 34, 36, 43], '<code>strandSort([43, 36, 20, 34, 24])</code> should return <code>[20, 24, 34, 36, 43]</code>.');
|
testString: assert.deepEqual(strandSort([43, 36, 20, 34, 24]), [20, 24, 34, 36, 43]);
|
||||||
- text: <code>strandSort([12, 33, 26, 18, 1, 16, 38])</code> should return <code>[1, 12, 16, 18, 26, 33, 38]</code>.
|
- text: <code>strandSort([12, 33, 26, 18, 1, 16, 38])</code> should return <code>[1, 12, 16, 18, 26, 33, 38]</code>.
|
||||||
testString: assert.deepEqual(strandSort([12, 33, 26, 18, 1, 16, 38]), [1, 12, 16, 18, 26, 33, 38], '<code>strandSort([12, 33, 26, 18, 1, 16, 38])</code> should return <code>[1, 12, 16, 18, 26, 33, 38]</code>.');
|
testString: assert.deepEqual(strandSort([12, 33, 26, 18, 1, 16, 38]), [1, 12, 16, 18, 26, 33, 38]);
|
||||||
- text: <code>strandSort([3, 39, 48, 16, 1, 4, 29])</code> should return <code>[1, 3, 4, 16, 29, 39, 48]</code>.
|
- text: <code>strandSort([3, 39, 48, 16, 1, 4, 29])</code> should return <code>[1, 3, 4, 16, 29, 39, 48]</code>.
|
||||||
testString: assert.deepEqual(strandSort([3, 39, 48, 16, 1, 4, 29]), [1, 3, 4, 16, 29, 39, 48], '<code>strandSort([3, 39, 48, 16, 1, 4, 29])</code> should return <code>[1, 3, 4, 16, 29, 39, 48]</code>.');
|
testString: assert.deepEqual(strandSort([3, 39, 48, 16, 1, 4, 29]), [1, 3, 4, 16, 29, 39, 48]);
|
||||||
```
|
```
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Challenge Seed
|
## Challenge Seed
|
||||||
|
|
||||||
<section id='challengeSeed'>
|
<section id='challengeSeed'>
|
||||||
<div id='js-seed'>
|
<div id='js-seed'>
|
||||||
|
|
||||||
@ -53,18 +57,16 @@ function strandSort(list) {
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Solution
|
## Solution
|
||||||
|
|
||||||
<section id='solution'>
|
<section id='solution'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function strandSort(list) {
|
function strandSort(list) {
|
||||||
|
|
||||||
function merge(left, right) {
|
function merge(left, right) {
|
||||||
var result = [];
|
var result = [];
|
||||||
while (left.length != 0 && right.length != 0) {
|
while (left.length != 0 && right.length != 0) {
|
||||||
if (left[0] <= right[0])
|
if (left[0] <= right[0]) result.push(left.shift());
|
||||||
result.push(left.shift());
|
else result.push(right.shift());
|
||||||
else
|
|
||||||
result.push(right.shift());
|
|
||||||
}
|
}
|
||||||
result.push.apply(result, left);
|
result.push.apply(result, left);
|
||||||
result.push.apply(result, right);
|
result.push.apply(result, right);
|
||||||
|
@ -6,11 +6,13 @@ forumTopicId: 302320
|
|||||||
---
|
---
|
||||||
|
|
||||||
## Description
|
## Description
|
||||||
|
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
|
|
||||||
Soundex is an algorithm for creating indices for words based on their pronunciation.
|
Soundex is an algorithm for creating indices for words based on their pronunciation.
|
||||||
The goal is for homophones to be encoded to the same representation so that they can be matched despite minor differences in spelling (from <a href="https://en.wikipedia.org/wiki/soundex" target="_blank">the WP article</a>).
|
The goal is for homophones to be encoded to the same representation so that they can be matched despite minor differences in spelling (from <a href="https://en.wikipedia.org/wiki/soundex" target="_blank">the WP article</a>).
|
||||||
There is a major issue in many of the implementations concerning the separation of two consonants that have the same soundex code! According to the <a href="https://www.archives.gov/research/census/soundex.html" target="_blank">official Rules</a>. So check for instance if <b>Ashcraft</b> is coded to <b>A-261</b>.
|
There is a major issue in many of the implementations concerning the separation of two consonants that have the same soundex code! According to the <a href="https://www.archives.gov/research/census/soundex.html" target="_blank">official Rules</a>. So check for instance if <b>Ashcraft</b> is coded to <b>A-261</b>.
|
||||||
|
|
||||||
<ul>
|
<ul>
|
||||||
<li>If a vowel (A, E, I, O, U) separates two consonants that have the same soundex code, the consonant to the right of the vowel is coded. Tymczak is coded as T-522 (T, 5 for the M, 2 for the C, Z ignored (see "Side-by-Side" rule above), 2 for the K). Since the vowel "A" separates the Z and K, the K is coded.</li>
|
<li>If a vowel (A, E, I, O, U) separates two consonants that have the same soundex code, the consonant to the right of the vowel is coded. Tymczak is coded as T-522 (T, 5 for the M, 2 for the C, Z ignored (see "Side-by-Side" rule above), 2 for the K). Since the vowel "A" separates the Z and K, the K is coded.</li>
|
||||||
<li>If "H" or "W" separate two consonants that have the same soundex code, the consonant to the right of the vowel is not coded. Example: Ashcraft is coded A-261 (A, 2 for the S, C ignored, 6 for the R, 1 for the F). It is not coded A-226.</li>
|
<li>If "H" or "W" separate two consonants that have the same soundex code, the consonant to the right of the vowel is not coded. Example: Ashcraft is coded A-261 (A, 2 for the S, C ignored, 6 for the R, 1 for the F). It is not coded A-226.</li>
|
||||||
@ -18,44 +20,47 @@ There is a major issue in many of the implementations concerning the separation
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
|
|
||||||
<section id='instructions'>
|
<section id='instructions'>
|
||||||
Write a function that takes a string as a parameter and returns the encoded string.
|
Write a function that takes a string as a parameter and returns the encoded string.
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
|
|
||||||
<section id='tests'>
|
<section id='tests'>
|
||||||
|
|
||||||
``` yml
|
```yml
|
||||||
tests:
|
tests:
|
||||||
- text: <code>soundex</code> should be a function.
|
- text: <code>soundex</code> should be a function.
|
||||||
testString: assert(typeof soundex == 'function', '<code>soundex</code> should be a function.');
|
testString: assert(typeof soundex == 'function');
|
||||||
- text: <code>soundex("Soundex")</code> should return a string.
|
- text: <code>soundex("Soundex")</code> should return a string.
|
||||||
testString: assert(typeof soundex("Soundex") == 'string', '<code>soundex("Soundex")</code> should return a string.');
|
testString: assert(typeof soundex("Soundex") == 'string');
|
||||||
- text: <code>soundex("Soundex")</code> should return <code>"S532"</code>.
|
- text: <code>soundex("Soundex")</code> should return <code>"S532"</code>.
|
||||||
testString: assert.equal(soundex("Soundex"), "S532", '<code>soundex("Soundex")</code> should return <code>"S532"</code>.');
|
testString: assert.equal(soundex("Soundex"), "S532");
|
||||||
- text: <code>soundex("Example")</code> should return <code>"E251"</code>.
|
- text: <code>soundex("Example")</code> should return <code>"E251"</code>.
|
||||||
testString: assert.equal(soundex("Example"), "E251", '<code>soundex("Example")</code> should return <code>"E251"</code>.');
|
testString: assert.equal(soundex("Example"), "E251");
|
||||||
- text: <code>soundex("Sownteks")</code> should return <code>"S532"</code>.
|
- text: <code>soundex("Sownteks")</code> should return <code>"S532"</code>.
|
||||||
testString: assert.equal(soundex("Sownteks"), "S532", '<code>soundex("Sownteks")</code> should return <code>"S532"</code>.');
|
testString: assert.equal(soundex("Sownteks"), "S532");
|
||||||
- text: <code>soundex("Ekzampul")</code> should return <code>"E251"</code>.
|
- text: <code>soundex("Ekzampul")</code> should return <code>"E251"</code>.
|
||||||
testString: assert.equal(soundex("Ekzampul"), "E251", '<code>soundex("Ekzampul")</code> should return <code>"E251"</code>.');
|
testString: assert.equal(soundex("Ekzampul"), "E251");
|
||||||
- text: <code>soundex("Euler")</code> should return <code>"E460"</code>.
|
- text: <code>soundex("Euler")</code> should return <code>"E460"</code>.
|
||||||
testString: assert.equal(soundex("Euler"), "E460", '<code>soundex("Euler")</code> should return <code>"E460"</code>.');
|
testString: assert.equal(soundex("Euler"), "E460");
|
||||||
- text: <code>soundex("Gauss")</code> should return <code>"G200"</code>.
|
- text: <code>soundex("Gauss")</code> should return <code>"G200"</code>.
|
||||||
testString: assert.equal(soundex("Gauss"), "G200", '<code>soundex("Gauss")</code> should return <code>"G200"</code>.');
|
testString: assert.equal(soundex("Gauss"), "G200");
|
||||||
- text: <code>soundex("Hilbert")</code> should return <code>"H416"</code>.
|
- text: <code>soundex("Hilbert")</code> should return <code>"H416"</code>.
|
||||||
testString: assert.equal(soundex("Hilbert"), "H416", '<code>soundex("Hilbert")</code> should return <code>"H416"</code>.');
|
testString: assert.equal(soundex("Hilbert"), "H416");
|
||||||
- text: <code>soundex("Knuth")</code> should return <code>"K530"</code>.
|
- text: <code>soundex("Knuth")</code> should return <code>"K530"</code>.
|
||||||
testString: assert.equal(soundex("Knuth"), "K530", '<code>soundex("Knuth")</code> should return <code>"K530"</code>.');
|
testString: assert.equal(soundex("Knuth"), "K530");
|
||||||
- text: <code>soundex("Lloyd")</code> should return <code>"L300"</code>.
|
- text: <code>soundex("Lloyd")</code> should return <code>"L300"</code>.
|
||||||
testString: assert.equal(soundex("Lloyd"), "L300", '<code>soundex("Lloyd")</code> should return <code>"L300"</code>.');
|
testString: assert.equal(soundex("Lloyd"), "L300");
|
||||||
- text: <code>soundex("Lukasiewicz")</code> should return <code>"L222"</code>.
|
- text: <code>soundex("Lukasiewicz")</code> should return <code>"L222"</code>.
|
||||||
testString: assert.equal(soundex("Lukasiewicz"), "L222", '<code>soundex("Lukasiewicz")</code> should return <code>"L222"</code>.');
|
testString: assert.equal(soundex("Lukasiewicz"), "L222");
|
||||||
```
|
```
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Challenge Seed
|
## Challenge Seed
|
||||||
|
|
||||||
<section id='challengeSeed'>
|
<section id='challengeSeed'>
|
||||||
|
|
||||||
<div id='js-seed'>
|
<div id='js-seed'>
|
||||||
@ -70,30 +75,49 @@ function soundex(s) {
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Solution
|
## Solution
|
||||||
|
|
||||||
<section id='solution'>
|
<section id='solution'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function soundex(s) {
|
function soundex(s) {
|
||||||
var a = s.toLowerCase().split('')
|
var a = s.toLowerCase().split('');
|
||||||
var f = a.shift(),
|
var f = a.shift(),
|
||||||
r = '',
|
r = '',
|
||||||
codes = {
|
codes = {
|
||||||
a: '', e: '', i: '', o: '', u: '',
|
a: '',
|
||||||
b: 1, f: 1, p: 1, v: 1,
|
e: '',
|
||||||
c: 2, g: 2, j: 2, k: 2, q: 2, s: 2, x: 2, z: 2,
|
i: '',
|
||||||
d: 3, t: 3,
|
o: '',
|
||||||
l: 4,
|
u: '',
|
||||||
m: 5, n: 5,
|
b: 1,
|
||||||
r: 6
|
f: 1,
|
||||||
|
p: 1,
|
||||||
|
v: 1,
|
||||||
|
c: 2,
|
||||||
|
g: 2,
|
||||||
|
j: 2,
|
||||||
|
k: 2,
|
||||||
|
q: 2,
|
||||||
|
s: 2,
|
||||||
|
x: 2,
|
||||||
|
z: 2,
|
||||||
|
d: 3,
|
||||||
|
t: 3,
|
||||||
|
l: 4,
|
||||||
|
m: 5,
|
||||||
|
n: 5,
|
||||||
|
r: 6
|
||||||
};
|
};
|
||||||
r = f + a
|
r =
|
||||||
.map(function(v, i, a) {
|
f +
|
||||||
return codes[v]
|
a
|
||||||
})
|
.map(function(v, i, a) {
|
||||||
.filter(function(v, i, a) {
|
return codes[v];
|
||||||
return ((i === 0) ? v !== codes[f] : v !== a[i - 1]);
|
})
|
||||||
})
|
.filter(function(v, i, a) {
|
||||||
.join('');
|
return i === 0 ? v !== codes[f] : v !== a[i - 1];
|
||||||
|
})
|
||||||
|
.join('');
|
||||||
|
|
||||||
return (r + '000').slice(0, 4).toUpperCase();
|
return (r + '000').slice(0, 4).toUpperCase();
|
||||||
}
|
}
|
||||||
|
@ -6,11 +6,13 @@ forumTopicId: 302321
|
|||||||
---
|
---
|
||||||
|
|
||||||
## Description
|
## Description
|
||||||
|
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
|
|
||||||
Produce a spiral array.
|
Produce a spiral array.
|
||||||
A <i>spiral array</i> is a square arrangement of the first N<sup>2</sup> natural numbers, where the numbers increase sequentially as you go around the edges of the array spiraling inwards.
|
A <i>spiral array</i> is a square arrangement of the first N<sup>2</sup> natural numbers, where the numbers increase sequentially as you go around the edges of the array spiraling inwards.
|
||||||
For example, given <b>5</b>, produce this array:
|
For example, given <b>5</b>, produce this array:
|
||||||
|
|
||||||
<pre>
|
<pre>
|
||||||
0 1 2 3 4
|
0 1 2 3 4
|
||||||
15 16 17 18 5
|
15 16 17 18 5
|
||||||
@ -21,30 +23,33 @@ For example, given <b>5</b>, produce this array:
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
|
|
||||||
<section id='instructions'>
|
<section id='instructions'>
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
|
|
||||||
<section id='tests'>
|
<section id='tests'>
|
||||||
|
|
||||||
``` yml
|
```yml
|
||||||
tests:
|
tests:
|
||||||
- text: <code>spiralArray</code> should be a function.
|
- text: <code>spiralArray</code> should be a function.
|
||||||
testString: assert(typeof spiralArray=='function','<code>spiralArray</code> should be a function.');
|
testString: assert(typeof spiralArray=='function');
|
||||||
- text: <code>spiralArray(3)</code> should return an array.
|
- text: <code>spiralArray(3)</code> should return an array.
|
||||||
testString: assert(Array.isArray(spiralArray(3)), '<code>spiralArray(3)</code> should return an array.');
|
testString: assert(Array.isArray(spiralArray(3)));
|
||||||
- text: <code>spiralArray(3)</code> should return <code>[[0, 1, 2],[7, 8, 3],[6, 5, 4]]</code>.
|
- text: <code>spiralArray(3)</code> should return <code>[[0, 1, 2],[7, 8, 3],[6, 5, 4]]</code>.
|
||||||
testString: assert.deepEqual(spiralArray(3), [[0, 1, 2], [7, 8, 3], [6, 5, 4]], '<code>spiralArray(3)</code> should return <code>[[0, 1, 2],[7, 8, 3],[6, 5, 4]]</code>.');
|
testString: assert.deepEqual(spiralArray(3), [[0, 1, 2], [7, 8, 3], [6, 5, 4]]);
|
||||||
- text: <code>spiralArray(4)</code> should return <code>[[0, 1, 2, 3],[11, 12, 13, 4],[10, 15, 14, 5],[9, 8, 7, 6]]</code>.
|
- text: <code>spiralArray(4)</code> should return <code>[[0, 1, 2, 3],[11, 12, 13, 4],[10, 15, 14, 5],[9, 8, 7, 6]]</code>.
|
||||||
testString: assert.deepEqual(spiralArray(4), [[0, 1, 2, 3], [11, 12, 13, 4], [10, 15, 14, 5], [9, 8, 7, 6]], '<code>spiralArray(4)</code> should return <code>[[0, 1, 2, 3],[11, 12, 13, 4],[10, 15, 14, 5],[9, 8, 7, 6]]</code>.');
|
testString: assert.deepEqual(spiralArray(4), [[0, 1, 2, 3], [11, 12, 13, 4], [10, 15, 14, 5], [9, 8, 7, 6]]);
|
||||||
- text: <code>spiralArray(5)</code> should return <code>[[0, 1, 2, 3, 4],[15, 16, 17, 18, 5],[14, 23, 24, 19, 6],[13, 22, 21, 20, 7],[12, 11, 10, 9, 8]]</code>.
|
- text: <code>spiralArray(5)</code> should return <code>[[0, 1, 2, 3, 4],[15, 16, 17, 18, 5],[14, 23, 24, 19, 6],[13, 22, 21, 20, 7],[12, 11, 10, 9, 8]]</code>.
|
||||||
testString: assert.deepEqual(spiralArray(5), [[0, 1, 2, 3, 4], [15, 16, 17, 18, 5], [14, 23, 24, 19, 6], [13, 22, 21, 20, 7], [12, 11, 10, 9, 8]], '<code>spiralArray(5)</code> should return <code>[[0, 1, 2, 3, 4],[15, 16, 17, 18, 5],[14, 23, 24, 19, 6],[13, 22, 21, 20, 7],[12, 11, 10, 9, 8]]</code>.');
|
testString: assert.deepEqual(spiralArray(5), [[0, 1, 2, 3, 4], [15, 16, 17, 18, 5], [14, 23, 24, 19, 6], [13, 22, 21, 20, 7], [12, 11, 10, 9, 8]]);
|
||||||
```
|
```
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Challenge Seed
|
## Challenge Seed
|
||||||
|
|
||||||
<section id='challengeSeed'>
|
<section id='challengeSeed'>
|
||||||
|
|
||||||
<div id='js-seed'>
|
<div id='js-seed'>
|
||||||
@ -59,25 +64,37 @@ function spiralArray(n) {
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Solution
|
## Solution
|
||||||
|
|
||||||
<section id='solution'>
|
<section id='solution'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function spiralArray(n) {
|
function spiralArray(n) {
|
||||||
var arr = Array(n),
|
var arr = Array(n),
|
||||||
x = 0, y = n,
|
x = 0,
|
||||||
total = n * n--,
|
y = n,
|
||||||
dx = 1, dy = 0,
|
total = n * n--,
|
||||||
i = 0, j = 0;
|
dx = 1,
|
||||||
while (y) arr[--y] = [];
|
dy = 0,
|
||||||
while (i < total) {
|
i = 0,
|
||||||
arr[y][x] = i++;
|
j = 0;
|
||||||
x += dx; y += dy;
|
while (y) arr[--y] = [];
|
||||||
if (++j == n) {
|
while (i < total) {
|
||||||
if (dy < 0) {x++; y++; n -= 2}
|
arr[y][x] = i++;
|
||||||
j = dx; dx = -dy; dy = j; j = 0;
|
x += dx;
|
||||||
}
|
y += dy;
|
||||||
|
if (++j == n) {
|
||||||
|
if (dy < 0) {
|
||||||
|
x++;
|
||||||
|
y++;
|
||||||
|
n -= 2;
|
||||||
|
}
|
||||||
|
j = dx;
|
||||||
|
dx = -dy;
|
||||||
|
dy = j;
|
||||||
|
j = 0;
|
||||||
}
|
}
|
||||||
return arr;
|
}
|
||||||
|
return arr;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -6,49 +6,56 @@ forumTopicId: 302322
|
|||||||
---
|
---
|
||||||
|
|
||||||
## Description
|
## Description
|
||||||
|
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
|
|
||||||
Split a (character) string into comma (plus a blank) delimited strings based on a change of character (left to right).
|
Split a (character) string into comma (plus a blank) delimited strings based on a change of character (left to right).
|
||||||
Blanks should be treated as any other character (except they are problematic to display clearly). The same applies to commas.
|
Blanks should be treated as any other character (except they are problematic to display clearly). The same applies to commas.
|
||||||
For instance, the string:
|
For instance, the string:
|
||||||
|
|
||||||
<pre>
|
<pre>
|
||||||
"gHHH5YY++///\"
|
"gHHH5YY++///\"
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
should be split as:
|
should be split as:
|
||||||
|
|
||||||
<pre>
|
<pre>
|
||||||
["g", "HHH", "5", "YY", "++", "///", "\" ];
|
["g", "HHH", "5", "YY", "++", "///", "\" ];
|
||||||
</pre>
|
</pre>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
|
|
||||||
<section id='instructions'>
|
<section id='instructions'>
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
|
|
||||||
<section id='tests'>
|
<section id='tests'>
|
||||||
|
|
||||||
``` yml
|
```yml
|
||||||
tests:
|
tests:
|
||||||
- text: <code>split</code> should be a function.
|
- text: <code>split</code> should be a function.
|
||||||
testString: assert(typeof split == 'function', '<code>split</code> should be a function.');
|
testString: assert(typeof split == 'function');
|
||||||
- text: <code>split("hello")</code> should return an array.
|
- text: <code>split("hello")</code> should return an array.
|
||||||
testString: assert(Array.isArray(split("hello")), '<code>split("hello")</code> should return an array.');
|
testString: assert(Array.isArray(split("hello")));
|
||||||
- text: <code>split("hello")</code> should return <code>["h", "e", "ll", "o"]</code>.
|
- text: <code>split("hello")</code> should return <code>["h", "e", "ll", "o"]</code>.
|
||||||
testString: assert.deepEqual(split("hello"), ["h", "e", "ll", "o"], '<code>split("hello")</code> should return <code>["h", "e", "ll", "o"]</code>.');
|
testString: assert.deepEqual(split("hello"), ["h", "e", "ll", "o"]);
|
||||||
- text: <code>split("commission")</code> should return <code>["c", "o", "mm", "i", "ss", "i", "o", "n"]</code>.
|
- text: <code>split("commission")</code> should return <code>["c", "o", "mm", "i", "ss", "i", "o", "n"]</code>.
|
||||||
testString: assert.deepEqual(split("commission"), ["c", "o", "mm", "i", "ss", "i", "o", "n"], '<code>split("commission")</code> should return <code>["c", "o", "mm", "i", "ss", "i", "o", "n"]</code>.');
|
testString: assert.deepEqual(split("commission"), ["c", "o", "mm", "i", "ss", "i", "o", "n"]);
|
||||||
- text: <code>split("ssss----====llloooo")</code> should return <code>["ssss", "----", "====", "lll", "oooo"]</code>.
|
- text: <code>split("ssss----====llloooo")</code> should return <code>["ssss", "----", "====", "lll", "oooo"]</code>.
|
||||||
testString: assert.deepEqual(split("ssss----====llloooo"), ["ssss", "----", "====", "lll", "oooo"], '<code>split("ssss----====llloooo")</code> should return <code>["ssss", "----", "====", "lll", "oooo"]</code>.');
|
testString: assert.deepEqual(split("ssss----====llloooo"), ["ssss", "----", "====", "lll", "oooo"]);
|
||||||
- text: <code>split("sssmmmaaammmaaat")</code> should return <code>["sss", "mmm", "aaa", "mmm", "aaa", "t"]</code>.
|
- text: <code>split("sssmmmaaammmaaat")</code> should return <code>["sss", "mmm", "aaa", "mmm", "aaa", "t"]</code>.
|
||||||
testString: assert.deepEqual(split("sssmmmaaammmaaat"), ["sss", "mmm", "aaa", "mmm", "aaa", "t"], '<code>split("sssmmmaaammmaaat")</code> should return <code>["sss", "mmm", "aaa", "mmm", "aaa", "t"]</code>.');
|
testString: assert.deepEqual(split("sssmmmaaammmaaat"), ["sss", "mmm", "aaa", "mmm", "aaa", "t"]);
|
||||||
- text: <code>split("gHHH5YY++///\")</code> should return <code>["g", "HHH", "5", "YY", "++", "///", "\\"]</code>.
|
- text: <code>split("gHHH5YY++///\")</code> should return <code>["g", "HHH", "5", "YY", "++", "///", "\\"]</code>.
|
||||||
testString: assert.deepEqual(split("gHHH5YY++///\\"), ["g", "HHH", "5", "YY", "++", "///", "\\"], '<code>split("gHHH5YY++///\")</code> should return <code>["g", "HHH", "5", "YY", "++", "///", "\\"]</code>.');
|
testString: assert.deepEqual(split("gHHH5YY++///\\"), ["g", "HHH", "5", "YY", "++", "///", "\\"]);
|
||||||
```
|
```
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Challenge Seed
|
## Challenge Seed
|
||||||
|
|
||||||
<section id='challengeSeed'>
|
<section id='challengeSeed'>
|
||||||
|
|
||||||
<div id='js-seed'>
|
<div id='js-seed'>
|
||||||
@ -63,32 +70,36 @@ function split(str) {
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Solution
|
## Solution
|
||||||
|
|
||||||
<section id='solution'>
|
<section id='solution'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function split(str) {
|
function split(str) {
|
||||||
const concat = xs =>
|
const concat = xs =>
|
||||||
xs.length > 0 ? (() => {
|
xs.length > 0
|
||||||
const unit = typeof xs[0] === 'string' ? '' : [];
|
? (() => {
|
||||||
return unit.concat.apply(unit, xs);
|
const unit = typeof xs[0] === 'string' ? '' : [];
|
||||||
})() : [];
|
return unit.concat.apply(unit, xs);
|
||||||
|
})()
|
||||||
|
: [];
|
||||||
|
|
||||||
const group = xs => groupBy((a, b) => a === b, xs);
|
const group = xs => groupBy((a, b) => a === b, xs);
|
||||||
|
|
||||||
const groupBy = (f, xs) => {
|
const groupBy = (f, xs) => {
|
||||||
const dct = xs.slice(1)
|
const dct = xs.slice(1).reduce(
|
||||||
.reduce((a, x) => {
|
(a, x) => {
|
||||||
const
|
const h = a.active.length > 0 ? a.active[0] : undefined,
|
||||||
h = a.active.length > 0 ? a.active[0] : undefined,
|
|
||||||
blnGroup = h !== undefined && f(h, x);
|
blnGroup = h !== undefined && f(h, x);
|
||||||
return {
|
return {
|
||||||
active: blnGroup ? a.active.concat([x]) : [x],
|
active: blnGroup ? a.active.concat([x]) : [x],
|
||||||
sofar: blnGroup ? a.sofar : a.sofar.concat([a.active])
|
sofar: blnGroup ? a.sofar : a.sofar.concat([a.active])
|
||||||
};
|
};
|
||||||
}, {
|
},
|
||||||
|
{
|
||||||
active: xs.length > 0 ? [xs[0]] : [],
|
active: xs.length > 0 ? [xs[0]] : [],
|
||||||
sofar: []
|
sofar: []
|
||||||
});
|
}
|
||||||
|
);
|
||||||
return dct.sofar.concat(dct.active.length > 0 ? [dct.active] : []);
|
return dct.sofar.concat(dct.active.length > 0 ? [dct.active] : []);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -96,7 +107,7 @@ function split(str) {
|
|||||||
|
|
||||||
const stringChars = s => s.split('');
|
const stringChars = s => s.split('');
|
||||||
|
|
||||||
return map(concat, group(stringChars(str)))
|
return map(concat, group(stringChars(str)));
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -6,39 +6,45 @@ forumTopicId: 302323
|
|||||||
---
|
---
|
||||||
|
|
||||||
## Description
|
## Description
|
||||||
|
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
|
|
||||||
This task is inspired by Mark Nelson's DDJ Column "Wordplay" and one of the weekly puzzle challenges from Will Shortz on NPR Weekend Edition <a href="https://www.npr.org/templates/story/story.php?storyId=9264290" target="_blank">[1]</a> and originally attributed to David Edelheit.
|
This task is inspired by Mark Nelson's DDJ Column "Wordplay" and one of the weekly puzzle challenges from Will Shortz on NPR Weekend Edition <a href="https://www.npr.org/templates/story/story.php?storyId=9264290" target="_blank">[1]</a> and originally attributed to David Edelheit.
|
||||||
The challenge was to take the names of two U.S. States, mix them all together, then rearrange the letters to form the names of two <i>different</i> U.S. States (so that all four state names differ from one another).
|
The challenge was to take the names of two U.S. States, mix them all together, then rearrange the letters to form the names of two <i>different</i> U.S. States (so that all four state names differ from one another).
|
||||||
What states are these?
|
What states are these?
|
||||||
The problem was reissued on <a href="https://tapestry.tucson.az.us/twiki/bin/view/Main/StateNamesPuzzle" target="_blank">the Unicon Discussion Web</a> which includes several solutions with analysis. Several techniques may be helpful and you may wish to refer to <a href="https://en.wikipedia.org/wiki/Goedel_numbering" target="_blank">Gödel numbering</a>, <a href="https://en.wikipedia.org/wiki/Equivalence_relation" target="_blank">equivalence relations</a>, and <a href="https://en.wikipedia.org/wiki/Equivalence_classes" target="_blank">equivalence classes</a>. The basic merits of these were discussed in the Unicon Discussion Web.
|
The problem was reissued on <a href="https://tapestry.tucson.az.us/twiki/bin/view/Main/StateNamesPuzzle" target="_blank">the Unicon Discussion Web</a> which includes several solutions with analysis. Several techniques may be helpful and you may wish to refer to <a href="https://en.wikipedia.org/wiki/Goedel_numbering" target="_blank">Gödel numbering</a>, <a href="https://en.wikipedia.org/wiki/Equivalence_relation" target="_blank">equivalence relations</a>, and <a href="https://en.wikipedia.org/wiki/Equivalence_classes" target="_blank">equivalence classes</a>. The basic merits of these were discussed in the Unicon Discussion Web.
|
||||||
A second challenge in the form of a set of fictitious new states was also presented.
|
A second challenge in the form of a set of fictitious new states was also presented.
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
|
|
||||||
<section id='instructions'>
|
<section id='instructions'>
|
||||||
|
|
||||||
Write a function to solve the challenge for the given array of names of states. The function should return an array. Each element should be an object in this form: <code>{"from":[],"to":[]}</code>. The "from" array should contain the original names and the "to" array should contain the resultant names.
|
Write a function to solve the challenge for the given array of names of states. The function should return an array. Each element should be an object in this form: <code>{"from":[],"to":[]}</code>. The "from" array should contain the original names and the "to" array should contain the resultant names.
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
|
|
||||||
<section id='tests'>
|
<section id='tests'>
|
||||||
|
|
||||||
``` yml
|
```yml
|
||||||
tests:
|
tests:
|
||||||
- text: <code>solve</code> should be a function.
|
- text: <code>solve</code> should be a function.
|
||||||
testString: assert(typeof solve == 'function', '<code>solve</code> should be a function.');
|
testString: assert(typeof solve == 'function');
|
||||||
- text: <code>solve(["New Mexico", "New York", "North Carolina ", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota"])</code> should return an array.
|
- text: <code>solve(["New Mexico", "New York", "North Carolina ", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota"])</code> should return an array.
|
||||||
testString: assert(Array.isArray(solve(["New Mexico", "New York", "North Carolina ", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota"])), '<code>solve(["New Mexico", "New York", "North Carolina ", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota"])</code> should return an array.');
|
testString: assert(Array.isArray(solve(["New Mexico", "New York", "North Carolina ", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota"])));
|
||||||
- text: '<code>solve(["New Mexico", "New York", "North Carolina ", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota"])</code> should return <code>[{ from: ["North Carolina ", "South Dakota"], to: ["North Dakota", "South Carolina"] }]</code>.'
|
- text: '<code>solve(["New Mexico", "New York", "North Carolina ", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota"])</code> should return <code>[{ from: ["North Carolina ", "South Dakota"], to: ["North Dakota", "South Carolina"] }]</code>.'
|
||||||
testString: assert.deepEqual(solve(["New Mexico", "New York", "North Carolina ", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota"]), [{ from:["North Carolina ", "South Dakota"], to:["North Dakota", "South Carolina"] }], '<code>solve(["New Mexico", "New York", "North Carolina ", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota"])</code> should return <code>[{ from:["North Carolina ", "South Dakota"], to:["North Dakota", "South Carolina"] }]</code>.');
|
testString: assert.deepEqual(solve(["New Mexico", "New York", "North Carolina ", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota"]), [{ from:["North Carolina ", "South Dakota"], to:["North Dakota", "South Carolina"] }]);
|
||||||
- text: '<code>solve(["New York", "New Kory", "Wen Kory", "York New", "Kory New", "New Kory"])</code> should return <code>[{ from: ["New Kory", "New York"], to: ["Wen Kory", "York New"] }, { from: ["New Kory", "New York"], to: ["Kory New", "Wen Kory"] }, { from: ["New Kory", "New York"], to: ["Kory New", "York New"] }, { from: ["New York", "Wen Kory"], to: ["New Kory", "York New"] }, { from: ["New York", "Wen Kory"], to: ["Kory New", "New Kory"] }, { from: ["New York", "Wen Kory"], to: ["Kory New", "York New"] }, { from: ["New York", "York New"], to: ["New Kory", "Wen Kory"] }, { from: ["New York", "York New"], to: ["Kory New", "New Kory"] }, { from: ["New York", "York New"], to: ["Kory New", "Wen Kory"] }, { from: ["Kory New", "New York"], to: ["New Kory", "Wen Kory"] }, { from: ["Kory New", "New York"], to: ["New Kory", "York New"] }, { from: ["Kory New", "New York"], to: ["Wen Kory", "York New"] }, { from: ["New Kory", "Wen Kory"], to: ["Kory New", "York New"] }, { from: ["New Kory", "York New"], to: ["Kory New", "Wen Kory"] }, { from: ["Kory New", "New Kory"], to: ["Wen Kory", "York New"] }]</code>.'
|
- text: '<code>solve(["New York", "New Kory", "Wen Kory", "York New", "Kory New", "New Kory"])</code> should return <code>[{ from: ["New Kory", "New York"], to: ["Wen Kory", "York New"] }, { from: ["New Kory", "New York"], to: ["Kory New", "Wen Kory"] }, { from: ["New Kory", "New York"], to: ["Kory New", "York New"] }, { from: ["New York", "Wen Kory"], to: ["New Kory", "York New"] }, { from: ["New York", "Wen Kory"], to: ["Kory New", "New Kory"] }, { from: ["New York", "Wen Kory"], to: ["Kory New", "York New"] }, { from: ["New York", "York New"], to: ["New Kory", "Wen Kory"] }, { from: ["New York", "York New"], to: ["Kory New", "New Kory"] }, { from: ["New York", "York New"], to: ["Kory New", "Wen Kory"] }, { from: ["Kory New", "New York"], to: ["New Kory", "Wen Kory"] }, { from: ["Kory New", "New York"], to: ["New Kory", "York New"] }, { from: ["Kory New", "New York"], to: ["Wen Kory", "York New"] }, { from: ["New Kory", "Wen Kory"], to: ["Kory New", "York New"] }, { from: ["New Kory", "York New"], to: ["Kory New", "Wen Kory"] }, { from: ["Kory New", "New Kory"], to: ["Wen Kory", "York New"] }]</code>.'
|
||||||
testString: assert.deepEqual(solve(["New York", "New Kory", "Wen Kory", "York New", "Kory New", "New Kory"]), [{ from:["New Kory", "New York"], to:["Wen Kory", "York New"] }, { from:["New Kory", "New York"], to:["Kory New", "Wen Kory"] }, { from:["New Kory", "New York"], to:["Kory New", "York New"] }, { from:["New York", "Wen Kory"], to:["New Kory", "York New"] }, { from:["New York", "Wen Kory"], to:["Kory New", "New Kory"] }, { from:["New York", "Wen Kory"], to:["Kory New", "York New"] }, { from:["New York", "York New"], to:["New Kory", "Wen Kory"] }, { from:["New York", "York New"], to:["Kory New", "New Kory"] }, { from:["New York", "York New"], to:["Kory New", "Wen Kory"] }, { from:["Kory New", "New York"], to:["New Kory", "Wen Kory"] }, { from:["Kory New", "New York"], to:["New Kory", "York New"] }, { from:["Kory New", "New York"], to:["Wen Kory", "York New"] }, { from:["New Kory", "Wen Kory"], to:["Kory New", "York New"] }, { from:["New Kory", "York New"], to:["Kory New", "Wen Kory"] }, { from:["Kory New", "New Kory"], to:["Wen Kory", "York New"] }], '<code>solve(["New York", "New Kory", "Wen Kory", "York New", "Kory New", "New Kory"])</code> should return <code>[{ from:["New Kory", "New York"], to:["Wen Kory", "York New"] }, { from:["New Kory", "New York"], to:["Kory New", "Wen Kory"] }, { from:["New Kory", "New York"], to:["Kory New", "York New"] }, { from:["New York", "Wen Kory"], to:["New Kory", "York New"] }, { from:["New York", "Wen Kory"], to:["Kory New", "New Kory"] }, { from:["New York", "Wen Kory"], to:["Kory New", "York New"] }, { from:["New York", "York New"], to:["New Kory", "Wen Kory"] }, { from:["New York", "York New"], to:["Kory New", "New Kory"] }, { from:["New York", "York New"], to:["Kory New", "Wen Kory"] }, { from:["Kory New", "New York"], to:["New Kory", "Wen Kory"] }, { from:["Kory New", "New York"], to:["New Kory", "York New"] }, { from:["Kory New", "New York"], to:["Wen Kory", "York New"] }, { from:["New Kory", "Wen Kory"], to:["Kory New", "York New"] }, { from:["New Kory", "York New"], to:["Kory New", "Wen Kory"] }, { from:["Kory New", "New Kory"], to:["Wen Kory", "York New"] }]</code>.');
|
testString: assert.deepEqual(solve(["New York", "New Kory", "Wen Kory", "York New", "Kory New", "New Kory"]), [{ from:["New Kory", "New York"], to:["Wen Kory", "York New"] }, { from:["New Kory", "New York"], to:["Kory New", "Wen Kory"] }, { from:["New Kory", "New York"], to:["Kory New", "York New"] }, { from:["New York", "Wen Kory"], to:["New Kory", "York New"] }, { from:["New York", "Wen Kory"], to:["Kory New", "New Kory"] }, { from:["New York", "Wen Kory"], to:["Kory New", "York New"] }, { from:["New York", "York New"], to:["New Kory", "Wen Kory"] }, { from:["New York", "York New"], to:["Kory New", "New Kory"] }, { from:["New York", "York New"], to:["Kory New", "Wen Kory"] }, { from:["Kory New", "New York"], to:["New Kory", "Wen Kory"] }, { from:["Kory New", "New York"], to:["New Kory", "York New"] }, { from:["Kory New", "New York"], to:["Wen Kory", "York New"] }, { from:["New Kory", "Wen Kory"], to:["Kory New", "York New"] }, { from:["New Kory", "York New"], to:["Kory New", "Wen Kory"] }, { from:["Kory New", "New Kory"], to:["Wen Kory", "York New"] }]);
|
||||||
```
|
```
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Challenge Seed
|
## Challenge Seed
|
||||||
|
|
||||||
<section id='challengeSeed'>
|
<section id='challengeSeed'>
|
||||||
|
|
||||||
<div id='js-seed'>
|
<div id='js-seed'>
|
||||||
@ -53,24 +59,24 @@ function solve(input) {
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Solution
|
## Solution
|
||||||
|
|
||||||
<section id='solution'>
|
<section id='solution'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function solve(input) {
|
function solve(input) {
|
||||||
var orig = {};
|
var orig = {};
|
||||||
input.forEach(function(e) {
|
input.forEach(function(e) {
|
||||||
orig[e.replace(/\s/g, "").toLowerCase()] = e;
|
orig[e.replace(/\s/g, '').toLowerCase()] = e;
|
||||||
})
|
});
|
||||||
|
|
||||||
input = Object.keys(orig)
|
input = Object.keys(orig);
|
||||||
var map = {};
|
var map = {};
|
||||||
for (var i = 0; i < input.length - 1; i++) {
|
for (var i = 0; i < input.length - 1; i++) {
|
||||||
var pair0 = input[i];
|
var pair0 = input[i];
|
||||||
for (var j = i + 1; j < input.length; j++) {
|
for (var j = i + 1; j < input.length; j++) {
|
||||||
|
|
||||||
var pair = [pair0, input[j]];
|
var pair = [pair0, input[j]];
|
||||||
var s = pair0 + pair[1];
|
var s = pair0 + pair[1];
|
||||||
var key = s.split("").sort();
|
var key = s.split('').sort();
|
||||||
|
|
||||||
var val = map[key] ? map[key] : [];
|
var val = map[key] ? map[key] : [];
|
||||||
val.push(pair);
|
val.push(pair);
|
||||||
@ -79,21 +85,19 @@ function solve(input) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var result = [];
|
var result = [];
|
||||||
Object.keys(map).forEach((key) => {
|
Object.keys(map).forEach(key => {
|
||||||
|
|
||||||
for (var i = 0; i < map[key].length - 1; i++) {
|
for (var i = 0; i < map[key].length - 1; i++) {
|
||||||
var a = map[key][i];
|
var a = map[key][i];
|
||||||
for (var j = i + 1; j < map[key].length; j++) {
|
for (var j = i + 1; j < map[key].length; j++) {
|
||||||
var b = map[key][j];
|
var b = map[key][j];
|
||||||
|
|
||||||
if ((new Set([a[0], b[0], a[1], b[1]])).size < 4)
|
if (new Set([a[0], b[0], a[1], b[1]]).size < 4) continue;
|
||||||
continue;
|
var from = [orig[a[0]], orig[a[1]]].sort();
|
||||||
var from = [orig[a[0]], orig[a[1]]].sort()
|
var to = [orig[b[0]], orig[b[1]]].sort();
|
||||||
var to = [orig[b[0]], orig[b[1]]].sort()
|
|
||||||
result.push({
|
result.push({
|
||||||
from,
|
from,
|
||||||
to
|
to
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -6,6 +6,7 @@ forumTopicId: 302324
|
|||||||
---
|
---
|
||||||
|
|
||||||
## Description
|
## Description
|
||||||
|
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
For this task, the Stern-Brocot sequence is to be generated by an algorithm similar to that employed in generating the <a href="https://rosettacode.org/wiki/Fibonacci sequence" target="_blank">Fibonacci sequence</a>.
|
For this task, the Stern-Brocot sequence is to be generated by an algorithm similar to that employed in generating the <a href="https://rosettacode.org/wiki/Fibonacci sequence" target="_blank">Fibonacci sequence</a>.
|
||||||
<ol>
|
<ol>
|
||||||
@ -34,34 +35,37 @@ For this task, the Stern-Brocot sequence is to be generated by an algorithm simi
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
|
|
||||||
<section id='instructions'>
|
<section id='instructions'>
|
||||||
Create a function that returns the $ n^{th} $ member of the sequence using the method outlined above.
|
Create a function that returns the $ n^{th} $ member of the sequence using the method outlined above.
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
|
|
||||||
<section id='tests'>
|
<section id='tests'>
|
||||||
|
|
||||||
``` yml
|
```yml
|
||||||
tests:
|
tests:
|
||||||
- text: <code>sternBrocot</code> should be a function.
|
- text: <code>sternBrocot</code> should be a function.
|
||||||
testString: assert(typeof sternBrocot == 'function', '<code>sternBrocot</code> should be a function.');
|
testString: assert(typeof sternBrocot == 'function');
|
||||||
- text: <code>sternBrocot(2)</code> should return a number.
|
- text: <code>sternBrocot(2)</code> should return a number.
|
||||||
testString: assert(typeof sternBrocot(2) == 'number', '<code>sternBrocot(2)</code> should return a number.');
|
testString: assert(typeof sternBrocot(2) == 'number');
|
||||||
- text: <code>sternBrocot(2)</code> should return <code>3</code>.
|
- text: <code>sternBrocot(2)</code> should return <code>3</code>.
|
||||||
testString: assert.equal(sternBrocot(2), 3, '<code>sternBrocot(2)</code> should return <code>3</code>.');
|
testString: assert.equal(sternBrocot(2), 3);
|
||||||
- text: <code>sternBrocot(3)</code> should return <code>5</code>.
|
- text: <code>sternBrocot(3)</code> should return <code>5</code>.
|
||||||
testString: assert.equal(sternBrocot(3), 5, '<code>sternBrocot(3)</code> should return <code>5</code>.');
|
testString: assert.equal(sternBrocot(3), 5);
|
||||||
- text: <code>sternBrocot(5)</code> should return <code>11</code>.
|
- text: <code>sternBrocot(5)</code> should return <code>11</code>.
|
||||||
testString: assert.equal(sternBrocot(5), 11, '<code>sternBrocot(5)</code> should return <code>11</code>.');
|
testString: assert.equal(sternBrocot(5), 11);
|
||||||
- text: <code>sternBrocot(7)</code> should return <code>19</code>.
|
- text: <code>sternBrocot(7)</code> should return <code>19</code>.
|
||||||
testString: assert.equal(sternBrocot(7), 19, '<code>sternBrocot(7)</code> should return <code>19</code>.');
|
testString: assert.equal(sternBrocot(7), 19);
|
||||||
- text: <code>sternBrocot(10)</code> should return <code>39</code>.
|
- text: <code>sternBrocot(10)</code> should return <code>39</code>.
|
||||||
testString: assert.equal(sternBrocot(10), 39, '<code>sternBrocot(10)</code> should return <code>39</code>.');
|
testString: assert.equal(sternBrocot(10), 39);
|
||||||
```
|
```
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Challenge Seed
|
## Challenge Seed
|
||||||
|
|
||||||
<section id='challengeSeed'>
|
<section id='challengeSeed'>
|
||||||
<div id='js-seed'>
|
<div id='js-seed'>
|
||||||
|
|
||||||
@ -75,16 +79,21 @@ function sternBrocot(num) {
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Solution
|
## Solution
|
||||||
|
|
||||||
<section id='solution'>
|
<section id='solution'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function sternBrocot(num) {
|
function sternBrocot(num) {
|
||||||
function f(n) {
|
function f(n) {
|
||||||
return n < 2 ? n : (n & 1) ? f(Math.floor(n / 2)) + f(Math.floor(n / 2 + 1)) : f(Math.floor(n / 2));
|
return n < 2
|
||||||
|
? n
|
||||||
|
: n & 1
|
||||||
|
? f(Math.floor(n / 2)) + f(Math.floor(n / 2 + 1))
|
||||||
|
: f(Math.floor(n / 2));
|
||||||
}
|
}
|
||||||
|
|
||||||
function gcd(a, b) {
|
function gcd(a, b) {
|
||||||
return a ? a < b ? gcd(b % a, a) : gcd(a % b, b) : b;
|
return a ? (a < b ? gcd(b % a, a) : gcd(a % b, b)) : b;
|
||||||
}
|
}
|
||||||
var n;
|
var n;
|
||||||
for (n = 1; f(n) != num; n++);
|
for (n = 1; f(n) != num; n++);
|
||||||
|
@ -6,47 +6,52 @@ forumTopicId: 302325
|
|||||||
---
|
---
|
||||||
|
|
||||||
## Description
|
## Description
|
||||||
|
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
|
|
||||||
Implement functions to encrypt and decrypt a message using the <a href="https://en.wikipedia.org/wiki/Straddling_checkerboard">straddling checkerboard</a> method. The functions will take a string and an array as parameters. The array has 3 strings representing the 3 rows of the checkerboard. The output will be a series of decimal digits.
|
Implement functions to encrypt and decrypt a message using the <a href="https://en.wikipedia.org/wiki/Straddling_checkerboard">straddling checkerboard</a> method. The functions will take a string and an array as parameters. The array has 3 strings representing the 3 rows of the checkerboard. The output will be a series of decimal digits.
|
||||||
Numbers should be encrypted by inserting the escape character before each digit, then including the digit unencrypted. This should be reversed for decryption.
|
Numbers should be encrypted by inserting the escape character before each digit, then including the digit unencrypted. This should be reversed for decryption.
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
|
|
||||||
<section id='instructions'>
|
<section id='instructions'>
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
|
|
||||||
<section id='tests'>
|
<section id='tests'>
|
||||||
|
|
||||||
``` yml
|
```yml
|
||||||
tests:
|
tests:
|
||||||
- text: <code>straddle</code> should be a function.
|
- text: <code>straddle</code> should be a function.
|
||||||
testString: assert(typeof straddle == 'function', '<code>straddle</code> should be a function.');
|
testString: assert(typeof straddle == 'function');
|
||||||
- text: <code>straddle("One night-it was on the twentieth of March, 1888-I was returning.",["ESTONIA R", "BCDFGHJKLM", "PQUVWXYZ./"])</code> should return a string.
|
- text: <code>straddle("One night-it was on the twentieth of March, 1888-I was returning.",["ESTONIA R", "BCDFGHJKLM", "PQUVWXYZ./"])</code> should return a string.
|
||||||
testString: assert(typeof straddle("One night-it was on the twentieth of March, 1888-I was returning.", ["ESTONIA R", "BCDFGHJKLM", "PQUVWXYZ./"]) == 'string', '<code>straddle("One night-it was on the twentieth of March, 1888-I was returning.",["ESTONIA R", "BCDFGHJKLM", "PQUVWXYZ./"])</code> should return a string.');
|
testString: assert(typeof straddle("One night-it was on the twentieth of March, 1888-I was returning.", ["ESTONIA R", "BCDFGHJKLM", "PQUVWXYZ./"]) == 'string');
|
||||||
- text: <code>straddle("One night-it was on the twentieth of March, 1888-I was returning.",["ESTONIA R", "BCDFGHJKLM", "PQUVWXYZ./"])</code> should return <code>"34045747525284613427502840425027537379697175891898898898584619028294547488"</code>.
|
- text: <code>straddle("One night-it was on the twentieth of March, 1888-I was returning.",["ESTONIA R", "BCDFGHJKLM", "PQUVWXYZ./"])</code> should return <code>"34045747525284613427502840425027537379697175891898898898584619028294547488"</code>.
|
||||||
testString: assert.equal(straddle("One night-it was on the twentieth of March, 1888-I was returning.", ["ESTONIA R", "BCDFGHJKLM", "PQUVWXYZ./"]), "34045747525284613427502840425027537379697175891898898898584619028294547488", '<code>straddle("One night-it was on the twentieth of March, 1888-I was returning.",["ESTONIA R", "BCDFGHJKLM", "PQUVWXYZ./"])</code> should return <code>"34045747525284613427502840425027537379697175891898898898584619028294547488"</code>.');
|
testString: assert.equal(straddle("One night-it was on the twentieth of March, 1888-I was returning.", ["ESTONIA R", "BCDFGHJKLM", "PQUVWXYZ./"]), "34045747525284613427502840425027537379697175891898898898584619028294547488");
|
||||||
- text: <code>straddle("One night-it was on the twentieth of March, 1888-I was returning",["HOL MES RT", "ABCDFGIJKN", "PQUVWXYZ./"])</code> should return <code>"139539363509369743061399059745399365901344308320791798798798367430685972839363935"</code>.
|
- text: <code>straddle("One night-it was on the twentieth of March, 1888-I was returning",["HOL MES RT", "ABCDFGIJKN", "PQUVWXYZ./"])</code> should return <code>"139539363509369743061399059745399365901344308320791798798798367430685972839363935"</code>.
|
||||||
testString: assert.equal(straddle("One night-it was on the twentieth of March, 1888-I was returning", ["HOL MES RT", "ABCDFGIJKN", "PQUVWXYZ./"]), "139539363509369743061399059745399365901344308320791798798798367430685972839363935", '<code>straddle("One night-it was on the twentieth of March, 1888-I was returning",["HOL MES RT", "ABCDFGIJKN", "PQUVWXYZ./"])</code> should return <code>"139539363509369743061399059745399365901344308320791798798798367430685972839363935"</code>.');
|
testString: assert.equal(straddle("One night-it was on the twentieth of March, 1888-I was returning", ["HOL MES RT", "ABCDFGIJKN", "PQUVWXYZ./"]), "139539363509369743061399059745399365901344308320791798798798367430685972839363935");
|
||||||
- text: <code>straddle("Thecheckerboardcakerecipespecifies3largeeggsand2.25cupsofflour.",["ET AON RIS", "BCDFGHJKLM", "PQ/UVWXYZ."])</code> should return <code>"125021250212707204372221327070218600960021823809623283724002424935226226962262521636094232328463769"</code>.
|
- text: <code>straddle("Thecheckerboardcakerecipespecifies3largeeggsand2.25cupsofflour.",["ET AON RIS", "BCDFGHJKLM", "PQ/UVWXYZ."])</code> should return <code>"125021250212707204372221327070218600960021823809623283724002424935226226962262521636094232328463769"</code>.
|
||||||
testString: assert.equal(straddle("Thecheckerboardcakerecipespecifies3largeeggsand2.25cupsofflour.", ["ET AON RIS", "BCDFGHJKLM", "PQ/UVWXYZ."]), "125021250212707204372221327070218600960021823809623283724002424935226226962262521636094232328463769", '<code>straddle("Thecheckerboardcakerecipespecifies3largeeggsand2.25cupsofflour.",["ET AON RIS", "BCDFGHJKLM", "PQ/UVWXYZ."])</code> should return <code>"125021250212707204372221327070218600960021823809623283724002424935226226962262521636094232328463769"</code>.');
|
testString: assert.equal(straddle("Thecheckerboardcakerecipespecifies3largeeggsand2.25cupsofflour.", ["ET AON RIS", "BCDFGHJKLM", "PQ/UVWXYZ."]), "125021250212707204372221327070218600960021823809623283724002424935226226962262521636094232328463769");
|
||||||
- text: <code>unstraddle</code> should be a function.
|
- text: <code>unstraddle</code> should be a function.
|
||||||
testString: assert(typeof unstraddle == 'function', '<code>unstraddle</code> should be a function.');
|
testString: assert(typeof unstraddle == 'function');
|
||||||
- text: <code>unstraddle("34045747525284613427502840425027537379697175891898898898584619028294547488",["ESTONIA R", "BCDFGHJKLM", "PQUVWXYZ./"])</code> should return a string.
|
- text: <code>unstraddle("34045747525284613427502840425027537379697175891898898898584619028294547488",["ESTONIA R", "BCDFGHJKLM", "PQUVWXYZ./"])</code> should return a string.
|
||||||
testString: assert(typeof unstraddle("34045747525284613427502840425027537379697175891898898898584619028294547488", ["ESTONIA R", "BCDFGHJKLM", "PQUVWXYZ./"]) == 'string', '<code>unstraddle("34045747525284613427502840425027537379697175891898898898584619028294547488",["ESTONIA R", "BCDFGHJKLM", "PQUVWXYZ./"])</code> should return a string.');
|
testString: assert(typeof unstraddle("34045747525284613427502840425027537379697175891898898898584619028294547488", ["ESTONIA R", "BCDFGHJKLM", "PQUVWXYZ./"]) == 'string');
|
||||||
- text: <code>unstraddle("34045747525284613427502840425027537379697175891898898898584619028294547488",["ESTONIA R", "BCDFGHJKLM", "PQUVWXYZ./"])</code> should return <code>"ONENIGHTITWASONTHETWENTIETHOFMARCH1888IWASRETURNING."</code>.
|
- text: <code>unstraddle("34045747525284613427502840425027537379697175891898898898584619028294547488",["ESTONIA R", "BCDFGHJKLM", "PQUVWXYZ./"])</code> should return <code>"ONENIGHTITWASONTHETWENTIETHOFMARCH1888IWASRETURNING."</code>.
|
||||||
testString: assert.equal(unstraddle("34045747525284613427502840425027537379697175891898898898584619028294547488", ["ESTONIA R", "BCDFGHJKLM", "PQUVWXYZ./"]), "ONENIGHTITWASONTHETWENTIETHOFMARCH1888IWASRETURNING.", '<code>unstraddle("34045747525284613427502840425027537379697175891898898898584619028294547488",["ESTONIA R", "BCDFGHJKLM", "PQUVWXYZ./"])</code> should return <code>"ONENIGHTITWASONTHETWENTIETHOFMARCH1888IWASRETURNING."</code>.');
|
testString: assert.equal(unstraddle("34045747525284613427502840425027537379697175891898898898584619028294547488", ["ESTONIA R", "BCDFGHJKLM", "PQUVWXYZ./"]), "ONENIGHTITWASONTHETWENTIETHOFMARCH1888IWASRETURNING.");
|
||||||
- text: <code>unstraddle("139539363509369743061399059745399365901344308320791798798798367430685972839363935",["HOL MES RT", "ABCDFGIJKN", "PQUVWXYZ./"])</code> should return <code>"ONENIGHTITWASONTHETWENTIETHOFMARCH1888IWASRETURNING"</code>.
|
- text: <code>unstraddle("139539363509369743061399059745399365901344308320791798798798367430685972839363935",["HOL MES RT", "ABCDFGIJKN", "PQUVWXYZ./"])</code> should return <code>"ONENIGHTITWASONTHETWENTIETHOFMARCH1888IWASRETURNING"</code>.
|
||||||
testString: assert.equal(unstraddle("139539363509369743061399059745399365901344308320791798798798367430685972839363935", ["HOL MES RT", "ABCDFGIJKN", "PQUVWXYZ./"]), "ONENIGHTITWASONTHETWENTIETHOFMARCH1888IWASRETURNING", '<code>unstraddle("139539363509369743061399059745399365901344308320791798798798367430685972839363935",["HOL MES RT", "ABCDFGIJKN", "PQUVWXYZ./"])</code> should return <code>"ONENIGHTITWASONTHETWENTIETHOFMARCH1888IWASRETURNING"</code>.');
|
testString: assert.equal(unstraddle("139539363509369743061399059745399365901344308320791798798798367430685972839363935", ["HOL MES RT", "ABCDFGIJKN", "PQUVWXYZ./"]), "ONENIGHTITWASONTHETWENTIETHOFMARCH1888IWASRETURNING");
|
||||||
- text: <code>unstraddle("125021250212707204372221327070218600960021823809623283724002424935226226962262521636094232328463769",["ET AON RIS", "BCDFGHJKLM", "PQ/UVWXYZ."])</code> should return <code>"THECHECKERBOARDCAKERECIPESPECIFIES3LARGEEGGSAND2.25CUPSOFFLOUR."</code>.
|
- text: <code>unstraddle("125021250212707204372221327070218600960021823809623283724002424935226226962262521636094232328463769",["ET AON RIS", "BCDFGHJKLM", "PQ/UVWXYZ."])</code> should return <code>"THECHECKERBOARDCAKERECIPESPECIFIES3LARGEEGGSAND2.25CUPSOFFLOUR."</code>.
|
||||||
testString: assert.equal(unstraddle("125021250212707204372221327070218600960021823809623283724002424935226226962262521636094232328463769", ["ET AON RIS", "BCDFGHJKLM", "PQ/UVWXYZ."]), "THECHECKERBOARDCAKERECIPESPECIFIES3LARGEEGGSAND2.25CUPSOFFLOUR.", '<code>unstraddle("125021250212707204372221327070218600960021823809623283724002424935226226962262521636094232328463769",["ET AON RIS", "BCDFGHJKLM", "PQ/UVWXYZ."])</code> should return <code>"THECHECKERBOARDCAKERECIPESPECIFIES3LARGEEGGSAND2.25CUPSOFFLOUR."</code>.');
|
testString: assert.equal(unstraddle("125021250212707204372221327070218600960021823809623283724002424935226226962262521636094232328463769", ["ET AON RIS", "BCDFGHJKLM", "PQ/UVWXYZ."]), "THECHECKERBOARDCAKERECIPESPECIFIES3LARGEEGGSAND2.25CUPSOFFLOUR.");
|
||||||
```
|
```
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Challenge Seed
|
## Challenge Seed
|
||||||
|
|
||||||
<section id='challengeSeed'>
|
<section id='challengeSeed'>
|
||||||
|
|
||||||
<div id='js-seed'>
|
<div id='js-seed'>
|
||||||
@ -65,46 +70,55 @@ function unstraddle(message, alphabet) {
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Solution
|
## Solution
|
||||||
|
|
||||||
<section id='solution'>
|
<section id='solution'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function straddle(message, alphabet) {
|
function straddle(message, alphabet) {
|
||||||
var prefixes = new Array("", alphabet[0].indexOf(" "), alphabet[0].lastIndexOf(" "))
|
var prefixes = new Array(
|
||||||
|
'',
|
||||||
|
alphabet[0].indexOf(' '),
|
||||||
|
alphabet[0].lastIndexOf(' ')
|
||||||
|
);
|
||||||
|
|
||||||
var out = ""
|
var out = '';
|
||||||
message = message.toUpperCase()
|
message = message.toUpperCase();
|
||||||
message = message.replace(/([0-9])/g, "/$1") // dumb way to escape numbers
|
message = message.replace(/([0-9])/g, '/$1'); // dumb way to escape numbers
|
||||||
for (var i = 0; i < message.length; i++) {
|
for (var i = 0; i < message.length; i++) {
|
||||||
var chr = message[i]
|
var chr = message[i];
|
||||||
if (chr == " ") continue
|
if (chr == ' ') continue;
|
||||||
for (var j = 0; j < 3; j++) {
|
for (var j = 0; j < 3; j++) {
|
||||||
var k = alphabet[j].indexOf(chr)
|
var k = alphabet[j].indexOf(chr);
|
||||||
if (k < 0) continue
|
if (k < 0) continue;
|
||||||
out += prefixes[j].toString() + k
|
out += prefixes[j].toString() + k;
|
||||||
}
|
}
|
||||||
if (chr == "/") out += message[++i]
|
if (chr == '/') out += message[++i];
|
||||||
}
|
}
|
||||||
return out
|
return out;
|
||||||
}
|
}
|
||||||
function unstraddle(message, alphabet) {
|
function unstraddle(message, alphabet) {
|
||||||
var prefixes = new Array("", alphabet[0].indexOf(" "), alphabet[0].lastIndexOf(" "))
|
var prefixes = new Array(
|
||||||
var out = ""
|
'',
|
||||||
var n, o
|
alphabet[0].indexOf(' '),
|
||||||
|
alphabet[0].lastIndexOf(' ')
|
||||||
|
);
|
||||||
|
var out = '';
|
||||||
|
var n, o;
|
||||||
for (var i = 0; i < message.length; i++) {
|
for (var i = 0; i < message.length; i++) {
|
||||||
n = message[i] * 1
|
n = message[i] * 1;
|
||||||
switch (n) {
|
switch (n) {
|
||||||
case prefixes[1]:
|
case prefixes[1]:
|
||||||
o = alphabet[1][message[++i]];
|
o = alphabet[1][message[++i]];
|
||||||
break
|
break;
|
||||||
case prefixes[2]:
|
case prefixes[2]:
|
||||||
o = alphabet[2][message[++i]];
|
o = alphabet[2][message[++i]];
|
||||||
break
|
break;
|
||||||
default:
|
default:
|
||||||
o = alphabet[0][n]
|
o = alphabet[0][n];
|
||||||
}
|
}
|
||||||
o == "/" ? out += message[++i] : out += o
|
o == '/' ? (out += message[++i]) : (out += o);
|
||||||
}
|
}
|
||||||
return out
|
return out;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -6,40 +6,45 @@ forumTopicId: 302326
|
|||||||
---
|
---
|
||||||
|
|
||||||
## Description
|
## Description
|
||||||
|
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
|
|
||||||
Write a function that takes multiple sorted arrays of items, and returns one array of sorted items.
|
Write a function that takes multiple sorted arrays of items, and returns one array of sorted items.
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
|
|
||||||
<section id='instructions'>
|
<section id='instructions'>
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
|
|
||||||
<section id='tests'>
|
<section id='tests'>
|
||||||
|
|
||||||
``` yml
|
```yml
|
||||||
tests:
|
tests:
|
||||||
- text: <code>mergeLists</code> should be a function.
|
- text: <code>mergeLists</code> should be a function.
|
||||||
testString: assert(typeof mergeLists == 'function', '<code>mergeLists</code> should be a function.');
|
testString: assert(typeof mergeLists == 'function');
|
||||||
- text: <code>mergeLists([[1, 3, 5, 9, 10], [2, 4, 6, 7, 8]])</code> should return an array.
|
- text: <code>mergeLists([[1, 3, 5, 9, 10], [2, 4, 6, 7, 8]])</code> should return an array.
|
||||||
testString: assert(Array.isArray(mergeLists([[1, 3, 5, 9, 10], [2, 4, 6, 7, 8]])), '<code>mergeLists([[1, 3, 5, 9, 10], [2, 4, 6, 7, 8]])</code> should return an array.');
|
testString: assert(Array.isArray(mergeLists([[1, 3, 5, 9, 10], [2, 4, 6, 7, 8]])));
|
||||||
- text: <code>mergeLists([[1, 3, 5, 9, 10], [2, 4, 6, 7, 8]])</code> should return <code>[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]</code>.
|
- text: <code>mergeLists([[1, 3, 5, 9, 10], [2, 4, 6, 7, 8]])</code> should return <code>[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]</code>.
|
||||||
testString: assert.deepEqual(mergeLists([[1, 3, 5, 9, 10], [2, 4, 6, 7, 8]]), [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], '<code>mergeLists([[1, 3, 5, 9, 10], [2, 4, 6, 7, 8]])</code> should return <code>[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]</code>.');
|
testString: assert.deepEqual(mergeLists([[1, 3, 5, 9, 10], [2, 4, 6, 7, 8]]), [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
|
||||||
- text: <code>mergeLists([[1, 4, 7, 10], [2, 5, 8, 11], [3, 6, 9, 12]])</code> should return <code>[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]</code>.
|
- text: <code>mergeLists([[1, 4, 7, 10], [2, 5, 8, 11], [3, 6, 9, 12]])</code> should return <code>[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]</code>.
|
||||||
testString: assert.deepEqual(mergeLists([[1, 4, 7, 10], [2, 5, 8, 11], [3, 6, 9, 12]]), [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], '<code>mergeLists([[1, 4, 7, 10], [2, 5, 8, 11], [3, 6, 9, 12]])</code> should return <code>[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]</code>.');
|
testString: assert.deepEqual(mergeLists([[1, 4, 7, 10], [2, 5, 8, 11], [3, 6, 9, 12]]), [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]);
|
||||||
- text: <code>mergeLists([[1, 3, 9, 14, 15, 17, 28], [7, 8, 14, 14, 23, 26, 28, 29, 30], [9, 23, 25, 29]])</code> should return <code>[1, 3, 7, 8, 9, 9, 14, 14, 14, 15, 17, 23, 23, 25, 26, 28, 28, 29, 29, 30]</code>.
|
- text: <code>mergeLists([[1, 3, 9, 14, 15, 17, 28], [7, 8, 14, 14, 23, 26, 28, 29, 30], [9, 23, 25, 29]])</code> should return <code>[1, 3, 7, 8, 9, 9, 14, 14, 14, 15, 17, 23, 23, 25, 26, 28, 28, 29, 29, 30]</code>.
|
||||||
testString: assert.deepEqual(mergeLists([[1, 3, 9, 14, 15, 17, 28], [7, 8, 14, 14, 23, 26, 28, 29, 30], [9, 23, 25, 29]]), [1, 3, 7, 8, 9, 9, 14, 14, 14, 15, 17, 23, 23, 25, 26, 28, 28, 29, 29, 30], '<code>mergeLists([[1, 3, 9, 14, 15, 17, 28], [7, 8, 14, 14, 23, 26, 28, 29, 30], [9, 23, 25, 29]])</code> should return <code>[1, 3, 7, 8, 9, 9, 14, 14, 14, 15, 17, 23, 23, 25, 26, 28, 28, 29, 29, 30]</code>.');
|
testString: assert.deepEqual(mergeLists([[1, 3, 9, 14, 15, 17, 28], [7, 8, 14, 14, 23, 26, 28, 29, 30], [9, 23, 25, 29]]), [1, 3, 7, 8, 9, 9, 14, 14, 14, 15, 17, 23, 23, 25, 26, 28, 28, 29, 29, 30]);
|
||||||
- text: <code>mergeLists([[3, 14, 15], [2, 17, 18], [], [2, 3, 5, 7]])</code> should return <code>[2, 2, 3, 3, 5, 7, 14, 15, 17, 18]</code>.
|
- text: <code>mergeLists([[3, 14, 15], [2, 17, 18], [], [2, 3, 5, 7]])</code> should return <code>[2, 2, 3, 3, 5, 7, 14, 15, 17, 18]</code>.
|
||||||
testString: assert.deepEqual(mergeLists([[3, 14, 15], [2, 17, 18], [], [2, 3, 5, 7]]), [2, 2, 3, 3, 5, 7, 14, 15, 17, 18], '<code>mergeLists([[3, 14, 15], [2, 17, 18], [], [2, 3, 5, 7]])</code> should return <code>[2, 2, 3, 3, 5, 7, 14, 15, 17, 18]</code>.');
|
testString: assert.deepEqual(mergeLists([[3, 14, 15], [2, 17, 18], [], [2, 3, 5, 7]]), [2, 2, 3, 3, 5, 7, 14, 15, 17, 18]);
|
||||||
- text: <code>mergeLists([[1, 19, 1999], [17, 33, 2999, 3000], [8, 500, 3999]])</code> should return <code>[1, 8, 17, 19, 33, 500, 1999, 2999, 3000, 3999]</code>.
|
- text: <code>mergeLists([[1, 19, 1999], [17, 33, 2999, 3000], [8, 500, 3999]])</code> should return <code>[1, 8, 17, 19, 33, 500, 1999, 2999, 3000, 3999]</code>.
|
||||||
testString: assert.deepEqual(mergeLists([[1, 19, 1999], [17, 33, 2999, 3000], [8, 500, 3999]]), [1, 8, 17, 19, 33, 500, 1999, 2999, 3000, 3999], '<code>mergeLists([[1, 19, 1999], [17, 33, 2999, 3000], [8, 500, 3999]])</code> should return <code>[1, 8, 17, 19, 33, 500, 1999, 2999, 3000, 3999]</code>.');
|
testString: assert.deepEqual(mergeLists([[1, 19, 1999], [17, 33, 2999, 3000], [8, 500, 3999]]), [1, 8, 17, 19, 33, 500, 1999, 2999, 3000, 3999]);
|
||||||
```
|
```
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Challenge Seed
|
## Challenge Seed
|
||||||
|
|
||||||
<section id='challengeSeed'>
|
<section id='challengeSeed'>
|
||||||
|
|
||||||
<div id='js-seed'>
|
<div id='js-seed'>
|
||||||
@ -54,16 +59,19 @@ function mergeLists(lists) {
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Solution
|
## Solution
|
||||||
|
|
||||||
<section id='solution'>
|
<section id='solution'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function mergeLists(lists) {
|
function mergeLists(lists) {
|
||||||
function merge (l1, l2) {
|
function merge(l1, l2) {
|
||||||
var result = [], i=0, j=0;
|
var result = [],
|
||||||
|
i = 0,
|
||||||
|
j = 0;
|
||||||
while (l1.length && l2.length) {
|
while (l1.length && l2.length) {
|
||||||
if(l1[i]<=l2[j]){
|
if (l1[i] <= l2[j]) {
|
||||||
result.push(l1.shift());
|
result.push(l1.shift());
|
||||||
}else{
|
} else {
|
||||||
result.push(l2.shift());
|
result.push(l2.shift());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -73,9 +81,9 @@ function mergeLists(lists) {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
var result=lists[0];
|
var result = lists[0];
|
||||||
for (var i = 1; i < lists.length; i++) {
|
for (var i = 1; i < lists.length; i++) {
|
||||||
result=merge(result, lists[i]);
|
result = merge(result, lists[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
@ -6,23 +6,27 @@ forumTopicId: 302327
|
|||||||
---
|
---
|
||||||
|
|
||||||
## Description
|
## Description
|
||||||
|
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
|
|
||||||
The task is to strip control codes and extended characters from a string. The solution should demonstrate how to achieve each of the following results:
|
The task is to strip control codes and extended characters from a string. The solution should demonstrate how to achieve each of the following results:
|
||||||
A string with control codes and extended characters stripped.
|
A string with control codes and extended characters stripped.
|
||||||
In ASCII, the control codes have decimal codes 0 through to 31 and 127. On an ASCII based system, if the control codes are stripped, the resultant string would have all of its characters within the range of 32 to 126 decimal on the ASCII table.
|
In ASCII, the control codes have decimal codes 0 through to 31 and 127. On an ASCII based system, if the control codes are stripped, the resultant string would have all of its characters within the range of 32 to 126 decimal on the ASCII table.
|
||||||
On a non-ASCII based system, we consider characters that do not have a corresponding glyph on the ASCII table (within the ASCII range of 32 to 126 decimal) to be an extended character for the purpose of this task.
|
On a non-ASCII based system, we consider characters that do not have a corresponding glyph on the ASCII table (within the ASCII range of 32 to 126 decimal) to be an extended character for the purpose of this task.
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
|
|
||||||
<section id='instructions'>
|
<section id='instructions'>
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
|
|
||||||
<section id='tests'>
|
<section id='tests'>
|
||||||
|
|
||||||
``` yml
|
```yml
|
||||||
tests:
|
tests:
|
||||||
- text: <code>strip</code> should be a function.
|
- text: <code>strip</code> should be a function.
|
||||||
testString: assert(typeof strip == 'function');
|
testString: assert(typeof strip == 'function');
|
||||||
@ -43,6 +47,7 @@ tests:
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Challenge Seed
|
## Challenge Seed
|
||||||
|
|
||||||
<section id='challengeSeed'>
|
<section id='challengeSeed'>
|
||||||
|
|
||||||
<div id='js-seed'>
|
<div id='js-seed'>
|
||||||
@ -57,15 +62,19 @@ function strip(s) {
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Solution
|
## Solution
|
||||||
|
|
||||||
<section id='solution'>
|
<section id='solution'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function strip(s) {
|
function strip(s) {
|
||||||
return s.split('').filter(function(x) {
|
return s
|
||||||
var n = x.charCodeAt(0);
|
.split('')
|
||||||
|
.filter(function(x) {
|
||||||
|
var n = x.charCodeAt(0);
|
||||||
|
|
||||||
return 31 < n && 127 > n;
|
return 31 < n && 127 > n;
|
||||||
}).join('');
|
})
|
||||||
|
.join('');
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -6,6 +6,7 @@ forumTopicId: 302328
|
|||||||
---
|
---
|
||||||
|
|
||||||
## Description
|
## Description
|
||||||
|
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
<a href="https://rosettacode.org/wiki/eso:Subleq" target="_blank">Subleq</a> is an example of a <a href="https://en.wikipedia.org/wiki/One_instruction_set_computer" target="_blank">One-Instruction Set Computer (OISC)</a>.
|
<a href="https://rosettacode.org/wiki/eso:Subleq" target="_blank">Subleq</a> is an example of a <a href="https://en.wikipedia.org/wiki/One_instruction_set_computer" target="_blank">One-Instruction Set Computer (OISC)</a>.
|
||||||
It is named after its only instruction, which is <b>SU</b>btract and <b>B</b>ranch if <b>L</b>ess than or <b>EQ</b>ual
|
It is named after its only instruction, which is <b>SU</b>btract and <b>B</b>ranch if <b>L</b>ess than or <b>EQ</b>ual
|
||||||
@ -49,27 +50,30 @@ message: "Hello, world!\n\0"
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
|
|
||||||
<section id='instructions'>
|
<section id='instructions'>
|
||||||
Write a function that takes an array of integers as a parameter. This represents the memory elements. The function
|
Write a function that takes an array of integers as a parameter. This represents the memory elements. The function
|
||||||
should interpret the sequence and return the output string. For this task, assume that there is no standard input.
|
should interpret the sequence and return the output string. For this task, assume that there is no standard input.
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
|
|
||||||
<section id='tests'>
|
<section id='tests'>
|
||||||
|
|
||||||
``` yml
|
```yml
|
||||||
tests:
|
tests:
|
||||||
- text: <code>Subleq</code> should be a function.
|
- text: <code>Subleq</code> should be a function.
|
||||||
testString: assert(typeof Subleq == 'function', '<code>Subleq</code> should be a function.');
|
testString: assert(typeof Subleq == 'function');
|
||||||
- text: <code>Subleq([15, 17, -1, 17, -1, -1, 16, 1, -1, 16, 3, -1, 15, 15, 0, 0, -1, 72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33, 0])</code> should return a string.
|
- text: <code>Subleq([15, 17, -1, 17, -1, -1, 16, 1, -1, 16, 3, -1, 15, 15, 0, 0, -1, 72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33, 0])</code> should return a string.
|
||||||
testString: assert(typeof Subleq([15, 17, -1, 17, -1, -1, 16, 1, -1, 16, 3, -1, 15, 15, 0, 0, -1, 72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33, 0]) == 'string', '<code>Subleq([15, 17, -1, 17, -1, -1, 16, 1, -1, 16, 3, -1, 15, 15, 0, 0, -1, 72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33, 0])</code> should return a string.');
|
testString: assert(typeof Subleq([15, 17, -1, 17, -1, -1, 16, 1, -1, 16, 3, -1, 15, 15, 0, 0, -1, 72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33, 0]) == 'string');
|
||||||
- text: <code>Subleq([15, 17, -1, 17, -1, -1, 16, 1, -1, 16, 3, -1, 15, 15, 0, 0, -1, 72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33, 0])</code> should return <code>"Hello, world!"</code>.
|
- text: <code>Subleq([15, 17, -1, 17, -1, -1, 16, 1, -1, 16, 3, -1, 15, 15, 0, 0, -1, 72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33, 0])</code> should return <code>"Hello, world!"</code>.
|
||||||
testString: assert.equal(Subleq([15, 17, -1, 17, -1, -1, 16, 1, -1, 16, 3, -1, 15, 15, 0, 0, -1, 72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33, 0]), "Hello, world!", '<code>Subleq([15, 17, -1, 17, -1, -1, 16, 1, -1, 16, 3, -1, 15, 15, 0, 0, -1, 72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33, 0])</code> should return <code>"Hello, world!"</code>.');
|
testString: assert.equal(Subleq([15, 17, -1, 17, -1, -1, 16, 1, -1, 16, 3, -1, 15, 15, 0, 0, -1, 72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33, 0]), "Hello, world!");
|
||||||
```
|
```
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Challenge Seed
|
## Challenge Seed
|
||||||
|
|
||||||
<section id='challengeSeed'>
|
<section id='challengeSeed'>
|
||||||
<div id='js-seed'>
|
<div id='js-seed'>
|
||||||
|
|
||||||
@ -83,16 +87,18 @@ function Subleq(mem) {
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Solution
|
## Solution
|
||||||
|
|
||||||
<section id='solution'>
|
<section id='solution'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function Subleq(mem) {
|
function Subleq(mem) {
|
||||||
var out = "";
|
var out = '';
|
||||||
var instructionPointer = 0;
|
var instructionPointer = 0;
|
||||||
do {
|
do {
|
||||||
var a = mem[instructionPointer];
|
var a = mem[instructionPointer];
|
||||||
var b = mem[instructionPointer + 1];
|
var b = mem[instructionPointer + 1];
|
||||||
if (a === -1) {} else if (b === -1) {
|
if (a === -1) {
|
||||||
|
} else if (b === -1) {
|
||||||
out += String.fromCharCode(mem[a]);
|
out += String.fromCharCode(mem[a]);
|
||||||
} else {
|
} else {
|
||||||
mem[b] -= mem[a];
|
mem[b] -= mem[a];
|
||||||
@ -102,7 +108,7 @@ function Subleq(mem) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
instructionPointer += 3;
|
instructionPointer += 3;
|
||||||
} while ((instructionPointer >= 0));
|
} while (instructionPointer >= 0);
|
||||||
|
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
@ -6,37 +6,42 @@ forumTopicId: 302329
|
|||||||
---
|
---
|
||||||
|
|
||||||
## Description
|
## Description
|
||||||
|
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
|
|
||||||
Write a function to solve a partially filled-in normal 9x9 <a href="https://en.wikipedia.org/wiki/Sudoku" target="_blank">Sudoku</a> grid and return the result. The blank fields are represented by 0s.
|
Write a function to solve a partially filled-in normal 9x9 <a href="https://en.wikipedia.org/wiki/Sudoku" target="_blank">Sudoku</a> grid and return the result. The blank fields are represented by 0s.
|
||||||
<a href="https://en.wikipedia.org/wiki/Algorithmics_of_sudoku" target="_blank">Algorithmics of Sudoku</a> may help implement this.
|
<a href="https://en.wikipedia.org/wiki/Algorithmics_of_sudoku" target="_blank">Algorithmics of Sudoku</a> may help implement this.
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
|
|
||||||
<section id='instructions'>
|
<section id='instructions'>
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
|
|
||||||
<section id='tests'>
|
<section id='tests'>
|
||||||
|
|
||||||
``` yml
|
```yml
|
||||||
tests:
|
tests:
|
||||||
- text: <code>solveSudoku</code> should be a function.
|
- text: <code>solveSudoku</code> should be a function.
|
||||||
testString: assert(typeof solveSudoku == 'function', '<code>solveSudoku</code> should be a function.');
|
testString: assert(typeof solveSudoku == 'function');
|
||||||
- text: <code>solveSudoku([[8, 1, 9, -1, -1, 5, -1, -1, -1],[-1, -1, 2, -1, -1, -1, 7, 5, -1],[-1, 3, 7, 1, -1, 4, -1, 6, -1],[4, -1, -1, 5, 9, -1, 1, -1, -1],[7, -1, -1, 3, -1, 8, -1, -1, 2],[-1, -1, 3, -1, 6, 2, -1, -1, 7],[-1, 5, -1, 7, -1, 9, 2, 1, -1],[-1, 6, 4, -1, -1, -1, 9, -1, -1],[-1, -1, -1, 2, -1, -1, 4, 3, 8]])</code> should return an array.
|
- text: <code>solveSudoku([[8, 1, 9, -1, -1, 5, -1, -1, -1],[-1, -1, 2, -1, -1, -1, 7, 5, -1],[-1, 3, 7, 1, -1, 4, -1, 6, -1],[4, -1, -1, 5, 9, -1, 1, -1, -1],[7, -1, -1, 3, -1, 8, -1, -1, 2],[-1, -1, 3, -1, 6, 2, -1, -1, 7],[-1, 5, -1, 7, -1, 9, 2, 1, -1],[-1, 6, 4, -1, -1, -1, 9, -1, -1],[-1, -1, -1, 2, -1, -1, 4, 3, 8]])</code> should return an array.
|
||||||
testString: assert(Array.isArray(solveSudoku([[8, 1, 9, -1, -1, 5, -1, -1, -1], [-1, -1, 2, -1, -1, -1, 7, 5, -1], [-1, 3, 7, 1, -1, 4, -1, 6, -1], [4, -1, -1, 5, 9, -1, 1, -1, -1], [7, -1, -1, 3, -1, 8, -1, -1, 2], [-1, -1, 3, -1, 6, 2, -1, -1, 7], [-1, 5, -1, 7, -1, 9, 2, 1, -1], [-1, 6, 4, -1, -1, -1, 9, -1, -1], [-1, -1, -1, 2, -1, -1, 4, 3, 8]])), '<code>solveSudoku([[8, 1, 9, -1, -1, 5, -1, -1, -1],[-1, -1, 2, -1, -1, -1, 7, 5, -1],[-1, 3, 7, 1, -1, 4, -1, 6, -1],[4, -1, -1, 5, 9, -1, 1, -1, -1],[7, -1, -1, 3, -1, 8, -1, -1, 2],[-1, -1, 3, -1, 6, 2, -1, -1, 7],[-1, 5, -1, 7, -1, 9, 2, 1, -1],[-1, 6, 4, -1, -1, -1, 9, -1, -1],[-1, -1, -1, 2, -1, -1, 4, 3, 8]])</code> should return an array.');
|
testString: assert(Array.isArray(solveSudoku([[8, 1, 9, -1, -1, 5, -1, -1, -1], [-1, -1, 2, -1, -1, -1, 7, 5, -1], [-1, 3, 7, 1, -1, 4, -1, 6, -1], [4, -1, -1, 5, 9, -1, 1, -1, -1], [7, -1, -1, 3, -1, 8, -1, -1, 2], [-1, -1, 3, -1, 6, 2, -1, -1, 7], [-1, 5, -1, 7, -1, 9, 2, 1, -1], [-1, 6, 4, -1, -1, -1, 9, -1, -1], [-1, -1, -1, 2, -1, -1, 4, 3, 8]])));
|
||||||
- text: <code>solveSudoku([[8, 1, 9, -1, -1, 5, -1, -1, -1],[-1, -1, 2, -1, -1, -1, 7, 5, -1],[-1, 3, 7, 1, -1, 4, -1, 6, -1],[4, -1, -1, 5, 9, -1, 1, -1, -1],[7, -1, -1, 3, -1, 8, -1, -1, 2],[-1, -1, 3, -1, 6, 2, -1, -1, 7],[-1, 5, -1, 7, -1, 9, 2, 1, -1],[-1, 6, 4, -1, -1, -1, 9, -1, -1],[-1, -1, -1, 2, -1, -1, 4, 3, 8]])</code> should return <code>[[8, 1, 9, 6, 7, 5, 3, 2, 4],[6, 4, 2, 9, 8, 3, 7, 5, 1],[5, 3, 7, 1, 2, 4, 8, 6, 9],[4, 2, 6, 5, 9, 7, 1, 8, 3],[7, 9, 5, 3, 1, 8, 6, 4, 2],[1, 8, 3, 4, 6, 2, 5, 9, 7],[3, 5, 8, 7, 4, 9, 2, 1, 6],[2, 6, 4, 8, 3, 1, 9, 7, 5],[9, 7, 1, 2, 5, 6, 4, 3, 8]]</code>.
|
- text: <code>solveSudoku([[8, 1, 9, -1, -1, 5, -1, -1, -1],[-1, -1, 2, -1, -1, -1, 7, 5, -1],[-1, 3, 7, 1, -1, 4, -1, 6, -1],[4, -1, -1, 5, 9, -1, 1, -1, -1],[7, -1, -1, 3, -1, 8, -1, -1, 2],[-1, -1, 3, -1, 6, 2, -1, -1, 7],[-1, 5, -1, 7, -1, 9, 2, 1, -1],[-1, 6, 4, -1, -1, -1, 9, -1, -1],[-1, -1, -1, 2, -1, -1, 4, 3, 8]])</code> should return <code>[[8, 1, 9, 6, 7, 5, 3, 2, 4],[6, 4, 2, 9, 8, 3, 7, 5, 1],[5, 3, 7, 1, 2, 4, 8, 6, 9],[4, 2, 6, 5, 9, 7, 1, 8, 3],[7, 9, 5, 3, 1, 8, 6, 4, 2],[1, 8, 3, 4, 6, 2, 5, 9, 7],[3, 5, 8, 7, 4, 9, 2, 1, 6],[2, 6, 4, 8, 3, 1, 9, 7, 5],[9, 7, 1, 2, 5, 6, 4, 3, 8]]</code>.
|
||||||
testString: assert.deepEqual(solveSudoku([[8, 1, 9, -1, -1, 5, -1, -1, -1], [-1, -1, 2, -1, -1, -1, 7, 5, -1], [-1, 3, 7, 1, -1, 4, -1, 6, -1], [4, -1, -1, 5, 9, -1, 1, -1, -1], [7, -1, -1, 3, -1, 8, -1, -1, 2], [-1, -1, 3, -1, 6, 2, -1, -1, 7], [-1, 5, -1, 7, -1, 9, 2, 1, -1], [-1, 6, 4, -1, -1, -1, 9, -1, -1], [-1, -1, -1, 2, -1, -1, 4, 3, 8]]), [[8, 1, 9, 6, 7, 5, 3, 2, 4], [6, 4, 2, 9, 8, 3, 7, 5, 1], [5, 3, 7, 1, 2, 4, 8, 6, 9], [4, 2, 6, 5, 9, 7, 1, 8, 3], [7, 9, 5, 3, 1, 8, 6, 4, 2], [1, 8, 3, 4, 6, 2, 5, 9, 7], [3, 5, 8, 7, 4, 9, 2, 1, 6], [2, 6, 4, 8, 3, 1, 9, 7, 5], [9, 7, 1, 2, 5, 6, 4, 3, 8]], '<code>solveSudoku([[8, 1, 9, -1, -1, 5, -1, -1, -1],[-1, -1, 2, -1, -1, -1, 7, 5, -1],[-1, 3, 7, 1, -1, 4, -1, 6, -1],[4, -1, -1, 5, 9, -1, 1, -1, -1],[7, -1, -1, 3, -1, 8, -1, -1, 2],[-1, -1, 3, -1, 6, 2, -1, -1, 7],[-1, 5, -1, 7, -1, 9, 2, 1, -1],[-1, 6, 4, -1, -1, -1, 9, -1, -1],[-1, -1, -1, 2, -1, -1, 4, 3, 8]])</code> should return <code>[[8, 1, 9, 6, 7, 5, 3, 2, 4],[6, 4, 2, 9, 8, 3, 7, 5, 1],[5, 3, 7, 1, 2, 4, 8, 6, 9],[4, 2, 6, 5, 9, 7, 1, 8, 3],[7, 9, 5, 3, 1, 8, 6, 4, 2],[1, 8, 3, 4, 6, 2, 5, 9, 7],[3, 5, 8, 7, 4, 9, 2, 1, 6],[2, 6, 4, 8, 3, 1, 9, 7, 5],[9, 7, 1, 2, 5, 6, 4, 3, 8]]</code>.');
|
testString: assert.deepEqual(solveSudoku([[8, 1, 9, -1, -1, 5, -1, -1, -1], [-1, -1, 2, -1, -1, -1, 7, 5, -1], [-1, 3, 7, 1, -1, 4, -1, 6, -1], [4, -1, -1, 5, 9, -1, 1, -1, -1], [7, -1, -1, 3, -1, 8, -1, -1, 2], [-1, -1, 3, -1, 6, 2, -1, -1, 7], [-1, 5, -1, 7, -1, 9, 2, 1, -1], [-1, 6, 4, -1, -1, -1, 9, -1, -1], [-1, -1, -1, 2, -1, -1, 4, 3, 8]]), [[8, 1, 9, 6, 7, 5, 3, 2, 4], [6, 4, 2, 9, 8, 3, 7, 5, 1], [5, 3, 7, 1, 2, 4, 8, 6, 9], [4, 2, 6, 5, 9, 7, 1, 8, 3], [7, 9, 5, 3, 1, 8, 6, 4, 2], [1, 8, 3, 4, 6, 2, 5, 9, 7], [3, 5, 8, 7, 4, 9, 2, 1, 6], [2, 6, 4, 8, 3, 1, 9, 7, 5], [9, 7, 1, 2, 5, 6, 4, 3, 8]]);
|
||||||
- text: <code>solveSudoku([[5, 3, -1, -1, 2, 4, 7, -1, -1],[-1, -1, 2, -1, -1, -1, 8, -1, -1],[1, -1, -1, 7, -1, 3, 9, -1, 2],[-1, -1, 8, -1, 7, 2, -1, 4, 9],[-1, 2, -1, 9, 8, -1, -1, 7, -1],[7, 9, -1, -1, -1, -1, -1, 8, -1],[-1, -1, -1, -1, 3, -1, 5, -1, 6],[9, 6, -1, -1, 1, -1, 3, -1, -1],[-1, 5, -1, 6, 9, -1, -1, 1, -1]])</code> should return <code>[[5, 3, 9, 8, 2, 4, 7, 6, 1],[6, 7, 2, 1, 5, 9, 8, 3, 4],[1, 8, 4, 7, 6, 3, 9, 5, 2],[3, 1, 8, 5, 7, 2, 6, 4, 9],[4, 2, 5, 9, 8, 6, 1, 7, 3],[7, 9, 6, 3, 4, 1, 2, 8, 5],[8, 4, 1, 2, 3, 7, 5, 9, 6],[9, 6, 7, 4, 1, 5, 3, 2, 8],[2, 5, 3, 6, 9, 8, 4, 1, 7]]</code>.
|
- text: <code>solveSudoku([[5, 3, -1, -1, 2, 4, 7, -1, -1],[-1, -1, 2, -1, -1, -1, 8, -1, -1],[1, -1, -1, 7, -1, 3, 9, -1, 2],[-1, -1, 8, -1, 7, 2, -1, 4, 9],[-1, 2, -1, 9, 8, -1, -1, 7, -1],[7, 9, -1, -1, -1, -1, -1, 8, -1],[-1, -1, -1, -1, 3, -1, 5, -1, 6],[9, 6, -1, -1, 1, -1, 3, -1, -1],[-1, 5, -1, 6, 9, -1, -1, 1, -1]])</code> should return <code>[[5, 3, 9, 8, 2, 4, 7, 6, 1],[6, 7, 2, 1, 5, 9, 8, 3, 4],[1, 8, 4, 7, 6, 3, 9, 5, 2],[3, 1, 8, 5, 7, 2, 6, 4, 9],[4, 2, 5, 9, 8, 6, 1, 7, 3],[7, 9, 6, 3, 4, 1, 2, 8, 5],[8, 4, 1, 2, 3, 7, 5, 9, 6],[9, 6, 7, 4, 1, 5, 3, 2, 8],[2, 5, 3, 6, 9, 8, 4, 1, 7]]</code>.
|
||||||
testString: assert.deepEqual(solveSudoku([[5, 3, -1, -1, 2, 4, 7, -1, -1], [-1, -1, 2, -1, -1, -1, 8, -1, -1], [1, -1, -1, 7, -1, 3, 9, -1, 2], [-1, -1, 8, -1, 7, 2, -1, 4, 9], [-1, 2, -1, 9, 8, -1, -1, 7, -1], [7, 9, -1, -1, -1, -1, -1, 8, -1], [-1, -1, -1, -1, 3, -1, 5, -1, 6], [9, 6, -1, -1, 1, -1, 3, -1, -1], [-1, 5, -1, 6, 9, -1, -1, 1, -1]]), [[5, 3, 9, 8, 2, 4, 7, 6, 1], [6, 7, 2, 1, 5, 9, 8, 3, 4], [1, 8, 4, 7, 6, 3, 9, 5, 2], [3, 1, 8, 5, 7, 2, 6, 4, 9], [4, 2, 5, 9, 8, 6, 1, 7, 3], [7, 9, 6, 3, 4, 1, 2, 8, 5], [8, 4, 1, 2, 3, 7, 5, 9, 6], [9, 6, 7, 4, 1, 5, 3, 2, 8], [2, 5, 3, 6, 9, 8, 4, 1, 7]], '<code>solveSudoku([[5, 3, -1, -1, 2, 4, 7, -1, -1],[-1, -1, 2, -1, -1, -1, 8, -1, -1],[1, -1, -1, 7, -1, 3, 9, -1, 2],[-1, -1, 8, -1, 7, 2, -1, 4, 9],[-1, 2, -1, 9, 8, -1, -1, 7, -1],[7, 9, -1, -1, -1, -1, -1, 8, -1],[-1, -1, -1, -1, 3, -1, 5, -1, 6],[9, 6, -1, -1, 1, -1, 3, -1, -1],[-1, 5, -1, 6, 9, -1, -1, 1, -1]])</code> should return <code>[[5, 3, 9, 8, 2, 4, 7, 6, 1],[6, 7, 2, 1, 5, 9, 8, 3, 4],[1, 8, 4, 7, 6, 3, 9, 5, 2],[3, 1, 8, 5, 7, 2, 6, 4, 9],[4, 2, 5, 9, 8, 6, 1, 7, 3],[7, 9, 6, 3, 4, 1, 2, 8, 5],[8, 4, 1, 2, 3, 7, 5, 9, 6],[9, 6, 7, 4, 1, 5, 3, 2, 8],[2, 5, 3, 6, 9, 8, 4, 1, 7]]</code>.');
|
testString: assert.deepEqual(solveSudoku([[5, 3, -1, -1, 2, 4, 7, -1, -1], [-1, -1, 2, -1, -1, -1, 8, -1, -1], [1, -1, -1, 7, -1, 3, 9, -1, 2], [-1, -1, 8, -1, 7, 2, -1, 4, 9], [-1, 2, -1, 9, 8, -1, -1, 7, -1], [7, 9, -1, -1, -1, -1, -1, 8, -1], [-1, -1, -1, -1, 3, -1, 5, -1, 6], [9, 6, -1, -1, 1, -1, 3, -1, -1], [-1, 5, -1, 6, 9, -1, -1, 1, -1]]), [[5, 3, 9, 8, 2, 4, 7, 6, 1], [6, 7, 2, 1, 5, 9, 8, 3, 4], [1, 8, 4, 7, 6, 3, 9, 5, 2], [3, 1, 8, 5, 7, 2, 6, 4, 9], [4, 2, 5, 9, 8, 6, 1, 7, 3], [7, 9, 6, 3, 4, 1, 2, 8, 5], [8, 4, 1, 2, 3, 7, 5, 9, 6], [9, 6, 7, 4, 1, 5, 3, 2, 8], [2, 5, 3, 6, 9, 8, 4, 1, 7]]);
|
||||||
- text: <code>solveSudoku([[-1, -1, 3, -1, 2, -1, 6, -1, -1],[9, -1, -1, 3, -1, 5, -1, -1, 1],[-1, -1, 1, 8, -1, 6, 4, -1, -1],[-1, -1, 8, 1, -1, 2, 9, -1, -1],[7, -1, -1, -1, -1, -1, -1, -1, 8],[-1, -1, 6, 7, -1, 8, 2, -1, -1],[-1, -1, 2, 6, -1, 9, 5, -1, -1],[8, -1, -1, 2, -1, 3, -1, -1, 9],[-1, -1, 5, -1, 1, -1, 3, -1, -1]])</code> should return <code>[[4, 8, 3, 9, 2, 1, 6, 5, 7],[9, 6, 7, 3, 4, 5, 8, 2, 1],[2, 5, 1, 8, 7, 6, 4, 9, 3],[5, 4, 8, 1, 3, 2, 9, 7, 6],[7, 2, 9, 5, 6, 4, 1, 3, 8],[1, 3, 6, 7, 9, 8, 2, 4, 5],[3, 7, 2, 6, 8, 9, 5, 1, 4],[8, 1, 4, 2, 5, 3, 7, 6, 9],[6, 9, 5, 4, 1, 7, 3, 8, 2]]</code>.
|
- text: <code>solveSudoku([[-1, -1, 3, -1, 2, -1, 6, -1, -1],[9, -1, -1, 3, -1, 5, -1, -1, 1],[-1, -1, 1, 8, -1, 6, 4, -1, -1],[-1, -1, 8, 1, -1, 2, 9, -1, -1],[7, -1, -1, -1, -1, -1, -1, -1, 8],[-1, -1, 6, 7, -1, 8, 2, -1, -1],[-1, -1, 2, 6, -1, 9, 5, -1, -1],[8, -1, -1, 2, -1, 3, -1, -1, 9],[-1, -1, 5, -1, 1, -1, 3, -1, -1]])</code> should return <code>[[4, 8, 3, 9, 2, 1, 6, 5, 7],[9, 6, 7, 3, 4, 5, 8, 2, 1],[2, 5, 1, 8, 7, 6, 4, 9, 3],[5, 4, 8, 1, 3, 2, 9, 7, 6],[7, 2, 9, 5, 6, 4, 1, 3, 8],[1, 3, 6, 7, 9, 8, 2, 4, 5],[3, 7, 2, 6, 8, 9, 5, 1, 4],[8, 1, 4, 2, 5, 3, 7, 6, 9],[6, 9, 5, 4, 1, 7, 3, 8, 2]]</code>.
|
||||||
testString: assert.deepEqual(solveSudoku([[-1, -1, 3, -1, 2, -1, 6, -1, -1], [9, -1, -1, 3, -1, 5, -1, -1, 1], [-1, -1, 1, 8, -1, 6, 4, -1, -1], [-1, -1, 8, 1, -1, 2, 9, -1, -1], [7, -1, -1, -1, -1, -1, -1, -1, 8], [-1, -1, 6, 7, -1, 8, 2, -1, -1], [-1, -1, 2, 6, -1, 9, 5, -1, -1], [8, -1, -1, 2, -1, 3, -1, -1, 9], [-1, -1, 5, -1, 1, -1, 3, -1, -1]]), [[4, 8, 3, 9, 2, 1, 6, 5, 7], [9, 6, 7, 3, 4, 5, 8, 2, 1], [2, 5, 1, 8, 7, 6, 4, 9, 3], [5, 4, 8, 1, 3, 2, 9, 7, 6], [7, 2, 9, 5, 6, 4, 1, 3, 8], [1, 3, 6, 7, 9, 8, 2, 4, 5], [3, 7, 2, 6, 8, 9, 5, 1, 4], [8, 1, 4, 2, 5, 3, 7, 6, 9], [6, 9, 5, 4, 1, 7, 3, 8, 2]], '<code>solveSudoku([[-1, -1, 3, -1, 2, -1, 6, -1, -1],[9, -1, -1, 3, -1, 5, -1, -1, 1],[-1, -1, 1, 8, -1, 6, 4, -1, -1],[-1, -1, 8, 1, -1, 2, 9, -1, -1],[7, -1, -1, -1, -1, -1, -1, -1, 8],[-1, -1, 6, 7, -1, 8, 2, -1, -1],[-1, -1, 2, 6, -1, 9, 5, -1, -1],[8, -1, -1, 2, -1, 3, -1, -1, 9],[-1, -1, 5, -1, 1, -1, 3, -1, -1]])</code> should return <code>[[4, 8, 3, 9, 2, 1, 6, 5, 7],[9, 6, 7, 3, 4, 5, 8, 2, 1],[2, 5, 1, 8, 7, 6, 4, 9, 3],[5, 4, 8, 1, 3, 2, 9, 7, 6],[7, 2, 9, 5, 6, 4, 1, 3, 8],[1, 3, 6, 7, 9, 8, 2, 4, 5],[3, 7, 2, 6, 8, 9, 5, 1, 4],[8, 1, 4, 2, 5, 3, 7, 6, 9],[6, 9, 5, 4, 1, 7, 3, 8, 2]]</code>.');
|
testString: assert.deepEqual(solveSudoku([[-1, -1, 3, -1, 2, -1, 6, -1, -1], [9, -1, -1, 3, -1, 5, -1, -1, 1], [-1, -1, 1, 8, -1, 6, 4, -1, -1], [-1, -1, 8, 1, -1, 2, 9, -1, -1], [7, -1, -1, -1, -1, -1, -1, -1, 8], [-1, -1, 6, 7, -1, 8, 2, -1, -1], [-1, -1, 2, 6, -1, 9, 5, -1, -1], [8, -1, -1, 2, -1, 3, -1, -1, 9], [-1, -1, 5, -1, 1, -1, 3, -1, -1]]), [[4, 8, 3, 9, 2, 1, 6, 5, 7], [9, 6, 7, 3, 4, 5, 8, 2, 1], [2, 5, 1, 8, 7, 6, 4, 9, 3], [5, 4, 8, 1, 3, 2, 9, 7, 6], [7, 2, 9, 5, 6, 4, 1, 3, 8], [1, 3, 6, 7, 9, 8, 2, 4, 5], [3, 7, 2, 6, 8, 9, 5, 1, 4], [8, 1, 4, 2, 5, 3, 7, 6, 9], [6, 9, 5, 4, 1, 7, 3, 8, 2]]);
|
||||||
```
|
```
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Challenge Seed
|
## Challenge Seed
|
||||||
|
|
||||||
<section id='challengeSeed'>
|
<section id='challengeSeed'>
|
||||||
|
|
||||||
<div id='js-seed'>
|
<div id='js-seed'>
|
||||||
@ -51,6 +56,7 @@ function solveSudoku(puzzle) {
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Solution
|
## Solution
|
||||||
|
|
||||||
<section id='solution'>
|
<section id='solution'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
@ -74,14 +80,14 @@ function solveSudoku(puzzle) {
|
|||||||
n.R = e.R;
|
n.R = e.R;
|
||||||
n.L = e;
|
n.L = e;
|
||||||
e.R.L = n;
|
e.R.L = n;
|
||||||
return e.R = n;
|
return (e.R = n);
|
||||||
};
|
};
|
||||||
|
|
||||||
const addBelow = (e, n) => {
|
const addBelow = (e, n) => {
|
||||||
n.D = e.D;
|
n.D = e.D;
|
||||||
n.U = e;
|
n.U = e;
|
||||||
e.D.U = n;
|
e.D.U = n;
|
||||||
return e.D = n;
|
return (e.D = n);
|
||||||
};
|
};
|
||||||
|
|
||||||
const search = function(h, s) {
|
const search = function(h, s) {
|
||||||
@ -154,8 +160,11 @@ function solveSudoku(puzzle) {
|
|||||||
const cellCount = g.length;
|
const cellCount = g.length;
|
||||||
const tokenCount = Math.sqrt(cellCount);
|
const tokenCount = Math.sqrt(cellCount);
|
||||||
const N = Math.sqrt(tokenCount);
|
const N = Math.sqrt(tokenCount);
|
||||||
const g2D = g.map(e => isNaN(e * 1) ?
|
const g2D = g.map(e =>
|
||||||
new Array(tokenCount).fill(1).map((_, i) => i + 1) : [e * 1]);
|
isNaN(e * 1)
|
||||||
|
? new Array(tokenCount).fill(1).map((_, i) => i + 1)
|
||||||
|
: [e * 1]
|
||||||
|
);
|
||||||
return [cellCount, N, tokenCount, g2D];
|
return [cellCount, N, tokenCount, g2D];
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -166,7 +175,6 @@ function solveSudoku(puzzle) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const reduceGrid = puzString => {
|
const reduceGrid = puzString => {
|
||||||
|
|
||||||
const [
|
const [
|
||||||
numCells, // The total number of cells in a grid (81 for a 9x9 grid)
|
numCells, // The total number of cells in a grid (81 for a 9x9 grid)
|
||||||
N, // the 'n' value of the grid. (3 for a 9x9 grid)
|
N, // the 'n' value of the grid. (3 for a 9x9 grid)
|
||||||
@ -193,9 +201,9 @@ function solveSudoku(puzzle) {
|
|||||||
|
|
||||||
// The 4 columns that we will populate.
|
// The 4 columns that we will populate.
|
||||||
const A = headRow[i];
|
const A = headRow[i];
|
||||||
const B = headRow[numCells + candIdx + (ri * U)];
|
const B = headRow[numCells + candIdx + ri * U];
|
||||||
const C = headRow[(numCells * 2) + candIdx + (ci * U)];
|
const C = headRow[numCells * 2 + candIdx + ci * U];
|
||||||
const D = headRow[(numCells * 3) + candIdx + (bi * U)];
|
const D = headRow[numCells * 3 + candIdx + bi * U];
|
||||||
|
|
||||||
// The Row-Column Constraint
|
// The Row-Column Constraint
|
||||||
let rcc = addBelow(A.U, new DoX(id, A));
|
let rcc = addBelow(A.U, new DoX(id, A));
|
||||||
@ -213,23 +221,21 @@ function solveSudoku(puzzle) {
|
|||||||
search(H, []);
|
search(H, []);
|
||||||
};
|
};
|
||||||
|
|
||||||
var stringPuzzle = "";
|
var stringPuzzle = '';
|
||||||
|
|
||||||
for (var i = 0; i < puzzle.length; i++) {
|
for (var i = 0; i < puzzle.length; i++) {
|
||||||
puzzle[i].forEach(function(e) {
|
puzzle[i].forEach(function(e) {
|
||||||
if (e == -1)
|
if (e == -1) stringPuzzle += '.';
|
||||||
stringPuzzle += ".";
|
else stringPuzzle += e;
|
||||||
else
|
});
|
||||||
stringPuzzle += e;
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
reduceGrid(stringPuzzle)
|
reduceGrid(stringPuzzle);
|
||||||
|
|
||||||
var result = [];
|
var result = [];
|
||||||
|
|
||||||
for (var i = 0; i < 9; i++) {
|
for (var i = 0; i < 9; i++) {
|
||||||
result.push(solution.slice(i * 9, (i + 1) * 9).map(e => parseInt(e)))
|
result.push(solution.slice(i * 9, (i + 1) * 9).map(e => parseInt(e)));
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
@ -6,6 +6,7 @@ forumTopicId: 302331
|
|||||||
---
|
---
|
||||||
|
|
||||||
## Description
|
## Description
|
||||||
|
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
Write a function that takes a string as a parameter. This string represents a number that can be in any base (less than 37) and return the sum of its digits.
|
Write a function that takes a string as a parameter. This string represents a number that can be in any base (less than 37) and return the sum of its digits.
|
||||||
<ul>
|
<ul>
|
||||||
@ -17,36 +18,39 @@ Write a function that takes a string as a parameter. This string represents a nu
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
|
|
||||||
<section id='instructions'>
|
<section id='instructions'>
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
|
|
||||||
<section id='tests'>
|
<section id='tests'>
|
||||||
|
|
||||||
``` yml
|
```yml
|
||||||
tests:
|
tests:
|
||||||
- text: <code>sumDigits</code> should be a function.
|
- text: <code>sumDigits</code> should be a function.
|
||||||
testString: assert(typeof sumDigits == 'function', '<code>sumDigits</code> should be a function.');
|
testString: assert(typeof sumDigits == 'function');
|
||||||
- text: <code>sumDigits("1")</code> should return a number.
|
- text: <code>sumDigits("1")</code> should return a number.
|
||||||
testString: assert(typeof sumDigits("1") == 'number', '<code>sumDigits("1")</code> should return a number.');
|
testString: assert(typeof sumDigits("1") == 'number');
|
||||||
- text: <code>sumDigits("1")</code> should return <code>1</code>.
|
- text: <code>sumDigits("1")</code> should return <code>1</code>.
|
||||||
testString: assert.equal(sumDigits("1"), 1, '<code>sumDigits("1")</code> should return <code>1</code>.');
|
testString: assert.equal(sumDigits("1"), 1);
|
||||||
- text: <code>sumDigits("12345")</code> should return <code>15</code>.
|
- text: <code>sumDigits("12345")</code> should return <code>15</code>.
|
||||||
testString: assert.equal(sumDigits("12345"), 15, '<code>sumDigits("12345")</code> should return <code>15</code>.');
|
testString: assert.equal(sumDigits("12345"), 15);
|
||||||
- text: <code>sumDigits("254")</code> should return <code>11</code>.
|
- text: <code>sumDigits("254")</code> should return <code>11</code>.
|
||||||
testString: assert.equal(sumDigits("254"), 11, '<code>sumDigits("254")</code> should return <code>11</code>.');
|
testString: assert.equal(sumDigits("254"), 11);
|
||||||
- text: <code>sumDigits("fe")</code> should return <code>29</code>.
|
- text: <code>sumDigits("fe")</code> should return <code>29</code>.
|
||||||
testString: assert.equal(sumDigits("fe"), 29, '<code>sumDigits("fe")</code> should return <code>29</code>.');
|
testString: assert.equal(sumDigits("fe"), 29);
|
||||||
- text: <code>sumDigits("f0e")</code> should return <code>29</code>.
|
- text: <code>sumDigits("f0e")</code> should return <code>29</code>.
|
||||||
testString: assert.equal(sumDigits("f0e"), 29, '<code>sumDigits("f0e")</code> should return <code>29</code>.');
|
testString: assert.equal(sumDigits("f0e"), 29);
|
||||||
- text: <code>sumDigits("999ABCXYZ")</code> should return <code>162</code>.
|
- text: <code>sumDigits("999ABCXYZ")</code> should return <code>162</code>.
|
||||||
testString: assert.equal(sumDigits("999ABCXYZ"), 162, '<code>sumDigits("999ABCXYZ")</code> should return <code>162</code>.');
|
testString: assert.equal(sumDigits("999ABCXYZ"), 162);
|
||||||
```
|
```
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Challenge Seed
|
## Challenge Seed
|
||||||
|
|
||||||
<section id='challengeSeed'>
|
<section id='challengeSeed'>
|
||||||
<div id='js-seed'>
|
<div id='js-seed'>
|
||||||
|
|
||||||
@ -60,13 +64,15 @@ function sumDigits(n) {
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Solution
|
## Solution
|
||||||
|
|
||||||
<section id='solution'>
|
<section id='solution'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function sumDigits(n) {
|
function sumDigits(n) {
|
||||||
n += ''
|
n += '';
|
||||||
for (var s=0, i=0, e=n.length; i<e; i+=1) s+=parseInt(n.charAt(i),36)
|
for (var s = 0, i = 0, e = n.length; i < e; i += 1)
|
||||||
return s
|
s += parseInt(n.charAt(i), 36);
|
||||||
|
return s;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -6,40 +6,45 @@ forumTopicId: 302332
|
|||||||
---
|
---
|
||||||
|
|
||||||
## Description
|
## Description
|
||||||
|
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
|
|
||||||
The objective is to write a function that finds the sum of all positive multiples of 3 or 5 below <i>n</i>.
|
The objective is to write a function that finds the sum of all positive multiples of 3 or 5 below <i>n</i>.
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
|
|
||||||
<section id='instructions'>
|
<section id='instructions'>
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
|
|
||||||
<section id='tests'>
|
<section id='tests'>
|
||||||
|
|
||||||
``` yml
|
```yml
|
||||||
tests:
|
tests:
|
||||||
- text: <code>sumMults</code> should be a function.
|
- text: <code>sumMults</code> should be a function.
|
||||||
testString: assert(typeof sumMults == 'function', '<code>sumMults</code> should be a function.');
|
testString: assert(typeof sumMults == 'function');
|
||||||
- text: <code>sumMults(10)</code> should return a number.
|
- text: <code>sumMults(10)</code> should return a number.
|
||||||
testString: assert(typeof sumMults(10) == 'number', '<code>sumMults(10)</code> should return a number.');
|
testString: assert(typeof sumMults(10) == 'number');
|
||||||
- text: <code>sumMults(10)</code> should return <code>23</code>.
|
- text: <code>sumMults(10)</code> should return <code>23</code>.
|
||||||
testString: assert.equal(sumMults(10), 23, '<code>sumMults(10)</code> should return <code>23</code>.');
|
testString: assert.equal(sumMults(10), 23);
|
||||||
- text: <code>sumMults(100)</code> should return <code>2318</code>.
|
- text: <code>sumMults(100)</code> should return <code>2318</code>.
|
||||||
testString: assert.equal(sumMults(100), 2318, '<code>sumMults(100)</code> should return <code>2318</code>.');
|
testString: assert.equal(sumMults(100), 2318);
|
||||||
- text: <code>sumMults(1000)</code> should return <code>233168</code>.
|
- text: <code>sumMults(1000)</code> should return <code>233168</code>.
|
||||||
testString: assert.equal(sumMults(1000), 233168, '<code>sumMults(1000)</code> should return <code>233168</code>.');
|
testString: assert.equal(sumMults(1000), 233168);
|
||||||
- text: <code>sumMults(10000)</code> should return <code>23331668</code>.
|
- text: <code>sumMults(10000)</code> should return <code>23331668</code>.
|
||||||
testString: assert.equal(sumMults(10000), 23331668, '<code>sumMults(10000)</code> should return <code>23331668</code>.');
|
testString: assert.equal(sumMults(10000), 23331668);
|
||||||
- text: <code>sumMults(100000)</code> should return <code>2333316668</code>.
|
- text: <code>sumMults(100000)</code> should return <code>2333316668</code>.
|
||||||
testString: assert.equal(sumMults(100000), 2333316668, '<code>sumMults(100000)</code> should return <code>2333316668</code>.');
|
testString: assert.equal(sumMults(100000), 2333316668);
|
||||||
```
|
```
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Challenge Seed
|
## Challenge Seed
|
||||||
|
|
||||||
<section id='challengeSeed'>
|
<section id='challengeSeed'>
|
||||||
|
|
||||||
<div id='js-seed'>
|
<div id='js-seed'>
|
||||||
@ -54,6 +59,7 @@ function sumMults(n) {
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Solution
|
## Solution
|
||||||
|
|
||||||
<section id='solution'>
|
<section id='solution'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
@ -6,48 +6,54 @@ forumTopicId: 302333
|
|||||||
---
|
---
|
||||||
|
|
||||||
## Description
|
## Description
|
||||||
|
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
|
|
||||||
Compute the <b>n</b><sup>th</sup> term of a <a href="https://en.wikipedia.org/wiki/Series (mathematics)" target="_blank">series</a>, i.e. the sum of the <b>n</b> first terms of the corresponding <a href="https://en.wikipedia.org/wiki/sequence" target="_blank">sequence</a>.
|
Compute the <b>n</b><sup>th</sup> term of a <a href="https://en.wikipedia.org/wiki/Series (mathematics)" target="_blank">series</a>, i.e. the sum of the <b>n</b> first terms of the corresponding <a href="https://en.wikipedia.org/wiki/sequence" target="_blank">sequence</a>.
|
||||||
Informally this value, or its limit when <b>n</b> tends to infinity, is also called the <i>sum of the series</i>, thus the title of this task.
|
Informally this value, or its limit when <b>n</b> tends to infinity, is also called the <i>sum of the series</i>, thus the title of this task.
|
||||||
For this task, use:
|
For this task, use:
|
||||||
<span style="margin-left: 2em;">$S_n = \sum_{k=1}^n \frac{1}{k^2}$</span>
|
<span style="margin-left: 2em;">$S_n = \sum_{k=1}^n \frac{1}{k^2}$</span>
|
||||||
and compute $S_{1000}$
|
and compute $S_{1000}$
|
||||||
This approximates the <a href="https://en.wikipedia.org/wiki/Riemann zeta function" target="_blank">zeta function</a> for S=2, whose exact value
|
This approximates the <a href="https://en.wikipedia.org/wiki/Riemann zeta function" target="_blank">zeta function</a> for S=2, whose exact value
|
||||||
<span style="margin-left: 2em;">$\zeta(2) = {\pi^2\over 6}$</span>
|
<span style="margin-left: 2em;">$\zeta(2) = {\pi^2\over 6}$</span>
|
||||||
is the solution of the <a href="https://en.wikipedia.org/wiki/Basel problem" target="_blank">Basel problem</a>.
|
is the solution of the <a href="https://en.wikipedia.org/wiki/Basel problem" target="_blank">Basel problem</a>.
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
|
|
||||||
<section id='instructions'>
|
<section id='instructions'>
|
||||||
|
|
||||||
Write a function that take $a$ and $b$ as parameters and returns the sum of $a^{th}$ to $b^{th}$ members of the sequence.
|
Write a function that take $a$ and $b$ as parameters and returns the sum of $a^{th}$ to $b^{th}$ members of the sequence.
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
|
|
||||||
<section id='tests'>
|
<section id='tests'>
|
||||||
|
|
||||||
``` yml
|
```yml
|
||||||
tests:
|
tests:
|
||||||
- text: <code>sum</code> should be a function.
|
- text: <code>sum</code> should be a function.
|
||||||
testString: assert(typeof sum == 'function', '<code>sum</code> should be a function.');
|
testString: assert(typeof sum == 'function');
|
||||||
- text: <code>sum(1, 100)</code> should return a number.
|
- text: <code>sum(1, 100)</code> should return a number.
|
||||||
testString: assert(typeof sum(1, 100) == 'number', '<code>sum(1, 100)</code> should return a number.');
|
testString: assert(typeof sum(1, 100) == 'number');
|
||||||
- text: <code>sum(1, 100)</code> should return <code>1.6349839001848923</code>.
|
- text: <code>sum(1, 100)</code> should return <code>1.6349839001848923</code>.
|
||||||
testString: assert.equal(sum(1, 100), 1.6349839001848923, '<code>sum(1, 100)</code> should return <code>1.6349839001848923</code>.');
|
testString: assert.equal(sum(1, 100), 1.6349839001848923);
|
||||||
- text: <code>sum(33, 46)</code> should return <code>0.009262256361481223</code>.
|
- text: <code>sum(33, 46)</code> should return <code>0.009262256361481223</code>.
|
||||||
testString: assert.equal(sum(33, 46), 0.009262256361481223, '<code>sum(33, 46)</code> should return <code>0.009262256361481223</code>.');
|
testString: assert.equal(sum(33, 46), 0.009262256361481223);
|
||||||
- text: <code>sum(21, 213)</code> should return <code>0.044086990748706555</code>.
|
- text: <code>sum(21, 213)</code> should return <code>0.044086990748706555</code>.
|
||||||
testString: assert.equal(sum(21, 213), 0.044086990748706555, '<code>sum(21, 213)</code> should return <code>0.044086990748706555</code>.');
|
testString: assert.equal(sum(21, 213), 0.044086990748706555);
|
||||||
- text: <code>sum(11, 111)</code> should return <code>0.08619778593108679</code>.
|
- text: <code>sum(11, 111)</code> should return <code>0.08619778593108679</code>.
|
||||||
testString: assert.equal(sum(11, 111), 0.08619778593108679, '<code>sum(11, 111)</code> should return <code>0.08619778593108679</code>.');
|
testString: assert.equal(sum(11, 111), 0.08619778593108679);
|
||||||
- text: <code>sum(1, 10)</code> should return <code>1.5497677311665408</code>.
|
- text: <code>sum(1, 10)</code> should return <code>1.5497677311665408</code>.
|
||||||
testString: assert.equal(sum(1, 10), 1.5497677311665408, '<code>sum(1, 10)</code> should return <code>1.5497677311665408</code>.');
|
testString: assert.equal(sum(1, 10), 1.5497677311665408);
|
||||||
```
|
```
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Challenge Seed
|
## Challenge Seed
|
||||||
|
|
||||||
<section id='challengeSeed'>
|
<section id='challengeSeed'>
|
||||||
|
|
||||||
<div id='js-seed'>
|
<div id='js-seed'>
|
||||||
@ -62,12 +68,13 @@ function sum(a, b) {
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Solution
|
## Solution
|
||||||
|
|
||||||
<section id='solution'>
|
<section id='solution'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function sum(a, b) {
|
function sum(a, b) {
|
||||||
function fn(x) {
|
function fn(x) {
|
||||||
return 1 / (x * x)
|
return 1 / (x * x);
|
||||||
}
|
}
|
||||||
var s = 0;
|
var s = 0;
|
||||||
for (; a <= b; a++) s += fn(a);
|
for (; a <= b; a++) s += fn(a);
|
||||||
|
@ -6,40 +6,45 @@ forumTopicId: 302334
|
|||||||
---
|
---
|
||||||
|
|
||||||
## Description
|
## Description
|
||||||
|
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
|
|
||||||
Write a function to find the sum of squares of an array of integers.
|
Write a function to find the sum of squares of an array of integers.
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
|
|
||||||
<section id='instructions'>
|
<section id='instructions'>
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
|
|
||||||
<section id='tests'>
|
<section id='tests'>
|
||||||
|
|
||||||
``` yml
|
```yml
|
||||||
tests:
|
tests:
|
||||||
- text: <code>sumsq</code> should be a function.
|
- text: <code>sumsq</code> should be a function.
|
||||||
testString: assert(typeof sumsq == 'function', '<code>sumsq</code> should be a function.');
|
testString: assert(typeof sumsq == 'function');
|
||||||
- text: <code>sumsq([1, 2, 3, 4, 5])</code> should return a number.
|
- text: <code>sumsq([1, 2, 3, 4, 5])</code> should return a number.
|
||||||
testString: assert(typeof sumsq([1, 2, 3, 4, 5]) == 'number', '<code>sumsq([1, 2, 3, 4, 5])</code> should return a number.');
|
testString: assert(typeof sumsq([1, 2, 3, 4, 5]) == 'number');
|
||||||
- text: <code>sumsq([1, 2, 3, 4, 5])</code> should return <code>55</code>.
|
- text: <code>sumsq([1, 2, 3, 4, 5])</code> should return <code>55</code>.
|
||||||
testString: assert.equal(sumsq([1, 2, 3, 4, 5]), 55, '<code>sumsq([1, 2, 3, 4, 5])</code> should return <code>55</code>.');
|
testString: assert.equal(sumsq([1, 2, 3, 4, 5]), 55);
|
||||||
- text: <code>sumsq([25, 32, 12, 7, 20])</code> should return <code>2242</code>.
|
- text: <code>sumsq([25, 32, 12, 7, 20])</code> should return <code>2242</code>.
|
||||||
testString: assert.equal(sumsq([25, 32, 12, 7, 20]), 2242, '<code>sumsq([25, 32, 12, 7, 20])</code> should return <code>2242</code>.');
|
testString: assert.equal(sumsq([25, 32, 12, 7, 20]), 2242);
|
||||||
- text: <code>sumsq([38, 45, 35, 8, 13])</code> should return <code>4927</code>.
|
- text: <code>sumsq([38, 45, 35, 8, 13])</code> should return <code>4927</code>.
|
||||||
testString: assert.equal(sumsq([38, 45, 35, 8, 13]), 4927, '<code>sumsq([38, 45, 35, 8, 13])</code> should return <code>4927</code>.');
|
testString: assert.equal(sumsq([38, 45, 35, 8, 13]), 4927);
|
||||||
- text: <code>sumsq([43, 36, 20, 34, 24])</code> should return <code>5277</code>.
|
- text: <code>sumsq([43, 36, 20, 34, 24])</code> should return <code>5277</code>.
|
||||||
testString: assert.equal(sumsq([43, 36, 20, 34, 24]), 5277, '<code>sumsq([43, 36, 20, 34, 24])</code> should return <code>5277</code>.');
|
testString: assert.equal(sumsq([43, 36, 20, 34, 24]), 5277);
|
||||||
- text: <code>sumsq([12, 33, 26, 18, 1, 16, 3])</code> should return <code>2499</code>.
|
- text: <code>sumsq([12, 33, 26, 18, 1, 16, 3])</code> should return <code>2499</code>.
|
||||||
testString: assert.equal(sumsq([12, 33, 26, 18, 1, 16, 3]), 2499, '<code>sumsq([12, 33, 26, 18, 1, 16, 3])</code> should return <code>2499</code>.');
|
testString: assert.equal(sumsq([12, 33, 26, 18, 1, 16, 3]), 2499);
|
||||||
```
|
```
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Challenge Seed
|
## Challenge Seed
|
||||||
|
|
||||||
<section id='challengeSeed'>
|
<section id='challengeSeed'>
|
||||||
|
|
||||||
<div id='js-seed'>
|
<div id='js-seed'>
|
||||||
@ -54,6 +59,7 @@ function sumsq(array) {
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Solution
|
## Solution
|
||||||
|
|
||||||
<section id='solution'>
|
<section id='solution'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
@ -6,6 +6,7 @@ forumTopicId: 302335
|
|||||||
---
|
---
|
||||||
|
|
||||||
## Description
|
## Description
|
||||||
|
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
Find solutions to the <i>sum to one hundred</i> puzzle.
|
Find solutions to the <i>sum to one hundred</i> puzzle.
|
||||||
Add (insert) the mathematical operators <b>+</b> or <b>─</b> (plus or minus) before any of the digits in the decimal numeric string <b>123456789</b> such that the resulting mathematical expression adds up to a particular sum (in this iconic case, <b>100</b>).
|
Add (insert) the mathematical operators <b>+</b> or <b>─</b> (plus or minus) before any of the digits in the decimal numeric string <b>123456789</b> such that the resulting mathematical expression adds up to a particular sum (in this iconic case, <b>100</b>).
|
||||||
@ -14,34 +15,37 @@ Example:
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
|
|
||||||
<section id='instructions'>
|
<section id='instructions'>
|
||||||
Write a function that takes a number as parameter. The function should return an array containing all solutions for the given number. The solutions should be strings representing the expressions. For example: "1+23-456+78-9". Sort the array before returning it.
|
Write a function that takes a number as parameter. The function should return an array containing all solutions for the given number. The solutions should be strings representing the expressions. For example: "1+23-456+78-9". Sort the array before returning it.
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
|
|
||||||
<section id='tests'>
|
<section id='tests'>
|
||||||
|
|
||||||
``` yml
|
```yml
|
||||||
tests:
|
tests:
|
||||||
- text: <code>sumTo100</code> should be a function.
|
- text: <code>sumTo100</code> should be a function.
|
||||||
testString: assert(typeof sumTo100 == 'function', '<code>sumTo100</code> should be a function.');
|
testString: assert(typeof sumTo100 == 'function');
|
||||||
- text: <code>sumTo100(199)</code> should return an array.
|
- text: <code>sumTo100(199)</code> should return an array.
|
||||||
testString: assert(Array.isArray(sumTo100(199)), '<code>sumTo100(199)</code> should return an array.');
|
testString: assert(Array.isArray(sumTo100(199)));
|
||||||
- text: <code>sumTo100(199)</code> should return <code>["-1+2-3+45+67+89", "123-4+5+6+78-9", "123-4+56+7+8+9"]</code>.
|
- text: <code>sumTo100(199)</code> should return <code>["-1+2-3+45+67+89", "123-4+5+6+78-9", "123-4+56+7+8+9"]</code>.
|
||||||
testString: assert.deepEqual(sumTo100(199), ["-1+2-3+45+67+89", "123-4+5+6+78-9", "123-4+56+7+8+9"], '<code>sumTo100(199)</code> should return <code>["-1+2-3+45+67+89", "123-4+5+6+78-9", "123-4+56+7+8+9"]</code>.');
|
testString: assert.deepEqual(sumTo100(199), ["-1+2-3+45+67+89", "123-4+5+6+78-9", "123-4+56+7+8+9"]);
|
||||||
- text: <code>sumTo100(209)</code> should return <code>["1+234+56+7-89"]</code>.
|
- text: <code>sumTo100(209)</code> should return <code>["1+234+56+7-89"]</code>.
|
||||||
testString: assert.deepEqual(sumTo100(209), ["1+234+56+7-89"], '<code>sumTo100(209)</code> should return <code>["1+234+56+7-89"]</code>.');
|
testString: assert.deepEqual(sumTo100(209), ["1+234+56+7-89"]);
|
||||||
- text: <code>sumTo100(243)</code> should return <code>["-1-234+567-89", "-12+345+6-7-89", "123+45+6+78-9"]</code>.
|
- text: <code>sumTo100(243)</code> should return <code>["-1-234+567-89", "-12+345+6-7-89", "123+45+6+78-9"]</code>.
|
||||||
testString: assert.deepEqual(sumTo100(243), ["-1-234+567-89", "-12+345+6-7-89", "123+45+6+78-9"], '<code>sumTo100(243)</code> should return <code>["-1-234+567-89", "-12+345+6-7-89", "123+45+6+78-9"]</code>.');
|
testString: assert.deepEqual(sumTo100(243), ["-1-234+567-89", "-12+345+6-7-89", "123+45+6+78-9"]);
|
||||||
- text: <code>sumTo100(199)</code> should return <code>["-1+2-3+45+67+89", "123-4+5+6+78-9", "123-4+56+7+8+9"]</code>.
|
- text: <code>sumTo100(199)</code> should return <code>["-1+2-3+45+67+89", "123-4+5+6+78-9", "123-4+56+7+8+9"]</code>.
|
||||||
testString: assert.deepEqual(sumTo100(199), ["-1+2-3+45+67+89", "123-4+5+6+78-9", "123-4+56+7+8+9"], '<code>sumTo100(199)</code> should return <code>["-1+2-3+45+67+89", "123-4+5+6+78-9", "123-4+56+7+8+9"]</code>.');
|
testString: assert.deepEqual(sumTo100(199), ["-1+2-3+45+67+89", "123-4+5+6+78-9", "123-4+56+7+8+9"]);
|
||||||
- text: <code>sumTo100(197)</code> should return <code>["1-2-3+45+67+89", "12+34-5+67+89", "123+4-5+6+78-9"]</code>.
|
- text: <code>sumTo100(197)</code> should return <code>["1-2-3+45+67+89", "12+34-5+67+89", "123+4-5+6+78-9"]</code>.
|
||||||
testString: assert.deepEqual(sumTo100(197), ["1-2-3+45+67+89", "12+34-5+67+89", "123+4-5+6+78-9"], '<code>sumTo100(197)</code> should return <code>["1-2-3+45+67+89", "12+34-5+67+89", "123+4-5+6+78-9"]</code>.');
|
testString: assert.deepEqual(sumTo100(197), ["1-2-3+45+67+89", "12+34-5+67+89", "123+4-5+6+78-9"]);
|
||||||
```
|
```
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Challenge Seed
|
## Challenge Seed
|
||||||
|
|
||||||
<section id='challengeSeed'>
|
<section id='challengeSeed'>
|
||||||
<div id='js-seed'>
|
<div id='js-seed'>
|
||||||
|
|
||||||
@ -55,23 +59,29 @@ function sumTo100(n) {
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Solution
|
## Solution
|
||||||
|
|
||||||
<section id='solution'>
|
<section id='solution'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function sumTo100(n) {
|
function sumTo100(n) {
|
||||||
var permutationsWithRepetition = function(n, as) {
|
var permutationsWithRepetition = function(n, as) {
|
||||||
return as.length > 0 ?
|
return as.length > 0
|
||||||
foldl1(curry(cartesianProduct)(as), replicate(n, as)) : [];
|
? foldl1(curry(cartesianProduct)(as), replicate(n, as))
|
||||||
|
: [];
|
||||||
};
|
};
|
||||||
|
|
||||||
var cartesianProduct = function(xs, ys) {
|
var cartesianProduct = function(xs, ys) {
|
||||||
return [].concat.apply([], xs.map(function(x) {
|
return [].concat.apply(
|
||||||
return [].concat.apply([], ys.map(function(y) {
|
[],
|
||||||
return [
|
xs.map(function(x) {
|
||||||
[x].concat(y)
|
return [].concat.apply(
|
||||||
];
|
[],
|
||||||
}));
|
ys.map(function(y) {
|
||||||
}));
|
return [[x].concat(y)];
|
||||||
|
})
|
||||||
|
);
|
||||||
|
})
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
var curry = function(f) {
|
var curry = function(f) {
|
||||||
@ -89,8 +99,7 @@ function sumTo100(n) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
var foldl1 = function(f, xs) {
|
var foldl1 = function(f, xs) {
|
||||||
return xs.length > 0 ? xs.slice(1)
|
return xs.length > 0 ? xs.slice(1).reduce(f, xs[0]) : [];
|
||||||
.reduce(f, xs[0]) : [];
|
|
||||||
};
|
};
|
||||||
|
|
||||||
var replicate = function(n, a) {
|
var replicate = function(n, a) {
|
||||||
@ -106,32 +115,35 @@ function sumTo100(n) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
var asSum = function(xs) {
|
var asSum = function(xs) {
|
||||||
var dct = xs.reduceRight(function(a, sign, i) {
|
var dct = xs.reduceRight(
|
||||||
var d = i + 1; // zero-based index to [1-9] positions
|
function(a, sign, i) {
|
||||||
if (sign !== 0) {
|
var d = i + 1; // zero-based index to [1-9] positions
|
||||||
// Sum increased, digits cleared
|
if (sign !== 0) {
|
||||||
return {
|
// Sum increased, digits cleared
|
||||||
digits: [],
|
return {
|
||||||
n: a.n + sign * parseInt([d].concat(a.digits)
|
digits: [],
|
||||||
.join(''), 10)
|
n: a.n + sign * parseInt([d].concat(a.digits).join(''), 10)
|
||||||
};
|
};
|
||||||
} else return { // Digits extended, sum unchanged
|
} else
|
||||||
digits: [d].concat(a.digits),
|
return {
|
||||||
n: a.n
|
// Digits extended, sum unchanged
|
||||||
};
|
digits: [d].concat(a.digits),
|
||||||
}, {
|
n: a.n
|
||||||
digits: [],
|
};
|
||||||
n: 0
|
},
|
||||||
});
|
{
|
||||||
return dct.n + (
|
digits: [],
|
||||||
dct.digits.length > 0 ? parseInt(dct.digits.join(''), 10) : 0
|
n: 0
|
||||||
|
}
|
||||||
|
);
|
||||||
|
return (
|
||||||
|
dct.n + (dct.digits.length > 0 ? parseInt(dct.digits.join(''), 10) : 0)
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
var asString = function(xs) {
|
var asString = function(xs) {
|
||||||
var ns = xs.reduce(function(a, sign, i) {
|
var ns = xs.reduce(function(a, sign, i) {
|
||||||
var d = (i + 1)
|
var d = (i + 1).toString();
|
||||||
.toString();
|
|
||||||
return sign === 0 ? a + d : a + (sign > 0 ? '+' : '-') + d;
|
return sign === 0 ? a + d : a + (sign > 0 ? '+' : '-') + d;
|
||||||
}, '');
|
}, '');
|
||||||
|
|
||||||
@ -141,8 +153,9 @@ function sumTo100(n) {
|
|||||||
var universe = permutationsWithRepetition(9, [0, 1, -1])
|
var universe = permutationsWithRepetition(9, [0, 1, -1])
|
||||||
.filter(function(x) {
|
.filter(function(x) {
|
||||||
return x[0] !== 1 && asSum(x) === n;
|
return x[0] !== 1 && asSum(x) === n;
|
||||||
}).map(asString);
|
})
|
||||||
return universe.sort()
|
.map(asString);
|
||||||
|
return universe.sort();
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -6,42 +6,50 @@ forumTopicId: 302336
|
|||||||
---
|
---
|
||||||
|
|
||||||
## Description
|
## Description
|
||||||
|
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
|
|
||||||
The <a href="https://en.wikipedia.org/wiki/Sutherland-Hodgman clipping algorithm" target="_blank">Sutherland-Hodgman clipping algorithm</a> finds the polygon that is the intersection between an arbitrary polygon (the "subject polygon") and a convex polygon (the "clip polygon").
|
The <a href="https://en.wikipedia.org/wiki/Sutherland-Hodgman clipping algorithm" target="_blank">Sutherland-Hodgman clipping algorithm</a> 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.
|
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:
|
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>
|
<pre>[(50, 150), (200, 50), (350, 150), (350, 300), (250, 300), (200, 250), (150, 350), (100, 250), (100, 200)]</pre>
|
||||||
|
|
||||||
and clip it by the rectangle defined by the points:
|
and clip it by the rectangle defined by the points:
|
||||||
|
|
||||||
<pre>[(100, 100), (300, 100), (300, 300), (100, 300)]</pre>
|
<pre>[(100, 100), (300, 100), (300, 300), (100, 300)]</pre>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
|
|
||||||
<section id='instructions'>
|
<section id='instructions'>
|
||||||
|
|
||||||
Write a function that takes 2 arrays as parameters. The first array contains the points of the subject polygon and the second array contains the points of the clipping polygon. The function should return an array containing the points of the clipped polygon. Each number should be rounded to 3 decimal places.
|
Write a function that takes 2 arrays as parameters. The first array contains the points of the subject polygon and the second array contains the points of the clipping polygon. The function should return an array containing the points of the clipped polygon. Each number should be rounded to 3 decimal places.
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
|
|
||||||
<section id='tests'>
|
<section id='tests'>
|
||||||
|
|
||||||
``` yml
|
```yml
|
||||||
tests:
|
tests:
|
||||||
- text: <code>clip</code> should be a function.
|
- text: <code>clip</code> should be a function.
|
||||||
testString: assert(typeof clip == 'function', '<code>clip</code> should be a function.');
|
testString: assert(typeof clip == 'function');
|
||||||
- text: <code>clip([[50, 150], [200, 50], [350, 150], [350, 300], [250, 300], [200, 250], [150, 350], [100, 250], [100, 200]], [[100, 100], [300, 100], [300, 300], [100, 300]])</code> should return an array.
|
- text: <code>clip([[50, 150], [200, 50], [350, 150], [350, 300], [250, 300], [200, 250], [150, 350], [100, 250], [100, 200]], [[100, 100], [300, 100], [300, 300], [100, 300]])</code> should return an array.
|
||||||
testString: assert(Array.isArray(clip([[50, 150], [200, 50], [350, 150], [350, 300], [250, 300], [200, 250], [150, 350], [100, 250], [100, 200]], [[100, 100], [300, 100], [300, 300], [100, 300]])), '<code>clip([[50, 150], [200, 50], [350, 150], [350, 300], [250, 300], [200, 250], [150, 350], [100, 250], [100, 200]], [[100, 100], [300, 100], [300, 300], [100, 300]])</code> should return an array.');
|
testString: assert(Array.isArray(clip([[50, 150], [200, 50], [350, 150], [350, 300], [250, 300], [200, 250], [150, 350], [100, 250], [100, 200]], [[100, 100], [300, 100], [300, 300], [100, 300]])));
|
||||||
- text: <code>clip([[50, 150], [200, 50], [350, 150], [350, 300], [250, 300], [200, 250], [150, 350], [100, 250], [100, 200]], [[100, 100], [300, 100], [300, 300], [100, 300]])</code> should return <code>[[100, 116.667], [125, 100], [275, 100], [300, 116.667], [300, 300], [250, 300], [200, 250], [175, 300], [125, 300], [100, 250]]</code>.
|
- text: <code>clip([[50, 150], [200, 50], [350, 150], [350, 300], [250, 300], [200, 250], [150, 350], [100, 250], [100, 200]], [[100, 100], [300, 100], [300, 300], [100, 300]])</code> should return <code>[[100, 116.667], [125, 100], [275, 100], [300, 116.667], [300, 300], [250, 300], [200, 250], [175, 300], [125, 300], [100, 250]]</code>.
|
||||||
testString: assert.deepEqual(clip([[50, 150], [200, 50], [350, 150], [350, 300], [250, 300], [200, 250], [150, 350], [100, 250], [100, 200]], [[100, 100], [300, 100], [300, 300], [100, 300]]), [[100, 116.667], [125, 100], [275, 100], [300, 116.667], [300, 300], [250, 300], [200, 250], [175, 300], [125, 300], [100, 250]], '<code>clip([[50, 150], [200, 50], [350, 150], [350, 300], [250, 300], [200, 250], [150, 350], [100, 250], [100, 200]], [[100, 100], [300, 100], [300, 300], [100, 300]])</code> should return <code>[[100, 116.667], [125, 100], [275, 100], [300, 116.667], [300, 300], [250, 300], [200, 250], [175, 300], [125, 300], [100, 250]]</code>.');
|
testString: assert.deepEqual(clip([[50, 150], [200, 50], [350, 150], [350, 300], [250, 300], [200, 250], [150, 350], [100, 250], [100, 200]], [[100, 100], [300, 100], [300, 300], [100, 300]]), [[100, 116.667], [125, 100], [275, 100], [300, 116.667], [300, 300], [250, 300], [200, 250], [175, 300], [125, 300], [100, 250]]);
|
||||||
- text: <code>clip([[150, 200], [400, 450], [30, 50]], [[10, 10], [300, 200], [400, 600], [100, 300]])</code> should return <code>[[150, 200], [350, 400], [348.611, 394.444], [30, 50]]</code>.
|
- text: <code>clip([[150, 200], [400, 450], [30, 50]], [[10, 10], [300, 200], [400, 600], [100, 300]])</code> should return <code>[[150, 200], [350, 400], [348.611, 394.444], [30, 50]]</code>.
|
||||||
testString: assert.deepEqual(clip([[150, 200], [400, 450], [30, 50]], [[10, 10], [300, 200], [400, 600], [100, 300]]), [[150, 200], [350, 400], [348.611, 394.444], [30, 50]], '<code>clip([[150, 200], [400, 450], [30, 50]], [[10, 10], [300, 200], [400, 600], [100, 300]])</code> should return <code>[[150, 200], [350, 400], [348.611, 394.444], [30, 50]]</code>.');
|
testString: assert.deepEqual(clip([[150, 200], [400, 450], [30, 50]], [[10, 10], [300, 200], [400, 600], [100, 300]]), [[150, 200], [350, 400], [348.611, 394.444], [30, 50]]);
|
||||||
- text: <code>clip([[250, 200], [100, 450], [130, 250]], [[50, 60], [100, 230], [400, 600], [100, 300]])</code> should return <code>[[129.167, 329.167], [119.565, 319.565], [121.854, 304.305]]</code>.
|
- text: <code>clip([[250, 200], [100, 450], [130, 250]], [[50, 60], [100, 230], [400, 600], [100, 300]])</code> should return <code>[[129.167, 329.167], [119.565, 319.565], [121.854, 304.305]]</code>.
|
||||||
testString: assert.deepEqual(clip([[250, 200], [100, 450], [130, 250]], [[50, 60], [100, 230], [400, 600], [100, 300]]), [[129.167, 329.167], [119.565, 319.565], [121.854, 304.305]], '<code>clip([[250, 200], [100, 450], [130, 250]], [[50, 60], [100, 230], [400, 600], [100, 300]])</code> should return <code>[[129.167, 329.167], [119.565, 319.565], [121.854, 304.305]]</code>.');
|
testString: assert.deepEqual(clip([[250, 200], [100, 450], [130, 250]], [[50, 60], [100, 230], [400, 600], [100, 300]]), [[129.167, 329.167], [119.565, 319.565], [121.854, 304.305]]);
|
||||||
```
|
```
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Challenge Seed
|
## Challenge Seed
|
||||||
|
|
||||||
<section id='challengeSeed'>
|
<section id='challengeSeed'>
|
||||||
|
|
||||||
<div id='js-seed'>
|
<div id='js-seed'>
|
||||||
@ -56,13 +64,16 @@ function clip(subjectPolygon, clipPolygon) {
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Solution
|
## Solution
|
||||||
|
|
||||||
<section id='solution'>
|
<section id='solution'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function clip(subjectPolygon, clipPolygon) {
|
function clip(subjectPolygon, clipPolygon) {
|
||||||
var cp1, cp2, s, e, i, j;
|
var cp1, cp2, s, e, i, j;
|
||||||
var inside = function(p) {
|
var inside = function(p) {
|
||||||
return (cp2[0] - cp1[0]) * (p[1] - cp1[1]) > (cp2[1] - cp1[1]) * (p[0] - cp1[0]);
|
return (
|
||||||
|
(cp2[0] - cp1[0]) * (p[1] - cp1[1]) > (cp2[1] - cp1[1]) * (p[0] - cp1[0])
|
||||||
|
);
|
||||||
};
|
};
|
||||||
var intersection = function() {
|
var intersection = function() {
|
||||||
var dc = [cp1[0] - cp2[0], cp1[1] - cp2[1]],
|
var dc = [cp1[0] - cp2[0], cp1[1] - cp2[1]],
|
||||||
|
@ -6,43 +6,49 @@ forumTopicId: 16086
|
|||||||
---
|
---
|
||||||
|
|
||||||
## Description
|
## Description
|
||||||
|
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
|
|
||||||
Given two <a href="https://rosettacode.org/wiki/set" target="_blank">set</a>s <i>A</i> and <i>B</i>, compute $(A \setminus B) \cup (B \setminus A).$
|
Given two <a href="https://rosettacode.org/wiki/set" target="_blank">set</a>s <i>A</i> and <i>B</i>, compute $(A \setminus B) \cup (B \setminus A).$
|
||||||
That is, enumerate the items that are in <i>A</i> or <i>B</i> but not both. This set is called the <a href="https://en.wikipedia.org/wiki/Symmetric difference" target="_blank">symmetric difference</a> of <i>A</i> and <i>B</i>.
|
That is, enumerate the items that are in <i>A</i> or <i>B</i> but not both. This set is called the <a href="https://en.wikipedia.org/wiki/Symmetric difference" target="_blank">symmetric difference</a> of <i>A</i> and <i>B</i>.
|
||||||
In other words: $(A \cup B) \setminus (A \cap B)$ (the set of items that are in at least one of <i>A</i> or <i>B</i> minus the set of items that are in both <i>A</i> and <i>B</i>).
|
In other words: $(A \cup B) \setminus (A \cap B)$ (the set of items that are in at least one of <i>A</i> or <i>B</i> minus the set of items that are in both <i>A</i> and <i>B</i>).
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
|
|
||||||
<section id='instructions'>
|
<section id='instructions'>
|
||||||
|
|
||||||
Write a function that takes two arrays as parameters and returns the symmetric difference. Sort the resultant array before returning it.
|
Write a function that takes two arrays as parameters and returns the symmetric difference. Sort the resultant array before returning it.
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
|
|
||||||
<section id='tests'>
|
<section id='tests'>
|
||||||
|
|
||||||
``` yml
|
```yml
|
||||||
tests:
|
tests:
|
||||||
- text: <code>symmetricDifference</code> should be a function.
|
- text: <code>symmetricDifference</code> should be a function.
|
||||||
testString: assert(typeof symmetricDifference == 'function', '<code>symmetricDifference</code> should be a function.');
|
testString: assert(typeof symmetricDifference == 'function');
|
||||||
- text: <code>symmetricDifference(["John", "Bob", "Mary", "Serena"], ["Jim", "Mary", "John", "Bob"])</code> should return an array.
|
- text: <code>symmetricDifference(["John", "Bob", "Mary", "Serena"], ["Jim", "Mary", "John", "Bob"])</code> should return an array.
|
||||||
testString: assert(Array.isArray(symmetricDifference(["John", "Bob", "Mary", "Serena"], ["Jim", "Mary", "John", "Bob"])), '<code>symmetricDifference(["John", "Bob", "Mary", "Serena"], ["Jim", "Mary", "John", "Bob"])</code> should return an array.');
|
testString: assert(Array.isArray(symmetricDifference(["John", "Bob", "Mary", "Serena"], ["Jim", "Mary", "John", "Bob"])));
|
||||||
- text: <code>symmetricDifference(["John", "Bob", "Mary", "Serena"], ["Jim", "Mary", "John", "Bob"])</code> should return <code>["Jim", "Serena"]</code>.
|
- text: <code>symmetricDifference(["John", "Bob", "Mary", "Serena"], ["Jim", "Mary", "John", "Bob"])</code> should return <code>["Jim", "Serena"]</code>.
|
||||||
testString: assert.deepEqual(symmetricDifference(["John", "Bob", "Mary", "Serena"], ["Jim", "Mary", "John", "Bob"]), ["Jim", "Serena"], '<code>symmetricDifference(["John", "Bob", "Mary", "Serena"], ["Jim", "Mary", "John", "Bob"])</code> should return <code>["Jim", "Serena"]</code>.');
|
testString: assert.deepEqual(symmetricDifference(["John", "Bob", "Mary", "Serena"], ["Jim", "Mary", "John", "Bob"]), ["Jim", "Serena"]);
|
||||||
- text: <code>symmetricDifference([1, 2, 3], [3, 4])</code> should return <code>[1, 2, 4]</code>.
|
- text: <code>symmetricDifference([1, 2, 3], [3, 4])</code> should return <code>[1, 2, 4]</code>.
|
||||||
testString: assert.deepEqual(symmetricDifference([1, 2, 3], [3, 4]), [1, 2, 4], '<code>symmetricDifference([1, 2, 3], [3, 4])</code> should return <code>[1, 2, 4]</code>.');
|
testString: assert.deepEqual(symmetricDifference([1, 2, 3], [3, 4]), [1, 2, 4]);
|
||||||
- text: <code>symmetricDifference([1, 2, 3, 4, 5], [3, 4, 8, 7])</code> should return <code>[1, 2, 5, 7, 8]</code>.
|
- text: <code>symmetricDifference([1, 2, 3, 4, 5], [3, 4, 8, 7])</code> should return <code>[1, 2, 5, 7, 8]</code>.
|
||||||
testString: assert.deepEqual(symmetricDifference([1, 2, 3, 4, 5], [3, 4, 8, 7]), [1, 2, 5, 7, 8], '<code>symmetricDifference([1, 2, 3, 4, 5], [3, 4, 8, 7])</code> should return <code>[1, 2, 5, 7, 8]</code>.');
|
testString: assert.deepEqual(symmetricDifference([1, 2, 3, 4, 5], [3, 4, 8, 7]), [1, 2, 5, 7, 8]);
|
||||||
- text: <code>symmetricDifference([1, 2, 3, 4, 5, 6, 7, 8], [1, 3, 5, 6, 7, 8, 9])</code> should return <code>[2, 4, 9]</code>.
|
- text: <code>symmetricDifference([1, 2, 3, 4, 5, 6, 7, 8], [1, 3, 5, 6, 7, 8, 9])</code> should return <code>[2, 4, 9]</code>.
|
||||||
testString: assert.deepEqual(symmetricDifference([1, 2, 3, 4, 5, 6, 7, 8], [1, 3, 5, 6, 7, 8, 9]), [2, 4, 9], '<code>symmetricDifference([1, 2, 3, 4, 5, 6, 7, 8], [1, 3, 5, 6, 7, 8, 9])</code> should return <code>[2, 4, 9]</code>.');
|
testString: assert.deepEqual(symmetricDifference([1, 2, 3, 4, 5, 6, 7, 8], [1, 3, 5, 6, 7, 8, 9]), [2, 4, 9]);
|
||||||
- text: <code>symmetricDifference([1, 2, 4, 7, 9], [2, 3, 7, 8, 9])</code> should return <code>[1, 3, 4, 8]</code>.
|
- text: <code>symmetricDifference([1, 2, 4, 7, 9], [2, 3, 7, 8, 9])</code> should return <code>[1, 3, 4, 8]</code>.
|
||||||
testString: assert.deepEqual(symmetricDifference([1, 2, 4, 7, 9], [2, 3, 7, 8, 9]), [1, 3, 4, 8], '<code>symmetricDifference([1, 2, 4, 7, 9], [2, 3, 7, 8, 9])</code> should return <code>[1, 3, 4, 8]</code>.');
|
testString: assert.deepEqual(symmetricDifference([1, 2, 4, 7, 9], [2, 3, 7, 8, 9]), [1, 3, 4, 8]);
|
||||||
```
|
```
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Challenge Seed
|
## Challenge Seed
|
||||||
|
|
||||||
<section id='challengeSeed'>
|
<section id='challengeSeed'>
|
||||||
|
|
||||||
<div id='js-seed'>
|
<div id='js-seed'>
|
||||||
@ -57,28 +63,29 @@ function symmetricDifference(A, B) {
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Solution
|
## Solution
|
||||||
|
|
||||||
<section id='solution'>
|
<section id='solution'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function symmetricDifference(A, B) {
|
function symmetricDifference(A, B) {
|
||||||
function relative_complement(A, B) {
|
function relative_complement(A, B) {
|
||||||
return A.filter(function(elem) {
|
return A.filter(function(elem) {
|
||||||
return B.indexOf(elem) == -1
|
return B.indexOf(elem) == -1;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function unique(ary) {
|
function unique(ary) {
|
||||||
var u = ary.concat().sort();
|
var u = ary.concat().sort();
|
||||||
for (var i = 1; i < u.length;) {
|
for (var i = 1; i < u.length; ) {
|
||||||
if (u[i - 1] === u[i])
|
if (u[i - 1] === u[i]) u.splice(i, 1);
|
||||||
u.splice(i, 1);
|
else i++;
|
||||||
else
|
|
||||||
i++;
|
|
||||||
}
|
}
|
||||||
return u;
|
return u;
|
||||||
}
|
}
|
||||||
|
|
||||||
return unique(relative_complement(A, B).concat(relative_complement(B, A))).sort();
|
return unique(
|
||||||
|
relative_complement(A, B).concat(relative_complement(B, A))
|
||||||
|
).sort();
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user