feat(interview-prep): Converting and transferring Rosetta problems
This commit is contained in:
committed by
Kristofer Koishigawa
parent
fde8c05a20
commit
e4a6f5563f
@ -0,0 +1,129 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc7edd
|
||||
title: Last letter-first letter
|
||||
challengeType: 5
|
||||
---
|
||||
|
||||
## 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. The first word in the return array can be anything. Use only 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 the length of the array is maximized.
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
``` yml
|
||||
tests:
|
||||
- text: <code>findLongestChain</code> should be a function.
|
||||
testString: assert(typeof findLongestChain == 'function', '<code>findLongestChain</code> should be a 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"])), '<code>findLongestChain(["certain", "each", "game", "involves", "starting", "with", "word"])</code> should return an array.');
|
||||
- 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'], '<code>findLongestChain(["certain", "each", "game", "involves", "starting", "with", "word"])</code> should return <code>["involves", "starting", "game", "each"]</code>.');
|
||||
- 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'], '<code>findLongestChain(["audino", "bagon", "kangaskhan", "banette", "bidoof", "braviary", "exeggcute", "yamask"])</code> should return <code>["braviary", "yamask", "kangaskhan"]</code>.');
|
||||
- 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'], '<code>findLongestChain(["harp", "poliwrath", "poochyena", "porygon2", "porygonz", "archana"])</code> should return <code>["poliwrath", "harp", "poochyena", "archana"]</code>.');
|
||||
- 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'], '<code>findLongestChain(["scolipede", "elephant", "zeaking", "sealeo", "silcoon", "tigers"])</code> should return <code>["scolipede", "elephant", "tigers", "sealeo"]</code>.');
|
||||
- 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'], '<code>findLongestChain(["loudred", "lumineon", "lunatone", "machamp", "magnezone", "nosepass", "petilil", "pidgeotto", "pikachu"])</code> should return <code>["machamp", "petilil", "lumineon", "nosepass"]</code>.');
|
||||
```
|
||||
|
||||
</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,82 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc7ee1
|
||||
title: Letter frequency
|
||||
challengeType: 5
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
You are given a string and your task is to calculate 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', '<code>letterFrequency</code> should be a 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")), '<code>letterFrequency("Not all that Mrs. Bennet, however")</code> should return an array.');
|
||||
- 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]], '<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>.');
|
||||
- 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]], '<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>.');
|
||||
- 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]], '<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>.');
|
||||
- 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]], '<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>.');
|
||||
- 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]], '<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>.');
|
||||
- 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]], '<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>.');
|
||||
```
|
||||
|
||||
</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,81 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc7ee2
|
||||
title: Levenshtein distance
|
||||
challengeType: 5
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
In information theory and computer science, the <b>Levenshtein distance</b> 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 "<b>kitten</b>" and "<b>sitting</b>" 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:
|
||||
<b>k</b>itten <b>s</b>itten (substitution of 'k' with 's')</b>
|
||||
sitt<b>e</b>n sitt<b>i</b>n (substitution of 'e' with 'i')</b>
|
||||
sittin sittin<b>g</b> (insert 'g' at the end).</b>
|
||||
''The Levenshtein distance between "<b>rosettacode</b>", "<b>raisethysword</b>" is <b>8</b>.
|
||||
<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', '<code>levenshtein</code> should be a function.');
|
||||
- text: <code>levenshtein("mist", "dist")</code> should return a number.
|
||||
testString: assert(typeof levenshtein("mist", "dist") == 'number', '<code>levenshtein("mist", "dist")</code> should return a number.');
|
||||
- text: <code>levenshtein("mist", "dist")</code> should return <code>1</code>.
|
||||
testString: assert.equal(levenshtein("mist", "dist"), 1, '<code>levenshtein("mist", "dist")</code> should return <code>1</code>.');
|
||||
- text: <code>levenshtein("tier", "tor")</code> should return <code>2</code>.
|
||||
testString: assert.equal(levenshtein("tier", "tor"), 2, '<code>levenshtein("tier", "tor")</code> should return <code>2</code>.');
|
||||
- text: <code>levenshtein("kitten", "sitting")</code> should return <code>3</code>.
|
||||
testString: assert.equal(levenshtein("kitten", "sitting"), 3, '<code>levenshtein("kitten", "sitting")</code> should return <code>3</code>.');
|
||||
- text: <code>levenshtein("stop", "tops")</code> should return <code>2</code>.
|
||||
testString: assert.equal(levenshtein("stop", "tops"), 2, '<code>levenshtein("stop", "tops")</code> should return <code>2</code>.');
|
||||
- text: <code>levenshtein("rosettacode", "raisethysword")</code> should return <code>8</code>.
|
||||
testString: assert.equal(levenshtein("rosettacode", "raisethysword"), 8, '<code>levenshtein("rosettacode", "raisethysword")</code> should return <code>8</code>.');
|
||||
- text: <code>levenshtein("mississippi", "swiss miss")</code> should return <code>8</code>.
|
||||
testString: assert.equal(levenshtein("mississippi", "swiss miss"), 8, '<code>levenshtein("mississippi", "swiss miss")</code> should return <code>8</code>.');
|
||||
```
|
||||
|
||||
</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,76 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc7ee3
|
||||
title: Linear congruential generator
|
||||
challengeType: 5
|
||||
---
|
||||
|
||||
## 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', '<code>linearCongGenerator</code> should be a function.');
|
||||
- text: <code>linearCongGenerator(324, 1145, 177, 2148, 3)</code> should return a number.
|
||||
testString: assert(typeof linearCongGenerator(324, 1145, 177, 2148, 3) == 'number', '<code>linearCongGenerator(324, 1145, 177, 2148, 3)</code> should return a 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, '<code>linearCongGenerator(324, 1145, 177, 2148, 3)</code> should return <code>855</code>.');
|
||||
- text: <code>linearCongGenerator(234, 11245, 145, 83648, 4)</code> should return <code>1110</code>.
|
||||
testString: assert.equal(linearCongGenerator(234, 11245, 145, 83648, 4), 1110, '<code>linearCongGenerator(234, 11245, 145, 83648, 4)</code> should return <code>1110</code>.');
|
||||
- text: <code>linearCongGenerator(85, 11, 1234, 214748, 5)</code> should return <code>62217</code>.
|
||||
testString: assert.equal(linearCongGenerator(85, 11, 1234, 214748, 5), 62217, '<code>linearCongGenerator(85, 11, 1234, 214748, 5)</code> should return <code>62217</code>.');
|
||||
- text: <code>linearCongGenerator(0, 1103515245, 12345, 2147483648, 1)</code> should return <code>12345</code>.
|
||||
testString: assert.equal(linearCongGenerator(0, 1103515245, 12345, 2147483648, 1), 12345, '<code>linearCongGenerator(0, 1103515245, 12345, 2147483648, 1)</code> should return <code>12345</code>.');
|
||||
- text: <code>linearCongGenerator(0, 1103515245, 12345, 2147483648, 2)</code> should return <code>1406932606</code>.
|
||||
testString: assert.equal(linearCongGenerator(0, 1103515245, 12345, 2147483648, 2), 1406932606, '<code>linearCongGenerator(0, 1103515245, 12345, 2147483648, 2)</code> should return <code>1406932606</code>.');
|
||||
```
|
||||
|
||||
</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,81 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc7eec
|
||||
title: Long multiplication
|
||||
challengeType: 5
|
||||
---
|
||||
|
||||
## 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.
|
||||
Note: In JavaScript, arithmetic operations are inaccurate with large numbers, so you will have to implement precise multiplication yourself.
|
||||
</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.
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
``` yml
|
||||
tests:
|
||||
- text: <code>mult</code> should be a function.
|
||||
testString: assert(typeof mult == 'function', '<code>mult</code> should be a function.');
|
||||
- text: <code>mult("18446744073709551616", "18446744073709551616")</code> should return a string.
|
||||
testString: assert(typeof mult("18446744073709551616", "18446744073709551616") == 'string', '<code>mult("18446744073709551616", "18446744073709551616")</code> should return a string.');
|
||||
- text: <code>mult("18446744073709551616", "18446744073709551616")</code> should return <code>"340282366920938463463374607431768211456"</code>.
|
||||
testString: assert.equal(mult("18446744073709551616", "18446744073709551616"), "340282366920938463463374607431768211456", '<code>mult("18446744073709551616", "18446744073709551616")</code> should return <code>"340282366920938463463374607431768211456"</code>.');
|
||||
- text: <code>mult("31844674073709551616", "1844674407309551616")</code> should return <code>"58743055272886011737990786529368211456"</code>.
|
||||
testString: assert.equal(mult("31844674073709551616", "1844674407309551616"), "58743055272886011737990786529368211456", '<code>mult("31844674073709551616", "1844674407309551616")</code> should return <code>"58743055272886011737990786529368211456"</code>.');
|
||||
- text: <code>mult("1846744073709551616", "44844644073709551616")</code> should return <code>"82816580680737279241781007431768211456"</code>.
|
||||
testString: assert.equal(mult("1846744073709551616", "44844644073709551616"), "82816580680737279241781007431768211456", '<code>mult("1846744073709551616", "44844644073709551616")</code> should return <code>"82816580680737279241781007431768211456"</code>.');
|
||||
- text: <code>mult("1844674407370951616", "1844674407709551616")</code> should return <code>"3402823669833978308014392742590611456"</code>.
|
||||
testString: assert.equal(mult("1844674407370951616", "1844674407709551616"), "3402823669833978308014392742590611456", '<code>mult("1844674407370951616", "1844674407709551616")</code> should return <code>"3402823669833978308014392742590611456"</code>.');
|
||||
- text: <code>mult("2844674407370951616", "1844674407370955616")</code> should return <code>"5247498076580334548376218009219475456"</code>.
|
||||
testString: assert.equal(mult("2844674407370951616", "1844674407370955616"), "5247498076580334548376218009219475456", '<code>mult("2844674407370951616", "1844674407370955616")</code> should return <code>"5247498076580334548376218009219475456"</code>.');
|
||||
```
|
||||
|
||||
</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>
|
Reference in New Issue
Block a user