feat(interview-prep): Porting Rosetta problems (#17480)

* feat(interview-prep): Porting Rosetta problems

* Changes done

* Update rosetta-code.json

* Update rosetta-code.json
This commit is contained in:
Bhanu Pratap Singh Rathore
2018-06-11 05:33:55 +05:30
committed by Kristofer Koishigawa
parent 8f4e8bb3dd
commit 5ba9c1d5c0

View File

@ -3954,6 +3954,175 @@
}
}
},
{
"title": "Generator/Exponential",
"description": ["A generator is an executable entity (like a function or procedure) that contains code that yields a sequence of values, one at a time, so that each time you call the generator, the next value in the sequence is provided.", "Generators are often built on top of coroutines or objects so that the internal state of the object is handled “naturally”.", "Generators are often used in situations where a sequence is potentially infinite, and where it is possible to construct the next value of the sequence with only minimal state.", "Write a function that uses generators to generate squares and cubes. Create a new generator that filters all cubes from the generator of squares.", "The function should return the \\( n^{th} \\) value of the filtered generator.", "For example for \\(n=7\\), the function should return 81 as the sequence would be 4,9,16,25,36,49,81. Here 64 is filtered out, as it is a cube."],
"solutions": ["function exponentialGenerator(n){\n function* PowersGenerator(m) {\n \tvar n=0;\n \twhile(1) {\n \t\tyield Math.pow(n, m);\n \t\tn += 1;\n \t}\n }\n\n function* FilteredGenerator(g, f){\n \tvar value = g.next().value;\n \tvar filter = f.next().value;\n \twhile(1) {\n \t\tif( value < filter ) {\n \t\t\tyield value;\n \t\t\tvalue = g.next().value;\n \t\t} else if ( value > filter ) {\n \t\t\tfilter = f.next().value;\n \t\t} else {\n \t\t\tvalue = g.next().value;\n \t\t\tfilter = f.next().value;\n \t\t}\n \t}\n }\n\n var squares = PowersGenerator(2);\n var cubes = PowersGenerator(3);\n\n var filtered = FilteredGenerator(squares, cubes);\n\n var curr=0;\n for(var i=0;i<n;i++) curr=filtered.next();\n\n return curr.value;\n}\n"],
"tests": [{
"text": "'<code>exponentialGenerator</code> should be a function.'",
"testString": "assert(typeof exponentialGenerator=='function','<code>exponentialGenerator</code> should be a function.');"
}, {
"text": "'<code>exponentialGenerator()</code> should return a number.'",
"testString": "assert(typeof exponentialGenerator(10)=='number','<code>exponentialGenerator()</code> should return a number.');"
}, {
"text": "'<code>exponentialGenerator(10)</code> should return <code>144</code>.'",
"testString": "assert.equal(exponentialGenerator(10),144,'<code>exponentialGenerator(10)</code> should return <code>144</code>.');"
}, {
"text": "'<code>exponentialGenerator(12)</code> should return <code>196</code>.'",
"testString": "assert.equal(exponentialGenerator(12),196,'<code>exponentialGenerator(12)</code> should return <code>196</code>.');"
}, {
"text": "'<code>exponentialGenerator(14)</code> should return <code>256</code>.'",
"testString": "assert.equal(exponentialGenerator(14),256,'<code>exponentialGenerator(14)</code> should return <code>256</code>.');"
}, {
"text": "'<code>exponentialGenerator(20)</code> should return <code>484</code>.'",
"testString": "assert.equal(exponentialGenerator(20),484,'<code>exponentialGenerator(20)</code> should return <code>484</code>.');"
}, {
"text": "'<code>exponentialGenerator(25)</code> should return <code>784</code>.'",
"testString": "assert.equal(exponentialGenerator(25),784,'<code>exponentialGenerator(25)</code> should return <code>784</code>.');"
}],
"id": "5a23c84252665b21eecc7e7b",
"challengeType": 5,
"releasedOn": "June 7, 2018",
"files": {
"indexjs": {
"key": "indexjs",
"ext": "js",
"name": "index",
"contents": ["function exponentialGenerator (n) {", " // Good luck!", "}"],
"head": [],
"tail": []
}
}
},
{
"title": "Gray code",
"description": ["<a href=\"https://en.wikipedia.org/wiki/Gray code\">Gray code</a> is a form of binary encoding where transitions between consecutive numbers differ by only one bit.", "This is a useful encoding for reducing hardware data hazards with values that change rapidly and/or connect to slower hardware as inputs. ", "It is also useful for generating inputs for <a href=\"https://en.wikipedia.org/wiki/Karnaugh map\">Karnaugh maps</a> in order from left to right or top to bottom. ", "Create a function to encode a number to and decode a number from Gray code. The function should will have 2 parameters. ", "The first would be a boolean. The function should encode for true and decode for false. The second parameter would be the number to be encoded/decoded.", "Display the normal binary representations, Gray code representations, and decoded Gray code values for all 5-bit binary numbers (0-31 inclusive, leading 0's not necessary).", "There are many possible Gray codes. The following encodes what is called \"binary reflected Gray code.\"<br>Encoding (MSB is bit 0, b is binary, g is Gray code): ", "<code><br>if b[i-1] = 1<br><span style=\"padding-left:1em\">g[i] = not b[i]</span><br>else<br><span style=\"padding-left:1em\">g[i] = b[i]</span><br>", "</code> Or: <br><code> g = b xor (b logically right shifted 1 time)</code><br>Decoding (MSB is bit 0, b is binary, g is Gray code): <br>", "<code>b[0] = g[0]<br>for other bits:<br>b[i] = g[i] xor b[i-1]<br></code>"],
"solutions": ["function gray(enc, number){\n if(enc){\n return number ^ (number >> 1);\n }else{\n let n = number;\n\n while (number >>= 1) {\n n ^= number;\n }\n return n;\n }\n}\n"],
"tests": [{
"text": "'<code>gray</code> should be a function.'",
"testString": "assert(typeof gray=='function','<code>gray</code> should be a function.');"
}, {
"text": "'<code>gray(true,177)</code> should return a number.'",
"testString": "assert(typeof gray(true,177)=='number','<code>gray(true,177)</code> should return a number.');"
}, {
"text": "'<code>gray(true,177)</code> should return <code>233</code>.'",
"testString": "assert.equal(gray(true,177),233,'<code>gray(true,177)</code> should return <code>233</code>.');"
}, {
"text": "'<code>gray(true,425)</code> should return <code>381</code>.'",
"testString": "assert.equal(gray(true,425),381,'<code>gray(true,425)</code> should return <code>381</code>.');"
}, {
"text": "'<code>gray(true,870)</code> should return <code>725</code>.'",
"testString": "assert.equal(gray(true,870),725,'<code>gray(true,870)</code> should return <code>725</code>.');"
}, {
"text": "'<code>gray(false,233)</code> should return <code>177</code>.'",
"testString": "assert.equal(gray(false,233),177,'<code>gray(false,233)</code> should return <code>177</code>.');"
}, {
"text": "'<code>gray(false,381)</code> should return <code>425</code>.'",
"testString": "assert.equal(gray(false,381),425,'<code>gray(false,381)</code> should return <code>425</code>.');"
}, {
"text": "'<code>gray(false,725)</code> should return <code>870</code>.'",
"testString": "assert.equal(gray(false,725),870,'<code>gray(false,725)</code> should return <code>870</code>.');"
}],
"id": "5a23c84252665b21eecc7e80",
"challengeType": 5,
"releasedOn": "June 7, 2018",
"files": {
"indexjs": {
"key": "indexjs",
"ext": "js",
"name": "index",
"contents": ["function gray(enc, number) {", " // Good luck!", "}"],
"head": [],
"tail": []
}
}
},
{
"title": "Greatest common divisor",
"description": ["Write a function that returns the greatest common divisor of two integers."],
"solutions": ["function gcd(a, b) {\n return b==0 ? Math.abs(a):gcd(b, a % b);\n}\n"],
"tests": [{
"text": "'<code>gcd</code> should be a function.'",
"testString": "assert(typeof gcd=='function','<code>gcd</code> should be a function.');"
}, {
"text": "'<code>gcd(24,36)</code> should return a number.'",
"testString": "assert(typeof gcd(24,36)=='number','<code>gcd(24,36)</code> should return a number.');"
}, {
"text": "'<code>gcd(24,36)</code> should return <code>12</code>.'",
"testString": "assert.equal(gcd(24,36),12,'<code>gcd(24,36)</code> should return <code>12</code>.');"
}, {
"text": "'<code>gcd(30,48)</code> should return <code>6</code>.'",
"testString": "assert.equal(gcd(30,48),6,'<code>gcd(30,48)</code> should return <code>6</code>.');"
}, {
"text": "'<code>gcd(10,15)</code> should return <code>5</code>.'",
"testString": "assert.equal(gcd(10,15),5,'<code>gcd(10,15)</code> should return <code>5</code>.');"
}, {
"text": "'<code>gcd(100,25)</code> should return <code>25</code>.'",
"testString": "assert.equal(gcd(100,25),25,'<code>gcd(100,25)</code> should return <code>25</code>.');"
}, {
"text": "'<code>gcd(13,250)</code> should return <code>1</code>.'",
"testString": "assert.equal(gcd(13,250),1,'<code>gcd(13,250)</code> should return <code>1</code>.');"
}, {
"text": "'<code>gcd(1300,250)</code> should return <code>50</code>.'",
"testString": "assert.equal(gcd(1300,250),50,'<code>gcd(1300,250)</code> should return <code>50</code>.');"
}],
"id": "5a23c84252665b21eecc7e82",
"challengeType": 5,
"releasedOn": "June 7, 2018",
"files": {
"indexjs": {
"key": "indexjs",
"ext": "js",
"name": "index",
"contents": ["function gcd(a, b) {", " // Good luck!", "}"],
"head": [],
"tail": []
}
}
},
{
"title": "Greatest subsequential sum",
"description": ["Given a sequence of integers, find a continuous subsequence which maximizes the sum of its elements, that is, the elements of no other single subsequence add up to a value larger than this one.", "An empty subsequence is considered to have the sum of \\( 0 \\); thus if all elements are negative, the result must be the empty sequence."],
"solutions": ["function maximumSubsequence(population) {\n function sumValues(arr) {\n var result = 0;\n for (var i = 0, len = arr.length; i < len; i++) {\n result += arr[i];\n }\n return result;\n }\n var greatest;\n var maxValue = 0;\n var subsequence = [];\n\n for (var i = 0, len = population.length; i < len; i++) {\n for (var j = i; j <= len; j++) {\n var subsequence = population.slice(i, j);\n var value = sumValues(subsequence);\n if (value > maxValue) {\n maxValue = value;\n greatest = subsequence;\n };\n }\n }\n\n return greatest;\n}\n\n\n"],
"tests": [{
"text": "'<code>maximumSubsequence</code> should be a function.'",
"testString": "assert(typeof maximumSubsequence=='function','<code>maximumSubsequence</code> should be a function.');"
}, {
"text": "'<code>maximumSubsequence('+JSON.stringify(tests[0])+')</code> should return an array.'",
"testString": "assert(Array.isArray(maximumSubsequence(tests[0])),'<code>maximumSubsequence('+JSON.stringify(tests[0])+')</code> should return an array.');"
}, {
"text": "'<code>maximumSubsequence('+JSON.stringify(tests[0])+')</code> should return <code>'+JSON.stringify(results[0])+'</code>.'",
"testString": "assert.deepEqual(maximumSubsequence(tests[0]),results[0],'<code>maximumSubsequence('+JSON.stringify(tests[0])+')</code> should return <code>'+JSON.stringify(results[0])+'</code>.');"
}, {
"text": "'<code>maximumSubsequence('+JSON.stringify(tests[1])+')</code> should return <code>'+JSON.stringify(results[1])+'</code>.'",
"testString": "assert.deepEqual(maximumSubsequence(tests[1]),results[1],'<code>maximumSubsequence('+JSON.stringify(tests[1])+')</code> should return <code>'+JSON.stringify(results[1])+'</code>.');"
}, {
"text": "'<code>maximumSubsequence('+JSON.stringify(tests[2])+')</code> should return <code>'+JSON.stringify(results[2])+'</code>.'",
"testString": "assert.deepEqual(maximumSubsequence(tests[2]),results[2],'<code>maximumSubsequence('+JSON.stringify(tests[2])+')</code> should return <code>'+JSON.stringify(results[2])+'</code>.');"
}, {
"text": "'<code>maximumSubsequence('+JSON.stringify(tests[3])+')</code> should return <code>'+JSON.stringify(results[3])+'</code>.'",
"testString": "assert.deepEqual(maximumSubsequence(tests[3]),results[3],'<code>maximumSubsequence('+JSON.stringify(tests[3])+')</code> should return <code>'+JSON.stringify(results[3])+'</code>.');"
}, {
"text": "'<code>maximumSubsequence('+JSON.stringify(tests[4])+')</code> should return <code>'+JSON.stringify(results[4])+'</code>.'",
"testString": "assert.deepEqual(maximumSubsequence(tests[4]),results[4],'<code>maximumSubsequence('+JSON.stringify(tests[4])+')</code> should return <code>'+JSON.stringify(results[4])+'</code>.');"
}, {
"text": "'<code>maximumSubsequence('+JSON.stringify(tests[5])+')</code> should return <code>'+JSON.stringify(results[5])+'</code>.'",
"testString": "assert.deepEqual(maximumSubsequence(tests[5]),results[5],'<code>maximumSubsequence('+JSON.stringify(tests[5])+')</code> should return <code>'+JSON.stringify(results[5])+'</code>.');"
}],
"id": "5a23c84252665b21eecc7e84",
"challengeType": 5,
"releasedOn": "June 7, 2018",
"files": {
"indexjs": {
"key": "indexjs",
"ext": "js",
"name": "index",
"contents": ["function maximumSubsequence (population) {", " // Good luck!", "}"],
"head": [],
"tail": ["let tests=[ [1,2,-1,3,10,-10],", " [0, 8, 10, -2, -4, -1, -5, -3],", " [9, 9, -10, 1],", " [7, 1, -5, -3, -8, 1],", " [-3, 6, -1, 4, -4, -6],", " [-1, -2, 3, 5, 6, -2, -1, 4, -4, 2, -1]]", "let results=[ [ 1, 2, -1, 3, 10 ],", " [ 0, 8, 10 ],", " [ 9, 9 ],", " [ 7, 1 ],", " [ 6, -1, 4 ],", " [ 3, 5, 6, -2, -1, 4 ] ]"]
}
}
},
{
"title": "Hailstone sequence",
"description": [