fix(challenges): The rest of the S problems

This commit is contained in:
Kris Koishigawa 2019-03-10 19:14:48 +09:00 committed by mrugesh
parent 442c9c291d
commit 539e93d01b
13 changed files with 64 additions and 70 deletions

View File

@ -6,8 +6,7 @@ challengeType: 5
## 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 For this task, the Stern-Brocot sequence is to be generated by an algorithm similar to that employed in generating the <a href="http://rosettacode.org/wiki/Fibonacci sequence" target="_blank">Fibonacci sequence</a>.
<a href="http://rosettacode.org/wiki/Fibonacci sequence">Fibonacci sequence</a>.
<ol> <ol>
<li>The first and second members of the sequence are both 1:</li> <li>The first and second members of the sequence are both 1:</li>
<ul><li>1, 1</li></ul> <ul><li>1, 1</li></ul>
@ -31,12 +30,11 @@ For this task, the Stern-Brocot sequence is to be generated by an algorithm simi
<ul><li>1, 1, 2, 1, 3, 2</li></ul> <ul><li>1, 1, 2, 1, 3, 2</li></ul>
<li>Consider the next member of the series, (the fourth member i.e. 1)</li> <li>Consider the next member of the series, (the fourth member i.e. 1)</li>
</ol> </ol>
Create a function that returns the $ n^{th} $ member of the sequence using the method outlined above.
</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.
</section> </section>
## Tests ## Tests
@ -67,7 +65,7 @@ tests:
<div id='js-seed'> <div id='js-seed'>
```js ```js
function sternBrocot (num) { function sternBrocot(num) {
// Good luck! // Good luck!
} }
``` ```
@ -79,7 +77,7 @@ function sternBrocot (num) {
<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));
} }

View File

@ -6,7 +6,7 @@ challengeType: 5
## 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" target="_blank">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>
@ -49,10 +49,11 @@ tests:
<div id='js-seed'> <div id='js-seed'>
```js ```js
function straddle (message, alphabet) { function straddle(message, alphabet) {
// Good luck! // Good luck!
} }
function unstraddle (message, alphabet) {
function unstraddle(message, alphabet) {
// Good luck! // Good luck!
} }
``` ```
@ -64,7 +65,7 @@ function unstraddle (message, alphabet) {
<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 = ""
@ -82,7 +83,7 @@ function straddle (message, alphabet) {
} }
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("", alphabet[0].indexOf(" "), alphabet[0].lastIndexOf(" "))
var out = "" var out = ""
var n, o var n, o

View File

