feat(interview-prep): Converting and Transferring Rosetta problems (#38371)
* feat(interview-prep): Converting and Transferring Rosetta problems
This commit is contained in:
		
				
					committed by
					
						
						GitHub
					
				
			
			
				
	
			
			
			
						parent
						
							5b4c89aa9a
						
					
				
				
					commit
					1204c72657
				
			@@ -380,6 +380,34 @@
 | 
			
		||||
      "5e4ce2a1ac708cc68c1df25d",
 | 
			
		||||
      "Long multiplication"
 | 
			
		||||
    ],
 | 
			
		||||
    [
 | 
			
		||||
      "5e6dd1278e6ca105cde40ea9",
 | 
			
		||||
      "Longest common subsequence"
 | 
			
		||||
    ],
 | 
			
		||||
    [
 | 
			
		||||
      "5e6dd139859c290b6ab80292",
 | 
			
		||||
      "Longest increasing subsequence"
 | 
			
		||||
    ],
 | 
			
		||||
    [
 | 
			
		||||
      "5e6dd14192286d95fc43046e",
 | 
			
		||||
      "Longest string challenge"
 | 
			
		||||
    ],
 | 
			
		||||
    [
 | 
			
		||||
      "5e6dd14797f5ce267c2f19d0",
 | 
			
		||||
      "Look-and-say sequence"
 | 
			
		||||
    ],
 | 
			
		||||
    [
 | 
			
		||||
      "5e6dd15004c88cf00d2a78b3",
 | 
			
		||||
      "Loop over multiple arrays simultaneously"
 | 
			
		||||
    ],
 | 
			
		||||
    [
 | 
			
		||||
      "5e6decd8ec8d7db960950d1c",
 | 
			
		||||
      "LU decomposition"
 | 
			
		||||
    ],
 | 
			
		||||
    [
 | 
			
		||||
      "5e6dee7749a0b85a3f1fc7d5",
 | 
			
		||||
      "Lucas-Lehmer test"
 | 
			
		||||
    ],
 | 
			
		||||
    [
 | 
			
		||||
      "59da22823d04c95919d46269",
 | 
			
		||||
      "Sailors, coconuts and a monkey problem"
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,82 @@
 | 
			
		||||
---
 | 
			
		||||
id: 5e6dd1278e6ca105cde40ea9
 | 
			
		||||
title: Longest common subsequence
 | 
			
		||||
challengeType: 5
 | 
			
		||||
---
 | 
			
		||||
 | 
			
		||||
## 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,91 @@
 | 
			
		||||
---
 | 
			
		||||
id: 5e6dd139859c290b6ab80292
 | 
			
		||||
title: Longest increasing subsequence
 | 
			
		||||
challengeType: 5
 | 
			
		||||
---
 | 
			
		||||
 | 
			
		||||
## 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,74 @@
 | 
			
		||||
---
 | 
			
		||||
id: 5e6dd14192286d95fc43046e
 | 
			
		||||
title: Longest string challenge
 | 
			
		||||
challengeType: 5
 | 
			
		||||
---
 | 
			
		||||
 | 
			
		||||
## 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,75 @@
 | 
			
		||||
---
 | 
			
		||||
id: 5e6dd14797f5ce267c2f19d0
 | 
			
		||||
title: Look-and-say sequence
 | 
			
		||||
challengeType: 5
 | 
			
		||||
---
 | 
			
		||||
 | 
			
		||||
## 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,75 @@
 | 
			
		||||
---
 | 
			
		||||
id: 5e6dd15004c88cf00d2a78b3
 | 
			
		||||
title: Loop over multiple arrays simultaneously
 | 
			
		||||
challengeType: 5
 | 
			
		||||
---
 | 
			
		||||
 | 
			
		||||
## 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,171 @@
 | 
			
		||||
---
 | 
			
		||||
id: 5e6decd8ec8d7db960950d1c
 | 
			
		||||
title: LU decomposition
 | 
			
		||||
challengeType: 5
 | 
			
		||||
---
 | 
			
		||||
 | 
			
		||||
## 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,92 @@
 | 
			
		||||
---
 | 
			
		||||
id: 5e6dee7749a0b85a3f1fc7d5
 | 
			
		||||
title: Lucas-Lehmer test
 | 
			
		||||
challengeType: 5
 | 
			
		||||
---
 | 
			
		||||
 | 
			
		||||
## 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>
 | 
			
		||||
		Reference in New Issue
	
	Block a user