diff --git a/challenges/08-coding-interview-prep/rosetta-code.json b/challenges/08-coding-interview-prep/rosetta-code.json index 42e310a224..fbdec2edef 100644 --- a/challenges/08-coding-interview-prep/rosetta-code.json +++ b/challenges/08-coding-interview-prep/rosetta-code.json @@ -3588,6 +3588,541 @@ } } }, + { + "title": "Fractran", + "description": [ + "

FRACTRAN is a Turing-complete esoteric programming language invented by the mathematician John Horton Conway.


A FRACTRAN program is an ordered list of positive fractions $P = (f_1, f_2, \\ldots, f_m)$, together with an initial positive integer input $n$.

", + "

The program is run by updating the integer $n$ as follows:


", + "
", + "

Conway gave a program for primes in FRACTRAN:


$17/91$, $78/85$, $19/51$, $23/38$, $29/33$, $77/29$, $95/23$, $77/19$, $1/17$, $11/13$, $13/11$, $15/14$, $15/2$, $55/1$


Starting with $n=2$, this FRACTRAN program will change $n$ to $15=2\\times (15/2)$, then $825=15\\times (55/1)$, generating the following sequence of integers:


$2$, $15$, $825$, $725$, $1925$, $2275$, $425$, $390$, $330$, $290$, $770$, $\\ldots$


After 2, this sequence contains the following powers of 2:


$2^2=4$, $2^3=8$, $2^5=32$, $2^7=128$, $2^{11}=2048$, $2^{13}=8192$, $2^{17}=131072$, $2^{19}=524288$, $\\ldots$


which are the prime powers of 2.

", + "
Task:
", + "

Write a function that takes a fractran program as a string parameter and returns the first 10 numbers of the program as an array. If the result does not have 10 numbers then return the numbers as is.

" + ], + "solutions": [ + "function fractran(progStr){\n var num = new Array();\n var den = new Array();\n var val ;\n var out=\"\";\n function compile(prog){\n var regex = /\\s*(\\d*)\\s*\\/\\s*(\\d*)\\s*(.*)/m;\n while(regex.test(prog)){\n num.push(regex.exec(prog)[1]);\n den.push(regex.exec(prog)[2]);\n prog = regex.exec(prog)[3];\n }\n }\n\n function step(val){\n var i=0;\n while(ifractran should be a function.", + "testString": "assert(typeof fractran=='function','fractran should be a function.');" + }, + { + "text": "fractran(\"'+tests[0]+'\") should return an array.", + "testString": "assert(Array.isArray(fractran(tests[0])),'fractran(\"'+tests[0]+'\") should return an array.');" + }, + { + "text": "fractran(\"'+tests[0]+'\") should return '+JSON.stringify(results[0])+'.", + "testString": "assert.deepEqual(fractran(tests[0]),results[0],'fractran(\"'+tests[0]+'\") should return '+JSON.stringify(results[0])+'.');" + }, + { + "text": "fractran(\"'+tests[1]+'\") should return '+JSON.stringify(results[1])+'.", + "testString": "assert.deepEqual(fractran(tests[1]),results[1],'fractran(\"'+tests[1]+'\") should return '+JSON.stringify(results[1])+'.');" + }, + { + "text": "fractran(\"'+tests[2]+'\") should return '+JSON.stringify(results[2])+'.", + "testString": "assert.deepEqual(fractran(tests[2]),results[2],'fractran(\"'+tests[2]+'\") should return '+JSON.stringify(results[2])+'.');" + }, + { + "text": "fractran(\"'+tests[3]+'\") should return '+JSON.stringify(results[3])+'.", + "testString": "assert.deepEqual(fractran(tests[3]),results[3],'fractran(\"'+tests[3]+'\") should return '+JSON.stringify(results[3])+'.');" + }, + { + "text": "fractran(\"'+tests[4]+'\") should return '+JSON.stringify(results[4])+'.", + "testString": "assert.deepEqual(fractran(tests[4]),results[4],'fractran(\"'+tests[4]+'\") should return '+JSON.stringify(results[4])+'.');" + } + ], + "id": "5a7dad05be01840e1778a0d1", + "challengeType": 3, + "releasedOn": "May 31, 2018", + "files": { + "indexjs": { + "key": "indexjs", + "ext": "js", + "name": "index", + "contents": [ + "function fractran (progStr) {", + " // Good luck!", + "}" + ], + "head": [], + "tail": [ + "let tests=[", + " '3/2,1/3',", + " '3/2,5/3,1/5',", + " '3/2,6/3',", + " '2/7,7/2',", + " '17/91 78/85 19/51 23/38 29/33 77/29 95/23 77/19 1/17 11/13 13/11 15/14 15/2 55/1'", + "]", + "let results=[", + " [ 2, 3, 1 ],", + " [ 2, 3, 5, 1 ],", + " [ 2, 3, 6, 9, 18, 27, 54, 81, 162, 243 ],", + " [ 2, 7, 2, 7, 2, 7, 2, 7, 2, 7 ],", + " [ 2, 15, 825, 725, 1925, 2275, 425, 390, 330, 290 ]", + "]" + ] + } + } + }, + { + "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": "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": [ diff --git a/last-4commits.patch b/last-4commits.patch new file mode 100644 index 0000000000..820371c6f5 --- /dev/null +++ b/last-4commits.patch @@ -0,0 +1,342 @@ +From 39cb13380b898752cc54cabc3e97372126447252 Mon Sep 17 00:00:00 2001 +From: Vimal Raghubir +Date: Sun, 10 Jun 2018 19:45:18 -0400 +Subject: [PATCH 1/4] fix: added new test and fixed incorrect variable (#17471) + +* Added new test and fixed incorrect variable + +* Fix: Incorrect sentences and descriptions + +* Update es6.json +--- + .../es6.json | 22 +++++++++++-------- + 1 file changed, 13 insertions(+), 9 deletions(-) + +diff --git a/seed/challenges/02-javascript-algorithms-and-data-structures/es6.json b/seed/challenges/02-javascript-algorithms-and-data-structures/es6.json +index 941222890..5d45abf56 100644 +--- a/seed/challenges/02-javascript-algorithms-and-data-structures/es6.json ++++ b/seed/challenges/02-javascript-algorithms-and-data-structures/es6.json +@@ -127,22 +127,26 @@ + "let is not the only new way to declare variables. In ES6, you can also declare variables using the const keyword.", + "const has all the awesome features that let has, with the added bonus that variables declared using const are read-only. They are a constant value, which means that once a variable is assigned with const, it cannot be reassigned.", + "
\"use strict\"
const FAV_PET = \"Cats\";
FAV_PET = \"Dogs\"; // returns error
", +- "As you can see, trying to reassign a variable declared with const will throw an error. You should always name variables you don't want to reassign using the const keyword. This helps when you accidentally attempt to reassign a variable that is meant to stay constant. A common practice is to name your constants in all upper-cases and with an underscore to separate words (e.g. EXAMPLE_VARIABLE).", ++ "As you can see, trying to reassign a variable declared with const will throw an error. You should always name variables you don't want to reassign using the const keyword. This helps when you accidentally attempt to reassign a variable that is meant to stay constant. A common practice when naming constants is to use all uppercase letters, with words separated by an underscore.", + "
", +- "Change the code so that all variables are declared using let or const. Use let when you want the variable to change, and const when you want the variable to remain constant. Also, rename variables declared with const to conform to common practices, meaning constants should be in all caps" ++ "Change the code so that all variables are declared using let or const. Use let when you want the variable to change, and const when you want the variable to remain constant. Also, rename variables declared with const to conform to common practices, meaning constants should be in all caps." + ], + "tests": [ + { +- "text": "var does not exist in code.", +- "testString": "getUserInput => assert(!getUserInput('index').match(/var/g),'var does not exist in code.');" ++ "text": "var does not exist in your code.", ++ "testString": "getUserInput => assert(!getUserInput('index').match(/var/g),'var does not exist in your code.');" ++ }, ++ { ++ "text": "SENTENCE should be a constant variable declared with const.", ++ "testString": "getUserInput => assert(getUserInput('index').match(/(const SENTENCE)/g), 'SENTENCE should be a constant variable declared with const.');" + }, + { +- "text": "SENTENCE should be a constant variable (by using const).", +- "testString": "getUserInput => assert(getUserInput('index').match(/(const SENTENCE)/g), 'SENTENCE should be a constant variable (by using const).');" ++ "text": "i should be declared with let.", ++ "testString": "getUserInput => assert(getUserInput('index').match(/(let i)/g), 'i should be declared with let.');" + }, + { +- "text": "i should be a variable only defined within the for loop scope (by usinglet).", +- "testString": "getUserInput => assert(getUserInput('index').match(/(let i)/g), 'i should be a variable only defined within the for loop scope (by usinglet).');" ++ "text": "console.log should be changed to print the SENTENCE variable.", ++ "testString": "getUserInput => assert(getUserInput('index').match(/console.log/(/s*?SENTENCE/s*?/)/s*?;/g), 'console.log should be adjusted to print the variable SENTENCE.');" + } + ], + "releasedOn": "Feb 17, 2017", +@@ -161,7 +165,7 @@ + "", + " var sentence = str + \" is cool!\";", + " for(var i = 0; i < str.length; i+=2) {", +- " console.log(str);", ++ " console.log(sentence);", + " }", + "", + " // change code above this line", +-- +2.17.1 + + +From b6f963bb9805d3fc3bd3724fffc0751f905f2620 Mon Sep 17 00:00:00 2001 +From: bogadodiegoh +Date: Sun, 10 Jun 2018 20:46:46 -0300 +Subject: [PATCH 2/4] fix(seed): updated basic javascript URLs for hints + (#17445) + +* Updated json file with right url values + +* Updated url links after review + +* Updated another broken url link after review +--- + .../basic-javascript.json | 6 +++--- + 1 file changed, 3 insertions(+), 3 deletions(-) + +diff --git a/seed/challenges/02-javascript-algorithms-and-data-structures/basic-javascript.json b/seed/challenges/02-javascript-algorithms-and-data-structures/basic-javascript.json +index edce769e2..0a2a90a22 100644 +--- a/seed/challenges/02-javascript-algorithms-and-data-structures/basic-javascript.json ++++ b/seed/challenges/02-javascript-algorithms-and-data-structures/basic-javascript.json +@@ -6757,9 +6757,9 @@ + "If prop is \"tracks\" but the album doesn't have a \"tracks\" property, create an empty array before adding the new value to the album's corresponding property.", + "If prop is \"tracks\" and value isn't empty (\"\"), push the value onto the end of the album's existing tracks array.", + "If value is empty (\"\"), delete the given prop property from the album.", +- "Hints
Use bracket notation when accessing object properties with variables.", ++ "Hints
Use bracket notation when accessing object properties with variables.", + "Push is an array method you can read about on Mozilla Developer Network.", +- "You may refer back to Manipulating Complex Objects Introducing JavaScript Object Notation (JSON) for a refresher." ++ "You may refer back to Manipulating Complex Objects Introducing JavaScript Object Notation (JSON) for a refresher." + ], + "releasedOn": "January 1, 2016", + "solutions": [ +@@ -6807,7 +6807,7 @@ + "Si la propiedad prop es \"tracks\" y value no está en blanco, empuja (push) el valor value al final del vector tracks.", + "Si el valor value está en blanco, elimina esa prop.", + "Siempre retorna el objeto collection entero.", +- "Nota
No olvides usar notación corchete cuando accedes a propiedades de objetos con variables." ++ "Nota
No olvides usar notación corchete cuando accedes a propiedades de objetos con variables." + ] + } + }, +-- +2.17.1 + + +From 8f4e8bb3dd918bd3872db71c52a41f3061445cc5 Mon Sep 17 00:00:00 2001 +From: Jarek Wojciechowski +Date: Sun, 10 Jun 2018 20:02:32 -0400 +Subject: [PATCH 3/4] fix(challenges): add info about ReactDOM.render in an + early react challenge (#17186) (#17473) + +--- + seed/challenges/03-front-end-libraries/react.json | 3 ++- + 1 file changed, 2 insertions(+), 1 deletion(-) + +diff --git a/seed/challenges/03-front-end-libraries/react.json b/seed/challenges/03-front-end-libraries/react.json +index 6b5e529ad..998106189 100644 +--- a/seed/challenges/03-front-end-libraries/react.json ++++ b/seed/challenges/03-front-end-libraries/react.json +@@ -22,6 +22,7 @@ + "React uses a syntax extension of JavaScript called JSX that allows you to write HTML directly within JavaScript. This has several benefits. It lets you use the full programmatic power of JavaScript within HTML, and helps to keep your code readable. For the most part, JSX is similar to the HTML that you have already learned, however there are a few key differences that will be covered throughout these challenges.", + "For instance, because JSX is a syntactic extension of JavaScript, you can actually write JavaScript directly within JSX. To do this, you simply include the code you want to be treated as JavaScript within curly braces: { 'this is treated as JavaScript code' }. Keep this in mind, since it's used in several future challenges.", + "However, because JSX is not valid JavaScript, JSX code must be compiled into JavaScript. The transpiler Babel is a popular tool for this process. For your convenience, it's already added behind the scenes for these challenges. If you happen to write syntactically invalid JSX, you will see the first test in these challenges fail.", ++ "It's worth noting that under the hood the challenges are calling ReactDOM.render(JSX, document.getElementById('root')). This function call is what places your JSX into React's own lightweight representation of the DOM. React then uses snapshots of its own DOM to optimize updating only specific parts of the actual DOM.", + "
", + "Instructions: The current code uses JSX to assign a div element to the constant JSX. Replace the div with an h1 element and add the text Hello JSX! inside it." + ], +@@ -3673,4 +3674,4 @@ + "react": true + } + ] +-} +\ No newline at end of file ++} +-- +2.17.1 + + +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 4/4] 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 fd929119d..692b37816 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": [ +-- +2.17.1 +