@ -21,8 +21,8 @@ Write a function that takes multiple sorted arrays of items, and returns one arr
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', '<code>mergeLists</code> should be a function.');
- text: <code>mergeLists([[1, 3, 5, 9, 10], [2, 4, 6, 7, 8]])</code> should return a 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 a 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.');
- 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], '<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, 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>.
@ -42,7 +42,7 @@ tests:
<div id='js-seed'> <div id='js-seed'>
```js ```js
function mergeLists (lists) { function mergeLists(lists) {
// Good luck! // Good luck!
} }
``` ```
@ -54,7 +54,7 @@ function mergeLists (lists) {
<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) {

View File

@ -6,8 +6,7 @@ challengeType: 5
## 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.
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>
@ -45,7 +44,7 @@ tests:
<div id='js-seed'> <div id='js-seed'>
```js ```js
function strip (s) { function strip(s) {
// Good luck! // Good luck!
} }
``` ```
@ -57,7 +56,7 @@ function strip (s) {
<section id='solution'> <section id='solution'>
```js ```js
function strip (s) { function strip(s) {
return s.split('').filter(function(x) { return s.split('').filter(function(x) {
var n = x.charCodeAt(0); var n = x.charCodeAt(0);

View File

@ -6,8 +6,7 @@ challengeType: 5
## Description ## Description
<section id='description'> <section id='description'>
<a href="http://rosettacode.org/wiki/eso:Subleq">Subleq</a> is an example of a <a href="https://en.wikipedia.org/wiki/One_instruction_set_computer">One-Instruction <a href="http://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>.
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
to zero. to zero.
Your task is to create an interpreter which emulates such a machine. Your task is to create an interpreter which emulates such a machine.
@ -35,7 +34,8 @@ character sets or Unicode. You may translate it into another character set if yo
non-ASCiI-compatible environment.) non-ASCiI-compatible environment.)
<pre>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 10 0</pre> <pre>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 10 0</pre>
Which corresponds to something like this in a hypothetical assembler language: Which corresponds to something like this in a hypothetical assembler language:
<pre>start: <pre>
start:
zero, message, -1 zero, message, -1
message, -1, -1 message, -1, -1
neg1, start+1, -1 neg1, start+1, -1
@ -43,14 +43,14 @@ Which corresponds to something like this in a hypothetical assembler language:
zero, zero, start zero, zero, start
zero: 0 zero: 0
neg1: -1 neg1: -1
message: "Hello, world!\n\0"</pre> message: "Hello, world!\n\0"
Write a function that takes an array of integers as a parameter. This represents the memory elements. The function </pre>
should interpret the sequence and return the output string. For this task, assume that there is no standard input.
</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
should interpret the sequence and return the output string. For this task, assume that there is no standard input.
</section> </section>
## Tests ## Tests
@ -73,7 +73,7 @@ tests:
<div id='js-seed'> <div id='js-seed'>
```js ```js
function Subleq (mem) { function Subleq(mem) {
// Good luck! // Good luck!
} }
``` ```
@ -85,7 +85,7 @@ function Subleq (mem) {
<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 {

View File

@ -6,8 +6,8 @@ challengeType: 5
## 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">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">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
@ -22,8 +22,8 @@ Write a function to solve a partially filled-in normal 9x9 <a href="https://en.w
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', '<code>solveSudoku</code> should be a 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 a 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 a 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.');
- 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]], '<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([[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>.
@ -39,7 +39,7 @@ tests:
<div id='js-seed'> <div id='js-seed'>
```js ```js
function solveSudoku (puzzle) { function solveSudoku(puzzle) {
// Good luck! // Good luck!
} }
``` ```
@ -51,7 +51,7 @@ function solveSudoku (puzzle) {
<section id='solution'> <section id='solution'>
```js ```js
function solveSudoku (puzzle) { function solveSudoku(puzzle) {
var solution; var solution;
class DoX { class DoX {

View File

@ -50,7 +50,7 @@ tests:
<div id='js-seed'> <div id='js-seed'>
```js ```js
function sumDigits (n) { function sumDigits(n) {
// Good luck! // Good luck!
} }
``` ```
@ -62,7 +62,7 @@ function sumDigits (n) {
<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) s+=parseInt(n.charAt(i),36)
return s return s

View File

@ -42,7 +42,7 @@ tests:
<div id='js-seed'> <div id='js-seed'>
```js ```js
function sumMults (n) { function sumMults(n) {
// Good luck! // Good luck!
} }
``` ```
@ -54,7 +54,7 @@ function sumMults (n) {
<section id='solution'> <section id='solution'>
```js ```js
function sumMults (n) { function sumMults(n) {
var sum = 0; var sum = 0;
for (var i = 1; i < n; i++) { for (var i = 1; i < n; i++) {
if (i % 3 == 0 || i % 5 == 0) sum += i; if (i % 3 == 0 || i % 5 == 0) sum += i;

View File

@ -6,20 +6,19 @@ challengeType: 5
## 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)">series</a>, i.e. the sum of the <b>n</b> first terms of the corresponding <a href="https://en.wikipedia.org/wiki/sequence">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:
$S_n = \sum_{k=1}^n \frac{1}{k^2}$ <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">zeta function</a> for S=2, whose exact value This approximates the <a href="https://en.wikipedia.org/wiki/Riemann zeta function">zeta function</a> for S=2, whose exact value
$\zeta(2) = {\pi^2\over 6}$ <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">Basel problem</a>. is the solution of the <a href="https://en.wikipedia.org/wiki/Basel problem">Basel problem</a>.
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>
## 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.
</section> </section>
## Tests ## Tests
@ -50,7 +49,7 @@ tests:
<div id='js-seed'> <div id='js-seed'>
```js ```js
function sum (a, b) { function sum(a, b) {
// Good luck! // Good luck!
} }
``` ```
@ -62,7 +61,7 @@ function sum (a, b) {
<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)
} }

View File

@ -42,7 +42,7 @@ tests:
<div id='js-seed'> <div id='js-seed'>
```js ```js
function sumsq (array) { function sumsq(array) {
// Good luck! // Good luck!
} }
``` ```
@ -54,7 +54,7 @@ function sumsq (array) {
<section id='solution'> <section id='solution'>
```js ```js
function sumsq (array) { function sumsq(array) {
var sum = 0; var sum = 0;
var i, iLen; var i, iLen;

View File

@ -10,12 +10,11 @@ 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>).
Example: Example:
<pre><b>123 + 4 - 5 + 67 - 89 = 100</b></pre> <pre><b>123 + 4 - 5 + 67 - 89 = 100</b></pre>
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". Note: sort the array before returning it.
</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.
</section> </section>
## Tests ## Tests
@ -25,8 +24,8 @@ Write a function that takes a number as parameter. The function should return an
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', '<code>sumTo100</code> should be a function.');
- text: <code>sumTo100(199)</code> should return a array. - text: <code>sumTo100(199)</code> should return an array.
testString: assert(Array.isArray(sumTo100(199)), '<code>sumTo100(199)</code> should return a array.'); testString: assert(Array.isArray(sumTo100(199)), '<code>sumTo100(199)</code> should return an array.');
- 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"], '<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(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>.
@ -46,7 +45,7 @@ tests:
<div id='js-seed'> <div id='js-seed'>
```js ```js
function sumTo100 (n) { function sumTo100(n) {
// Good luck! // Good luck!
} }
``` ```
@ -58,7 +57,7 @@ function sumTo100 (n) {
<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)) : [];

View File

@ -6,18 +6,17 @@ challengeType: 5
## Description ## Description
<section id='description'> <section id='description'>
The <a href="https://en.wikipedia.org/wiki/Sutherland-Hodgman clipping algorithm">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:
$[(50, 150), (200, 50), (350, 150), (350, 300), (250, 300), (200, 250), (150, 350), (100, 250), (100, 200)]$ <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:
$[(100, 100), (300, 100), (300, 300), (100, 300)]$ <pre>[(100, 100), (300, 100), (300, 300), (100, 300)]</pre>
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>
## 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.
</section> </section>
## Tests ## Tests
@ -27,8 +26,8 @@ Write a function that takes 2 arrays as parameters. The first array contains the
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', '<code>clip</code> should be a 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 a 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 a 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.');
- 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]], '<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([[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>.
@ -44,7 +43,7 @@ tests:
<div id='js-seed'> <div id='js-seed'>
```js ```js
function clip (subjectPolygon, clipPolygon) { function clip(subjectPolygon, clipPolygon) {
// Good luck! // Good luck!
} }
``` ```
@ -56,7 +55,7 @@ function clip (subjectPolygon, clipPolygon) {
<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]);

View File

@ -6,15 +6,14 @@ challengeType: 5
## Description ## Description
<section id='description'> <section id='description'>
Given two <a href="http://rosettacode.org/wiki/set">set</a>s <i>A</i> and <i>B</i>, compute $(A \setminus B) \cup (B \setminus A).$ Given two <a href="http://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">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>).
Write a function that takes two arrays as parameters and returns the symmetric difference. Note: Sort the resultant array before returning it.
</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.
</section> </section>
## Tests ## Tests
@ -24,8 +23,8 @@ Write a function that takes two arrays as parameters and returns the symmetric d
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', '<code>symmetricDifference</code> should be a function.');
- text: <code>symmetricDifference(["John", "Bob", "Mary", "Serena"], ["Jim", "Mary", "John", "Bob"])</code> should return a 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 a 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.');
- 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"], '<code>symmetricDifference(["John", "Bob", "Mary", "Serena"], ["Jim", "Mary", "John", "Bob"])</code> should return <code>["Jim", "Serena"]</code>.');
- 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>.
@ -45,7 +44,7 @@ tests:
<div id='js-seed'> <div id='js-seed'>
```js ```js
function symmetricDifference (A, B) { function symmetricDifference(A, B) {
// Good luck! // Good luck!
} }
``` ```
@ -57,7 +56,7 @@ function symmetricDifference (A, B) {
<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