feat(interview-prep): Converting and Transfering Rosetta problems (#38624)

* feat(interview-prep): Converting and Transfering Rosetta problems

* feat(interview-prep): Meta fix

* fix: adjusted descriptions and some test cases

* fix: Descriptions fixed

Co-authored-by: Kris Koishigawa <scissorsneedfoodtoo@gmail.com>
This commit is contained in:
Bhanu Pratap Singh Rathore 2020-05-05 15:22:32 +05:30 committed by GitHub
parent b42671ec41
commit 1fc55513a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 526 additions and 0 deletions

View File

@ -408,6 +408,22 @@
"5e6dee7749a0b85a3f1fc7d5",
"Lucas-Lehmer test"
],
[
"5ea281203167d2b0bdefca00",
"Ludic numbers"
],
[
"5ea28156e79528a9ab248f27",
"Luhn test of credit card numbers"
],
[
"5ea2815a8640bcc6cb7dab3c",
"Lychrel numbers"
],
[
"5ea2815e364d9a2222ea55f8",
"LZW compression"
],
[
"59da22823d04c95919d46269",
"Sailors, coconuts and a monkey problem"

View File

@ -0,0 +1,112 @@
---
id: 5ea281203167d2b0bdefca00
title: Ludic numbers
challengeType: 5
---
## 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>

View File

@ -0,0 +1,112 @@
---
id: 5ea28156e79528a9ab248f27
title: Luhn test of credit card numbers
challengeType: 5
---
## 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>

View File

@ -0,0 +1,139 @@
---
id: 5ea2815a8640bcc6cb7dab3c
title: Lychrel numbers
challengeType: 5
---
## 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>

View File

@ -0,0 +1,147 @@
---
id: 5ea2815e364d9a2222ea55f8
title: LZW compression
challengeType: 5
---
## 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>