From 5ba9c1d5c07152a60e926161f21bf5204a43ff66 Mon Sep 17 00:00:00 2001 From: Bhanu Pratap Singh Rathore Date: Mon, 11 Jun 2018 05:33:55 +0530 Subject: [PATCH] feat(interview-prep): Porting Rosetta problems (#17480) * feat(interview-prep): Porting Rosetta problems * Changes done * Update rosetta-code.json * Update rosetta-code.json --- .../rosetta-code.json | 169 ++++++++++++++++++ 1 file changed, 169 insertions(+) diff --git a/seed/challenges/08-coding-interview-prep/rosetta-code.json b/seed/challenges/08-coding-interview-prep/rosetta-code.json index fd929119d6..692b37816e 100644 --- a/seed/challenges/08-coding-interview-prep/rosetta-code.json +++ b/seed/challenges/08-coding-interview-prep/rosetta-code.json @@ -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;iexponentialGenerator should be a function.'", + "testString": "assert(typeof exponentialGenerator=='function','exponentialGenerator should be a function.');" + }, { + "text": "'exponentialGenerator() should return a number.'", + "testString": "assert(typeof exponentialGenerator(10)=='number','exponentialGenerator() should return a number.');" + }, { + "text": "'exponentialGenerator(10) should return 144.'", + "testString": "assert.equal(exponentialGenerator(10),144,'exponentialGenerator(10) should return 144.');" + }, { + "text": "'exponentialGenerator(12) should return 196.'", + "testString": "assert.equal(exponentialGenerator(12),196,'exponentialGenerator(12) should return 196.');" + }, { + "text": "'exponentialGenerator(14) should return 256.'", + "testString": "assert.equal(exponentialGenerator(14),256,'exponentialGenerator(14) should return 256.');" + }, { + "text": "'exponentialGenerator(20) should return 484.'", + "testString": "assert.equal(exponentialGenerator(20),484,'exponentialGenerator(20) should return 484.');" + }, { + "text": "'exponentialGenerator(25) should return 784.'", + "testString": "assert.equal(exponentialGenerator(25),784,'exponentialGenerator(25) should return 784.');" + }], + "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": ["Gray code 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 Karnaugh maps 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.\"
Encoding (MSB is bit 0, b is binary, g is Gray code): ", "
if b[i-1] = 1
g[i] = not b[i]
else
g[i] = b[i]
", "
Or:
g = b xor (b logically right shifted 1 time)
Decoding (MSB is bit 0, b is binary, g is Gray code):
", "b[0] = g[0]
for other bits:
b[i] = g[i] xor b[i-1]
"], + "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": "'gray should be a function.'", + "testString": "assert(typeof gray=='function','gray should be a function.');" + }, { + "text": "'gray(true,177) should return a number.'", + "testString": "assert(typeof gray(true,177)=='number','gray(true,177) should return a number.');" + }, { + "text": "'gray(true,177) should return 233.'", + "testString": "assert.equal(gray(true,177),233,'gray(true,177) should return 233.');" + }, { + "text": "'gray(true,425) should return 381.'", + "testString": "assert.equal(gray(true,425),381,'gray(true,425) should return 381.');" + }, { + "text": "'gray(true,870) should return 725.'", + "testString": "assert.equal(gray(true,870),725,'gray(true,870) should return 725.');" + }, { + "text": "'gray(false,233) should return 177.'", + "testString": "assert.equal(gray(false,233),177,'gray(false,233) should return 177.');" + }, { + "text": "'gray(false,381) should return 425.'", + "testString": "assert.equal(gray(false,381),425,'gray(false,381) should return 425.');" + }, { + "text": "'gray(false,725) should return 870.'", + "testString": "assert.equal(gray(false,725),870,'gray(false,725) should return 870.');" + }], + "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": "'gcd should be a function.'", + "testString": "assert(typeof gcd=='function','gcd should be a function.');" + }, { + "text": "'gcd(24,36) should return a number.'", + "testString": "assert(typeof gcd(24,36)=='number','gcd(24,36) should return a number.');" + }, { + "text": "'gcd(24,36) should return 12.'", + "testString": "assert.equal(gcd(24,36),12,'gcd(24,36) should return 12.');" + }, { + "text": "'gcd(30,48) should return 6.'", + "testString": "assert.equal(gcd(30,48),6,'gcd(30,48) should return 6.');" + }, { + "text": "'gcd(10,15) should return 5.'", + "testString": "assert.equal(gcd(10,15),5,'gcd(10,15) should return 5.');" + }, { + "text": "'gcd(100,25) should return 25.'", + "testString": "assert.equal(gcd(100,25),25,'gcd(100,25) should return 25.');" + }, { + "text": "'gcd(13,250) should return 1.'", + "testString": "assert.equal(gcd(13,250),1,'gcd(13,250) should return 1.');" + }, { + "text": "'gcd(1300,250) should return 50.'", + "testString": "assert.equal(gcd(1300,250),50,'gcd(1300,250) should return 50.');" + }], + "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": "'maximumSubsequence should be a function.'", + "testString": "assert(typeof maximumSubsequence=='function','maximumSubsequence should be a function.');" + }, { + "text": "'maximumSubsequence('+JSON.stringify(tests[0])+') should return an array.'", + "testString": "assert(Array.isArray(maximumSubsequence(tests[0])),'maximumSubsequence('+JSON.stringify(tests[0])+') should return an array.');" + }, { + "text": "'maximumSubsequence('+JSON.stringify(tests[0])+') should return '+JSON.stringify(results[0])+'.'", + "testString": "assert.deepEqual(maximumSubsequence(tests[0]),results[0],'maximumSubsequence('+JSON.stringify(tests[0])+') should return '+JSON.stringify(results[0])+'.');" + }, { + "text": "'maximumSubsequence('+JSON.stringify(tests[1])+') should return '+JSON.stringify(results[1])+'.'", + "testString": "assert.deepEqual(maximumSubsequence(tests[1]),results[1],'maximumSubsequence('+JSON.stringify(tests[1])+') should return '+JSON.stringify(results[1])+'.');" + }, { + "text": "'maximumSubsequence('+JSON.stringify(tests[2])+') should return '+JSON.stringify(results[2])+'.'", + "testString": "assert.deepEqual(maximumSubsequence(tests[2]),results[2],'maximumSubsequence('+JSON.stringify(tests[2])+') should return '+JSON.stringify(results[2])+'.');" + }, { + "text": "'maximumSubsequence('+JSON.stringify(tests[3])+') should return '+JSON.stringify(results[3])+'.'", + "testString": "assert.deepEqual(maximumSubsequence(tests[3]),results[3],'maximumSubsequence('+JSON.stringify(tests[3])+') should return '+JSON.stringify(results[3])+'.');" + }, { + "text": "'maximumSubsequence('+JSON.stringify(tests[4])+') should return '+JSON.stringify(results[4])+'.'", + "testString": "assert.deepEqual(maximumSubsequence(tests[4]),results[4],'maximumSubsequence('+JSON.stringify(tests[4])+') should return '+JSON.stringify(results[4])+'.');" + }, { + "text": "'maximumSubsequence('+JSON.stringify(tests[5])+') should return '+JSON.stringify(results[5])+'.'", + "testString": "assert.deepEqual(maximumSubsequence(tests[5]),results[5],'maximumSubsequence('+JSON.stringify(tests[5])+') should return '+JSON.stringify(results[5])+'.');" + }], + "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": [