diff --git a/challenges/08-coding-interview-prep/rosetta-code.json b/challenges/08-coding-interview-prep/rosetta-code.json
index a14c94c9a..fd943c422 100644
--- a/challenges/08-coding-interview-prep/rosetta-code.json
+++ b/challenges/08-coding-interview-prep/rosetta-code.json
@@ -16979,6 +16979,988 @@
}
}
},
+ {
+ "title": "Search a list of records",
+ "description": [
+ "Write a function that takes a string as a parameter. The function should return the index of the list item for which the name is the same as the given string."
+ ],
+ "solutions": [
+ "var lst = [\n { \"name\": \"Lagos\", \"population\": 21.0 },\n { \"name\": \"Cairo\", \"population\": 15.2 },\n { \"name\": \"Kinshasa-Brazzaville\", \"population\": 11.3 },\n { \"name\": \"Greater Johannesburg\", \"population\": 7.55 },\n { \"name\": \"Mogadishu\", \"population\": 5.85 },\n { \"name\": \"Khartoum-Omdurman\", \"population\": 4.98 },\n { \"name\": \"Dar Es Salaam\", \"population\": 4.7 },\n { \"name\": \"Alexandria\", \"population\": 4.58 },\n { \"name\": \"Abidjan\", \"population\": 4.4 },\n { \"name\": \"Casablanca\", \"population\": 3.98 }\n];\nfunction searchCity (name) {\n for (var i = 0; i < lst.length; i++) {\n if(lst[i].name==name)\n return i;\n }\n}\n"
+ ],
+ "tests": [
+ {
+ "text": "'searchCity should be a function.'",
+ "testString": "assert(typeof searchCity=='function','searchCity should be a function.');"
+ },
+ {
+ "text": "'searchCity(\"Dar Es Salaam\") should return a number.'",
+ "testString": "assert(typeof searchCity(\"Dar Es Salaam\") == 'number','searchCity(\"Dar Es Salaam\") should return a number.');"
+ },
+ {
+ "text": "'searchCity(\"Dar Es Salaam\") should return 6.'",
+ "testString": "assert.equal(searchCity(\"Dar Es Salaam\"),6,'searchCity(\"Dar Es Salaam\") should return 6.');"
+ },
+ {
+ "text": "'searchCity(\"Casablanca\") should return 9.'",
+ "testString": "assert.equal(searchCity(\"Casablanca\"),9,'searchCity(\"Casablanca\") should return 9.');"
+ },
+ {
+ "text": "'searchCity(\"Cairo\") should return 1.'",
+ "testString": "assert.equal(searchCity(\"Cairo\"),1,'searchCity(\"Cairo\") should return 1.');"
+ },
+ {
+ "text": "'searchCity(\"Mogadishu\") should return 4.'",
+ "testString": "assert.equal(searchCity(\"Mogadishu\"),4,'searchCity(\"Mogadishu\") should return 4.');"
+ },
+ {
+ "text": "'searchCity(\"Lagos\") should return 0.'",
+ "testString": "assert.equal(searchCity(\"Lagos\"),0,'searchCity(\"Lagos\") should return 0.');"
+ }
+ ],
+ "id": "5a23c84252665b21eecc7fcf",
+ "challengeType": 5,
+ "releasedOn": "September 8, 2018",
+ "files": {
+ "indexjs": {
+ "key": "indexjs",
+ "ext": "js",
+ "name": "index",
+ "contents": [
+ "var lst = [",
+ " { \"name\": \"Lagos\", \"population\": 21.0 },",
+ " { \"name\": \"Cairo\", \"population\": 15.2 },",
+ " { \"name\": \"Kinshasa-Brazzaville\", \"population\": 11.3 },",
+ " { \"name\": \"Greater Johannesburg\", \"population\": 7.55 },",
+ " { \"name\": \"Mogadishu\", \"population\": 5.85 },",
+ " { \"name\": \"Khartoum-Omdurman\", \"population\": 4.98 },",
+ " { \"name\": \"Dar Es Salaam\", \"population\": 4.7 },",
+ " { \"name\": \"Alexandria\", \"population\": 4.58 },",
+ " { \"name\": \"Abidjan\", \"population\": 4.4 },",
+ " { \"name\": \"Casablanca\", \"population\": 3.98 }",
+ "];",
+ "",
+ "function searchCity (name) {",
+ " // Good luck!",
+ "}"
+ ],
+ "head": [],
+ "tail": []
+ }
+ }
+ },
+ {
+ "title": "Self-describing numbers",
+ "description": [
+ "There are several so-called \"self-describing\" or \"self-descriptive\" integers.",
+ "An integer is said to be \"self-describing\" if it has the property that, when digit positions are labeled 0 to N-1, the digit in each position is equal to the number of times that that digit appears in the number.",
+ "For example, 2020 is a four-digit self describing number:
isSelfDescribing should be a function.'",
+ "testString": "assert(typeof isSelfDescribing=='function','isSelfDescribing should be a function.');"
+ },
+ {
+ "text": "'isSelfDescribing(2020) should return a boolean.'",
+ "testString": "assert(typeof isSelfDescribing(2020) == 'boolean','isSelfDescribing(2020) should return a boolean.');"
+ },
+ {
+ "text": "'isSelfDescribing(2020) should return true.'",
+ "testString": "assert.equal(isSelfDescribing(2020),true,'isSelfDescribing(2020) should return true.');"
+ },
+ {
+ "text": "'isSelfDescribing(26) should return false.'",
+ "testString": "assert.equal(isSelfDescribing(26),false,'isSelfDescribing(26) should return false.');"
+ },
+ {
+ "text": "'isSelfDescribing(55) should return false.'",
+ "testString": "assert.equal(isSelfDescribing(55),false,'isSelfDescribing(55) should return false.');"
+ },
+ {
+ "text": "'isSelfDescribing(1) should return false.'",
+ "testString": "assert.equal(isSelfDescribing(1),false,'isSelfDescribing(1) should return false.');"
+ },
+ {
+ "text": "'isSelfDescribing(1210) should return true.'",
+ "testString": "assert.equal(isSelfDescribing(1210),true,'isSelfDescribing(1210) should return true.');"
+ },
+ {
+ "text": "'isSelfDescribing(10) should return false.'",
+ "testString": "assert.equal(isSelfDescribing(10),false,'isSelfDescribing(10) should return false.');"
+ },
+ {
+ "text": "'isSelfDescribing(2020) should return true.'",
+ "testString": "assert.equal(isSelfDescribing(2020),true,'isSelfDescribing(2020) should return true.');"
+ },
+ {
+ "text": "'isSelfDescribing(33) should return false.'",
+ "testString": "assert.equal(isSelfDescribing(33),false,'isSelfDescribing(33) should return false.');"
+ },
+ {
+ "text": "'isSelfDescribing(21200) should return true.'",
+ "testString": "assert.equal(isSelfDescribing(21200),true,'isSelfDescribing(21200) should return true.');"
+ },
+ {
+ "text": "'isSelfDescribing(3211000) should return true.'",
+ "testString": "assert.equal(isSelfDescribing(3211000),true,'isSelfDescribing(3211000) should return true.');"
+ }
+ ],
+ "id": "5a23c84252665b21eecc7fd3",
+ "challengeType": 5,
+ "releasedOn": "September 8, 2018",
+ "files": {
+ "indexjs": {
+ "key": "indexjs",
+ "ext": "js",
+ "name": "index",
+ "contents": [
+ "function isSelfDescribing (n) {",
+ " // Good luck!",
+ "}"
+ ],
+ "head": [],
+ "tail": []
+ }
+ }
+ },
+ {
+ "title": "Self-referential sequence",
+ "description": [
+ "There are several ways to generate a self-referential sequence. One very common one (the Look-and-say sequence) is to start with a positive integer, then generate the next term by concatenating enumerated groups of adjacent alike digits:",
+ "0, 10, 1110, 3110, 132110, 1113122110, 311311222110 ...",
+ "The terms generated grow in length geometrically and never converge.",
+ "Another way to generate a self-referential sequence is to summarize the previous term.",
+ "Count how many of each alike digit there is, then concatenate the sum and digit for each of the sorted enumerated digits. Note that the first five terms are the same as for the previous sequence.",
+ "0, 10, 1110, 3110, 132110, 13123110, 23124110 ... see The On-Line Encyclopedia of Integer Sequences",
+ "Sort the digits largest to smallest. Do not include counts of digits that do not appear in the previous term.",
+ "Depending on the seed value, series generated this way always either converge to a stable value or to a short cyclical pattern. (For our purposes, I'll use converge to mean an element matches a previously seen element.) The sequence shown, with a seed value of 0, converges to a stable value of 1433223110 after 11 iterations. The seed value that converges most quickly is 22. It goes stable after the first element. (The next element is 22, which has been seen before.)",
+ "Task:",
+ " Write a function that takes the seed value as parameter and returns the self referential sequence as an array."
+ ],
+ "solutions": [
+ "function selfReferential (n) {\n var descending, i, incr, j, max_i, max_len, max_seq, seq, sequence,\n indexOf = [].indexOf || function(item) {\n for (var i = 0, l = this.length; i < l; i++) {\n if (i in this && this[i] === item) return i;\n }\n return -1;\n };\n\n sequence = function(n) {\n var c, cnt, cnts, d, digit, i, j, l, len, new_cnts, ref, s, seq;\n cnts = {};\n ref = n.toString();\n for (j = 0, len = ref.length; j < len; j++) {\n c = ref[j];\n d = parseInt(c);\n incr(cnts, d);\n }\n seq = [];\n while (true) {\n s = '';\n for (i = l = 9; l >= 0; i = --l) {\n if (cnts[i]) {\n s += \"\" + cnts[i] + i;\n }\n }\n if (indexOf.call(seq, s) >= 0) {\n break;\n }\n seq.push(s);\n new_cnts = {};\n for (digit in cnts) {\n cnt = cnts[digit];\n incr(new_cnts, cnt);\n incr(new_cnts, digit);\n }\n cnts = new_cnts;\n }\n return seq;\n };\n\n incr = function(h, k) {\n if (h[k] == null) {\n h[k] = 0;\n }\n return h[k] += 1;\n };\n\n descending = function(n) {\n var tens;\n if (n < 10) {\n return true;\n }\n tens = n / 10;\n if (n % 10 > tens % 10) {\n return false;\n }\n return descending(tens);\n };\n\n return sequence(n)\n}\n"
+ ],
+ "tests": [
+ {
+ "text": "'selfReferential should be a function.'",
+ "testString": "assert(typeof selfReferential=='function','selfReferential should be a function.');"
+ },
+ {
+ "text": "'selfReferential(40) should return a array.'",
+ "testString": "assert(Array.isArray(selfReferential(40)),'selfReferential(40) should return a array.');"
+ },
+ {
+ "text": "'selfReferential(40) should return [\"1410\",\"142110\",\"14123110\",\"1413124110\",\"2413125110\",\"151413224110\",\"152413225110\",\"251413324110\",\"152423224110\",\"152413423110\"].'",
+ "testString": "assert.deepEqual(selfReferential(40),[\"1410\",\"142110\",\"14123110\",\"1413124110\",\"2413125110\",\"151413224110\",\"152413225110\",\"251413324110\",\"152423224110\",\"152413423110\"],'selfReferential(40) should return [\"1410\",\"142110\",\"14123110\",\"1413124110\",\"2413125110\",\"151413224110\",\"152413225110\",\"251413324110\",\"152423224110\",\"152413423110\"].');"
+ },
+ {
+ "text": "'selfReferential(132110) should return [\"13123110\",\"23124110\",\"1413223110\",\"1423224110\",\"2413323110\",\"1433223110\"].'",
+ "testString": "assert.deepEqual(selfReferential(132110),[\"13123110\",\"23124110\",\"1413223110\",\"1423224110\",\"2413323110\",\"1433223110\"],'selfReferential(132110) should return [\"13123110\",\"23124110\",\"1413223110\",\"1423224110\",\"2413323110\",\"1433223110\"].');"
+ },
+ {
+ "text": "'selfReferential(132211) should return [\"132231\",\"232221\",\"134211\",\"14131231\",\"14231241\",\"24132231\",\"14233221\"].'",
+ "testString": "assert.deepEqual(selfReferential(132211),[\"132231\",\"232221\",\"134211\",\"14131231\",\"14231241\",\"24132231\",\"14233221\"],'selfReferential(132211) should return [\"132231\",\"232221\",\"134211\",\"14131231\",\"14231241\",\"24132231\",\"14233221\"].');"
+ },
+ {
+ "text": "'selfReferential(1413223110) should return [\"1423224110\",\"2413323110\",\"1433223110\"].'",
+ "testString": "assert.deepEqual(selfReferential(1413223110),[\"1423224110\",\"2413323110\",\"1433223110\"],'selfReferential(1413223110) should return [\"1423224110\",\"2413323110\",\"1433223110\"].');"
+ },
+ {
+ "text": "'selfReferential(251413126110) should return [\"16151413225110\",\"16251413226110\",\"26151413325110\",\"16251423225110\",\"16251413424110\",\"16153413225110\"].'",
+ "testString": "assert.deepEqual(selfReferential(251413126110),[\"16151413225110\",\"16251413226110\",\"26151413325110\",\"16251423225110\",\"16251413424110\",\"16153413225110\"],'selfReferential(251413126110) should return [\"16151413225110\",\"16251413226110\",\"26151413325110\",\"16251423225110\",\"16251413424110\",\"16153413225110\"].');"
+ }
+ ],
+ "id": "5a23c84252665b21eecc7fd4",
+ "challengeType": 5,
+ "releasedOn": "September 8, 2018",
+ "files": {
+ "indexjs": {
+ "key": "indexjs",
+ "ext": "js",
+ "name": "index",
+ "contents": [
+ "function selfReferential (n) {",
+ " // Good luck!",
+ "}"
+ ],
+ "head": [],
+ "tail": []
+ }
+ }
+ },
+ {
+ "title": "Semiprime",
+ "description": [
+ "Semiprime numbers are natural numbers that are products of exactly two (possibly equal) prime numbers.",
+ " 1679 = 23 × 73",
+ "Write a function determining whether a given number is semiprime."
+ ],
+ "solutions": [
+ "function isSemiPrime (n) {\n if (n <= 3)\n return false;\n\n var ans = [];\n var done = false;\n while (!done)\n {\n if (n%2 === 0){\n ans.push(2);\n n /= 2;\n continue;\n }\n if (n%3 === 0){\n ans.push(3);\n n /= 3;\n continue;\n }\n if ( n === 1)\n return ans.length==2;\n var sr = Math.sqrt(n);\n done = true;\n // try to divide the checked number by all numbers till its square root.\n for (var i=6; i<=sr; i+=6){\n if (n%(i-1) === 0){ // is n divisible by i-1?\n ans.push( (i-1) );\n n /= (i-1);\n done = false;\n break;\n }\n if (n%(i+1) === 0){ // is n divisible by i+1?\n ans.push( (i+1) );\n n /= (i+1);\n done = false;\n break;\n }\n }\n }\n ans.push( n );\n return ans.length==2;\n}\n"
+ ],
+ "tests": [
+ {
+ "text": "'isSemiPrime should be a function.'",
+ "testString": "assert(typeof isSemiPrime=='function','isSemiPrime should be a function.');"
+ },
+ {
+ "text": "'isSemiPrime(100) should return a boolean.'",
+ "testString": "assert(typeof isSemiPrime(100) == 'boolean','isSemiPrime(100) should return a boolean.');"
+ },
+ {
+ "text": "'isSemiPrime(100) should return false.'",
+ "testString": "assert.equal(isSemiPrime(100),false,'isSemiPrime(100) should return false.');"
+ },
+ {
+ "text": "'isSemiPrime(504) should return false.'",
+ "testString": "assert.equal(isSemiPrime(504),false,'isSemiPrime(504) should return false.');"
+ },
+ {
+ "text": "'isSemiPrime(4) should return true.'",
+ "testString": "assert.equal(isSemiPrime(4),true,'isSemiPrime(4) should return true.');"
+ },
+ {
+ "text": "'isSemiPrime(46) should return true.'",
+ "testString": "assert.equal(isSemiPrime(46),true,'isSemiPrime(46) should return true.');"
+ },
+ {
+ "text": "'isSemiPrime(13) should return false.'",
+ "testString": "assert.equal(isSemiPrime(13),false,'isSemiPrime(13) should return false.');"
+ },
+ {
+ "text": "'isSemiPrime(74) should return true.'",
+ "testString": "assert.equal(isSemiPrime(74),true,'isSemiPrime(74) should return true.');"
+ },
+ {
+ "text": "'isSemiPrime(1679) should return true.'",
+ "testString": "assert.equal(isSemiPrime(1679),true,'isSemiPrime(1679) should return true.');"
+ },
+ {
+ "text": "'isSemiPrime(2) should return false.'",
+ "testString": "assert.equal(isSemiPrime(2),false,'isSemiPrime(2) should return false.');"
+ },
+ {
+ "text": "'isSemiPrime(95) should return true.'",
+ "testString": "assert.equal(isSemiPrime(95),true,'isSemiPrime(95) should return true.');"
+ },
+ {
+ "text": "'isSemiPrime(124) should return false.'",
+ "testString": "assert.equal(isSemiPrime(124),false,'isSemiPrime(124) should return false.');"
+ }
+ ],
+ "id": "5a23c84252665b21eecc7fd5",
+ "challengeType": 5,
+ "releasedOn": "September 8, 2018",
+ "files": {
+ "indexjs": {
+ "key": "indexjs",
+ "ext": "js",
+ "name": "index",
+ "contents": [
+ "function isSemiPrime (n) {",
+ " // Good luck!",
+ "}"
+ ],
+ "head": [],
+ "tail": []
+ }
+ }
+ },
+ {
+ "title": "Set consolidation",
+ "description": [
+ "Given two sets of items then if any item is common to any set then the result of applying consolidation to those sets is a set of sets whose contents is:",
+ "setConsolidation should be a function.'",
+ "testString": "assert(typeof setConsolidation=='function','setConsolidation should be a function.');"
+ },
+ {
+ "text": "'setConsolidation([\"AB\",\"CD\"]) should return a array.'",
+ "testString": "assert(Array.isArray(setConsolidation([\"AB\",\"CD\"])),'setConsolidation([\"AB\",\"CD\"]) should return a array.');"
+ },
+ {
+ "text": "'setConsolidation([\"AB\",\"CD\"]) should return [[\"C\",\"D\"],[\"A\",\"B\"]].'",
+ "testString": "assert.deepEqual(setConsolidation([\"AB\",\"CD\"]),[[\"C\",\"D\"],[\"A\",\"B\"]],'setConsolidation([\"AB\",\"CD\"]) should return [[\"C\",\"D\"],[\"A\",\"B\"]].');"
+ },
+ {
+ "text": "'setConsolidation([\"AB\",\"BD\"]) should return [[\"A\",\"B\",\"D\"]].'",
+ "testString": "assert.deepEqual(setConsolidation([\"AB\",\"BD\"]),[[\"A\",\"B\",\"D\"]],'setConsolidation([\"AB\",\"BD\"]) should return [[\"A\",\"B\",\"D\"]].');"
+ },
+ {
+ "text": "'setConsolidation([\"AB\",\"CD\",\"DB\"]) should return [[\"A\",\"B\",\"C\",\"D\"]].'",
+ "testString": "assert.deepEqual(setConsolidation([\"AB\",\"CD\",\"DB\"]),[[\"A\",\"B\",\"C\",\"D\"]],'setConsolidation([\"AB\",\"CD\",\"DB\"]) should return [[\"A\",\"B\",\"C\",\"D\"]].');"
+ },
+ {
+ "text": "'setConsolidation([\"HIK\",\"AB\",\"CD\",\"DB\",\"FGH\"]) should return [[\"F\",\"G\",\"H\",\"I\",\"K\"],[\"A\",\"B\",\"C\",\"D\"]].'",
+ "testString": "assert.deepEqual(setConsolidation([\"HIK\",\"AB\",\"CD\",\"DB\",\"FGH\"]),[[\"F\",\"G\",\"H\",\"I\",\"K\"],[\"A\",\"B\",\"C\",\"D\"]],'setConsolidation([\"HIK\",\"AB\",\"CD\",\"DB\",\"FGH\"]) should return [[\"F\",\"G\",\"H\",\"I\",\"K\"],[\"A\",\"B\",\"C\",\"D\"]].');"
+ }
+ ],
+ "id": "5a23c84252665b21eecc7fdb",
+ "challengeType": 5,
+ "releasedOn": "September 8, 2018",
+ "files": {
+ "indexjs": {
+ "key": "indexjs",
+ "ext": "js",
+ "name": "index",
+ "contents": [
+ "function setConsolidation (sets) {",
+ " // Good luck!",
+ "}"
+ ],
+ "head": [],
+ "tail": []
+ }
+ }
+ },
+ {
+ "title": "Set of real numbers",
+ "description": [
+ "All real numbers form the uncountable set R. Among its subsets, relatively simple are the convex sets, each expressed as a range between two real numbers a and b where a ≤ b. There are actually four cases for the meaning of \"between\", depending on open or closed boundary:",
+ "realSet should be a function.'",
+ "testString": "assert(typeof realSet=='function','realSet should be a function.');"
+ },
+ {
+ "text": "'realSet({\"low\":0,\"high\":1,\"rangeType\":2},{\"low\":0,\"high\":2,\"rangeType\":3},\"union\",[1,2,3]) should return a array.'",
+ "testString": "assert(Array.isArray(realSet({\"low\":0,\"high\":1,\"rangeType\":2},{\"low\":0,\"high\":2,\"rangeType\":3},\"union\",[1,2,3])),'realSet({\"low\":0,\"high\":1,\"rangeType\":2},{\"low\":0,\"high\":2,\"rangeType\":3},\"union\",[1,2,3]) should return a array.');"
+ },
+ {
+ "text": "'realSet({\"low\":0,\"high\":1,\"rangeType\":2},{\"low\":0,\"high\":2,\"rangeType\":3},\"union\",[1,2,3]) should return [true,false,false].'",
+ "testString": "assert.deepEqual(realSet({\"low\":0,\"high\":1,\"rangeType\":2},{\"low\":0,\"high\":2,\"rangeType\":3},\"union\",[1,2,3]),[true,false,false],'realSet({\"low\":0,\"high\":1,\"rangeType\":2},{\"low\":0,\"high\":2,\"rangeType\":3},\"union\",[1,2,3]) should return [true,false,false].');"
+ },
+ {
+ "text": "'realSet({\"low\":0,\"high\":2,\"rangeType\":3},{\"low\":1,\"high\":2,\"rangeType\":2},\"intersect\",[0,1,2]) should return [false,false,false].'",
+ "testString": "assert.deepEqual(realSet({\"low\":0,\"high\":2,\"rangeType\":3},{\"low\":1,\"high\":2,\"rangeType\":2},\"intersect\",[0,1,2]),[false,false,false],'realSet({\"low\":0,\"high\":2,\"rangeType\":3},{\"low\":1,\"high\":2,\"rangeType\":2},\"intersect\",[0,1,2]) should return [false,false,false].');"
+ },
+ {
+ "text": "'realSet({\"low\":0,\"high\":3,\"rangeType\":3},{\"low\":0,\"high\":1,\"rangeType\":1},\"subtract\",[0,1,2]) should return [true,true,true].'",
+ "testString": "assert.deepEqual(realSet({\"low\":0,\"high\":3,\"rangeType\":3},{\"low\":0,\"high\":1,\"rangeType\":1},\"subtract\",[0,1,2]),[true,true,true],'realSet({\"low\":0,\"high\":3,\"rangeType\":3},{\"low\":0,\"high\":1,\"rangeType\":1},\"subtract\",[0,1,2]) should return [true,true,true].');"
+ },
+ {
+ "text": "'realSet({\"low\":0,\"high\":3,\"rangeType\":3},{\"low\":0,\"high\":1,\"rangeType\":0},\"subtract\",[0,1,2]) should return [false,false,true].'",
+ "testString": "assert.deepEqual(realSet({\"low\":0,\"high\":3,\"rangeType\":3},{\"low\":0,\"high\":1,\"rangeType\":0},\"subtract\",[0,1,2]),[false,false,true],'realSet({\"low\":0,\"high\":3,\"rangeType\":3},{\"low\":0,\"high\":1,\"rangeType\":0},\"subtract\",[0,1,2]) should return [false,false,true].');"
+ },
+ {
+ "text": "'realSet({\"low\":0,\"high\":33,\"rangeType\":1},{\"low\":30,\"high\":31,\"rangeType\":0},\"intersect\",[30,31,32]) should return [true,true,false].'",
+ "testString": "assert.deepEqual(realSet({\"low\":0,\"high\":33,\"rangeType\":1},{\"low\":30,\"high\":31,\"rangeType\":0},\"intersect\",[30,31,32]),[true,true,false],'realSet({\"low\":0,\"high\":33,\"rangeType\":1},{\"low\":30,\"high\":31,\"rangeType\":0},\"intersect\",[30,31,32]) should return [true,true,false].');"
+ }
+ ],
+ "id": "5a23c84252665b21eecc7fdc",
+ "challengeType": 5,
+ "releasedOn": "September 8, 2018",
+ "files": {
+ "indexjs": {
+ "key": "indexjs",
+ "ext": "js",
+ "name": "index",
+ "contents": [
+ "function realSet (set1, set2, operation, values) {",
+ " // Good luck!",
+ "}"
+ ],
+ "head": [],
+ "tail": []
+ }
+ }
+ },
+ {
+ "title": "SHA-1",
+ "description": [
+ "SHA-1 or SHA1 is a one-way hash function; it computes a 160-bit message digest.",
+ "SHA-1 often appears in security protocols; for example, many HTTPS websites use RSA with SHA-1 to secure their connections.",
+ "BitTorrent uses SHA-1 to verify downloads.",
+ "Git and Mercurial use SHA-1 digests to identify commits.",
+ "A US government standard, FIPS 180-1, defines SHA-1.",
+ "Find the SHA-1 message digest for a given string."
+ ],
+ "solutions": [
+ "function SHA1 (input) {\n var hexcase = 0;\n var b64pad = \"\";\n var chrsz = 8;\n\n function hex_sha1(s) {\n return binb2hex(core_sha1(str2binb(s), s.length * chrsz));\n }\n\n function core_sha1(x, len) {\n x[len >> 5] |= 0x80 << (24 - len % 32);\n x[((len + 64 >> 9) << 4) + 15] = len;\n\n var w = Array(80);\n var a = 1732584193;\n var b = -271733879;\n var c = -1732584194;\n var d = 271733878;\n var e = -1009589776;\n\n for (var i = 0; i < x.length; i += 16) {\n var olda = a;\n var oldb = b;\n var oldc = c;\n var oldd = d;\n var olde = e;\n\n for (var j = 0; j < 80; j++) {\n if (j < 16) w[j] = x[i + j];\n else w[j] = rol(w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16], 1);\n var t = safe_add(safe_add(rol(a, 5), sha1_ft(j, b, c, d)),\n safe_add(safe_add(e, w[j]), sha1_kt(j)));\n e = d;\n d = c;\n c = rol(b, 30);\n b = a;\n a = t;\n }\n\n a = safe_add(a, olda);\n b = safe_add(b, oldb);\n c = safe_add(c, oldc);\n d = safe_add(d, oldd);\n e = safe_add(e, olde);\n }\n return Array(a, b, c, d, e);\n\n }\n\n function sha1_ft(t, b, c, d) {\n if (t < 20) return (b & c) | ((~b) & d);\n if (t < 40) return b ^ c ^ d;\n if (t < 60) return (b & c) | (b & d) | (c & d);\n return b ^ c ^ d;\n }\n\n function sha1_kt(t) {\n return (t < 20) ? 1518500249 : (t < 40) ? 1859775393 :\n (t < 60) ? -1894007588 : -899497514;\n }\n\n function safe_add(x, y) {\n var lsw = (x & 0xFFFF) + (y & 0xFFFF);\n var msw = (x >> 16) + (y >> 16) + (lsw >> 16);\n return (msw << 16) | (lsw & 0xFFFF);\n }\n\n function rol(num, cnt) {\n return (num << cnt) | (num >>> (32 - cnt));\n }\n\n function str2binb(str) {\n var bin = Array();\n var mask = (1 << chrsz) - 1;\n for (var i = 0; i < str.length * chrsz; i += chrsz)\n bin[i >> 5] |= (str.charCodeAt(i / chrsz) & mask) << (24 - i % 32);\n return bin;\n }\n\n function binb2hex(binarray) {\n var hex_tab = hexcase ? \"0123456789ABCDEF\" : \"0123456789abcdef\";\n var str = \"\";\n for (var i = 0; i < binarray.length * 4; i++) {\n str += hex_tab.charAt((binarray[i >> 2] >> ((3 - i % 4) * 8 + 4)) & 0xF) +\n hex_tab.charAt((binarray[i >> 2] >> ((3 - i % 4) * 8)) & 0xF);\n }\n return str;\n }\n\n return hex_sha1(input);\n}\n"
+ ],
+ "tests": [
+ {
+ "text": "'SHA1 should be a function.'",
+ "testString": "assert(typeof SHA1=='function','SHA1 should be a function.');"
+ },
+ {
+ "text": "'SHA1(\"abc\") should return a string.'",
+ "testString": "assert(typeof SHA1(\"abc\") == 'string','SHA1(\"abc\") should return a string.');"
+ },
+ {
+ "text": "'SHA1(\"abc\") should return \"a9993e364706816aba3e25717850c26c9cd0d89d\".'",
+ "testString": "assert.equal(SHA1(\"abc\"),\"a9993e364706816aba3e25717850c26c9cd0d89d\",'SHA1(\"abc\") should return \"a9993e364706816aba3e25717850c26c9cd0d89d\".');"
+ },
+ {
+ "text": "'SHA1(\"Rosetta Code\") should return \"48c98f7e5a6e736d790ab740dfc3f51a61abe2b5\".'",
+ "testString": "assert.equal(SHA1(\"Rosetta Code\"),\"48c98f7e5a6e736d790ab740dfc3f51a61abe2b5\",'SHA1(\"Rosetta Code\") should return \"48c98f7e5a6e736d790ab740dfc3f51a61abe2b5\".');"
+ },
+ {
+ "text": "'SHA1(\"Hello world\") should return \"7b502c3a1f48c8609ae212cdfb639dee39673f5e\".'",
+ "testString": "assert.equal(SHA1(\"Hello world\"),\"7b502c3a1f48c8609ae212cdfb639dee39673f5e\",'SHA1(\"Hello world\") should return \"7b502c3a1f48c8609ae212cdfb639dee39673f5e\".');"
+ },
+ {
+ "text": "'SHA1(\"Programming\") should return \"d1a946bf8b2f2a7292c250063ee28989d742cd4b\".'",
+ "testString": "assert.equal(SHA1(\"Programming\"),\"d1a946bf8b2f2a7292c250063ee28989d742cd4b\",'SHA1(\"Programming\") should return \"d1a946bf8b2f2a7292c250063ee28989d742cd4b\".');"
+ },
+ {
+ "text": "'SHA1(\"is Awesome\") should return \"6537205da59c72b57ed3881843c2d24103d683a3\".'",
+ "testString": "assert.equal(SHA1(\"is Awesome\"),\"6537205da59c72b57ed3881843c2d24103d683a3\",'SHA1(\"is Awesome\") should return \"6537205da59c72b57ed3881843c2d24103d683a3\".');"
+ }
+ ],
+ "id": "5b93a3a3d73df01ea208e89e",
+ "challengeType": 5,
+ "releasedOn": "September 8, 2018",
+ "files": {
+ "indexjs": {
+ "key": "indexjs",
+ "ext": "js",
+ "name": "index",
+ "contents": [
+ "function SHA1 (input) {",
+ " // Good luck!",
+ "}"
+ ],
+ "head": [],
+ "tail": []
+ }
+ }
+ },
+ {
+ "title": "SHA-256",
+ "description": [
+ "SHA-256 is the recommended stronger alternative to SHA-1.",
+ "Write a function that takes a string as a parameter and returns its the SHA-256 digest."
+ ],
+ "solutions": [
+ "function SHA256 (input) {\n var chrsz = 8;\n var hexcase = 0;\n\n function safe_add(x, y) {\n var lsw = (x & 0xFFFF) + (y & 0xFFFF);\n var msw = (x >> 16) + (y >> 16) + (lsw >> 16);\n return (msw << 16) | (lsw & 0xFFFF);\n }\n\n function S(X, n) {\n return (X >>> n) | (X << (32 - n));\n }\n\n function R(X, n) {\n return (X >>> n);\n }\n\n function Ch(x, y, z) {\n return ((x & y) ^ ((~x) & z));\n }\n\n function Maj(x, y, z) {\n return ((x & y) ^ (x & z) ^ (y & z));\n }\n\n function Sigma0256(x) {\n return (S(x, 2) ^ S(x, 13) ^ S(x, 22));\n }\n\n function Sigma1256(x) {\n return (S(x, 6) ^ S(x, 11) ^ S(x, 25));\n }\n\n function Gamma0256(x) {\n return (S(x, 7) ^ S(x, 18) ^ R(x, 3));\n }\n\n function Gamma1256(x) {\n return (S(x, 17) ^ S(x, 19) ^ R(x, 10));\n }\n\n function Sigma0512(x) {\n return (S(x, 28) ^ S(x, 34) ^ S(x, 39));\n }\n\n function Sigma1512(x) {\n return (S(x, 14) ^ S(x, 18) ^ S(x, 41));\n }\n\n function Gamma0512(x) {\n return (S(x, 1) ^ S(x, 8) ^ R(x, 7));\n }\n\n function Gamma1512(x) {\n return (S(x, 19) ^ S(x, 61) ^ R(x, 6));\n }\n\n function core_sha256(m, l) {\n var K = new Array(0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5, 0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5, 0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3, 0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174, 0xE49B69C1, 0xEFBE4786, 0xFC19DC6, 0x240CA1CC, 0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA, 0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7, 0xC6E00BF3, 0xD5A79147, 0x6CA6351, 0x14292967, 0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13, 0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85, 0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3, 0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070, 0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5, 0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3, 0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208, 0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2);\n var HASH = new Array(0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A, 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19);\n var W = new Array(64);\n var a, b, c, d, e, f, g, h, i, j;\n var T1, T2;\n\n m[l >> 5] |= 0x80 << (24 - l % 32);\n m[((l + 64 >> 9) << 4) + 15] = l;\n\n for (var i = 0; i < m.length; i += 16) {\n a = HASH[0];\n b = HASH[1];\n c = HASH[2];\n d = HASH[3];\n e = HASH[4];\n f = HASH[5];\n g = HASH[6];\n h = HASH[7];\n\n for (var j = 0; j < 64; j++) {\n if (j < 16) W[j] = m[j + i];\n else W[j] = safe_add(safe_add(safe_add(Gamma1256(W[j - 2]), W[j - 7]), Gamma0256(W[j - 15])), W[j - 16]);\n\n T1 = safe_add(safe_add(safe_add(safe_add(h, Sigma1256(e)), Ch(e, f, g)), K[j]), W[j]);\n T2 = safe_add(Sigma0256(a), Maj(a, b, c));\n\n h = g;\n g = f;\n f = e;\n e = safe_add(d, T1);\n d = c;\n c = b;\n b = a;\n a = safe_add(T1, T2);\n }\n\n HASH[0] = safe_add(a, HASH[0]);\n HASH[1] = safe_add(b, HASH[1]);\n HASH[2] = safe_add(c, HASH[2]);\n HASH[3] = safe_add(d, HASH[3]);\n HASH[4] = safe_add(e, HASH[4]);\n HASH[5] = safe_add(f, HASH[5]);\n HASH[6] = safe_add(g, HASH[6]);\n HASH[7] = safe_add(h, HASH[7]);\n }\n return HASH;\n }\n\n function core_sha512(m, l) {\n var K = new Array(0x428a2f98d728ae22, 0x7137449123ef65cd, 0xb5c0fbcfec4d3b2f, 0xe9b5dba58189dbbc, 0x3956c25bf348b538, 0x59f111f1b605d019, 0x923f82a4af194f9b, 0xab1c5ed5da6d8118, 0xd807aa98a3030242, 0x12835b0145706fbe, 0x243185be4ee4b28c, 0x550c7dc3d5ffb4e2, 0x72be5d74f27b896f, 0x80deb1fe3b1696b1, 0x9bdc06a725c71235, 0xc19bf174cf692694, 0xe49b69c19ef14ad2, 0xefbe4786384f25e3, 0x0fc19dc68b8cd5b5, 0x240ca1cc77ac9c65, 0x2de92c6f592b0275, 0x4a7484aa6ea6e483, 0x5cb0a9dcbd41fbd4, 0x76f988da831153b5, 0x983e5152ee66dfab, 0xa831c66d2db43210, 0xb00327c898fb213f, 0xbf597fc7beef0ee4, 0xc6e00bf33da88fc2, 0xd5a79147930aa725, 0x06ca6351e003826f, 0x142929670a0e6e70, 0x27b70a8546d22ffc, 0x2e1b21385c26c926, 0x4d2c6dfc5ac42aed, 0x53380d139d95b3df, 0x650a73548baf63de, 0x766a0abb3c77b2a8, 0x81c2c92e47edaee6, 0x92722c851482353b, 0xa2bfe8a14cf10364, 0xa81a664bbc423001, 0xc24b8b70d0f89791, 0xc76c51a30654be30, 0xd192e819d6ef5218, 0xd69906245565a910, 0xf40e35855771202a, 0x106aa07032bbd1b8, 0x19a4c116b8d2d0c8, 0x1e376c085141ab53, 0x2748774cdf8eeb99, 0x34b0bcb5e19b48a8, 0x391c0cb3c5c95a63, 0x4ed8aa4ae3418acb, 0x5b9cca4f7763e373, 0x682e6ff3d6b2b8a3, 0x748f82ee5defb2fc, 0x78a5636f43172f60, 0x84c87814a1f0ab72, 0x8cc702081a6439ec, 0x90befffa23631e28, 0xa4506cebde82bde9, 0xbef9a3f7b2c67915, 0xc67178f2e372532b, 0xca273eceea26619c, 0xd186b8c721c0c207, 0xeada7dd6cde0eb1e, 0xf57d4f7fee6ed178, 0x06f067aa72176fba, 0x0a637dc5a2c898a6, 0x113f9804bef90dae, 0x1b710b35131c471b, 0x28db77f523047d84, 0x32caab7b40c72493, 0x3c9ebe0a15c9bebc, 0x431d67c49c100d4c, 0x4cc5d4becb3e42b6, 0x597f299cfc657e2a, 0x5fcb6fab3ad6faec, 0x6c44198c4a475817);\n var HASH = new Array(0x6a09e667f3bcc908, 0xbb67ae8584caa73b, 0x3c6ef372fe94f82b, 0xa54ff53a5f1d36f1, 0x510e527fade682d1, 0x9b05688c2b3e6c1f, 0x1f83d9abfb41bd6b, 0x5be0cd19137e2179);\n var W = new Array(80);\n var a, b, c, d, e, f, g, h, i, j;\n var T1, T2;\n\n }\n\n function str2binb(str) {\n var bin = Array();\n var mask = (1 << chrsz) - 1;\n for (var i = 0; i < str.length * chrsz; i += chrsz)\n bin[i >> 5] |= (str.charCodeAt(i / chrsz) & mask) << (24 - i % 32);\n return bin;\n }\n\n function binb2str(bin) {\n var str = \"\";\n var mask = (1 << chrsz) - 1;\n for (var i = 0; i < bin.length * 32; i += chrsz)\n str += String.fromCharCode((bin[i >> 5] >>> (24 - i % 32)) & mask);\n return str;\n }\n\n function binb2hex(binarray) {\n var hex_tab = hexcase ? \"0123456789ABCDEF\" : \"0123456789abcdef\";\n var str = \"\";\n for (var i = 0; i < binarray.length * 4; i++) {\n str += hex_tab.charAt((binarray[i >> 2] >> ((3 - i % 4) * 8 + 4)) & 0xF) +\n hex_tab.charAt((binarray[i >> 2] >> ((3 - i % 4) * 8)) & 0xF);\n }\n return str;\n }\n\n function binb2b64(binarray) {\n var tab = \"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/\";\n var str = \"\";\n for (var i = 0; i < binarray.length * 4; i += 3) {\n var triplet = (((binarray[i >> 2] >> 8 * (3 - i % 4)) & 0xFF) << 16) |\n (((binarray[i + 1 >> 2] >> 8 * (3 - (i + 1) % 4)) & 0xFF) << 8) |\n ((binarray[i + 2 >> 2] >> 8 * (3 - (i + 2) % 4)) & 0xFF);\n for (var j = 0; j < 4; j++) {\n if (i * 8 + j * 6 > binarray.length * 32) str += \"\";\n else str += tab.charAt((triplet >> 6 * (3 - j)) & 0x3F);\n }\n }\n return str;\n }\n\n function hex_sha2(s) {\n return binb2hex(core_sha256(str2binb(s), s.length * chrsz));\n }\n\n function b64_sha2(s) {\n return binb2b64(core_sha256(str2binb(s), s.length * chrsz));\n }\n\n function str_sha2(s) {\n return binb2str(core_sha256(str2binb(s), s.length * chrsz));\n }\n return hex_sha2(input);\n}\n"
+ ],
+ "tests": [
+ {
+ "text": "assert(typeof SHA256 == 'function', 'SHA256 should be a function.'",
+ "testString": "assert(typeof SHA256 == 'function', 'SHA256 should be a function.');"
+ },
+ {
+ "text": "assert(typeof SHA256(\"Rosetta code\") == 'string', 'SHA256(\"Rosetta code\") should return a string.'",
+ "testString": "assert(typeof SHA256(\"Rosetta code\") == 'string', 'SHA256(\"Rosetta code\") should return a string.');"
+ },
+ {
+ "text": "assert.equal(SHA256(\"Rosetta code\"), \"764faf5c61ac315f1497f9dfa542713965b785e5cc2f707d6468d7d1124cdfcf\", 'SHA256(\"Rosetta code\") should return \"764faf5c61ac315f1497f9dfa542713965b785e5cc2f707d6468d7d1124cdfcf\".'",
+ "testString": "assert.equal(SHA256(\"Rosetta code\"), \"764faf5c61ac315f1497f9dfa542713965b785e5cc2f707d6468d7d1124cdfcf\", 'SHA256(\"Rosetta code\") should return \"764faf5c61ac315f1497f9dfa542713965b785e5cc2f707d6468d7d1124cdfcf\".');"
+ },
+ {
+ "text": "assert.equal(SHA256(\"SHA-256 Hash\"), \"bee8c0cabdcf8c7835f40217dd35a8b0dba9134520e633f1c57285f35ca7ee3e\", 'SHA256(\"SHA-256 Hash\") should return \"bee8c0cabdcf8c7835f40217dd35a8b0dba9134520e633f1c57285f35ca7ee3e\".'",
+ "testString": "assert.equal(SHA256(\"SHA-256 Hash\"), \"bee8c0cabdcf8c7835f40217dd35a8b0dba9134520e633f1c57285f35ca7ee3e\", 'SHA256(\"SHA-256 Hash\") should return \"bee8c0cabdcf8c7835f40217dd35a8b0dba9134520e633f1c57285f35ca7ee3e\".');"
+ },
+ {
+ "text": "assert.equal(SHA256(\"implementation\"), \"da31012c40330e7e21538e7dd57503b16e8a0839159e96137090cccc9910b171\", 'SHA256(\"implementation\") should return \"da31012c40330e7e21538e7dd57503b16e8a0839159e96137090cccc9910b171\".'",
+ "testString": "assert.equal(SHA256(\"implementation\"), \"da31012c40330e7e21538e7dd57503b16e8a0839159e96137090cccc9910b171\", 'SHA256(\"implementation\") should return \"da31012c40330e7e21538e7dd57503b16e8a0839159e96137090cccc9910b171\".');"
+ },
+ {
+ "text": "assert.equal(SHA256(\"algorithm\"), \"b1eb2ec8ac9f31ff7918231e67f96e6deda83a9ff33ed2c67443f1df81e5ed14\", 'SHA256(\"algorithm\") should return \"b1eb2ec8ac9f31ff7918231e67f96e6deda83a9ff33ed2c67443f1df81e5ed14\".'",
+ "testString": "assert.equal(SHA256(\"algorithm\"), \"b1eb2ec8ac9f31ff7918231e67f96e6deda83a9ff33ed2c67443f1df81e5ed14\", 'SHA256(\"algorithm\") should return \"b1eb2ec8ac9f31ff7918231e67f96e6deda83a9ff33ed2c67443f1df81e5ed14\".');"
+ },
+ {
+ "text": "assert.equal(SHA256(\"language\"), \"a4ef304ba42a200bafd78b046e0869af9183f6eee5524aead5dcb3a5ab5f8f3f\", 'SHA256(\"language\") should return \"a4ef304ba42a200bafd78b046e0869af9183f6eee5524aead5dcb3a5ab5f8f3f\".'",
+ "testString": "assert.equal(SHA256(\"language\"), \"a4ef304ba42a200bafd78b046e0869af9183f6eee5524aead5dcb3a5ab5f8f3f\", 'SHA256(\"language\") should return \"a4ef304ba42a200bafd78b046e0869af9183f6eee5524aead5dcb3a5ab5f8f3f\".');"
+ }
+ ],
+ "id": "5a23c84252665b21eecc7fe2",
+ "challengeType": 5,
+ "releasedOn": "September 8, 2018",
+ "files": {
+ "indexjs": {
+ "key": "indexjs",
+ "ext": "js",
+ "name": "index",
+ "contents": [
+ "function SHA256 (input) {",
+ " // Good luck!",
+ "}"
+ ],
+ "head": [],
+ "tail": []
+ }
+ }
+ },
+ {
+ "title": "Sieve of Eratosthenes",
+ "description": [
+ "The Sieve of Eratosthenes is a simple algorithm that finds the prime numbers up to a given integer.",
+ "Implement the Sieve of Eratosthenes algorithm, with the only allowed optimization that the outer loop can stop at the square root of the limit, and the inner loop may start at the square of the prime just found.",
+ "Write a function that takes a number as a parameter and returns an array containing prime numbers less than the given number using the above method."
+ ],
+ "solutions": [
+ "function eratosthenes (limit) {\n var primes = [];\n if (limit >= 2) {\n var sqrtlmt = Math.sqrt(limit) - 2;\n var nums = new Array();\n for (var i = 2; i <= limit; i++)\n nums.push(i);\n for (var i = 0; i <= sqrtlmt; i++) {\n var p = nums[i]\n if (p)\n for (var j = p * p - 2; j < nums.length; j += p)\n nums[j] = 0;\n }\n for (var i = 0; i < nums.length; i++) {\n var p = nums[i];\n if (p)\n primes.push(p);\n }\n }\n return primes;\n}\n"
+ ],
+ "tests": [
+ {
+ "text": "'eratosthenes should be a function.'",
+ "testString": "assert(typeof eratosthenes=='function','eratosthenes should be a function.');"
+ },
+ {
+ "text": "'eratosthenes(10) should return a array.'",
+ "testString": "assert(Array.isArray(eratosthenes(10)),'eratosthenes(10) should return a array.');"
+ },
+ {
+ "text": "'eratosthenes(10) should return [2,3,5,7].'",
+ "testString": "assert.deepEqual(eratosthenes(10),[2,3,5,7],'eratosthenes(10) should return [2,3,5,7].');"
+ },
+ {
+ "text": "'eratosthenes(20) should return [2,3,5,7,11,13,17,19].'",
+ "testString": "assert.deepEqual(eratosthenes(20),[2,3,5,7,11,13,17,19],'eratosthenes(20) should return [2,3,5,7,11,13,17,19].');"
+ },
+ {
+ "text": "'eratosthenes(30) should return [2,3,5,7,11,13,17,19,23,29].'",
+ "testString": "assert.deepEqual(eratosthenes(30),[2,3,5,7,11,13,17,19,23,29],'eratosthenes(30) should return [2,3,5,7,11,13,17,19,23,29].');"
+ },
+ {
+ "text": "'eratosthenes(40) should return [2,3,5,7,11,13,17,19,23,29,31,37].'",
+ "testString": "assert.deepEqual(eratosthenes(40),[2,3,5,7,11,13,17,19,23,29,31,37],'eratosthenes(40) should return [2,3,5,7,11,13,17,19,23,29,31,37].');"
+ },
+ {
+ "text": "'eratosthenes(50) should return [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47].'",
+ "testString": "assert.deepEqual(eratosthenes(50),[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47],'eratosthenes(50) should return [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47].');"
+ }
+ ],
+ "id": "5a23c84252665b21eecc7fea",
+ "challengeType": 5,
+ "releasedOn": "September 8, 2018",
+ "files": {
+ "indexjs": {
+ "key": "indexjs",
+ "ext": "js",
+ "name": "index",
+ "contents": [
+ "function eratosthenes (limit) {",
+ " // Good luck!",
+ "}"
+ ],
+ "head": [],
+ "tail": []
+ }
+ }
+ },
+ {
+ "title": "Singly-linked list/Element definition",
+ "description": [
+ "Define the data structure for a singly-linked list element. Said element should contain a data member capable of holding a numeric value, and the link to the next element should be mutable.",
+ "Write a function that takes an array as a parameter. The array contains queries to be performed on the singly linked list. The first element of the query is the type of the operation and it can be \"head\" or \"after\". For head, the second element should be inserted at the head of the linked list. For after, the second element's value should be searched in the linked list. The third element should be inserted after the node whose value is equal to the second element's value."
+ ],
+ "solutions": [
+ "function LinkedList() {\n this.length = 0;\n this.head = null;\n}\nfunction Node(val) {\n this.val = val;\n this.prev = null;\n}\n\nfunction singlyLinkedList (queries) {\n LinkedList.prototype.insertAtHead = function(val) {\n var node=new Node(val);\n if(this.head){\n node.next=this.head;\n this.head=node;\n }else{\n this.head=node;\n }\n this.length++;\n }\n\n LinkedList.prototype.insertAfter = function(val1,val2) {\n var temp=this.head;\n\n while(temp!=null && temp.val!=val1){\n temp=temp.next;\n }\n\n if(temp){\n var node=new Node(val2);\n var tempNext=temp.next;\n temp.next=node;\n node.next=tempNext;\n }\n this.length++;\n }\n\n var list = new LinkedList();\n\n queries.forEach(function(e) {\n switch (e[0]) {\n case 'head':\n list.insertAtHead(e[1]);\n break;\n case 'after':\n list.insertAfter(e[1],e[2]);\n break;\n }\n })\n\n return list\n}\n"
+ ],
+ "tests": [
+ {
+ "text": "'singlyLinkedList should be a function.'",
+ "testString": "assert(typeof singlyLinkedList=='function','singlyLinkedList should be a function.');"
+ },
+ {
+ "text": "'traverse(singlyLinkedList([[\"head\",1],[\"head\",2],[\"head\",3],[\"head\",4],[\"head\",5]])) should return a array.'",
+ "testString": "assert(Array.isArray(traverse(singlyLinkedList([[\"head\",1],[\"head\",2],[\"head\",3],[\"head\",4],[\"head\",5]]))),'traverse(singlyLinkedList([[\"head\",1],[\"head\",2],[\"head\",3],[\"head\",4],[\"head\",5]])) should return a array.');"
+ },
+ {
+ "text": "'traverse(singlyLinkedList([[\"head\",1],[\"head\",2],[\"head\",3],[\"head\",4],[\"head\",5]])) should return [5,4,3,2,1].'",
+ "testString": "assert.deepEqual(traverse(singlyLinkedList([[\"head\",1],[\"head\",2],[\"head\",3],[\"head\",4],[\"head\",5]])),[5,4,3,2,1],'traverse(singlyLinkedList([[\"head\",1],[\"head\",2],[\"head\",3],[\"head\",4],[\"head\",5]])) should return [5,4,3,2,1].');"
+ },
+ {
+ "text": "'traverse(singlyLinkedList([[\"head\",1],[\"after\",1,2],[\"head\",3],[\"head\",5],[\"after\",3,4]])) should return [5,3,4,1,2].'",
+ "testString": "assert.deepEqual(traverse(singlyLinkedList([[\"head\",1],[\"after\",1,2],[\"head\",3],[\"head\",5],[\"after\",3,4]])),[5,3,4,1,2],'traverse(singlyLinkedList([[\"head\",1],[\"after\",1,2],[\"head\",3],[\"head\",5],[\"after\",3,4]])) should return [5,3,4,1,2].');"
+ },
+ {
+ "text": "'traverse(singlyLinkedList([[\"head\",31],[\"head\",26],[\"head\",33],[\"head\",15],[\"head\",24]])) should return [24,15,33,26,31].'",
+ "testString": "assert.deepEqual(traverse(singlyLinkedList([[\"head\",31],[\"head\",26],[\"head\",33],[\"head\",15],[\"head\",24]])),[24,15,33,26,31],'traverse(singlyLinkedList([[\"head\",31],[\"head\",26],[\"head\",33],[\"head\",15],[\"head\",24]])) should return [24,15,33,26,31].');"
+ },
+ {
+ "text": "'traverse(singlyLinkedList([[\"head\",2],[\"head\",0],[\"after\",2,3],[\"after\",0,4],[\"head\",12]])) should return [12,0,4,2,3].'",
+ "testString": "assert.deepEqual(traverse(singlyLinkedList([[\"head\",2],[\"head\",0],[\"after\",2,3],[\"after\",0,4],[\"head\",12]])),[12,0,4,2,3],'traverse(singlyLinkedList([[\"head\",2],[\"head\",0],[\"after\",2,3],[\"after\",0,4],[\"head\",12]])) should return [12,0,4,2,3].');"
+ },
+ {
+ "text": "'traverse(singlyLinkedList([[\"head\",1],[\"after\",1,2],[\"head\",3],[\"head\",5],[\"head\",42]])) should return [42,5,3,1,2].'",
+ "testString": "assert.deepEqual(traverse(singlyLinkedList([[\"head\",1],[\"after\",1,2],[\"head\",3],[\"head\",5],[\"head\",42]])),[42,5,3,1,2],'traverse(singlyLinkedList([[\"head\",1],[\"after\",1,2],[\"head\",3],[\"head\",5],[\"head\",42]])) should return [42,5,3,1,2].');"
+ }
+ ],
+ "id": "5a23c84252665b21eecc7ff0",
+ "challengeType": 5,
+ "releasedOn": "September 8, 2018",
+ "files": {
+ "indexjs": {
+ "key": "indexjs",
+ "ext": "js",
+ "name": "index",
+ "contents": [
+ "function LinkedList() {",
+ " this.length = 0;",
+ " this.head = null;",
+ "}",
+ "function Node(val) {",
+ " this.val = val;",
+ " this.prev = null;",
+ "}",
+ "",
+ "function singlyLinkedList (queries) {",
+ " // Good luck!",
+ "}"
+ ],
+ "head": [],
+ "tail": [
+ "function traverse(list) {",
+ " var vals=[],temp=list.head;",
+ " while(temp){",
+ " vals.push(temp.val)",
+ " temp=temp.next",
+ " }",
+ " return vals",
+ "}"
+ ]
+ }
+ }
+ },
+ {
+ "title": "Smith numbers",
+ "description": [
+ "Smith numbers are numbers such that the sum of the decimal digits of the integers that make up that number is the same as the sum of the decimal digits of its prime factors excluding 1.",
+ "By definition, all primes are excluded as they (naturally) satisfy this condition!Smith numbers are also known as joke numbers.",
+ "Using the number 166",
+ "Find the prime factors of 166 which are: 2 x 83",
+ "Then, take those two prime factors and sum all their decimal digits: 2 + 8 + 3 which is 13",
+ "Then, take the decimal digits of 166 and add their decimal digits: 1 + 6 + 6 which is 13",
+ "Therefore, the number 166 is a Smith number.",
+ "Write a function that checks if a given number is a Smith number. It should return true if it is a Smith number. Otherwise returns false."
+ ],
+ "solutions": [
+ "function isSmith (n) {\n const concat = xs => {\n if (xs.length > 0) {\n const unit = typeof xs[0] === 'string' ? '' : [];\n return unit.concat.apply(unit, xs);\n } else return [];\n }\n const range = (m, n) =>\n Array.from({\n length: Math.floor(n - m) + 1\n }, (_, i) => m + i);\n const dropWhile = (p, xs) => {\n let i = 0;\n for (let lng = xs.length;\n (i < lng) && p(xs[i]); i++) {}\n return xs.slice(i);\n }\n const head = xs => xs.length ? xs[0] : undefined;\n const take = (n, xs) => xs.slice(0, n);\n const primeFactors = n => {\n const fs = take(1, (dropWhile(x => n % x !== 0, range(2, Math.floor(Math.sqrt(n))))));\n return fs.length === 0 ? (\n [n]\n ) : fs.concat(primeFactors(Math.floor(n / head(fs))));\n };\n\n const digitSum = ds =>\n ds\n .reduce((a, b) => parseInt(a, 10) + parseInt(b, 10), 0);\n\n const pfs = primeFactors(n);\n return (head(pfs) !== n) &&\n digitSum(n.toString()\n .split('')) == digitSum(\n concat(pfs.map(x => x.toString()))\n .split('')\n );\n}\n"
+ ],
+ "tests": [
+ {
+ "text": "'isSmith should be a function.'",
+ "testString": "assert(typeof isSmith=='function','isSmith should be a function.');"
+ },
+ {
+ "text": "'isSmith(4) should return a boolean.'",
+ "testString": "assert(typeof isSmith(4) == 'boolean','isSmith(4) should return a boolean.');"
+ },
+ {
+ "text": "'isSmith(4) should return true.'",
+ "testString": "assert.equal(isSmith(4),true,'isSmith(4) should return true.');"
+ },
+ {
+ "text": "'isSmith(52) should return false.'",
+ "testString": "assert.equal(isSmith(52),false,'isSmith(52) should return false.');"
+ },
+ {
+ "text": "'isSmith(355) should return true.'",
+ "testString": "assert.equal(isSmith(355),true,'isSmith(355) should return true.');"
+ },
+ {
+ "text": "'isSmith(378) should return true.'",
+ "testString": "assert.equal(isSmith(378),true,'isSmith(378) should return true.');"
+ },
+ {
+ "text": "'isSmith(44) should return false.'",
+ "testString": "assert.equal(isSmith(44),false,'isSmith(44) should return false.');"
+ },
+ {
+ "text": "'isSmith(133) should return false.'",
+ "testString": "assert.equal(isSmith(133),false,'isSmith(133) should return false.');"
+ },
+ {
+ "text": "'isSmith(121) should return true.'",
+ "testString": "assert.equal(isSmith(121),true,'isSmith(121) should return true.');"
+ },
+ {
+ "text": "'isSmith(76) should return false.'",
+ "testString": "assert.equal(isSmith(76),false,'isSmith(76) should return false.');"
+ }
+ ],
+ "id": "5a23c84252665b21eecc7ff4",
+ "challengeType": 5,
+ "releasedOn": "September 8, 2018",
+ "files": {
+ "indexjs": {
+ "key": "indexjs",
+ "ext": "js",
+ "name": "index",
+ "contents": [
+ "function isSmith (n) {",
+ " // Good luck!",
+ "}"
+ ],
+ "head": [],
+ "tail": []
+ }
+ }
+ },
+ {
+ "title": "Sokoban",
+ "description": [
+ "Demonstrate how to find a solution to a given Sokoban level. For the purpose of this task (formally, a PSPACE-complete problem) any method may be used. However a move-optimal or push-optimal (or any other -optimal) solutions is preferred.",
+ "Sokoban levels are usually stored as a character array where",
+ "solveSokoban should be a function.'",
+ "testString": "assert(typeof solveSokoban=='function','solveSokoban should be a function.');"
+ },
+ {
+ "text": "'solveSokoban(\"#######,# #,# #,#. # #,#. $$ #,#.$$ #,#.# @#,#######\") should return a string.'",
+ "testString": "assert(typeof solveSokoban(\"#######,# #,# #,#. # #,#. $$ #,#.$$ #,#.# @#,#######\") == 'string','solveSokoban(\"#######,# #,# #,#. # #,#. $$ #,#.$$ #,#.# @#,#######\") should return a string.');"
+ },
+ {
+ "text": "'solveSokoban(\"#######,# #,# #,#. # #,#. $$ #,#.$$ #,#.# @#,#######\") should return \"ulULLulDDurrrddlULrruLLrrUruLLLulD\".'",
+ "testString": "assert.equal(solveSokoban(\"#######,# #,# #,#. # #,#. $$ #,#.$$ #,#.# @#,#######\"),\"ulULLulDDurrrddlULrruLLrrUruLLLulD\",'solveSokoban(\"#######,# #,# #,#. # #,#. $$ #,#.$$ #,#.# @#,#######\") should return \"ulULLulDDurrrddlULrruLLrrUruLLLulD\".');"
+ }
+ ],
+ "id": "5a23c84252665b21eecc7ff7",
+ "challengeType": 5,
+ "releasedOn": "September 8, 2018",
+ "files": {
+ "indexjs": {
+ "key": "indexjs",
+ "ext": "js",
+ "name": "index",
+ "contents": [
+ "function solveSokoban (level) {",
+ " // Good luck!",
+ "}"
+ ],
+ "head": [],
+ "tail": []
+ }
+ }
+ },
+ {
+ "title": "Solve a Hidato puzzle",
+ "description": [
+ "The task is to write a program which solves Hidato (aka Hidoku) puzzles.",
+ "The rules are:",
+ "solveHidato should be a function.'",
+ "testString": "assert(typeof solveHidato=='function','solveHidato should be a function.');"
+ },
+ {
+ "text": "'solveHidato([[-1,4,-1],[0,7,0],[1,0,0]]) should return a array.'",
+ "testString": "assert(Array.isArray(solveHidato([[-1,4,-1],[0,7,0],[1,0,0]])),'solveHidato([[-1,4,-1],[0,7,0],[1,0,0]]) should return a array.');"
+ },
+ {
+ "text": "'solveHidato([[-1,4,-1],[0,7,0],[1,0,0]]) should return [[-1,4,-1],[3,7,5],[1,2,6]].'",
+ "testString": "assert.deepEqual(solveHidato([[-1,4,-1],[0,7,0],[1,0,0]]),[[-1,4,-1],[3,7,5],[1,2,6]],'solveHidato([[-1,4,-1],[0,7,0],[1,0,0]]) should return [[-1,4,-1],[3,7,5],[1,2,6]].');"
+ },
+ {
+ "text": "'solveHidato([[1,0,0,-1,-1,0,0,-1,-1,0,0,-1,-1,0,0,-1,-1,0,0,-1,-1,0,0,-1,-1,0,0,-1,-1,0,0,-1,-1,0,0,-1,-1,0,0,-1,-1,0,0,-1,-1,0,0,-1,-1,74],[-1,-1,0,-1,0,-1,0,-1,0,-1,0,-1,0,-1,0,-1,0,-1,0,-1,0,-1,0,-1,0,-1,0,-1,0,-1,0,-1,0,-1,0,-1,0,-1,0,-1,0,-1,0,-1,0,-1,0,-1,0,-1],[-1,-1,-1,0,0,-1,-1,0,0,-1,-1,0,0,-1,-1,0,0,-1,-1,0,0,-1,-1,0,0,-1,-1,0,0,-1,-1,0,0,-1,-1,0,0,-1,-1,0,0,-1,-1,0,0,-1,-1,0,0,-1]]) should return [[1,2,3,-1,-1,8,9,-1,-1,14,15,-1,-1,20,21,-1,-1,26,27,-1,-1,32,33,-1,-1,38,39,-1,-1,44,45,-1,-1,50,51,-1,-1,56,57,-1,-1,62,63,-1,-1,68,69,-1,-1,74],[-1,-1,4,-1,7,-1,10,-1,13,-1,16,-1,19,-1,22,-1,25,-1,28,-1,31,-1,34,-1,37,-1,40,-1,43,-1,46,-1,49,-1,52,-1,55,-1,58,-1,61,-1,64,-1,67,-1,70,-1,73,-1],[-1,-1,-1,5,6,-1,-1,11,12,-1,-1,17,18,-1,-1,23,24,-1,-1,29,30,-1,-1,35,36,-1,-1,41,42,-1,-1,47,48,-1,-1,53,54,-1,-1,59,60,-1,-1,65,66,-1,-1,71,72,-1]].'",
+ "testString": "assert.deepEqual(solveHidato([[1,0,0,-1,-1,0,0,-1,-1,0,0,-1,-1,0,0,-1,-1,0,0,-1,-1,0,0,-1,-1,0,0,-1,-1,0,0,-1,-1,0,0,-1,-1,0,0,-1,-1,0,0,-1,-1,0,0,-1,-1,74],[-1,-1,0,-1,0,-1,0,-1,0,-1,0,-1,0,-1,0,-1,0,-1,0,-1,0,-1,0,-1,0,-1,0,-1,0,-1,0,-1,0,-1,0,-1,0,-1,0,-1,0,-1,0,-1,0,-1,0,-1,0,-1],[-1,-1,-1,0,0,-1,-1,0,0,-1,-1,0,0,-1,-1,0,0,-1,-1,0,0,-1,-1,0,0,-1,-1,0,0,-1,-1,0,0,-1,-1,0,0,-1,-1,0,0,-1,-1,0,0,-1,-1,0,0,-1]]),[[1,2,3,-1,-1,8,9,-1,-1,14,15,-1,-1,20,21,-1,-1,26,27,-1,-1,32,33,-1,-1,38,39,-1,-1,44,45,-1,-1,50,51,-1,-1,56,57,-1,-1,62,63,-1,-1,68,69,-1,-1,74],[-1,-1,4,-1,7,-1,10,-1,13,-1,16,-1,19,-1,22,-1,25,-1,28,-1,31,-1,34,-1,37,-1,40,-1,43,-1,46,-1,49,-1,52,-1,55,-1,58,-1,61,-1,64,-1,67,-1,70,-1,73,-1],[-1,-1,-1,5,6,-1,-1,11,12,-1,-1,17,18,-1,-1,23,24,-1,-1,29,30,-1,-1,35,36,-1,-1,41,42,-1,-1,47,48,-1,-1,53,54,-1,-1,59,60,-1,-1,65,66,-1,-1,71,72,-1]],'solveHidato([[1,0,0,-1,-1,0,0,-1,-1,0,0,-1,-1,0,0,-1,-1,0,0,-1,-1,0,0,-1,-1,0,0,-1,-1,0,0,-1,-1,0,0,-1,-1,0,0,-1,-1,0,0,-1,-1,0,0,-1,-1,74],[-1,-1,0,-1,0,-1,0,-1,0,-1,0,-1,0,-1,0,-1,0,-1,0,-1,0,-1,0,-1,0,-1,0,-1,0,-1,0,-1,0,-1,0,-1,0,-1,0,-1,0,-1,0,-1,0,-1,0,-1,0,-1],[-1,-1,-1,0,0,-1,-1,0,0,-1,-1,0,0,-1,-1,0,0,-1,-1,0,0,-1,-1,0,0,-1,-1,0,0,-1,-1,0,0,-1,-1,0,0,-1,-1,0,0,-1,-1,0,0,-1,-1,0,0,-1]]) should return [[1,2,3,-1,-1,8,9,-1,-1,14,15,-1,-1,20,21,-1,-1,26,27,-1,-1,32,33,-1,-1,38,39,-1,-1,44,45,-1,-1,50,51,-1,-1,56,57,-1,-1,62,63,-1,-1,68,69,-1,-1,74],[-1,-1,4,-1,7,-1,10,-1,13,-1,16,-1,19,-1,22,-1,25,-1,28,-1,31,-1,34,-1,37,-1,40,-1,43,-1,46,-1,49,-1,52,-1,55,-1,58,-1,61,-1,64,-1,67,-1,70,-1,73,-1],[-1,-1,-1,5,6,-1,-1,11,12,-1,-1,17,18,-1,-1,23,24,-1,-1,29,30,-1,-1,35,36,-1,-1,41,42,-1,-1,47,48,-1,-1,53,54,-1,-1,59,60,-1,-1,65,66,-1,-1,71,72,-1]].');"
+ },
+ {
+ "text": "'solveHidato([[0,33,35,0,0,-1,-1,-1],[0,0,24,22,0,-1,-1,-1],[0,0,0,21,0,0,-1,-1],[0,26,0,13,40,11,-1,-1],[27,0,0,0,9,0,1,-1],[-1,-1,0,0,18,0,0,-1],[-1,-1,-1,-1,0,7,0,0],[-1,-1,-1,-1,-1,-1,5,0]]) should return [[32,33,35,36,37,-1,-1,-1],[31,34,24,22,38,-1,-1,-1],[30,25,23,21,12,39,-1,-1],[29,26,20,13,40,11,-1,-1],[27,28,14,19,9,10,1,-1],[-1,-1,15,16,18,8,2,-1],[-1,-1,-1,-1,17,7,6,3],[-1,-1,-1,-1,-1,-1,5,4]].'",
+ "testString": "assert.deepEqual(solveHidato([[0,33,35,0,0,-1,-1,-1],[0,0,24,22,0,-1,-1,-1],[0,0,0,21,0,0,-1,-1],[0,26,0,13,40,11,-1,-1],[27,0,0,0,9,0,1,-1],[-1,-1,0,0,18,0,0,-1],[-1,-1,-1,-1,0,7,0,0],[-1,-1,-1,-1,-1,-1,5,0]]),[[32,33,35,36,37,-1,-1,-1],[31,34,24,22,38,-1,-1,-1],[30,25,23,21,12,39,-1,-1],[29,26,20,13,40,11,-1,-1],[27,28,14,19,9,10,1,-1],[-1,-1,15,16,18,8,2,-1],[-1,-1,-1,-1,17,7,6,3],[-1,-1,-1,-1,-1,-1,5,4]],'solveHidato([[0,33,35,0,0,-1,-1,-1],[0,0,24,22,0,-1,-1,-1],[0,0,0,21,0,0,-1,-1],[0,26,0,13,40,11,-1,-1],[27,0,0,0,9,0,1,-1],[-1,-1,0,0,18,0,0,-1],[-1,-1,-1,-1,0,7,0,0],[-1,-1,-1,-1,-1,-1,5,0]]) should return [[32,33,35,36,37,-1,-1,-1],[31,34,24,22,38,-1,-1,-1],[30,25,23,21,12,39,-1,-1],[29,26,20,13,40,11,-1,-1],[27,28,14,19,9,10,1,-1],[-1,-1,15,16,18,8,2,-1],[-1,-1,-1,-1,17,7,6,3],[-1,-1,-1,-1,-1,-1,5,4]].');"
+ }
+ ],
+ "id": "5a23c84252665b21eecc7ff8",
+ "challengeType": 5,
+ "releasedOn": "September 8, 2018",
+ "files": {
+ "indexjs": {
+ "key": "indexjs",
+ "ext": "js",
+ "name": "index",
+ "contents": [
+ "function solveHidato (boardMat) {",
+ " // Good luck!",
+ "}"
+ ],
+ "head": [],
+ "tail": []
+ }
+ }
+ },
+ {
+ "title": "Solve a Numbrix puzzle",
+ "description": [
+ "Numbrix puzzles are similar to Hidato.",
+ "The most important difference is that it is only possible to move 1 node left, right, up, or down (sometimes referred to as the Von Neumann neighborhood).",
+ "Published puzzles also tend not to have holes in the grid and may not always indicate the end node.",
+ "Two examples follow:",
+ "Problem.",
+ "0 0 0 0 0 0 0 0 0 \\n 0 0 46 45 0 55 74 0 0 \\n 0 38 0 0 43 0 0 78 0 \\n 0 35 0 0 0 0 0 71 0 \\n 0 0 33 0 0 0 59 0 0 \\n 0 17 0 0 0 0 0 67 0 \\n 0 18 0 0 11 0 0 64 0 \\n 0 0 24 21 0 1 2 0 0 \\n 0 0 0 0 0 0 0 0 0 \\n", + "Solution.", + "
49 50 51 52 53 54 75 76 81 \\n 48 47 46 45 44 55 74 77 80 \\n 37 38 39 40 43 56 73 78 79 \\n 36 35 34 41 42 57 72 71 70 \\n 31 32 33 14 13 58 59 68 69 \\n 30 17 16 15 12 61 60 67 66 \\n 29 18 19 20 11 62 63 64 65 \\n 28 25 24 21 10 1 2 3 4 \\n 27 26 23 22 9 8 7 6 5 \\n", + "Problem.", + "
0 0 0 0 0 0 0 0 0 \\n 0 11 12 15 18 21 62 61 0 \\n 0 6 0 0 0 0 0 60 0 \\n 0 33 0 0 0 0 0 57 0 \\n 0 32 0 0 0 0 0 56 0 \\n 0 37 0 1 0 0 0 73 0 \\n 0 38 0 0 0 0 0 72 0 \\n 0 43 44 47 48 51 76 77 0 \\n 0 0 0 0 0 0 0 0 0 \\n", + "Solution.", + "
9 10 13 14 19 20 63 64 65 \\n 8 11 12 15 18 21 62 61 66 \\n 7 6 5 16 17 22 59 60 67 \\n 34 33 4 3 24 23 58 57 68 \\n 35 32 31 2 25 54 55 56 69 \\n 36 37 30 1 26 53 74 73 70 \\n 39 38 29 28 27 52 75 72 71 \\n 40 43 44 47 48 51 76 77 78 \\n 41 42 45 46 49 50 81 80 79 \\n", + "Write a function that takes a 2D array as a parameter. This array represents the Numbrix puzzle. The function should return the solution by replacing the 0s with valid numbers." + ], + "solutions": [ + "function solveNumbrix (board) {\n var moves = [\n [1, 0],\n [0, 1],\n [-1, 0],\n [0, -1]\n ]\n\n var nRows = board.length + 2;\n var nCols = board[0].length + 2;\n var startRow = 0;\n var startCol = 0;\n var grid = new Array(nRows);\n\n for (var i = 0; i < grid.length; i++) {\n grid[i]=new Array(nCols);\n grid[i].fill(-1)\n }\n var totalToFill = (nRows - 2) * (nCols - 2);\n var lst = ([]);\n for (var r = 0; r < nRows; r++) {\n if (r >= 1 && r < nRows - 1) {\n var row = board[r - 1];\n for (var c = 1; c < nCols - 1; c++) {\n var val = row[c - 1];\n if (val > 0)\n (lst.push(val) > 0);\n if (val === 1) {\n startRow = r;\n startCol = c;\n }\n grid[r][c] = val;\n };\n }\n }\n lst.sort()\n var clues = lst.slice();\n solve(startRow, startCol, 1, 0)\n\n function solve(r, c, count, nextClue) {\n if (count > totalToFill)\n return true;\n if (grid[r][c] !== 0 && grid[r][c] !== count)\n return false;\n if (grid[r][c] === 0 && nextClue < clues.length)\n if (clues[nextClue] === count)\n return false;\n var back = grid[r][c];\n if (back === count)\n nextClue++;\n grid[r][c] = count;\n for (var index371 = 0; index371 < moves.length; index371++) {\n var move = moves[index371];\n if (solve(r + move[1], c + move[0], count + 1, nextClue))\n return true;\n }\n grid[r][c] = back;\n return false;\n };\n\n grid = grid.slice(1, -1).map(e => e.slice(1, -1))\n return grid;\n}\n" + ], + "tests": [ + { + "text": "'
solveNumbrix should be a function.'",
+ "testString": "assert(typeof solveNumbrix=='function','solveNumbrix should be a function.');"
+ },
+ {
+ "text": "'solveNumbrix([[0,0,0,0,0,0,0,0,0],[0,0,46,45,0,55,74,0,0],[0,38,0,0,43,0,0,78,0],[0,35,0,0,0,0,0,71,0],[0,0,33,0,0,0,59,0,0],[0,17,0,0,0,0,0,67,0],[0,18,0,0,11,0,0,64,0],[0,0,24,21,0,1,2,0,0],[0,0,0,0,0,0,0,0,0]]) should return a array.'",
+ "testString": "assert(Array.isArray(solveNumbrix([[0,0,0,0,0,0,0,0,0],[0,0,46,45,0,55,74,0,0],[0,38,0,0,43,0,0,78,0],[0,35,0,0,0,0,0,71,0],[0,0,33,0,0,0,59,0,0],[0,17,0,0,0,0,0,67,0],[0,18,0,0,11,0,0,64,0],[0,0,24,21,0,1,2,0,0],[0,0,0,0,0,0,0,0,0]])),'solveNumbrix([[0,0,0,0,0,0,0,0,0],[0,0,46,45,0,55,74,0,0],[0,38,0,0,43,0,0,78,0],[0,35,0,0,0,0,0,71,0],[0,0,33,0,0,0,59,0,0],[0,17,0,0,0,0,0,67,0],[0,18,0,0,11,0,0,64,0],[0,0,24,21,0,1,2,0,0],[0,0,0,0,0,0,0,0,0]]) should return a array.');"
+ },
+ {
+ "text": "'solveNumbrix([[0,0,0,0,0,0,0,0,0],[0,0,46,45,0,55,74,0,0],[0,38,0,0,43,0,0,78,0],[0,35,0,0,0,0,0,71,0],[0,0,33,0,0,0,59,0,0],[0,17,0,0,0,0,0,67,0],[0,18,0,0,11,0,0,64,0],[0,0,24,21,0,1,2,0,0],[0,0,0,0,0,0,0,0,0]]) should return [[49,50,51,52,53,54,75,76,81],[48,47,46,45,44,55,74,77,80],[37,38,39,40,43,56,73,78,79],[36,35,34,41,42,57,72,71,70],[31,32,33,14,13,58,59,68,69],[30,17,16,15,12,61,60,67,66],[29,18,19,20,11,62,63,64,65],[28,25,24,21,10,1,2,3,4],[27,26,23,22,9,8,7,6,5]].'",
+ "testString": "assert.deepEqual(solveNumbrix([[0,0,0,0,0,0,0,0,0],[0,0,46,45,0,55,74,0,0],[0,38,0,0,43,0,0,78,0],[0,35,0,0,0,0,0,71,0],[0,0,33,0,0,0,59,0,0],[0,17,0,0,0,0,0,67,0],[0,18,0,0,11,0,0,64,0],[0,0,24,21,0,1,2,0,0],[0,0,0,0,0,0,0,0,0]]),[[49,50,51,52,53,54,75,76,81],[48,47,46,45,44,55,74,77,80],[37,38,39,40,43,56,73,78,79],[36,35,34,41,42,57,72,71,70],[31,32,33,14,13,58,59,68,69],[30,17,16,15,12,61,60,67,66],[29,18,19,20,11,62,63,64,65],[28,25,24,21,10,1,2,3,4],[27,26,23,22,9,8,7,6,5]],'solveNumbrix([[0,0,0,0,0,0,0,0,0],[0,0,46,45,0,55,74,0,0],[0,38,0,0,43,0,0,78,0],[0,35,0,0,0,0,0,71,0],[0,0,33,0,0,0,59,0,0],[0,17,0,0,0,0,0,67,0],[0,18,0,0,11,0,0,64,0],[0,0,24,21,0,1,2,0,0],[0,0,0,0,0,0,0,0,0]]) should return [[49,50,51,52,53,54,75,76,81],[48,47,46,45,44,55,74,77,80],[37,38,39,40,43,56,73,78,79],[36,35,34,41,42,57,72,71,70],[31,32,33,14,13,58,59,68,69],[30,17,16,15,12,61,60,67,66],[29,18,19,20,11,62,63,64,65],[28,25,24,21,10,1,2,3,4],[27,26,23,22,9,8,7,6,5]].');"
+ },
+ {
+ "text": "'solveNumbrix([[0,0,0,0,0,0,0,0,0],[0,11,12,15,18,21,62,61,0],[0,6,0,0,0,0,0,60,0],[0,33,0,0,0,0,0,57,0],[0,32,0,0,0,0,0,56,0],[0,37,0,1,0,0,0,73,0],[0,38,0,0,0,0,0,72,0],[0,43,44,47,48,51,76,77,0],[0,0,0,0,0,0,0,0,0]]) should return [[9,10,13,14,19,20,63,64,65],[8,11,12,15,18,21,62,61,66],[7,6,5,16,17,22,59,60,67],[34,33,4,3,24,23,58,57,68],[35,32,31,2,25,54,55,56,69],[36,37,30,1,26,53,74,73,70],[39,38,29,28,27,52,75,72,71],[40,43,44,47,48,51,76,77,78],[41,42,45,46,49,50,81,80,79]].'",
+ "testString": "assert.deepEqual(solveNumbrix([[0,0,0,0,0,0,0,0,0],[0,11,12,15,18,21,62,61,0],[0,6,0,0,0,0,0,60,0],[0,33,0,0,0,0,0,57,0],[0,32,0,0,0,0,0,56,0],[0,37,0,1,0,0,0,73,0],[0,38,0,0,0,0,0,72,0],[0,43,44,47,48,51,76,77,0],[0,0,0,0,0,0,0,0,0]]),[[9,10,13,14,19,20,63,64,65],[8,11,12,15,18,21,62,61,66],[7,6,5,16,17,22,59,60,67],[34,33,4,3,24,23,58,57,68],[35,32,31,2,25,54,55,56,69],[36,37,30,1,26,53,74,73,70],[39,38,29,28,27,52,75,72,71],[40,43,44,47,48,51,76,77,78],[41,42,45,46,49,50,81,80,79]],'solveNumbrix([[0,0,0,0,0,0,0,0,0],[0,11,12,15,18,21,62,61,0],[0,6,0,0,0,0,0,60,0],[0,33,0,0,0,0,0,57,0],[0,32,0,0,0,0,0,56,0],[0,37,0,1,0,0,0,73,0],[0,38,0,0,0,0,0,72,0],[0,43,44,47,48,51,76,77,0],[0,0,0,0,0,0,0,0,0]]) should return [[9,10,13,14,19,20,63,64,65],[8,11,12,15,18,21,62,61,66],[7,6,5,16,17,22,59,60,67],[34,33,4,3,24,23,58,57,68],[35,32,31,2,25,54,55,56,69],[36,37,30,1,26,53,74,73,70],[39,38,29,28,27,52,75,72,71],[40,43,44,47,48,51,76,77,78],[41,42,45,46,49,50,81,80,79]].');"
+ },
+ {
+ "text": "'solveNumbrix([[17,0,0,0,11,0,0,0,59],[0,15,0,0,6,0,0,61,0],[0,0,3,0,0,0,63,0,0],[0,0,0,1,66,0,0,0,0],[23,24,0,68,67,78,0,54,55],[0,0,0,0,72,0,0,0,0],[0,0,35,0,0,0,49,0,0],[0,29,0,0,40,0,0,47,0],[31,0,0,0,39,0,0,0,45]]) should return [[17,16,13,12,11,10,9,60,59],[18,15,14,5,6,7,8,61,58],[19,20,3,4,65,64,63,62,57],[22,21,2,1,66,79,80,81,56],[23,24,69,68,67,78,77,54,55],[26,25,70,71,72,75,76,53,52],[27,28,35,36,73,74,49,50,51],[30,29,34,37,40,41,48,47,46],[31,32,33,38,39,42,43,44,45]].'",
+ "testString": "assert.deepEqual(solveNumbrix([[17,0,0,0,11,0,0,0,59],[0,15,0,0,6,0,0,61,0],[0,0,3,0,0,0,63,0,0],[0,0,0,1,66,0,0,0,0],[23,24,0,68,67,78,0,54,55],[0,0,0,0,72,0,0,0,0],[0,0,35,0,0,0,49,0,0],[0,29,0,0,40,0,0,47,0],[31,0,0,0,39,0,0,0,45]]),[[17,16,13,12,11,10,9,60,59],[18,15,14,5,6,7,8,61,58],[19,20,3,4,65,64,63,62,57],[22,21,2,1,66,79,80,81,56],[23,24,69,68,67,78,77,54,55],[26,25,70,71,72,75,76,53,52],[27,28,35,36,73,74,49,50,51],[30,29,34,37,40,41,48,47,46],[31,32,33,38,39,42,43,44,45]],'solveNumbrix([[17,0,0,0,11,0,0,0,59],[0,15,0,0,6,0,0,61,0],[0,0,3,0,0,0,63,0,0],[0,0,0,1,66,0,0,0,0],[23,24,0,68,67,78,0,54,55],[0,0,0,0,72,0,0,0,0],[0,0,35,0,0,0,49,0,0],[0,29,0,0,40,0,0,47,0],[31,0,0,0,39,0,0,0,45]]) should return [[17,16,13,12,11,10,9,60,59],[18,15,14,5,6,7,8,61,58],[19,20,3,4,65,64,63,62,57],[22,21,2,1,66,79,80,81,56],[23,24,69,68,67,78,77,54,55],[26,25,70,71,72,75,76,53,52],[27,28,35,36,73,74,49,50,51],[30,29,34,37,40,41,48,47,46],[31,32,33,38,39,42,43,44,45]].');"
+ }
+ ],
+ "id": "5a23c84252665b21eecc7ffb",
+ "challengeType": 5,
+ "releasedOn": "September 8, 2018",
+ "files": {
+ "indexjs": {
+ "key": "indexjs",
+ "ext": "js",
+ "name": "index",
+ "contents": [
+ "function solveNumbrix (board) {",
+ " // Good luck!",
+ "}"
+ ],
+ "head": [],
+ "tail": []
+ }
+ }
+ },
+ {
+ "title": "Sort a list of object identifiers",
+ "description": [
+ "Object identifiers (OID) are strings used to identify objects in network data.",
+ "The task to sort a list of OIDs, in their natural sort order.",
+ "| Input | Output |
|---|---|
| 1.3.6.1.4.1.11.2.17.19.3.4.0.10 | 1.3.6.1.4.1.11.2.17.5.2.0.79 |
| 1.3.6.1.4.1.11.2.17.5.2.0.79 | 1.3.6.1.4.1.11.2.17.19.3.4.0.1 |
| 1.3.6.1.4.1.11.2.17.19.3.4.0.4 | 1.3.6.1.4.1.11.2.17.19.3.4.0.4 |
| 1.3.6.1.4.1.11150.3.4.0.1 | 1.3.6.1.4.1.11.2.17.19.3.4.0.10 |
| 1.3.6.1.4.1.11.2.17.19.3.4.0.1 | 1.3.6.1.4.1.11150.3.4.0 |
| 1.3.6.1.4.1.11150.3.4.0 | 1.3.6.1.4.1.11150.3.4.0.1 |
sortOids should be a function.'",
+ "testString": "assert(typeof sortOids=='function','sortOids should be a function.');"
+ },
+ {
+ "text": "'sortOids([\"1.2.3.4.5\",\"1.1.2.3.4.5\",\"11.2.3.11.33\",\"4.3.2.4.1.3.1\"]) should return a array.'",
+ "testString": "assert(Array.isArray(sortOids([\"1.2.3.4.5\",\"1.1.2.3.4.5\",\"11.2.3.11.33\",\"4.3.2.4.1.3.1\"])),'sortOids([\"1.2.3.4.5\",\"1.1.2.3.4.5\",\"11.2.3.11.33\",\"4.3.2.4.1.3.1\"]) should return a array.');"
+ },
+ {
+ "text": "'sortOids([\"1.2.3.4.5\",\"1.1.2.3.4.5\",\"11.2.3.11.33\",\"4.3.2.4.1.3.1\"]) should return [\"1.1.2.3.4.5\",\"1.2.3.4.5\",\"4.3.2.4.1.3.1\",\"11.2.3.11.33\"].'",
+ "testString": "assert.deepEqual(sortOids([\"1.2.3.4.5\",\"1.1.2.3.4.5\",\"11.2.3.11.33\",\"4.3.2.4.1.3.1\"]),[\"1.1.2.3.4.5\",\"1.2.3.4.5\",\"4.3.2.4.1.3.1\",\"11.2.3.11.33\"],'sortOids([\"1.2.3.4.5\",\"1.1.2.3.4.5\",\"11.2.3.11.33\",\"4.3.2.4.1.3.1\"]) should return [\"1.1.2.3.4.5\",\"1.2.3.4.5\",\"4.3.2.4.1.3.1\",\"11.2.3.11.33\"].');"
+ },
+ {
+ "text": "'sortOids([\"53.1195.63.1.2.3\",\"46.53.77.89.22\",\"46.53.76.88.12\",\"66.57.88.3.21.1\",\"53.1000.22.13.44\"]) should return [\"46.53.76.88.12\",\"46.53.77.89.22\",\"53.1000.22.13.44\",\"53.1195.63.1.2.3\",\"66.57.88.3.21.1\"].'",
+ "testString": "assert.deepEqual(sortOids([\"53.1195.63.1.2.3\",\"46.53.77.89.22\",\"46.53.76.88.12\",\"66.57.88.3.21.1\",\"53.1000.22.13.44\"]),[\"46.53.76.88.12\",\"46.53.77.89.22\",\"53.1000.22.13.44\",\"53.1195.63.1.2.3\",\"66.57.88.3.21.1\"],'sortOids([\"53.1195.63.1.2.3\",\"46.53.77.89.22\",\"46.53.76.88.12\",\"66.57.88.3.21.1\",\"53.1000.22.13.44\"]) should return [\"46.53.76.88.12\",\"46.53.77.89.22\",\"53.1000.22.13.44\",\"53.1195.63.1.2.3\",\"66.57.88.3.21.1\"].');"
+ },
+ {
+ "text": "'sortOids([\"1.3.6.1.4.1.11.2.17.19.3.4.0.10\",\"1.3.6.1.4.1.11.2.17.5.2.0.79\",\"1.3.6.1.4.1.11.2.17.19.3.4.0.4\",\"1.3.6.1.4.1.11150.3.4.0.1\",\"1.3.6.1.4.1.11.2.17.19.3.4.0.1\",\"1.3.6.1.4.1.11150.3.4.0\"]) should return [\"1.3.6.1.4.1.11.2.17.5.2.0.79\",\"1.3.6.1.4.1.11.2.17.19.3.4.0.1\",\"1.3.6.1.4.1.11.2.17.19.3.4.0.4\",\"1.3.6.1.4.1.11.2.17.19.3.4.0.10\",\"1.3.6.1.4.1.11150.3.4.0\",\"1.3.6.1.4.1.11150.3.4.0.1\"].'",
+ "testString": "assert.deepEqual(sortOids([\"1.3.6.1.4.1.11.2.17.19.3.4.0.10\",\"1.3.6.1.4.1.11.2.17.5.2.0.79\",\"1.3.6.1.4.1.11.2.17.19.3.4.0.4\",\"1.3.6.1.4.1.11150.3.4.0.1\",\"1.3.6.1.4.1.11.2.17.19.3.4.0.1\",\"1.3.6.1.4.1.11150.3.4.0\"]),[\"1.3.6.1.4.1.11.2.17.5.2.0.79\",\"1.3.6.1.4.1.11.2.17.19.3.4.0.1\",\"1.3.6.1.4.1.11.2.17.19.3.4.0.4\",\"1.3.6.1.4.1.11.2.17.19.3.4.0.10\",\"1.3.6.1.4.1.11150.3.4.0\",\"1.3.6.1.4.1.11150.3.4.0.1\"],'sortOids([\"1.3.6.1.4.1.11.2.17.19.3.4.0.10\",\"1.3.6.1.4.1.11.2.17.5.2.0.79\",\"1.3.6.1.4.1.11.2.17.19.3.4.0.4\",\"1.3.6.1.4.1.11150.3.4.0.1\",\"1.3.6.1.4.1.11.2.17.19.3.4.0.1\",\"1.3.6.1.4.1.11150.3.4.0\"]) should return [\"1.3.6.1.4.1.11.2.17.5.2.0.79\",\"1.3.6.1.4.1.11.2.17.19.3.4.0.1\",\"1.3.6.1.4.1.11.2.17.19.3.4.0.4\",\"1.3.6.1.4.1.11.2.17.19.3.4.0.10\",\"1.3.6.1.4.1.11150.3.4.0\",\"1.3.6.1.4.1.11150.3.4.0.1\"].');"
+ }
+ ],
+ "id": "5a23c84252665b21eecc7ffd",
+ "challengeType": 5,
+ "releasedOn": "September 8, 2018",
+ "files": {
+ "indexjs": {
+ "key": "indexjs",
+ "ext": "js",
+ "name": "index",
+ "contents": [
+ "function sortOids (input) {",
+ " // Good luck!",
+ "}"
+ ],
+ "head": [],
+ "tail": []
+ }
+ }
+ },
{
"title": "Taxicab numbers",
"description": [