fix: move around new CIP challenges
Move newly added rosetta code challenges
This commit is contained in:
@ -0,0 +1,87 @@
|
||||
---
|
||||
title: FizzBuzz
|
||||
id: 5e9ddb06ec35240f39657419
|
||||
challengeType: 5
|
||||
isHidden: false
|
||||
forumTopicId: 385370
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
|
||||
Write a program that generates an array of integers from 1 to 100 (inclusive). But:
|
||||
<ul>
|
||||
<li>for multiples of 3, add <code>"Fizz"</code> to the array instead of the number</li>
|
||||
<li>for multiples of 5, add <code>"Buzz"</code> to the array instead of the number</li>
|
||||
<li>for multiples of 3 and 5, add <code>"FizzBuzz"</code> to the array instead of the number</li>
|
||||
</ul>
|
||||
|
||||
</section>
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
Your program should return an array containing the results based on the rules above.
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>fizzBuzz</code> should be a function.
|
||||
testString: assert(typeof fizzBuzz=='function');
|
||||
- text: <code>fizzBuzz()</code> should return an Array.
|
||||
testString: assert(Array.isArray(fizzBuzz())==true);
|
||||
- text: Numbers divisible by only 3 should return <code>"Fizz"</code>.
|
||||
testString: assert.equal(fizzBuzz()[2], "Fizz");
|
||||
- text: Numbers divisible by only 5 should return <code>"Buzz"</code>.
|
||||
testString: assert.equal(fizzBuzz()[99], "Buzz");
|
||||
- text: Numbers divisible by both 3 and 5 should return <code>"FizzBuzz"</code>.
|
||||
testString: assert.equal(fizzBuzz()[89], "FizzBuzz");
|
||||
- text: Numbers not divisible by either 3 or 5 should return the number itself.
|
||||
testString: assert.equal(fizzBuzz()[12], 13);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function fizzBuzz() {
|
||||
// Good luck!
|
||||
}
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
function fizzBuzz() {
|
||||
let res=[];
|
||||
for (let i =1; i < 101; i++) {
|
||||
if (i % 3 === 0 && i % 5 === 0) {
|
||||
res.push("FizzBuzz");
|
||||
}
|
||||
else if (i % 3 === 0) {
|
||||
res.push("Fizz");
|
||||
}
|
||||
else if (i % 5 === 0) {
|
||||
res.push("Buzz");
|
||||
}
|
||||
else {
|
||||
res.push(i);
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
@ -0,0 +1,130 @@
|
||||
---
|
||||
id: 5e4ce2b6ac708cc68c1df25e
|
||||
title: Last letter-first letter
|
||||
challengeType: 5
|
||||
isHidden: false
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
A certain children's game involves starting with a word in a particular category. Each participant in turn says a word, but that word must begin with the final letter of the previous word. Once a word has been given, it cannot be repeated. If an opponent cannot give a word in the category, they fall out of the game.
|
||||
For example, with "animals" as the category,
|
||||
<pre>
|
||||
Child 1: dog
|
||||
Child 2: goldfish
|
||||
Child 1: hippopotamus
|
||||
Child 2: snake
|
||||
...
|
||||
</pre>
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Write a function that takes an input array of words. The function should return an array of words where the first letter of each word is the same as the last letter of the previous word. Only use the words in the input array, and once a word is used it cannot be repeated. The words in the return array should be selected and sequenced so that that its length is maximized.
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
``` yml
|
||||
tests:
|
||||
- text: <code>findLongestChain</code> should be a function.
|
||||
testString: assert(typeof findLongestChain == 'function');
|
||||
- text: <code>findLongestChain(["certain", "each", "game", "involves", "starting", "with", "word"])</code> should return an array.
|
||||
testString: assert(Array.isArray(findLongestChain(["certain", "each", "game", "involves", "starting", "with", "word"])));
|
||||
- text: <code>findLongestChain(["certain", "each", "game", "involves", "starting", "with", "word"])</code> should return <code>["involves", "starting", "game", "each"]</code>.
|
||||
testString: assert.deepEqual(findLongestChain(["certain", "each", "game", "involves", "starting", "with", "word"]), ['involves', 'starting', 'game', 'each']);
|
||||
- text: <code>findLongestChain(["audino", "bagon", "kangaskhan", "banette", "bidoof", "braviary", "exeggcute", "yamask"])</code> should return <code>["braviary", "yamask", "kangaskhan"]</code>
|
||||
testString: assert.deepEqual(findLongestChain(["audino", "bagon", "kangaskhan", "banette", "bidoof", "braviary", "exeggcute", "yamask"]), ['braviary', 'yamask', 'kangaskhan']);
|
||||
- text: <code>findLongestChain(["harp", "poliwrath", "poochyena", "porygon2", "porygonz", "archana"])</code> should return <code>["poliwrath", "harp", "poochyena", "archana"]</code>.
|
||||
testString: assert.deepEqual(findLongestChain(["harp", "poliwrath", "poochyena", "porygon2", "porygonz", "archana"]), ['poliwrath', 'harp', 'poochyena', 'archana']);
|
||||
- text: <code>findLongestChain(["scolipede", "elephant", "zeaking", "sealeo", "silcoon", "tigers"])</code> should return <code>["scolipede", "elephant", "tigers", "sealeo"]</code>.
|
||||
testString: assert.deepEqual(findLongestChain(["scolipede", "elephant", "zeaking", "sealeo", "silcoon", "tigers"]), ['scolipede', 'elephant', 'tigers', 'sealeo']);
|
||||
- text: <code>findLongestChain(["loudred", "lumineon", "lunatone", "machamp", "magnezone", "nosepass", "petilil", "pidgeotto", "pikachu"])</code> should return <code>["machamp", "petilil", "lumineon", "nosepass"]</code>.
|
||||
testString: assert.deepEqual(findLongestChain(["loudred", "lumineon", "lunatone", "machamp", "magnezone", "nosepass", "petilil", "pidgeotto", "pikachu"]), ['machamp', 'petilil', 'lumineon', 'nosepass']);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function findLongestChain(items) {
|
||||
// Good luck!
|
||||
}
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
function findLongestChain(items) {
|
||||
function Ref(index, first_char, last_char) {
|
||||
this.index = index;
|
||||
this.first_char = first_char;
|
||||
this.last_char = last_char;
|
||||
}
|
||||
|
||||
var items_len = items.length
|
||||
var refs_len = items_len;
|
||||
var refs = []
|
||||
|
||||
// enough space for all items
|
||||
var longest_path_refs_len = 0;
|
||||
var longest_path_refs = new Array(items_len);
|
||||
|
||||
function search(curr_len) {
|
||||
if (curr_len > longest_path_refs_len) {
|
||||
longest_path_refs_len = curr_len;
|
||||
|
||||
for (var i = 0; i < curr_len; i++) {
|
||||
longest_path_refs[i] = refs[i];
|
||||
}
|
||||
}
|
||||
|
||||
// recursive search
|
||||
var last_char = refs[curr_len - 1].last_char;
|
||||
for (var i = curr_len; i < refs_len; i++)
|
||||
if (refs[i].first_char == last_char) {
|
||||
var aux = refs[curr_len];
|
||||
refs[curr_len] = refs[i];
|
||||
refs[i] = aux;
|
||||
search(curr_len + 1);
|
||||
refs[i] = refs[curr_len];
|
||||
refs[curr_len] = aux;
|
||||
}
|
||||
}
|
||||
|
||||
for (var i = 0; i < items_len; i++) {
|
||||
var itemsi_len = items[i].length;
|
||||
refs.push(new Ref(i, items[i][0], items[i][itemsi_len - 1]));
|
||||
}
|
||||
|
||||
// try each item as possible start
|
||||
for (var i = 0; i < items_len; i++) {
|
||||
var aux = refs[0];
|
||||
refs[0] = refs[i];
|
||||
refs[i] = aux;
|
||||
search(1);
|
||||
refs[i] = refs[0];
|
||||
refs[0] = aux;
|
||||
}
|
||||
|
||||
var longest_path_len = longest_path_refs_len;
|
||||
var longest_path = new Array(longest_path_len);
|
||||
|
||||
for (var i = 0; i < longest_path_len; i++)
|
||||
longest_path[i] = items[longest_path_refs[i].index];
|
||||
|
||||
return longest_path;
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
@ -0,0 +1,83 @@
|
||||
---
|
||||
id: 5e4ce2bbac708cc68c1df25f
|
||||
title: Letter frequency
|
||||
challengeType: 5
|
||||
isHidden: false
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
Given a string, calculate the frequency of each character.
|
||||
All characters should be counted. This includes lower and upper case letters, digits, whitespace, special characters, or any other distinct characters.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Write a function to count the occurrences of each character in a given string.
|
||||
The function should return a 2D array with each of the elements in the following form: <code>['char', freq]</code>. The character should be a string with a length of 1, and frequency is a number denoting the count.
|
||||
For example, given the string "ab", your function should return <code>[['a', 1], ['b', 1]]</code>.
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
``` yml
|
||||
tests:
|
||||
- text: <code>letterFrequency</code> should be a function.
|
||||
testString: assert(typeof letterFrequency == 'function');
|
||||
- text: <code>letterFrequency("Not all that Mrs. Bennet, however")</code> should return an array.
|
||||
testString: assert(Array.isArray(letterFrequency("Not all that Mrs. Bennet, however")));
|
||||
- text: <code>letterFrequency("Not all that Mrs. Bennet, however")</code> should return <code>[[" ", 5], [", ", 1], [".", 1], ["B", 1], ["M", 1], ["N", 1], ["a", 2], ["e", 4], ["h", 2], ["l", 2], ["n", 2], ["o", 2], ["r", 2], ["s", 1], ["t", 4], ["v", 1], ["w", 1]]</code>.
|
||||
testString: assert.deepEqual(letterFrequency("Not all that Mrs. Bennet, however"), [[' ', 5], [',', 1], ['.', 1], ['B', 1], ['M', 1], ['N', 1], ['a', 2], ['e', 4], ['h', 2], ['l', 2], ['n', 2], ['o', 2], ['r', 2], ['s', 1], ['t', 4], ['v', 1], ['w', 1]]);
|
||||
- text: <code>letterFrequency("daughters, could ask on the ")</code> should return <code>[[' ',5],[',',1],['a',2],['c',1],['d',2],['e',2],['g',1],['h',2],['k',1],['l',1],['n',1],['o',2],['r',1],['s',2],['t',2],['u',2]]</code>.
|
||||
testString: assert.deepEqual(letterFrequency("daughters, could ask on the "), [[' ', 5], [',', 1], ['a', 2], ['c', 1], ['d', 2], ['e', 2], ['g', 1], ['h', 2], ['k', 1], ['l', 1], ['n', 1], ['o', 2], ['r', 1], ['s', 2], ['t', 2], ['u', 2]]);
|
||||
- text: <code>letterFrequency("husband any satisfactory description")</code> should return <code>[[" ", 3], ["a", 4], ["b", 1], ["c", 2], ["d", 2], ["e", 1], ["f", 1], ["h", 1], ["i", 3], ["n", 3], ["o", 2], ["p", 1], ["r", 2], ["s", 4], ["t", 3], ["u", 1], ["y", 2]]</code>.
|
||||
testString: assert.deepEqual(letterFrequency("husband any satisfactory description"), [[' ', 3], ['a', 4], ['b', 1], ['c', 2], ['d', 2], ['e', 1], ['f', 1], ['h', 1], ['i', 3], ['n', 3], ['o', 2], ['p', 1], ['r', 2], ['s', 4], ['t', 3], ['u', 1], ['y', 2]]);
|
||||
- text: <code>letterFrequency("in various ways--with barefaced")</code> should return <code>[[" ", 3], ["-", 2], ["a", 4], ["b", 1], ["c", 1], ["d", 1], ["e", 2], ["f", 1], ["h", 1], ["i", 3], ["n", 1], ["o", 1], ["r", 2], ["s", 2], ["t", 1], ["u", 1], ["v", 1], ["w", 2], ["y", 1]]</code>.
|
||||
testString: assert.deepEqual(letterFrequency("in various ways--with barefaced"), [[' ', 3], ['-', 2], ['a', 4], ['b', 1], ['c', 1], ['d', 1], ['e', 2], ['f', 1], ['h', 1], ['i', 3], ['n', 1], ['o', 1], ['r', 2], ['s', 2], ['t', 1], ['u', 1], ['v', 1], ['w', 2], ['y', 1]]);
|
||||
- text: <code>letterFrequency("distant surmises; but he eluded")</code> should return <code>[[" ", 4], ["; ", 1], ["a", 1], ["b", 1], ["d", 3], ["e", 4], ["h", 1], ["i", 2], ["l", 1], ["m", 1], ["n", 1], ["r", 1], ["s", 4], ["t", 3], ["u", 3]]</code>.
|
||||
testString: assert.deepEqual(letterFrequency("distant surmises; but he eluded"), [[' ', 4], [';', 1], ['a', 1], ['b', 1], ['d', 3], ['e', 4], ['h', 1], ['i', 2], ['l', 1], ['m', 1], ['n', 1], ['r', 1], ['s', 4], ['t', 3], ['u', 3]]);
|
||||
- text: <code>letterFrequency("last obliged to accept the second-hand,")</code> should return <code>[[" ", 5], [", ", 1], ["-", 1], ["a", 3], ["b", 1], ["c", 3], ["d", 3], ["e", 4], ["g", 1], ["h", 2], ["i", 1], ["l", 2], ["n", 2], ["o", 3], ["p", 1], ["s", 2], ["t", 4]]</code>.
|
||||
testString: assert.deepEqual(letterFrequency("last obliged to accept the second-hand,"), [[' ', 5], [',', 1], ['-', 1], ['a', 3], ['b', 1], ['c', 3], ['d', 3], ['e', 4], ['g', 1], ['h', 2], ['i', 1], ['l', 2], ['n', 2], ['o', 3], ['p', 1], ['s', 2], ['t', 4]]);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function letterFrequency(txt) {
|
||||
// Good luck!
|
||||
}
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
function letterFrequency(txt) {
|
||||
var cs = txt.split(''),
|
||||
i = cs.length,
|
||||
dct = {},
|
||||
c = '',
|
||||
keys;
|
||||
|
||||
while (i--) {
|
||||
c = cs[i];
|
||||
dct[c] = (dct[c] || 0) + 1;
|
||||
}
|
||||
|
||||
keys = Object.keys(dct);
|
||||
keys.sort();
|
||||
return keys.map(function (c) { return [c, dct[c]]; });
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
@ -0,0 +1,84 @@
|
||||
---
|
||||
id: 5e4ce2eaac708cc68c1df260
|
||||
title: Levenshtein distance
|
||||
challengeType: 5
|
||||
isHidden: false
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
In information theory and computer science, the <strong>Levenshtein distance</strong> is a <a href="https://en.wikipedia.org/wiki/string metric">metric</a> for measuring the amount of difference between two sequences (i.e. an <a href="https://en.wikipedia.org/wiki/edit distance">edit distance</a>). The Levenshtein distance between two strings is defined as the minimum number of edits needed to transform one string into the other, with the allowable edit operations being insertion, deletion, or substitution of a single character.
|
||||
Example:
|
||||
The Levenshtein distance between "<strong>kitten</strong>" and "<strong>sitting</strong>" is 3, since the following three edits change one into the other, and there isn't a way to do it with fewer than three edits:
|
||||
<ul>
|
||||
<li><strong>k</strong>itten <strong>s</strong>itten (substitution of 'k' with 's')</strong></li>
|
||||
<li>sitt<strong>e</strong>n sitt<strong>i</strong>n (substitution of 'e' with 'i')</strong></li>
|
||||
<li>sittin sittin<strong>g</strong> (insert 'g' at the end).</strong></li>
|
||||
</ul>
|
||||
<i>The Levenshtein distance between "<strong>rosettacode</strong>", "<strong>raisethysword</strong>" is <strong>8</strong>.</i>
|
||||
<i>The distance between two strings is same as that when both strings are reversed.</i>
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Write a function that returns the Levenshtein distance between two strings given as parameters.
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
``` yml
|
||||
tests:
|
||||
- text: <code>levenshtein</code> should be a function.
|
||||
testString: assert(typeof levenshtein == 'function');
|
||||
- text: <code>levenshtein("mist", "dist")</code> should return a number.
|
||||
testString: assert(typeof levenshtein("mist", "dist") == 'number');
|
||||
- text: <code>levenshtein("mist", "dist")</code> should return <code>1</code>.
|
||||
testString: assert.equal(levenshtein("mist", "dist"), 1);
|
||||
- text: <code>levenshtein("tier", "tor")</code> should return <code>2</code>.
|
||||
testString: assert.equal(levenshtein("tier", "tor"), 2);
|
||||
- text: <code>levenshtein("kitten", "sitting")</code> should return <code>3</code>.
|
||||
testString: assert.equal(levenshtein("kitten", "sitting"), 3);
|
||||
- text: <code>levenshtein("stop", "tops")</code> should return <code>2</code>.
|
||||
testString: assert.equal(levenshtein("stop", "tops"), 2);
|
||||
- text: <code>levenshtein("rosettacode", "raisethysword")</code> should return <code>8</code>.
|
||||
testString: assert.equal(levenshtein("rosettacode", "raisethysword"), 8);
|
||||
- text: <code>levenshtein("mississippi", "swiss miss")</code> should return <code>8</code>.
|
||||
testString: assert.equal(levenshtein("mississippi", "swiss miss"), 8);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function levenshtein(a, b) {
|
||||
// Good luck!
|
||||
}
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
function levenshtein(a, b) {
|
||||
var t = [], u, i, j, m = a.length, n = b.length;
|
||||
if (!m) { return n; }
|
||||
if (!n) { return m; }
|
||||
for (j = 0; j <= n; j++) { t[j] = j; }
|
||||
for (i = 1; i <= m; i++) {
|
||||
for (u = [i], j = 1; j <= n; j++) {
|
||||
u[j] = a[i - 1] === b[j - 1] ? t[j - 1] : Math.min(t[j - 1], t[j], u[j - 1]) + 1;
|
||||
} t = u;
|
||||
} return u[n];
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
@ -0,0 +1,77 @@
|
||||
---
|
||||
id: 5e4ce2f5ac708cc68c1df261
|
||||
title: Linear congruential generator
|
||||
challengeType: 5
|
||||
isHidden: false
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
The <a href="https://en.wikipedia.org/wiki/linear congruential generator">linear congruential generator</a> is a very simple example of a <a href="http://rosettacode.org/wiki/random number generator">random number generator</a>. All linear congruential generators use this formula:
|
||||
$$r_{n + 1} = a \times r_n + c \pmod m$$
|
||||
Where:
|
||||
<ul>
|
||||
<li>$ r_0 $ is a seed.</li>
|
||||
<li>$r_1$, $r_2$, $r_3$, ..., are the random numbers.</li>
|
||||
<li>$a$, $c$, $m$ are constants.</li>
|
||||
</ul>
|
||||
If one chooses the values of $a$, $c$ and $m$ with care, then the generator produces a uniform distribution of integers from $0$ to $m - 1$.
|
||||
LCG numbers have poor quality. $r_n$ and $r_{n + 1}$ are not independent, as true random numbers would be. Anyone who knows $r_n$ can predict $r_{n + 1}$, therefore LCG is not cryptographically secure. The LCG is still good enough for simple tasks like <a href="http://rosettacode.org/wiki/Miller-Rabin primality test">Miller-Rabin primality test</a>, or <a href="http://rosettacode.org/wiki/deal cards for FreeCell">FreeCell deals</a>. Among the benefits of the LCG, one can easily reproduce a sequence of numbers, from the same $r_0$. One can also reproduce such sequence with a different programming language, because the formula is so simple.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Write a function that takes $r_0,a,c,m,n$ as parameters and returns $r_n$.
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
``` yml
|
||||
tests:
|
||||
- text: <code>linearCongGenerator</code> should be a function.
|
||||
testString: assert(typeof linearCongGenerator == 'function');
|
||||
- text: <code>linearCongGenerator(324, 1145, 177, 2148, 3)</code> should return a number.
|
||||
testString: assert(typeof linearCongGenerator(324, 1145, 177, 2148, 3) == 'number');
|
||||
- text: <code>linearCongGenerator(324, 1145, 177, 2148, 3)</code> should return <code>855</code>.
|
||||
testString: assert.equal(linearCongGenerator(324, 1145, 177, 2148, 3), 855);
|
||||
- text: <code>linearCongGenerator(234, 11245, 145, 83648, 4)</code> should return <code>1110</code>.
|
||||
testString: assert.equal(linearCongGenerator(234, 11245, 145, 83648, 4), 1110);
|
||||
- text: <code>linearCongGenerator(85, 11, 1234, 214748, 5)</code> should return <code>62217</code>.
|
||||
testString: assert.equal(linearCongGenerator(85, 11, 1234, 214748, 5), 62217);
|
||||
- text: <code>linearCongGenerator(0, 1103515245, 12345, 2147483648, 1)</code> should return <code>12345</code>.
|
||||
testString: assert.equal(linearCongGenerator(0, 1103515245, 12345, 2147483648, 1), 12345);
|
||||
- text: <code>linearCongGenerator(0, 1103515245, 12345, 2147483648, 2)</code> should return <code>1406932606</code>.
|
||||
testString: assert.equal(linearCongGenerator(0, 1103515245, 12345, 2147483648, 2), 1406932606);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function linearCongGenerator(r0, a, c, m, n) {
|
||||
// Good luck!
|
||||
}
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
function linearCongGenerator(r0, a, c, m, n) {
|
||||
for (let i = 0; i < n; i++) {
|
||||
r0 = (a * r0 + c) % m;
|
||||
}
|
||||
return r0;
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
@ -0,0 +1,82 @@
|
||||
---
|
||||
id: 5e4ce2a1ac708cc68c1df25d
|
||||
title: Long multiplication
|
||||
challengeType: 5
|
||||
isHidden: false
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
Explicitly implement <a href="https://en.wikipedia.org/wiki/long multiplication">long multiplication</a>.
|
||||
This is one possible approach to arbitrary-precision integer algebra.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Write a function that takes two strings of large numbers as parameters. Your function should return the product of these two large numbers as a string.
|
||||
<strong>Note:</strong> In JavaScript, arithmetic operations are inaccurate with large numbers, so you will have to implement precise multiplication yourself.
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
``` yml
|
||||
tests:
|
||||
- text: <code>mult</code> should be a function.
|
||||
testString: assert(typeof mult == 'function');
|
||||
- text: <code>mult("18446744073709551616", "18446744073709551616")</code> should return a string.
|
||||
testString: assert(typeof mult("18446744073709551616", "18446744073709551616") == 'string');
|
||||
- text: <code>mult("18446744073709551616", "18446744073709551616")</code> should return <code>"340282366920938463463374607431768211456"</code>.
|
||||
testString: assert.equal(mult("18446744073709551616", "18446744073709551616"), "340282366920938463463374607431768211456");
|
||||
- text: <code>mult("31844674073709551616", "1844674407309551616")</code> should return <code>"58743055272886011737990786529368211456"</code>.
|
||||
testString: assert.equal(mult("31844674073709551616", "1844674407309551616"), "58743055272886011737990786529368211456");
|
||||
- text: <code>mult("1846744073709551616", "44844644073709551616")</code> should return <code>"82816580680737279241781007431768211456"</code>.
|
||||
testString: assert.equal(mult("1846744073709551616", "44844644073709551616"), "82816580680737279241781007431768211456");
|
||||
- text: <code>mult("1844674407370951616", "1844674407709551616")</code> should return <code>"3402823669833978308014392742590611456"</code>.
|
||||
testString: assert.equal(mult("1844674407370951616", "1844674407709551616"), "3402823669833978308014392742590611456");
|
||||
- text: <code>mult("2844674407370951616", "1844674407370955616")</code> should return <code>"5247498076580334548376218009219475456"</code>.
|
||||
testString: assert.equal(mult("2844674407370951616", "1844674407370955616"), "5247498076580334548376218009219475456");
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function mult(strNum1, strNum2) {
|
||||
// Good luck!
|
||||
}
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
function mult(strNum1, strNum2) {
|
||||
var a1 = strNum1.split("").reverse();
|
||||
var a2 = strNum2.toString().split("").reverse();
|
||||
var aResult = new Array;
|
||||
|
||||
for ( var iterNum1 = 0; iterNum1 < a1.length; iterNum1++ ) {
|
||||
for ( var iterNum2 = 0; iterNum2 < a2.length; iterNum2++ ) {
|
||||
var idxIter = iterNum1 + iterNum2; // Get the current array position.
|
||||
aResult[idxIter] = a1[iterNum1] * a2[iterNum2] + ( idxIter >= aResult.length ? 0 : aResult[idxIter] );
|
||||
|
||||
if ( aResult[idxIter] > 9 ) { // Carrying
|
||||
aResult[idxIter + 1] = Math.floor( aResult[idxIter] / 10 ) + ( idxIter + 1 >= aResult.length ? 0 : aResult[idxIter + 1] );
|
||||
aResult[idxIter] %= 10;
|
||||
}
|
||||
}
|
||||
}
|
||||
return aResult.reverse().join("");
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
@ -0,0 +1,83 @@
|
||||
---
|
||||
id: 5e6dd1278e6ca105cde40ea9
|
||||
title: Longest common subsequence
|
||||
challengeType: 5
|
||||
isHidden: false
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
The <b>longest common subsequence</b> (or <a href="http://en.wikipedia.org/wiki/Longest_common_subsequence_problem" target="_blank"><b>LCS</b></a>) of groups A and B is the longest group of elements from A and B that are common between the two groups and in the same order in each group. For example, the sequences "1234" and "1224533324" have an LCS of "1234":
|
||||
<b><u>1234</u></b>
|
||||
<b><u>12</u></b>245<b><u>3</u></b>332<b><u>4</u></b>
|
||||
For a string example, consider the sequences "thisisatest" and "testing123testing". An LCS would be "tsitest":
|
||||
<b><u>t</u></b>hi<b><u>si</u></b>sa<b><u>test</u></b>
|
||||
<b><u>t</u></b>e<b><u>s</u></b>t<b><u>i</u></b>ng123<b><u>test</u></b>ing.
|
||||
Your code only needs to deal with strings.
|
||||
For more information on this problem please see <a href="https://en.wikipedia.org/wiki/Longest_common_subsequence_problem" target="_black">Wikipedia</a>.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Write a case-sensitive function that returns the LCS of two strings. You don't need to show multiple LCS's.
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
``` yml
|
||||
tests:
|
||||
- text: <code>lcs</code> should be a function.
|
||||
testString: assert(typeof lcs == 'function');
|
||||
- text: <code>lcs("thisisatest", "testing123testing")</code> should return a string.
|
||||
testString: assert(typeof lcs("thisisatest", "testing123testing") == 'string');
|
||||
- text: <code>lcs("thisisatest", "testing123testing")</code> should return <code>"tsitest"</code>.
|
||||
testString: assert.equal(lcs("thisisatest", "testing123testing"), "tsitest");
|
||||
- text: <code>lcs("ABCDGH", "AEDFHR")</code> should return <code>"ADH"</code>.
|
||||
testString: assert.equal(lcs("ABCDGH", "AEDFHR"), "ADH");
|
||||
- text: <code>lcs("AGGTAB", "GXTXAYB")</code> should return <code>"GTAB"</code>.
|
||||
testString: assert.equal(lcs("AGGTAB", "GXTXAYB"), "GTAB");
|
||||
- text: <code>lcs("BDACDB", "BDCB")</code> should return <code>"BDCB"</code>.
|
||||
testString: assert.equal(lcs("BDACDB", "BDCB"), "BDCB");
|
||||
- text: <code>lcs("ABAZDC", "BACBAD")</code> should return <code>"ABAD"</code>.
|
||||
testString: assert.equal(lcs("ABAZDC", "BACBAD"), "ABAD");
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function lcs(a, b) {
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
function lcs(a, b) {
|
||||
var aSub = a.substr(0, a.length - 1);
|
||||
var bSub = b.substr(0, b.length - 1);
|
||||
|
||||
if (a.length === 0 || b.length === 0) {
|
||||
return '';
|
||||
} else if (a.charAt(a.length - 1) === b.charAt(b.length - 1)) {
|
||||
return lcs(aSub, bSub) + a.charAt(a.length - 1);
|
||||
} else {
|
||||
var x = lcs(a, bSub);
|
||||
var y = lcs(aSub, b);
|
||||
return (x.length > y.length) ? x : y;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
@ -0,0 +1,92 @@
|
||||
---
|
||||
id: 5e6dd139859c290b6ab80292
|
||||
title: Longest increasing subsequence
|
||||
challengeType: 5
|
||||
isHidden: false
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
The longest increasing subsequence problem is to find a subsequence of a given sequence in which the subsequence's elements are in sorted order, lowest to highest, and in which the subsequence is as long as possible. An example:
|
||||
For the following array:
|
||||
$\{3, 10, 2, 1, 20\}$
|
||||
Longest increasing sequence is:
|
||||
$\{3, 10, 20\}$
|
||||
For more information on this problem please see <a href="https://en.wikipedia.org/wiki/Longest increasing subsequence" target="_blank">Wikipedia</a>.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Write a function that takes an array of numbers as a parameter and returns the longest increasing subsequence.
|
||||
It is guaranteed that every array will have a longest increasing subsequence.
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
``` yml
|
||||
tests:
|
||||
- text: <code>findSequence</code> should be a function.
|
||||
testString: assert(typeof findSequence == 'function');
|
||||
- text: <code>findSequence([3, 10, 2, 1, 20])</code> should return a array.
|
||||
testString: assert(Array.isArray(findSequence([3, 10, 2, 1, 20])));
|
||||
- text: <code>findSequence([3, 10, 2, 1, 20])</code> should return <code>[3, 10, 20]</code>.
|
||||
testString: assert.deepEqual(findSequence([3, 10, 2, 1, 20]), [3, 10, 20]);
|
||||
- text: <code>findSequence([2, 7, 3, 5, 8])</code> should return <code>[2, 3, 5, 8]</code>.
|
||||
testString: assert.deepEqual(findSequence([2, 7, 3, 5, 8]), [2, 3, 5, 8]);
|
||||
- text: <code>findSequence([2, 6, 4, 5, 1])</code> should return <code>[2, 4, 5]</code>.
|
||||
testString: assert.deepEqual(findSequence([2, 6, 4, 5, 1]), [2, 4, 5]);
|
||||
- text: <code>findSequence([10, 22, 9, 33, 21, 50, 60, 80])</code> should return <code>[10, 22, 33, 50, 60, 80]</code>.
|
||||
testString: assert.deepEqual(findSequence([10, 22, 9, 33, 21, 50, 60, 80]), [10, 22, 33, 50, 60, 80]);
|
||||
- text: <code>findSequence([0, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15])</code> should return <code>[0, 2, 6, 9, 11, 15</code>.
|
||||
testString: assert.deepEqual(findSequence([0, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15]), [0, 2, 6, 9, 11, 15]);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function findSequence(input) {
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
function findSequence(input) {
|
||||
var len = input.length;
|
||||
var result = []
|
||||
for (var i = 0; i < len; i++) result.push(1)
|
||||
|
||||
for (var i = 0; i < len; i++)
|
||||
for (var j = i - 1; j >= 0; j--)
|
||||
if (input[i] > input[j] && result[j] >= result[i])
|
||||
result[i] = result[j] + 1;
|
||||
|
||||
var maxValue = Math.max.apply(null, result);
|
||||
var maxIndex = result.indexOf(Math.max.apply(Math, result));
|
||||
var output = [];
|
||||
output.push(input[maxIndex]);
|
||||
for (var i = maxIndex; i >= 0; i--) {
|
||||
if (maxValue == 0) break;
|
||||
if (input[maxIndex] > input[i] && result[i] == maxValue - 1) {
|
||||
output.push(input[i]);
|
||||
maxValue--;
|
||||
}
|
||||
}
|
||||
output.reverse();
|
||||
return output;
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
@ -0,0 +1,75 @@
|
||||
---
|
||||
id: 5e6dd14192286d95fc43046e
|
||||
title: Longest string challenge
|
||||
challengeType: 5
|
||||
isHidden: false
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
In this challenge, you have to find the strings that are the longest among the given strings.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Write a function that takes an array of strings and returns the strings that have a length equal to the longest length.
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
``` yml
|
||||
tests:
|
||||
- text: <code>longestString</code> should be a function.
|
||||
testString: assert(typeof longestString == 'function');
|
||||
- text: <code>longestString(["a", "bb", "ccc", "ee", "f", "ggg"])</code> should return a array.
|
||||
testString: assert(Array.isArray(longestString(["a", "bb", "ccc", "ee", "f", "ggg"])));
|
||||
- text: <code>longestString(["a", "bb", "ccc", "ee", "f", "ggg"])</code> should return <code>["ccc", "ggg"]'</code>.
|
||||
testString: assert.deepEqual(longestString(["a", "bb", "ccc", "ee", "f", "ggg"]), ["ccc", "ggg"]);
|
||||
- text: <code>longestString(["afedg", "bb", "sdccc", "efdee", "f", "geegg"])</code> should return <code>["afedg", "sdccc", "efdee", "geegg"]</code>.
|
||||
testString: assert.deepEqual(longestString(["afedg", "bb", "sdccc", "efdee", "f", "geegg"]), ["afedg", "sdccc", "efdee", "geegg"]);
|
||||
- text: <code>longestString(["a", "bhghgb", "ccc", "efde", "fssdrr", "ggg"])</code> should return <code>["bhghgb", "fssdrr"]</code>.
|
||||
testString: assert.deepEqual(longestString(["a", "bhghgb", "ccc", "efde", "fssdrr", "ggg"]), ["bhghgb", "fssdrr"]);
|
||||
- text: <code>longestString(["ahgfhg", "bdsfsb", "ccc", "ee", "f", "ggdsfg"])</code> should return <code>["ahgfhg", "bdsfsb", "ggdsfg"]</code>.
|
||||
testString: assert.deepEqual(longestString(["ahgfhg", "bdsfsb", "ccc", "ee", "f", "ggdsfg"]), ["ahgfhg", "bdsfsb", "ggdsfg"]);
|
||||
- text: <code>longestString(["a", "bbdsf", "ccc", "edfe", "gzzzgg"])</code> should return <code>["gzzzgg"]</code>.
|
||||
testString: assert.deepEqual(longestString(["a", "bbdsf", "ccc", "edfe", "gzzzgg"]), ["gzzzgg"]);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function longestString(strings) {
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
function longestString(strings) {
|
||||
var mx = 0;
|
||||
var result = []
|
||||
strings.forEach(function (e) {
|
||||
if (e.length > mx) {
|
||||
mx = e.length
|
||||
result = [e]
|
||||
} else if (e.length == mx)
|
||||
result.push(e)
|
||||
})
|
||||
|
||||
return result
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
@ -0,0 +1,76 @@
|
||||
---
|
||||
id: 5e6dd14797f5ce267c2f19d0
|
||||
title: Look-and-say sequence
|
||||
challengeType: 5
|
||||
isHidden: false
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
The <a href="https://en.wikipedia.org/wiki/Look and say sequence" target="_blank">Look and say sequence</a> is a recursively defined sequence of numbers.
|
||||
Sequence Definition
|
||||
<ul><li>Take a decimal number</li>
|
||||
<li><span>Look</span> at the number, visually grouping consecutive runs of the same digit.</li>
|
||||
<li><span>Say</span> the number, from left to right, group by group; as how many of that digit there are - followed by the digit grouped.</li></ul><span> This becomes the next number of the sequence.</span>
|
||||
<span>An example:</span>
|
||||
<ul><li>Starting with the number 1, you have <span>one</span> 1 which produces 11</li>
|
||||
<li>Starting with 11, you have <span>two</span> 1's. I.E.: 21</li>
|
||||
<li>Starting with 21, you have <span>one</span> 2, then <span>one</span> 1. I.E.: (12)(11) which becomes 1211</li>
|
||||
<li>Starting with 1211, you have <span>one</span> 1, <span>one</span> 2, then <span>two</span> 1's. I.E.: (11)(12)(21) which becomes 111221</li></ul>
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Write a function that accepts a string as a parameter, processes it, and returns the resultant string.
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
``` yml
|
||||
tests:
|
||||
- text: <code>lookAndSay</code> should be a function.
|
||||
testString: assert(typeof lookAndSay == 'function');
|
||||
- text: <code>lookAndSay("1")</code> should return a string.
|
||||
testString: assert(typeof lookAndSay("1") == 'string');
|
||||
- text: <code>lookAndSay("1")</code> should return <code>"11"</code>.
|
||||
testString: assert.equal(lookAndSay("1"), "11");
|
||||
- text: <code>lookAndSay("11")</code> should return <code>"21"</code>.
|
||||
testString: assert.equal(lookAndSay("11"), "21");
|
||||
- text: <code>lookAndSay("21")</code> should return <code>"1211"</code>.
|
||||
testString: assert.equal(lookAndSay("21"), "1211");
|
||||
- text: <code>lookAndSay("1211")</code> should return <code>"111221"</code>.
|
||||
testString: assert.equal(lookAndSay("1211"), "111221");
|
||||
- text: <code>lookAndSay("3542")</code> should return <code>"13151412"</code>.
|
||||
testString: assert.equal(lookAndSay("3542"), "13151412");
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function lookAndSay(str) {
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
function lookAndSay(str) {
|
||||
return str.replace(/(.)\1*/g, function(seq, p1) {
|
||||
return seq.length.toString() + p1;
|
||||
});
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
@ -0,0 +1,76 @@
|
||||
---
|
||||
id: 5e6dd15004c88cf00d2a78b3
|
||||
title: Loop over multiple arrays simultaneously
|
||||
challengeType: 5
|
||||
isHidden: false
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
Loop over multiple arrays and create a new array whose $i^{th}$ element is the concatenation of $i^{th}$ element of each of the given.
|
||||
For this example, if you are given this array of arrays:
|
||||
<code>[ ["a", "b", "c"], ["A", "B", "C"], [1, 2, 3] ]</code>
|
||||
the output should be:
|
||||
<code>["aA1","bB2","cC3"]</code>
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Write a function that takes an array of arrays as a parameter and returns an array of strings satisfying the given description.
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
``` yml
|
||||
tests:
|
||||
- text: <code>loopSimult</code> should be a function.
|
||||
testString: assert(typeof loopSimult == 'function');
|
||||
- text: <code>loopSimult([["a", "b", "c"], ["A", "B", "C"], [1, 2, 3]])</code> should return a array.
|
||||
testString: assert(Array.isArray(loopSimult([["a", "b", "c"], ["A", "B", "C"], [1, 2, 3]])));
|
||||
- text: <code>loopSimult([["a", "b", "c"], ["A", "B", "C"], [1, 2, 3]])</code> should return <code>["aA1", "bB2", "cC3"]</code>.
|
||||
testString: assert.deepEqual(loopSimult([["a", "b", "c"], ["A", "B", "C"], [1, 2, 3]]), ["aA1", "bB2", "cC3"]);
|
||||
- text: <code>loopSimult([["c", "b", "c"], ["4", "5", "C"], [7, 7, 3]])</code> should return <code>["c47", "b57", "cC3"]</code>.
|
||||
testString: assert.deepEqual(loopSimult([["c", "b", "c"], ["4", "5", "C"], [7, 7, 3]]), ["c47", "b57", "cC3"]);
|
||||
- text: <code>loopSimult([["a", "b", "c", "d"], ["A", "B", "C", "d"], [1, 2, 3, 4]])</code> should return <code>["aA1", "bB2", "cC3", "dd4"]</code>.
|
||||
testString: assert.deepEqual(loopSimult([["a", "b", "c", "d"], ["A", "B", "C", "d"], [1, 2, 3, 4]]), ["aA1", "bB2", "cC3", "dd4"]);
|
||||
- text: <code>loopSimult([["a", "b"], ["A", "B"], [1, 2]])</code> should return <code>["aA1", "bB2"]</code>.
|
||||
testString: assert.deepEqual(loopSimult([["a", "b"], ["A", "B"], [1, 2]]), ["aA1", "bB2"]);
|
||||
- text: <code>loopSimult([["b", "c"], ["B", "C"], [2, 3]])</code> should return <code>["bB2", "cC3"]</code>.
|
||||
testString: assert.deepEqual(loopSimult([["b", "c"], ["B", "C"], [2, 3]]), ["bB2", "cC3"]);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function loopSimult(A) {
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
function loopSimult(A) {
|
||||
var res = [], output;
|
||||
for (var i = 0; i < A[0].length; i += 1) {
|
||||
output = "";
|
||||
for (var j = 0; j < A.length; j++)
|
||||
output += A[j][i];
|
||||
res.push(output);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
@ -0,0 +1,172 @@
|
||||
---
|
||||
id: 5e6decd8ec8d7db960950d1c
|
||||
title: LU decomposition
|
||||
challengeType: 5
|
||||
isHidden: false
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
Every square matrix $A$ can be decomposed into a product of a lower triangular matrix $L$ and a upper triangular matrix $U$, as described in <a href="https://en.wikipedia.org/wiki/LU decomposition">LU decomposition</a>.
|
||||
$A = LU$
|
||||
It is a modified form of Gaussian elimination.
|
||||
While the <a href="http://rosettacode.org/wiki/Cholesky decomposition">Cholesky decomposition</a> only works for symmetric, positive definite matrices, the more general LU decomposition works for any square matrix.
|
||||
There are several algorithms for calculating $L$ and $U$.
|
||||
To derive <i>Crout's algorithm</i> for a 3x3 example, we have to solve the following system:
|
||||
\begin{align}A = \begin{pmatrix} a_{11} & a_{12} & a_{13}\\ a_{21} & a_{22} & a_{23}\\ a_{31} & a_{32} & a_{33}\\ \end{pmatrix}= \begin{pmatrix} l_{11} & 0 & 0 \\ l_{21} & l_{22} & 0 \\ l_{31} & l_{32} & l_{33}\\ \end{pmatrix} \begin{pmatrix} u_{11} & u_{12} & u_{13} \\ 0 & u_{22} & u_{23} \\ 0 & 0 & u_{33} \end{pmatrix} = LU\end{align}
|
||||
We now would have to solve 9 equations with 12 unknowns. To make the system uniquely solvable, usually the diagonal elements of $L$ are set to 1
|
||||
$l_{11}=1$
|
||||
$l_{22}=1$
|
||||
$l_{33}=1$
|
||||
so we get a solvable system of 9 unknowns and 9 equations.
|
||||
\begin{align}A = \begin{pmatrix} a_{11} & a_{12} & a_{13}\\ a_{21} & a_{22} & a_{23}\\ a_{31} & a_{32} & a_{33}\\ \end{pmatrix} = \begin{pmatrix} 1 & 0 & 0 \\ l_{21} & 1 & 0 \\ l_{31} & l_{32} & 1\\ \end{pmatrix} \begin{pmatrix} u_{11} & u_{12} & u_{13} \\ 0 & u_{22} & u_{23} \\ 0 & 0 & u_{33} \end{pmatrix} = \begin{pmatrix} u_{11} & u_{12} & u_{13} \\ u_{11}l_{21} & u_{12}l_{21}+u_{22} & u_{13}l_{21}+u_{23} \\ u_{11}l_{31} & u_{12}l_{31}+u_{22}l_{32} & u_{13}l_{31} + u_{23}l_{32}+u_{33} \end{pmatrix} = LU\end{align}
|
||||
Solving for the other $l$ and $u$, we get the following equations:
|
||||
$u_{11}=a_{11}$
|
||||
$u_{12}=a_{12}$
|
||||
$u_{13}=a_{13}$
|
||||
$u_{22}=a_{22} - u_{12}l_{21}$
|
||||
$u_{23}=a_{23} - u_{13}l_{21}$
|
||||
$u_{33}=a_{33} - (u_{13}l_{31} + u_{23}l_{32})$
|
||||
and for $l$:
|
||||
$l_{21}=\frac{1}{u_{11}} a_{21}$
|
||||
$l_{31}=\frac{1}{u_{11}} a_{31}$
|
||||
$l_{32}=\frac{1}{u_{22}} (a_{32} - u_{12}l_{31})$
|
||||
We see that there is a calculation pattern, which can be expressed as the following formulas, first for $U$
|
||||
$u_{ij} = a_{ij} - \sum_{k=1}^{i-1} u_{kj}l_{ik}$
|
||||
and then for $L$
|
||||
$l_{ij} = \frac{1}{u_{jj}} (a_{ij} - \sum_{k=1}^{j-1} u_{kj}l_{ik})$
|
||||
We see in the second formula that to get the $l_{ij}$ below the diagonal, we have to divide by the diagonal element (pivot) $u_{jj}$, so we get problems when $u_{jj}$ is either 0 or very small, which leads to numerical instability.
|
||||
The solution to this problem is <i>pivoting</i> $A$, which means rearranging the rows of $A$, prior to the $LU$ decomposition, in a way that the largest element of each column gets onto the diagonal of $A$. Rearranging the rows means to multiply $A$ by a permutation matrix $P$:
|
||||
$PA \Rightarrow A'$
|
||||
Example:
|
||||
\begin{align} \begin{pmatrix} 0 & 1 \\ 1 & 0 \end{pmatrix} \begin{pmatrix} 1 & 4 \\ 2 & 3 \end{pmatrix} \Rightarrow \begin{pmatrix} 2 & 3 \\ 1 & 4 \end{pmatrix} \end{align}
|
||||
The decomposition algorithm is then applied on the rearranged matrix so that
|
||||
$PA = LU$
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
The task is to implement a routine which will take a square nxn matrix $A$ and return a lower triangular matrix $L$, a upper triangular matrix $U$ and a permutation matrix $P$, so that the above equation is fullfilled. The returned value should be in the form <code>[L, U, P]</code>.
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
``` yml
|
||||
tests:
|
||||
- text: <code>luDecomposition</code> should be a function.
|
||||
testString: assert(typeof luDecomposition == 'function');
|
||||
- text: <code>luDecomposition([[1, 3, 5], [2, 4, 7], [1, 1, 0]])</code> should return a array.
|
||||
testString: assert(Array.isArray(luDecomposition([[1, 3, 5], [2, 4, 7], [1, 1, 0]])));
|
||||
- text: <code>luDecomposition([[1, 3, 5], [2, 4, 7], [1, 1, 0]])</code> should return <code>[[[1, 0, 0], [0.5, 1, 0], [0.5, -1, 1]], [[2, 4, 7], [0, 1, 1.5], [0, 0, -2]], [[0, 1, 0], [1, 0, 0], [0, 0, 1]]]</code>.
|
||||
testString: assert.deepEqual(luDecomposition([[1, 3, 5], [2, 4, 7], [1, 1, 0]]), [[[1, 0, 0], [0.5, 1, 0], [0.5, -1, 1]], [[2, 4, 7], [0, 1, 1.5], [0, 0, -2]], [[0, 1, 0], [1, 0, 0], [0, 0, 1]]]);
|
||||
- text: <code>luDecomposition([[11, 9, 24, 2], [1, 5, 2, 6], [3, 17, 18, 1], [2, 5, 7, 1]])</code> should return <code>[[[1, 0, 0, 0], [0.2727272727272727, 1, 0, 0], [0.09090909090909091, 0.2875, 1, 0], [0.18181818181818182, 0.23124999999999996, 0.0035971223021580693, 1]], [[11, 9, 24, 2], [0, 14.545454545454547, 11.454545454545455, 0.4545454545454546], [0, 0, -3.4749999999999996, 5.6875], [0, 0, 0, 0.510791366906476]], [[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1]]]</code>.
|
||||
testString: assert.deepEqual(luDecomposition([[11, 9, 24, 2], [1, 5, 2, 6], [3, 17, 18, 1], [2, 5, 7, 1]]), [[[1, 0, 0, 0], [0.2727272727272727, 1, 0, 0], [0.09090909090909091, 0.2875, 1, 0], [0.18181818181818182, 0.23124999999999996, 0.0035971223021580693, 1]], [[11, 9, 24, 2], [0, 14.545454545454547, 11.454545454545455, 0.4545454545454546], [0, 0, -3.4749999999999996, 5.6875], [0, 0, 0, 0.510791366906476]], [[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1]]]);
|
||||
- text: <code>luDecomposition([[1, 1, 1], [4, 3, -1], [3, 5, 3]])</code> should return <code>[[[1, 0, 0], [0.75, 1, 0], [0.25, 0.09090909090909091, 1]], [[4, 3, -1], [0, 2.75, 3.75], [0, 0, 0.9090909090909091]], [[0, 1, 0], [0, 0, 1], [1, 0, 0]]]</code>.
|
||||
testString: assert.deepEqual(luDecomposition([[1, 1, 1], [4, 3, -1], [3, 5, 3]]), [[[1, 0, 0], [0.75, 1, 0], [0.25, 0.09090909090909091, 1]], [[4, 3, -1], [0, 2.75, 3.75], [0, 0, 0.9090909090909091]], [[0, 1, 0], [0, 0, 1], [1, 0, 0]]]);
|
||||
- text: <code>luDecomposition([[1, -2, 3], [2, -5, 12], [0, 2, -10]])</code> should return <code>[[[1, 0, 0], [0, 1, 0], [0.5, 0.25, 1]], [[2, -5, 12], [0, 2, -10], [0, 0, -0.5]], [[0, 1, 0], [0, 0, 1], [1, 0, 0]]]</code>.
|
||||
testString: assert.deepEqual(luDecomposition([[1, -2, 3], [2, -5, 12], [0, 2, -10]]), [[[1, 0, 0], [0, 1, 0], [0.5, 0.25, 1]], [[2, -5, 12], [0, 2, -10], [0, 0, -0.5]], [[0, 1, 0], [0, 0, 1], [1, 0, 0]]]);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function luDecomposition(A) {
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
function luDecomposition(A) {
|
||||
|
||||
function dotProduct(a, b) {
|
||||
var sum = 0;
|
||||
for (var i = 0; i < a.length; i++)
|
||||
sum += a[i] * b[i]
|
||||
return sum;
|
||||
}
|
||||
|
||||
function matrixMul(A, B) {
|
||||
var result = new Array(A.length);
|
||||
for (var i = 0; i < A.length; i++)
|
||||
result[i] = new Array(B[0].length)
|
||||
var aux = new Array(B.length);
|
||||
|
||||
for (var j = 0; j < B[0].length; j++) {
|
||||
|
||||
for (var k = 0; k < B.length; k++)
|
||||
aux[k] = B[k][j];
|
||||
|
||||
for (var i = 0; i < A.length; i++)
|
||||
result[i][j] = dotProduct(A[i], aux);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function pivotize(m) {
|
||||
var n = m.length;
|
||||
var id = new Array(n);
|
||||
for (var i = 0; i < n; i++) {
|
||||
id[i] = new Array(n);
|
||||
id[i].fill(0)
|
||||
id[i][i] = 1;
|
||||
}
|
||||
|
||||
for (var i = 0; i < n; i++) {
|
||||
var maxm = m[i][i];
|
||||
var row = i;
|
||||
for (var j = i; j < n; j++)
|
||||
if (m[j][i] > maxm) {
|
||||
maxm = m[j][i];
|
||||
row = j;
|
||||
}
|
||||
|
||||
if (i != row) {
|
||||
var tmp = id[i];
|
||||
id[i] = id[row];
|
||||
id[row] = tmp;
|
||||
}
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
var n = A.length;
|
||||
var L = new Array(n);
|
||||
for (var i = 0; i < n; i++) { L[i] = new Array(n); L[i].fill(0) }
|
||||
var U = new Array(n);
|
||||
for (var i = 0; i < n; i++) { U[i] = new Array(n); U[i].fill(0) }
|
||||
var P = pivotize(A);
|
||||
var A2 = matrixMul(P, A);
|
||||
|
||||
for (var j = 0; j < n; j++) {
|
||||
L[j][j] = 1;
|
||||
for (var i = 0; i < j + 1; i++) {
|
||||
var s1 = 0;
|
||||
for (var k = 0; k < i; k++)
|
||||
s1 += U[k][j] * L[i][k];
|
||||
U[i][j] = A2[i][j] - s1;
|
||||
}
|
||||
for (var i = j; i < n; i++) {
|
||||
var s2 = 0;
|
||||
for (var k = 0; k < j; k++)
|
||||
s2 += U[k][j] * L[i][k];
|
||||
L[i][j] = (A2[i][j] - s2) / U[j][j];
|
||||
}
|
||||
}
|
||||
return [L, U, P];
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
@ -0,0 +1,93 @@
|
||||
---
|
||||
id: 5e6dee7749a0b85a3f1fc7d5
|
||||
title: Lucas-Lehmer test
|
||||
challengeType: 5
|
||||
isHidden: false
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
Lucas-Lehmer Test: for $p$ an odd prime, the Mersenne number $2^p-1$ is prime if and only if $2^p-1$ divides $S(p-1)$ where $S(n+1)=(S(n))^2-2$, and $S(1)=4$.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Write a function that returns whether the given Mersenne number is prime or not.
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
``` yml
|
||||
tests:
|
||||
- text: <code>lucasLehmer</code> should be a function.
|
||||
testString: assert(typeof lucasLehmer == 'function');
|
||||
- text: <code>lucasLehmer(11)</code> should return a boolean.
|
||||
testString: assert(typeof lucasLehmer(11) == 'boolean');
|
||||
- text: <code>lucasLehmer(11)</code> should return <code>false</code>.
|
||||
testString: assert.equal(lucasLehmer(11), false);
|
||||
- text: <code>lucasLehmer(15)</code> should return <code>false</code>.
|
||||
testString: assert.equal(lucasLehmer(15), false);
|
||||
- text: <code>lucasLehmer(13)</code> should return <code>true</code>.
|
||||
testString: assert.equal(lucasLehmer(13), true);
|
||||
- text: <code>lucasLehmer(17)</code> should return <code>true</code>.
|
||||
testString: assert.equal(lucasLehmer(17), true);
|
||||
- text: <code>lucasLehmer(19)</code> should return <code>true</code>.
|
||||
testString: assert.equal(lucasLehmer(19), true);
|
||||
- text: <code>lucasLehmer(21)</code> should return <code>false</code>.
|
||||
testString: assert.equal(lucasLehmer(21), false);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function lucasLehmer(p) {
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
function lucasLehmer(p) {
|
||||
function isPrime(p) {
|
||||
if (p == 2)
|
||||
return true;
|
||||
else if (p <= 1 || p % 2 == 0)
|
||||
return false;
|
||||
else {
|
||||
var to = Math.sqrt(p);
|
||||
for (var i = 3; i <= to; i += 2)
|
||||
if (p % i == 0)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
function isMersennePrime(p) {
|
||||
if (p == 2)
|
||||
return true;
|
||||
else {
|
||||
var m_p = Math.pow(2, p) - 1
|
||||
var s = 4;
|
||||
for (var i = 3; i <= p; i++)
|
||||
s = (s * s - 2) % m_p
|
||||
return s == 0;
|
||||
}
|
||||
}
|
||||
|
||||
return isPrime(p) && isMersennePrime(p)
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
@ -0,0 +1,113 @@
|
||||
---
|
||||
id: 5ea281203167d2b0bdefca00
|
||||
title: Ludic numbers
|
||||
challengeType: 5
|
||||
isHidden: false
|
||||
---
|
||||
|
||||
## Description
|
||||
|
||||
<section id='description'>
|
||||
<a href="https://oeis.org/wiki/Ludic_numbers" target="_blank">Ludic numbers</a> are related to prime numbers as they are generated by a sieve quite like the <a href="https://rosettacode.org/wiki/Sieve_of_Eratosthenes" target="_blank">Sieve of Eratosthenes</a> is used to generate prime numbers.
|
||||
The first ludic number is <span style="color:blue;font-weight:bold">1</span>.
|
||||
To generate succeeding ludic numbers create an array of increasing integers starting from <span style="color:blue;font-weight:bold">2</span>.
|
||||
<code style="margin-left: 2em;"><span style="color:blue;font-weight:bold">2</span> 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 ...</code>
|
||||
(Loop)
|
||||
<ul>
|
||||
<li>Take the first member of the resultant array as the next ludic number <span style="color:blue;font-weight:bold">2</span>.</li>
|
||||
<li>Remove every <strong>2<sup>nd</sup></strong> indexed item from the array (including the first).</li>
|
||||
<code style="margin-left: 2em;"><span style="color:blue;font-weight:bold;"><s>2</s></span> 3 <s>4</s> 5 <s>6</s> 7 <s>8</s> 9 <s>10</s> 11 <s>12</s> 13 <s>14</s> 15 <s>16</s> 17 <s>18</s> 19 <s>20</s> 21 <s>22</s> 23 <s>24</s> 25 <s>26</s> ...</code>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>(Unrolling a few loops...)</li>
|
||||
<li>Take the first member of the resultant array as the next ludic number <span style="color:blue;font-weight:bold">3</span>.</li>
|
||||
<li>Remove every <strong>3<sup>rd</sup></strong> indexed item from the array (including the first).</li>
|
||||
<code style="margin-left: 2em;"><span style="color:blue;font-weight:bold"><s>3</s></span> 5 7 <s>9</s> 11 13 <s>15</s> 17 19 <s>21</s> 23 25 <s>27</s> 29 31 <s>33</s> 35 37 <s>39</s> 41 43 <s>45</s> 47 49 <s>51</s> ...</code></span>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Take the first member of the resultant array as the next ludic number <span style="color:blue;font-weight:bold">5</span>.</li>
|
||||
<li>Remove every <strong>5<sup>th</sup></strong> indexed item from the array (including the first).</li>
|
||||
<code style="margin-left: 2em;"><span style="color:blue;font-weight:bold"><s>5</s></span> 7 11 13 17 <s>19</s> 23 25 29 31 <s>35</s> 37 41 43 47 <s>49</s> 53 55 59 61 <s>65</s> 67 71 73 77 ...</code></span>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Take the first member of the resultant array as the next ludic number <span style="color:blue;font-weight:bold">7</span>.</li>
|
||||
<li>Remove every <strong>7<sup>th</sup></strong> indexed item from the array (including the first).</li>
|
||||
<code style="margin-left: 2em;"><span style="color:blue;font-weight:bold"><s>7</s></span> 11 13 17 23 25 29 <s>31</s> 37 41 43 47 53 55 <s>59</s> 61 67 71 73 77 83 <s>85</s> 89 91 97 ...</code>
|
||||
</ul>
|
||||
<ul>
|
||||
<li><big><b> ... </b></big></li>
|
||||
<li>Take the first member of the current array as the next ludic number <span style="color:blue;font-weight:bold">L</span>.</li>
|
||||
<li>Remove every <strong>L<sup>th</sup></strong> indexed item from the array (including the first).</li>
|
||||
<li><big><b> ... </b></big></li>
|
||||
</ul>
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
|
||||
<section id='instructions'>
|
||||
Write a function that returns all the ludic numbers less than or equal to the given number.
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>ludic</code> should be a function.
|
||||
testString: assert(typeof ludic === 'function', '<code>ludic</code> should be a function.');
|
||||
- text: <code>ludic(2)</code> should return a array.
|
||||
testString: assert(Array.isArray(ludic(2)));
|
||||
- text: <code>ludic(2)</code> should return <code>[1, 2]</code>.
|
||||
testString: assert.deepEqual(ludic(2), [1, 2]);
|
||||
- text: <code>ludic(3)</code> should return <code>[1, 2, 3]</code>.
|
||||
testString: assert.deepEqual(ludic(3), [1, 2, 3]);
|
||||
- text: <code>ludic(5)</code> should return <code>[1, 2, 3, 5]</code>.
|
||||
testString: assert.deepEqual(ludic(5), [1, 2, 3, 5]);
|
||||
- text: <code>ludic(20)</code> should return <code>[1, 2, 3, 5, 7, 11, 13, 17]</code>.
|
||||
testString: assert.deepEqual(ludic(20), [1, 2, 3, 5, 7, 11, 13, 17]);
|
||||
- text: <code>ludic(26)</code> should return <code>[1, 2, 3, 5, 7, 11, 13, 17, 23, 25]</code>.
|
||||
testString: assert.deepEqual(ludic(26), [1, 2, 3, 5, 7, 11, 13, 17, 23, 25]);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function ludic(n) {
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
function ludic(n) {
|
||||
const makeArr = (s, e) => new Array(e + 1 - s).fill(s).map((e, i) => e + i);
|
||||
|
||||
const filterAtInc = (arr, n) => arr.filter((e, i) => (i + 1) % n);
|
||||
|
||||
const makeLudic = (arr, result) => {
|
||||
const iter = arr.shift();
|
||||
result.push(iter);
|
||||
return arr.length ? makeLudic(filterAtInc(arr, iter), result) : result;
|
||||
};
|
||||
|
||||
const ludicResult = makeLudic(makeArr(2, n), [1]);
|
||||
|
||||
return ludicResult;
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
@ -0,0 +1,113 @@
|
||||
---
|
||||
id: 5ea28156e79528a9ab248f27
|
||||
title: Luhn test of credit card numbers
|
||||
challengeType: 5
|
||||
isHidden: false
|
||||
---
|
||||
|
||||
## Description
|
||||
|
||||
<section id='description'>
|
||||
The <a href="https://en.wikipedia.org/wiki/Luhn algorithm" target="_blank">Luhn test</a> is used by some credit card companies to distinguish valid credit card numbers from what could be a random selection of digits.
|
||||
Those companies using credit card numbers that can be validated by the Luhn test have numbers that pass the following test:
|
||||
<ol>
|
||||
<li> Reverse the order of the digits in the number.</li>
|
||||
<li> Take the first, third, ... and every other odd digit in the reversed digits and sum them to form the partial sum s1</li>
|
||||
<li> Taking the second, fourth ... and every other even digit in the reversed digits:</li>
|
||||
<ol>
|
||||
<li>Multiply each digit by two and sum the digits if the answer is greater than nine to form partial sums for the even digits.</li>
|
||||
<li>Sum the partial sums of the even digits to form s2.</li>
|
||||
</ol>
|
||||
<li>If s1 + s2 ends in zero then the original number is in the form of a valid credit card number as verified by the Luhn test.</li>
|
||||
</ol>
|
||||
|
||||
For example, if the trial number is 49927398716:
|
||||
|
||||
```bash
|
||||
Reverse the digits:
|
||||
61789372994
|
||||
Sum the odd digits:
|
||||
6 + 7 + 9 + 7 + 9 + 4 = 42 = s1
|
||||
The even digits:
|
||||
1, 8, 3, 2, 9
|
||||
Two times each even digit:
|
||||
2, 16, 6, 4, 18
|
||||
Sum the digits of each multiplication:
|
||||
2, 7, 6, 4, 9
|
||||
Sum the last:
|
||||
2 + 7 + 6 + 4 + 9 = 28 = s2
|
||||
|
||||
s1 + s2 = 70 which ends in zero which means that 49927398716 passes the Luhn test.
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
|
||||
<section id='instructions'>
|
||||
Write a function that will validate a number with the Luhn test. Return true if it's a valid number. Otherwise, return false.
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>luhnTest</code> should be a function.
|
||||
testString: assert(typeof luhnTest === 'function');
|
||||
- text: <code>luhnTest("4111111111111111")</code> should return a boolean.
|
||||
testString: assert(typeof luhnTest("4111111111111111") === 'boolean');
|
||||
- text: <code>luhnTest("4111111111111111")</code> should return <code>true</code>.
|
||||
testString: assert.equal(luhnTest("4111111111111111"), true);
|
||||
- text: <code>luhnTest("4111111111111112")</code> should return <code>false</code>.
|
||||
testString: assert.equal(luhnTest("4111111111111112"), false);
|
||||
- text: <code>luhnTest("49927398716")</code> should return <code>true</code>.
|
||||
testString: assert.equal(luhnTest("49927398716"), true);
|
||||
- text: <code>luhnTest("49927398717")</code> should return <code>false</code>.
|
||||
testString: assert.equal(luhnTest("49927398717"), false);
|
||||
- text: <code>luhnTest("1234567812345678")</code> should return <code>false</code>.
|
||||
testString: assert.equal(luhnTest("1234567812345678"), false);
|
||||
- text: <code>luhnTest("1234567812345670")</code> should return <code>true</code>.
|
||||
testString: assert.equal(luhnTest("1234567812345670"), true);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function luhnTest(str) {
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
function luhnTest(str) {
|
||||
var luhnArr = [0, 2, 4, 6, 8, 1, 3, 5, 7, 9];
|
||||
var counter = 0;
|
||||
var incNum;
|
||||
var odd = false;
|
||||
var temp = String(str).replace(/[^\d]/g, '');
|
||||
if (temp.length == 0) return false;
|
||||
for (var i = temp.length - 1; i >= 0; --i) {
|
||||
incNum = parseInt(temp.charAt(i), 10);
|
||||
counter += (odd = !odd) ? incNum : luhnArr[incNum];
|
||||
}
|
||||
return counter % 10 == 0;
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
@ -0,0 +1,140 @@
|
||||
---
|
||||
id: 5ea2815a8640bcc6cb7dab3c
|
||||
title: Lychrel numbers
|
||||
challengeType: 5
|
||||
isHidden: false
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
<ol>
|
||||
<li>Take an integer <code>n₀</code>, greater than zero.</li>
|
||||
<li>Form the next number <code>n</code> of the series by reversing <code>n₀</code> and adding it to <code>n₀</code></li>
|
||||
<li>Stop when <code>n</code> becomes palindromic - i.e. the digits of <code>n</code> in reverse order == <code>n</code>.</li>
|
||||
</ol>
|
||||
|
||||
The above recurrence relation when applied to most starting numbers `n` = 1, 2, ... terminates in a palindrome quite quickly.
|
||||
|
||||
For example if `n₀` = 12 we get:
|
||||
|
||||
```bash
|
||||
12
|
||||
12 + 21 = 33, a palindrome!
|
||||
```
|
||||
|
||||
And if `n₀` = 55 we get:
|
||||
|
||||
```bash
|
||||
55
|
||||
55 + 55 = 110
|
||||
110 + 011 = 121, a palindrome!
|
||||
```
|
||||
|
||||
Notice that the check for a palindrome happens <i>after</i> an addition.
|
||||
|
||||
Some starting numbers seem to go on forever; the recurrence relation for 196 has been calculated for millions of repetitions forming numbers with millions of digits, without forming a palindrome. These numbers that do not end in a palindrome are called <b>Lychrel numbers</b>.
|
||||
|
||||
For the purposes of this task a Lychrel number is any starting number that does not form a palindrome within 500 (or more) iterations.
|
||||
|
||||
<strong>Seed and related Lychrel numbers:</strong>
|
||||
|
||||
Any integer produced in the sequence of a Lychrel number is also a Lychrel number.
|
||||
|
||||
In general, any sequence from one Lychrel number <i>might</i> converge to join the sequence from a prior Lychrel number candidate; for example the sequences for the numbers 196 and then 689 begin:
|
||||
|
||||
```bash
|
||||
196
|
||||
196 + 691 = 887
|
||||
887 + 788 = 1675
|
||||
1675 + 5761 = 7436
|
||||
7436 + 6347 = 13783
|
||||
13783 + 38731 = 52514
|
||||
52514 + 41525 = 94039
|
||||
...
|
||||
689
|
||||
689 + 986 = 1675
|
||||
1675 + 5761 = 7436
|
||||
...
|
||||
```
|
||||
|
||||
So we see that the sequence starting with 689 converges to, and continues with the same numbers as that for 196.
|
||||
|
||||
Because of this we can further split the Lychrel numbers into true <b>Seed</b> Lychrel number candidates, and <b>Related</b> numbers that produce no palindromes but have integers in their sequence seen as part of the sequence generated from a lower Lychrel number.
|
||||
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
|
||||
<section id='instructions'>
|
||||
Write a function that takes a number as a parameter. Return true if the number is a Lynchrel number. Otherwise, return false. Remember that the iteration limit is 500.
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>isLychrel</code> should be a function.
|
||||
testString: assert(typeof isLychrel === 'function');
|
||||
- text: <code>isLychrel(12)</code> should return a boolean.
|
||||
testString: assert(typeof isLychrel(12) === 'boolean');
|
||||
- text: <code>isLychrel(12)</code> should return <code>false</code>.
|
||||
testString: assert.equal(isLychrel(12), false);
|
||||
- text: <code>isLychrel(55)</code> should return <code>false</code>.
|
||||
testString: assert.equal(isLychrel(55), false);
|
||||
- text: <code>isLychrel(196)</code> should return <code>true</code>.
|
||||
testString: assert.equal(isLychrel(196), true);
|
||||
- text: <code>isLychrel(879)</code> should return <code>true</code>.
|
||||
testString: assert.equal(isLychrel(879), true);
|
||||
- text: <code>isLychrel(44987)</code> should return <code>false</code>.
|
||||
testString: assert.equal(isLychrel(44987), false);
|
||||
- text: <code>isLychrel(7059)</code> should return <code>true</code>.
|
||||
testString: assert.equal(isLychrel(7059), true);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function isLychrel(n) {
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
function isLychrel(n) {
|
||||
function reverse(num) {
|
||||
return parseInt(
|
||||
num
|
||||
.toString()
|
||||
.split('')
|
||||
.reverse()
|
||||
.join('')
|
||||
);
|
||||
}
|
||||
|
||||
var i;
|
||||
for (i = 0; i < 500; i++) {
|
||||
n = n + reverse(n);
|
||||
if (n == reverse(n)) break;
|
||||
}
|
||||
|
||||
return i == 500;
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
@ -0,0 +1,148 @@
|
||||
---
|
||||
id: 5ea2815e364d9a2222ea55f8
|
||||
title: LZW compression
|
||||
challengeType: 5
|
||||
isHidden: false
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
The Lempel-Ziv-Welch (LZW) algorithm provides loss-less data compression.
|
||||
You can read a complete description of it in the <a href="https://en.wikipedia.org/wiki/Lempel-Ziv-Welch" target="_blank">Wikipedia article</a> on the subject.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Write a function that takes two parameters. The first parameter is a boolean where `true` indicates compress and `false` indicates decompress. The second parameter is either a string or an array to be processed. If it is a string to be compressed, return an array of numbers. If it's an array of numbers to be decompressed, return a string.
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>LZW</code> should be a function.
|
||||
testString: assert(typeof LZW === 'function');
|
||||
- text: <code>LZW(true, "TOBEORNOTTOBEORTOBEORNOT")</code> should return a array.
|
||||
testString: assert(Array.isArray(LZW(true, "TOBEORNOTTOBEORTOBEORNOT")));
|
||||
- text: <code>LZW(false, [84, 79, 66, 69, 79, 82, 78, 79, 84, 256, 258, 260, 265, 259, 261, 263])</code> should return a string.
|
||||
testString: assert(typeof LZW(false, [84, 79, 66, 69, 79, 82, 78, 79, 84, 256, 258, 260, 265, 259, 261, 263]) === 'string');
|
||||
- text: <code>LZW(true, "TOBEORNOTTOBEORTOBEORNOT")</code> should return <code>[84, 79, 66, 69, 79, 82, 78, 79, 84, 256, 258, 260, 265, 259, 261, 263]</code>.
|
||||
testString: assert.deepEqual(LZW(true, "TOBEORNOTTOBEORTOBEORNOT"), [84, 79, 66, 69, 79, 82, 78, 79, 84, 256, 258, 260, 265, 259, 261, 263]);
|
||||
- text: <code>LZW(false, [84, 79, 66, 69, 79, 82, 78, 79, 84, 256, 258, 260, 265, 259, 261, 263])</code> should return <code>"TOBEORNOTTOBEORTOBEORNOT"</code>.
|
||||
testString: assert.equal(LZW(false, [84, 79, 66, 69, 79, 82, 78, 79, 84, 256, 258, 260, 265, 259, 261, 263]), "TOBEORNOTTOBEORTOBEORNOT");
|
||||
- text: <code>LZW(true, "0123456789")</code> should return <code>[48, 49, 50, 51, 52, 53, 54, 55, 56, 57]</code>.
|
||||
testString: assert.deepEqual(LZW(true, "0123456789"), [48, 49, 50, 51, 52, 53, 54, 55, 56, 57]);
|
||||
- text: <code>LZW(false, [48, 49, 50, 51, 52, 53, 54, 55, 56, 57])</code> should return <code>"0123456789"</code>.
|
||||
testString: assert.equal(LZW(false, [48, 49, 50, 51, 52, 53, 54, 55, 56, 57]), "0123456789");
|
||||
- text: <code>LZW(true, "BABAABAAA")</code> should return <code>[66, 65, 256, 257, 65, 260]</code>.
|
||||
testString: assert.deepEqual(LZW(true, "BABAABAAA"), [66, 65, 256, 257, 65, 260]);
|
||||
- text: <code>LZW(false, [66, 65, 256, 257, 65, 260])</code> should return <code>"BABAABAAA"</code>.
|
||||
testString: assert.equal(LZW(false, [66, 65, 256, 257, 65, 260]), "BABAABAAA");
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function LZW (compressData, input) {
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
function LZW (compressData, input) {
|
||||
function compress(uncompressed) {
|
||||
// Build the dictionary.
|
||||
var i,
|
||||
dictionary = {},
|
||||
c,
|
||||
wc,
|
||||
w = "",
|
||||
result = [],
|
||||
dictSize = 256;
|
||||
for (i = 0; i < 256; i += 1) {
|
||||
dictionary[String.fromCharCode(i)] = i;
|
||||
}
|
||||
|
||||
for (i = 0; i < uncompressed.length; i += 1) {
|
||||
c = uncompressed.charAt(i);
|
||||
wc = w + c;
|
||||
//Do not use dictionary[wc] because javascript arrays
|
||||
//will return values for array['pop'], array['push'] etc
|
||||
// if (dictionary[wc]) {
|
||||
if (dictionary.hasOwnProperty(wc)) {
|
||||
w = wc;
|
||||
} else {
|
||||
result.push(dictionary[w]);
|
||||
// Add wc to the dictionary.
|
||||
dictionary[wc] = dictSize++;
|
||||
w = String(c);
|
||||
}
|
||||
}
|
||||
|
||||
// Output the code for w.
|
||||
if (w !== "") {
|
||||
result.push(dictionary[w]);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
function decompress(compressed) {
|
||||
// Build the dictionary.
|
||||
var i,
|
||||
dictionary = [],
|
||||
w,
|
||||
result,
|
||||
k,
|
||||
entry = "",
|
||||
dictSize = 256;
|
||||
for (i = 0; i < 256; i += 1) {
|
||||
dictionary[i] = String.fromCharCode(i);
|
||||
}
|
||||
|
||||
w = String.fromCharCode(compressed[0]);
|
||||
result = w;
|
||||
for (i = 1; i < compressed.length; i += 1) {
|
||||
k = compressed[i];
|
||||
if (dictionary[k]) {
|
||||
entry = dictionary[k];
|
||||
} else {
|
||||
if (k === dictSize) {
|
||||
entry = w + w.charAt(0);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
result += entry;
|
||||
|
||||
// Add w+entry[0] to the dictionary.
|
||||
dictionary[dictSize++] = w + entry.charAt(0);
|
||||
|
||||
w = entry;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
if(compressData){
|
||||
return compress(input)
|
||||
}else{
|
||||
return decompress(input)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
@ -0,0 +1,100 @@
|
||||
---
|
||||
title: Self Describing Numbers
|
||||
id: 5eaf48389ee512d4d103684b
|
||||
challengeType: 5
|
||||
isHidden: false
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
There are several so-called "self describing" or <a target="_blank" href="https://en.wikipedia.org/wiki/Self-descriptive_number">"self-descriptive"</a> integers.
|
||||
|
||||
An integer is said to be "self-describing" if it has the property that, when digit positions are labeled 0 to N-1, the digit in each position is equal to the number of times that digit appears in the number.
|
||||
|
||||
For example, <b>2020</b> is a four-digit self describing number:
|
||||
<ul>
|
||||
<li> position 0 has value 2 and there are two 0s in the number; </li>
|
||||
<li> position 1 has value 0 and there are no 1s in the number; </li>
|
||||
<li> position 2 has value 2 and there are two 2s; </li>
|
||||
<li> position 3 has value 0 and there are zero 3s; </li>
|
||||
</ul>
|
||||
|
||||
Self-describing numbers < 100,000,000 are: 1210, 2020, 21200, 3211000, 42101000.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
Write a function that takes a positive integer as a parameter. If it is self-describing return true. Otherwise, return false.
|
||||
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>isSelfDescribing</code> should be a function.
|
||||
testString: assert(typeof isSelfDescribing=='function');
|
||||
- text: <code>isSelfDescribing()</code> should return a boolean.
|
||||
testString: assert(typeof isSelfDescribing(2020) =='boolean');
|
||||
- text: <code>isSelfDescribing(2020)</code> should return <code>true</code>.
|
||||
testString: assert.equal(isSelfDescribing(2020), true);
|
||||
- text: <code>isSelfDescribing(3021)</code> should return <code>false</code>.
|
||||
testString: assert.equal(isSelfDescribing(3021), false);
|
||||
- text: <code>isSelfDescribing(3211000)</code> should return <code>true</code>.
|
||||
testString: assert.equal(isSelfDescribing(3211000), true);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function isSelfDescribing(n) {
|
||||
// Good luck!
|
||||
}
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
|
||||
```js
|
||||
function isSelfDescribing(n) {
|
||||
let digits = String(n).split("");
|
||||
digits = digits.map(function(e) {return parseInt(e)});
|
||||
let count = digits.map((x) => {return 0})
|
||||
digits.forEach((d) =>{
|
||||
if (d >= count.length) {
|
||||
return false
|
||||
}
|
||||
count[d] += 1;
|
||||
});
|
||||
|
||||
if (digits === count) {
|
||||
return true;
|
||||
}
|
||||
if (digits.length != count.length) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (let i=0; i< digits.length; i++){
|
||||
if (digits[i] !== count[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
```
|
||||
|
||||
</section>
|
Reference in New Issue
Block a user