From f52ccf54f5fd2feeae8937fd004b07e39cf6a391 Mon Sep 17 00:00:00 2001 From: Bhanu Pratap Singh Rathore Date: Wed, 6 Jun 2018 19:09:22 +0530 Subject: [PATCH] feat(interview-prep): Porting Rosetta problems (#17388) * feat(interview-prep): Format descriptions * Changes done * Description fixed --- .../rosetta-code.json | 352 ++++++++++++++---- 1 file changed, 289 insertions(+), 63 deletions(-) diff --git a/seed/challenges/08-coding-interview-prep/rosetta-code.json b/seed/challenges/08-coding-interview-prep/rosetta-code.json index f4e4df632e..fd929119d6 100644 --- a/seed/challenges/08-coding-interview-prep/rosetta-code.json +++ b/seed/challenges/08-coding-interview-prep/rosetta-code.json @@ -3665,6 +3665,295 @@ } } }, + { + "title": "Gamma function", + "description": [ + "Implement one algorithm (or more) to compute the Gamma ($\\Gamma$) function (in the real field only).", + "The Gamma function can be defined as:", + "
$\\Gamma(x) = \\displaystyle\\int_0^\\infty t^{x-1}e^{-t} dt$
" + ], + "solutions": [ + "function gamma(x) {\n var p = [0.99999999999980993, 676.5203681218851, -1259.1392167224028,\n 771.32342877765313, -176.61502916214059, 12.507343278686905,\n -0.13857109526572012, 9.9843695780195716e-6, 1.5056327351493116e-7\n ];\n \n var g = 7;\n if (x < 0.5) {\n return Math.PI / (Math.sin(Math.PI * x) * gamma(1 - x));\n }\n\n x -= 1;\n var a = p[0];\n var t = x + g + 0.5;\n for (var i = 1; i < p.length; i++) {\n a += p[i] / (x + i);\n }\n \n var result=Math.sqrt(2 * Math.PI) * Math.pow(t, x + 0.5) * Math.exp(-t) * a;\n\n return result;\n}\n" + ], + "tests": [ + { + "text": "gamma should be a function.", + "testString": "assert(typeof gamma=='function','gamma should be a function.')" + }, + { + "text": "gamma('+tests[0]+') should return a number.", + "testString": "assert(typeof gamma(tests[0])=='number','gamma('+tests[0]+') should return a number.')" + }, + { + "text": "gamma('+tests[0]+') should return '+results[0]+'.", + "testString": "assert.equal(gamma(tests[0]),results[0],'gamma('+tests[0]+') should return '+results[0]+'.')" + }, + { + "text": "gamma('+tests[1]+') should return '+results[1]+'.", + "testString": "assert.equal(gamma(tests[1]),results[1],'gamma('+tests[1]+') should return '+results[1]+'.')" + }, + { + "text": "gamma('+tests[2]+') should return '+results[2]+'.", + "testString": "assert.equal(gamma(tests[2]),results[2],'gamma('+tests[2]+') should return '+results[2]+'.')" + }, + { + "text": "gamma('+tests[3]+') should return '+results[3]+'.", + "testString": "assert.equal(gamma(tests[3]),results[3],'gamma('+tests[3]+') should return '+results[3]+'.')" + }, + { + "text": "gamma('+tests[4]+') should return '+results[4]+'.", + "testString": "assert.equal(gamma(tests[4]),results[4],'gamma('+tests[4]+') should return '+results[4]+'.')" + } + ], + "id": "5a23c84252665b21eecc7e76", + "challengeType": 5, + "releasedOn": "June 1, 2018", + "files": { + "indexjs": { + "key": "indexjs", + "ext": "js", + "name": "index", + "contents": [ + "function gamma (x) {", + " // Good luck!", + "}" + ], + "head": [], + "tail": [ + "let tests=[.1,.2,.3,.4,.5];", + "let results=[", + " 9.513507698668736,", + " 4.590843711998803,", + " 2.9915689876875904,", + " 2.218159543757687,", + " 1.7724538509055159", + "];" + ] + } + } + }, + { + "title": "Gaussian elimination", + "description": [ + "Write a function to solve \\(A.x = b\\) using Gaussian elimination then backwards substitution. \\(A\\) being an \\(n \\times n\\) matrix. Also, \\(x\\) and \\(b\\) are \\(n\\) by 1 vectors. To improve accuracy, please use partial pivoting and scaling." + ], + "solutions": [ + "function gaussianElimination(A, b) {\n // Lower Upper Decomposition\n function ludcmp(A) {\n \t// A is a matrix that we want to decompose into Lower and Upper matrices.\n \tvar d = true\n \tvar n = A.length\n \tvar idx = new Array(n) // Output vector with row permutations from partial pivoting\n \tvar vv = new Array(n) // Scaling information\n \n \tfor (var i=0; i max) max = temp\n \t\t}\n \t\tif (max == 0) return // Singular Matrix!\n \t\tvv[i] = 1 / max // Scaling\n \t}\n \n\t\tvar Acpy = new Array(n)\n\t\tfor (var i=0; i= max) {\n \t\t\t\tmax = temp\n \t\t\t\tjmax = j\n \t\t\t}\n \t\t}\n \t\tif (i <= jmax) {\n \t\t\tfor (var j=0; j -1)\n \t\t\tfor (var j=ii; j=0; i--) {\n \t\tvar sum = b[i]\n \t\tfor (var j=i+1; jgaussianElimination should be a function.'", + "testString": "assert(typeof gaussianElimination=='function','gaussianElimination should be a function.');" + }, + { + "text": "'gaussianElimination('+JSON.stringify(tests[0][0])+','+JSON.stringify(tests[0][1])+') should return an array.'", + "testString": "assert(Array.isArray(gaussianElimination(tests[0][0],tests[0][1])),'gaussianElimination('+JSON.stringify(tests[0][0])+','+JSON.stringify(tests[0][1])+') should return an array.');" + }, + { + "text": "'gaussianElimination('+JSON.stringify(tests[0][0])+','+JSON.stringify(tests[0][1])+') should return '+JSON.stringify(results[0])+'.'", + "testString": "assert.deepEqual(gaussianElimination(tests[0][0],tests[0][1]),results[0],'gaussianElimination('+JSON.stringify(tests[0][0])+','+JSON.stringify(tests[0][1])+') should return '+JSON.stringify(results[0])+'.');" + }, + { + "text": "'gaussianElimination('+JSON.stringify(tests[1][0])+','+JSON.stringify(tests[1][1])+') should return '+JSON.stringify(results[1])+'.'", + "testString": "assert.deepEqual(gaussianElimination(tests[1][0],tests[1][1]),results[1],'gaussianElimination('+JSON.stringify(tests[1][0])+','+JSON.stringify(tests[1][1])+') should return '+JSON.stringify(results[1])+'.');" + }, + { + "text": "'gaussianElimination('+JSON.stringify(tests[2][0])+','+JSON.stringify(tests[2][1])+') should return '+JSON.stringify(results[2])+'.'", + "testString": "assert.deepEqual(gaussianElimination(tests[2][0],tests[2][1]),results[2],'gaussianElimination('+JSON.stringify(tests[2][0])+','+JSON.stringify(tests[2][1])+') should return '+JSON.stringify(results[2])+'.');" + }, + { + "text": "'gaussianElimination('+JSON.stringify(tests[3][0])+','+JSON.stringify(tests[3][1])+') should return '+JSON.stringify(results[3])+'.'", + "testString": "assert.deepEqual(gaussianElimination(tests[3][0],tests[3][1]),results[3],'gaussianElimination('+JSON.stringify(tests[3][0])+','+JSON.stringify(tests[3][1])+') should return '+JSON.stringify(results[3])+'.');" + }, + { + "text": "'gaussianElimination('+JSON.stringify(tests[4][0])+','+JSON.stringify(tests[4][1])+') should return '+JSON.stringify(results[4])+'.'", + "testString": "assert.deepEqual(gaussianElimination(tests[4][0],tests[4][1]),results[4],'gaussianElimination('+JSON.stringify(tests[4][0])+','+JSON.stringify(tests[4][1])+') should return '+JSON.stringify(results[4])+'.');" + } + ], + "id": "5a23c84252665b21eecc7e77", + "challengeType": 5, + "releasedOn": "June 2, 2018", + "files": { + "indexjs": { + "key": "indexjs", + "ext": "js", + "name": "index", + "contents": [ + "function gaussianElimination (A,b) {", + " // Good luck!", + "}" + ], + "head": [], + "tail": [ + "let tests=[", + " [ [[1,1],[1,-1]] , [5,1] ],", + " [ [[2,3],[2,1]] , [8,4] ],", + " [ [[1,3],[5,-2]] , [14,19] ],", + " [ [[1,1],[5,-1]] , [10,14] ],", + " [ [[1,2,3],[4,5,6],[7,8,8]] , [6,15,23] ]", + "];", + "let results=[", + " [ 3, 2 ],", + " [ 1, 2 ],", + " [ 5, 3 ],", + " [ 4, 6 ],", + " [ 1, 1, 1 ]", + "]" + ] + } + } + }, + { + "title": "General FizzBuzz", +"description": [ + "Write a generalized version of FizzBuzz that works for any list of factors, along with their words.", + "This is basically a \"fizzbuzz\" implementation where the rules of the game are supplied to the user. Create a function to implement this. The function should take two parameters.", + "The first will be an array with the FizzBuzz rules. For example: [ [3,\"Fizz\"] , [5,\"Buzz\"] ].", + "This indcates that Fizz should be printed if the number is a multiple of 3 and Buzz if it is a multiple of 5. If it is a multiple of both then the strings should be concatenated in the order specified in the array. In this case, FizzBuzz if the number is a multiple of 3 and 5.", + "The second parameter is the number for which the function should return a string as stated above." + ], + "solutions": [ + "function genFizzBuzz(rules, num) {\n let res='';\n rules.forEach(function (e) {\n if(num % e[0] == 0)\n res+=e[1];\n })\n\n if(res==''){\n res=num.toString();\n }\n\n return res;\n}\n\n\n\n" + ], + "tests": [ + { + "text": "'genFizzBuzz should be a function.'", + "testString": "assert(typeof genFizzBuzz=='function','genFizzBuzz should be a function.');" + }, + { + "text": "'genFizzBuzz('+JSON.stringify(tests[0][0])+','+tests[0][1]+') should return a type.'", + "testString": "assert(typeof genFizzBuzz(tests[0][0],tests[0][1])=='string','genFizzBuzz('+JSON.stringify(tests[0][0])+','+tests[0][1]+') should return a type.');" + }, + { + "text": "'genFizzBuzz('+JSON.stringify(tests[0][0])+','+tests[0][1]+') should return \"'+results[0]+'\".'", + "testString": "assert.equal(genFizzBuzz(tests[0][0],tests[0][1]),results[0],'genFizzBuzz('+JSON.stringify(tests[0][0])+','+tests[0][1]+') should return \"'+results[0]+'\".');" + }, + { + "text": "'genFizzBuzz('+JSON.stringify(tests[1][0])+','+tests[1][1]+') should return \"'+results[1]+'\".'", + "testString": "assert.equal(genFizzBuzz(tests[1][0],tests[1][1]),results[1],'genFizzBuzz('+JSON.stringify(tests[1][0])+','+tests[1][1]+') should return \"'+results[1]+'\".');" + }, + { + "text": "'genFizzBuzz('+JSON.stringify(tests[2][0])+','+tests[2][1]+') should return \"'+results[2]+'\".'", + "testString": "assert.equal(genFizzBuzz(tests[2][0],tests[2][1]),results[2],'genFizzBuzz('+JSON.stringify(tests[2][0])+','+tests[2][1]+') should return \"'+results[2]+'\".');" + }, + { + "text": "'genFizzBuzz('+JSON.stringify(tests[3][0])+','+tests[3][1]+') should return \"'+results[3]+'\".'", + "testString": "assert.equal(genFizzBuzz(tests[3][0],tests[3][1]),results[3],'genFizzBuzz('+JSON.stringify(tests[3][0])+','+tests[3][1]+') should return \"'+results[3]+'\".');" + }, + { + "text": "'genFizzBuzz('+JSON.stringify(tests[4][0])+','+tests[4][1]+') should return \"'+results[4]+'\".'", + "testString": "assert.equal(genFizzBuzz(tests[4][0],tests[4][1]),results[4],'genFizzBuzz('+JSON.stringify(tests[4][0])+','+tests[4][1]+') should return \"'+results[4]+'\".');" + }, + { + "text": "'genFizzBuzz('+JSON.stringify(tests[5][0])+','+tests[5][1]+') should return \"'+results[5]+'\".'", + "testString": "assert.equal(genFizzBuzz(tests[5][0],tests[5][1]),results[5],'genFizzBuzz('+JSON.stringify(tests[5][0])+','+tests[5][1]+') should return \"'+results[5]+'\".');" + }, + { + "text": "'genFizzBuzz('+JSON.stringify(tests[6][0])+','+tests[6][1]+') should return \"'+results[6]+'\".'", + "testString": "assert.equal(genFizzBuzz(tests[6][0],tests[6][1]),results[6],'genFizzBuzz('+JSON.stringify(tests[6][0])+','+tests[6][1]+') should return \"'+results[6]+'\".');" + } + ], + "id": "5a23c84252665b21eecc7e78", + "challengeType": 5, + "releasedOn": "June 2, 2018", + "files": { + "indexjs": { + "key": "indexjs", + "ext": "js", + "name": "index", + "contents": [ + "function genFizzBuzz (rules, num) {", + " // Good luck!", + "}" + ], + "head": [], + "tail": [ + "let tests=[", + " [ [[3, 'Fizz'],[5, 'Buzz']], 6 ],", + " [ [[3, 'Fizz'],[5, 'Buzz']], 10 ],", + " [ [[3, 'Buzz'],[5, 'Fizz']], 12 ],", + " [ [[3, 'Buzz'],[5, 'Fizz']], 13 ],", + " [ [[3, 'Buzz'],[5, 'Fizz']], 15 ],", + " [ [[3, 'Fizz'],[5, 'Buzz']], 15 ],", + " [ [[3, 'Fizz'],[5, 'Buzz'],[7, 'Baxx']], 105 ],", + "]", + "let results=[", + " \"Fizz\",", + " \"Buzz\",", + " \"Buzz\",", + " \"13\",", + " \"BuzzFizz\",", + " \"FizzBuzz\",", + " \"FizzBuzzBaxx\"", + "]" + ] + } + } + }, + { + "title": "Generate lower case ASCII alphabet", + "description": [ + "Write a function to generate an array of lower case ASCII characters, for a given range. For example: for range 1 to 4 the function should return ['a','b','c','d']." + ], + "solutions": [ + "function lascii(cFrom, cTo) {\n\n function cRange(cFrom, cTo) {\n var iStart = cFrom.charCodeAt(0);\n\n return Array.apply(\n null, Array(cTo.charCodeAt(0) - iStart + 1)\n ).map(function (_, i) {\n\n return String.fromCharCode(iStart + i);\n\n });\n }\n\n return cRange(cFrom, cTo);\n\n}\n" + ], + "tests": [ + { + "text": "'lascii should be a function.'", + "testString": "assert(typeof lascii=='function','lascii should be a function.');" + }, + { + "text": "'lascii(\"a\",\"d\") should return an array.'", + "testString": "assert(Array.isArray(lascii('a','d')),'lascii(\"a\",\"d\") should return an array.');" + }, + { + "text": "\"lascii('a','d') should return [ 'a', 'b', 'c', 'd' ].\"", + "testString": "assert.deepEqual(lascii(\"a\",\"d\"),results[0],\"lascii('a','d') should return [ 'a', 'b', 'c', 'd' ].\");" + }, + { + "text": "\"lascii('c','i') should return [ 'c', 'd', 'e', 'f', 'g', 'h', 'i' ].\"", + "testString": "assert.deepEqual(lascii(\"c\",\"i\"),results[1],\"lascii('c','i') should return [ 'c', 'd', 'e', 'f', 'g', 'h', 'i' ].\");" + }, + { + "text": "\"lascii('m','q') should return [ 'm', 'n', 'o', 'p', 'q' ].\"", + "testString": "assert.deepEqual(lascii(\"m\",\"q\"),results[2],\"lascii('m','q') should return [ 'm', 'n', 'o', 'p', 'q' ].\");" + }, + { + "text": "\"lascii('k','n') should return [ 'k', 'l', 'm', 'n' ].\")", + "testString": "assert.deepEqual(lascii(\"k\",\"n\"),results[3],\"lascii('k','n') should return [ 'k', 'l', 'm', 'n' ].\");" + }, + { + "text": "\"lascii('t','z') should return [ 't', 'u', 'v', 'w', 'x', 'y', 'z' ].\"", + "testString": "assert.deepEqual(lascii(\"t\",\"z\"),results[4],\"lascii('t','z') should return [ 't', 'u', 'v', 'w', 'x', 'y', 'z' ].\");" + } + ], + "id": "5a23c84252665b21eecc7e7a", + "challengeType": 5, + "releasedOn": "June 2, 2018", + "files": { + "indexjs": { + "key": "indexjs", + "ext": "js", + "name": "index", + "contents": [ + "function lascii (cFrom, cTo) {", + " // Good luck!", + "}" + ], + "head": [], + "tail": [ + "let results=[", + " [ 'a', 'b', 'c', 'd' ],", + " [ 'c', 'd', 'e', 'f', 'g', 'h', 'i' ],", + " [ 'm', 'n', 'o', 'p', 'q' ],", + " [ 'k', 'l', 'm', 'n' ],", + " [ 't', 'u', 'v', 'w', 'x', 'y', 'z' ]", + "]" + ] + } + } + }, { "title": "Hailstone sequence", "description": [ @@ -4367,69 +4656,6 @@ } } }, - { - "title": "Gamma function", - "description": ["

Implement one algorithm (or more) to compute the Gamma ($\\Gamma$) function (in the real field only).

The Gamma function can be defined as:


::::: $\\Gamma(x) = \\displaystyle\\int_0^\\infty t^{x-1}e^{-t} dt$

"], - "solutions": [ - "function gamma(x) {\n var p = [0.99999999999980993, 676.5203681218851, -1259.1392167224028,\n 771.32342877765313, -176.61502916214059, 12.507343278686905,\n -0.13857109526572012, 9.9843695780195716e-6, 1.5056327351493116e-7\n ];\n \n var g = 7;\n if (x < 0.5) {\n return Math.PI / (Math.sin(Math.PI * x) * gamma(1 - x));\n }\n\n x -= 1;\n var a = p[0];\n var t = x + g + 0.5;\n for (var i = 1; i < p.length; i++) {\n a += p[i] / (x + i);\n }\n \n var result=Math.sqrt(2 * Math.PI) * Math.pow(t, x + 0.5) * Math.exp(-t) * a;\n\n return result;\n}\n" - ], - "tests": [ - { - "text": "gamma should be a function.", - "testString": "assert(typeof gamma=='function','gamma should be a function.')" - }, - { - "text": "gamma('+tests[0]+') should return a number.", - "testString": "assert(typeof gamma(tests[0])=='number','gamma('+tests[0]+') should return a number.')" - }, - { - "text": "gamma('+tests[0]+') should return '+results[0]+'.", - "testString": "assert.equal(gamma(tests[0]),results[0],'gamma('+tests[0]+') should return '+results[0]+'.')" - }, - { - "text": "gamma('+tests[1]+') should return '+results[1]+'.", - "testString": "assert.equal(gamma(tests[1]),results[1],'gamma('+tests[1]+') should return '+results[1]+'.')" - }, - { - "text": "gamma('+tests[2]+') should return '+results[2]+'.", - "testString": "assert.equal(gamma(tests[2]),results[2],'gamma('+tests[2]+') should return '+results[2]+'.')" - }, - { - "text": "gamma('+tests[3]+') should return '+results[3]+'.", - "testString": "assert.equal(gamma(tests[3]),results[3],'gamma('+tests[3]+') should return '+results[3]+'.')" - }, - { - "text": "gamma('+tests[4]+') should return '+results[4]+'.", - "testString": "assert.equal(gamma(tests[4]),results[4],'gamma('+tests[4]+') should return '+results[4]+'.')" - } - ], - "id": "5a23c84252665b21eecc7e76", - "challengeType": 3, - "releasedOn": "June 1, 2018", - "files": { - "indexjs": { - "key": "indexjs", - "ext": "js", - "name": "index", - "contents": [ - "function gaussianElimination (A,b) {", - " // Good luck!", - "}" - ], - "head": [], - "tail": [ - "let tests=[.1,.2,.3,.4,.5];", - "let results=[", - " 9.513507698668736,", - " 4.590843711998803,", - " 2.9915689876875904,", - " 2.218159543757687,", - " 1.7724538509055159", - "];" - ] - } - } - }, { "title": "Sailors, coconuts and a monkey problem", "description": [