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:", + "Self-describing numbers < 100.000.000 are: 1210, 2020, 21200, 3211000, 42101000.", + "Write a function that will check whether a given positive integer is self-describing." + ], + "solutions": [ + "function isSelfDescribing (n) {\n var digits = Number(n).toString().split(\"\").map(function(elem) {return Number(elem)});\n var len = digits.length;\n var count = digits.map(function(x){return 0});\n\n digits.forEach(function(digit, idx, ary) {\n if (digit >= count.length)\n return false\n count[digit] ++;\n });\n\n return digits.every((e,i)=>e==count[i]);\n}\n" + ], + "tests": [ + { + "text": "'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:", + "", + "Given N sets of items where N>2 then the result is the same as repeatedly replacing all combinations of two sets by their consolidation until no further consolidation between set pairs is possible.", + "If N < 2 then consolidation has no strict meaning and the input can be returned.", + "Given the two sets {A,B} and {C,D} then there is no common element between the sets and the result is the same as the input.", + "Given the two sets {A,B} and {B,D} then there is a common element B between the sets and the result is the single set {B,D,A}. (Note that order of items in a set is immaterial: {A,B,D} is the same as {B,D,A} and {D,A,B}, etc).", + "Given the three sets {A,B} and {C,D} and {D,B} then there is no common element between the sets {A,B} and {C,D} but the sets {A,B} and {D,B} do share a common element that consolidates to produce the result {B,D,A}. On examining this result with the remaining set, {C,D}, they share a common element and so consolidate to the final output of the single set {A,B,C,D}", + "The consolidation of the five sets:", + ":{H,I,K}, {A,B}, {C,D}, {D,B}, and {F,G,H}", + "Is the two sets:", + ":{A, C, B, D}, and {G, F, I, H, K}", + "Write a function that takes an array of strings as a parameter. Each string is represents a set with the characters representing the set elements. The function should return a 2D array containing the consolidated sets. Note: each set should be sorted." + ], + "solutions": [ + "function setConsolidation (sets) {\n function addAll(l1,l2) {\n l2.forEach(function (e) {\n if(l1.indexOf(e)==-1) l1.push(e)\n })\n }\n\n function consolidate(sets) {\n var r = [];\n for (var i = 0; i < sets.length; i++) {\n var s = sets[i]; {\n var new_r = [];\n new_r.push(s)\n for (var j = 0; j < r.length; j++) {\n var x = r[j]; {\n if (!(function(c1, c2) {\n for (var i = 0; i < c1.length; i++) {\n if (c2.indexOf(c1[i]) >= 0)\n return false;\n }\n return true;\n }) (s, x)) {\n (function(l1, l2) {\n addAll(l1,l2)\n })(s, x);\n } else {\n new_r.push(x)\n }\n }\n }\n r = new_r;\n }\n }\n return r;\n };\n\n function consolidateR(sets) {\n if (sets.length < 2)\n return sets;\n var r = ([]);\n r.push(sets[0])\n {\n var arr1 = consolidateR( sets.slice(1, sets.length));\n for (var i = 0; i < arr1.length; i++) {\n var x = arr1[i]; {\n if (!(function(c1, c2) {\n for (var i = 0; i < c1.length; i++) {\n if (c2.indexOf(c1[i]) >= 0)\n return false;\n }\n return true;\n })(r[0], x)) {\n (function(l1, l2) {\n return l1.push.apply(l1, l2);\n })(r[0], x);\n } else {\n r.push(x)\n }\n }\n }\n }\n return r;\n };\n\n function hashSetList(set) {\n var r = [];\n for (var i = 0; i < set.length; i++) {\n r.push([])\n for (var j = 0; j < set[i].length; j++)\n (function(s, e) {\n if (s.indexOf(e) == -1) {\n s.push(e);\n return true;\n } else {\n return false;\n }\n })(r[i], set[i].charAt(j));\n };\n return r;\n };\n\n var h1 = consolidate(hashSetList(sets)).map(function (e) {\n e.sort()\n return e\n })\n return h1;\n}\n" + ], + "tests": [ + { + "text": "'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 ab. There are actually four cases for the meaning of \"between\", depending on open or closed boundary:", + "Note that if a = b, of the four only [a, a] would be non-empty.", + "Task", + "xA: determine if x is an element of A", + "example: 1 is in [1, 2), while 2, 3, ... are not.", + "AB: union of A and B, i.e. {x | xA or xB}", + "example: [0, 2) ∪ (1, 3) = [0, 3); [0, 1) ∪ (2, 3] = well, [0, 1) ∪ (2, 3]", + "AB: intersection of A and B, i.e. {x | xA and xB}", + "example: [0, 2) ∩ (1, 3) = (1, 2); [0, 1) ∩ (2, 3] = empty set", + "A - B: difference between A and B, also written as A \\ B, i.e. {x | xA and xB}", + "example: [0, 2) − (1, 3) = [0, 1]", + "Write a function that takes 2 objects, a string and an array as parameters. The objects represents the set and have attributes: low, high and rangeType. The rangeType can have values 0, 1, 2 and 3 for CLOSED, BOTH_OPEN, LEFT_OPEN and RIGHT_OPEN respectively. The function should implement a set using this information. The string represents the operation to be performed on the sets. It can be: \"union\", \"intersect\" and \"subtract\" (difference). After performing the operation, the function should check if the values in the array are present in the resultant set and store a corresponding boolean value to an array. The function should return this array." + ], + "solutions": [ + "function realSet (set1, set2, operation, values) {\n const RangeType = {\n CLOSED: 0,\n BOTH_OPEN: 1,\n LEFT_OPEN: 2,\n RIGHT_OPEN: 3,\n }\n\n function Predicate(test) {\n this.test = test;\n this.or = function(other) {\n return new Predicate((t) => this.test(t) || other.test(t));\n }\n this.and = function(other) {\n return new Predicate((t) => this.test(t) && other.test(t))\n }\n this.negate = function() {\n return new Predicate((t) => !this.test(t))\n }\n }\n\n function RealSet(start, end, rangeType, predF) {\n this.low = start;\n this.high = end;\n\n if (predF) {\n this.predicate = new Predicate(predF);\n } else {\n this.predicate = new Predicate(d => {\n switch (rangeType) {\n case RangeType.CLOSED:\n return start <= d && d <= end;\n case RangeType.BOTH_OPEN:\n return start < d && d < end;\n case RangeType.LEFT_OPEN:\n return start < d && d <= end;\n case RangeType.RIGHT_OPEN:\n return start <= d && d < end;\n };\n })\n }\n\n this.contains = function(d) {\n return this.predicate.test(d);\n }\n\n this.union = function(other) {\n var low2 = Math.min(this.low, other.low);\n var high2 = Math.max(this.high, other.high);\n return new RealSet(low2, high2, null, d => this.predicate.or(other.predicate).test(d));\n }\n\n this.intersect = function(other) {\n var low2 = Math.min(this.low, other.low);\n var high2 = Math.max(this.high, other.high);\n return new RealSet(low2, high2, null, d => this.predicate.and(other.predicate).test(d));\n }\n\n this.subtract = function(other) {\n return new RealSet(this.low, this.high, null, d => this.predicate.and(other.predicate.negate()).test(d));\n }\n }\n set1=new RealSet(set1.low,set1.high,set1.rangeType);\n set2=new RealSet(set2.low,set2.high,set2.rangeType);\n var result = []\n values.forEach(function (value) {\n result.push(set1[operation](set2).contains(value))\n })\n return result\n}\n" + ], + "tests": [ + { + "text": "'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", + "", + "Sokoban solutions are usually stored in the LURD format, where lowercase l, u, r and d represent a move in that (left, up, right, down) direction and capital LURD represents a push.", + "Write a function that takes a string as a parameter. The string represents the Sokoban puzzle. Each row is separated by commas. The function should return the solution as a string of valid moves." + ], + "solutions": [ + "// noprotect\nfunction solveSokoban (level) {\n function Sokoban(board) {\n this.nCols = board[0].length;\n var destBuf = \"\";\n var currBuf = \"\";\n\n for (var r = 0; r < board.length; r++) {\n for (var c = 0; c < this.nCols; c++) {\n\n var ch = board[r][c];\n\n destBuf += ch != '$' && ch != '@' ? ch : ' ';\n currBuf += ch != '.' ? ch : ' ';\n\n if (ch == '@') {\n this.playerX = c;\n this.playerY = r;\n }\n }\n }\n this.destBoard = destBuf;\n this.currBoard = currBuf;\n this.move = function(x, y, dx, dy, trialBoard) {\n var newPlayerPos = (y + dy) * this.nCols + x + dx;\n if (trialBoard[newPlayerPos] != ' ')\n return null;\n\n var trial = trialBoard.split(\"\");\n trial[y * this.nCols + x] = ' ';\n trial[newPlayerPos] = '@';\n return trial.join(\"\");\n }\n\n this.push = function(x, y, dx, dy, trialBoard) {\n var newBoxPos = (y + 2 * dy) * this.nCols + x + 2 * dx;\n\n if (trialBoard[newBoxPos] != ' ')\n return null;\n\n var trial = trialBoard.split(\"\");\n trial[y * this.nCols + x] = ' ';\n trial[(y + dy) * this.nCols + x + dx] = '@';\n trial[newBoxPos] = '$';\n return trial.join(\"\");\n }\n\n this.isSolved = function(trialBoard) {\n for (var i = 0; i < trialBoard.length; i++)\n if ((this.destBoard[i] == '.') !=\n (trialBoard[i] == '$'))\n return false;\n return true;\n }\n\n function Board(s1, s2, px, py) {\n this.cur = s1;\n this.sol = s2;\n this.x = px;\n this.y = py;\n }\n this.solve = function() {\n var dirLabels = [\n ['u', 'U'],\n ['r', 'R'],\n ['d', 'D'],\n ['l', 'L']\n ];\n var dirs = [\n [0, -1],\n [1, 0],\n [0, 1],\n [-1, 0]\n ];\n var history = new Set();\n var open = [];\n\n history.add(this.currBoard);\n open.push(new Board(this.currBoard, \"\", this.playerX, this.playerY));\n while (open.length != 0) {\n var item = open.shift();\n var cur = item.cur;\n var sol = item.sol;\n var x = item.x;\n var y = item.y;\n for (var i = 0; i < dirs.length; i++) {\n var trial = cur;\n var dx = dirs[i][0];\n var dy = dirs[i][1];\n // are we standing next to a box ?\n if (trial[(y + dy) * this.nCols + x + dx] == '$') {\n\n if (trial = this.push(x, y, dx, dy, trial)) {\n\n // or did we already try this one ?\n if (!history.has(trial)) {\n\n var newSol = sol + dirLabels[i][1];\n\n if (this.isSolved(trial))\n return newSol;\n\n open.push(new Board(trial, newSol, x + dx, y + dy));\n history.add(trial);\n }\n }\n\n // otherwise try changing position\n } else if ((trial = this.move(x, y, dx, dy, trial)) != null) {\n if (!history.has(trial)) {\n var newSol = sol + dirLabels[i][0];\n open.push(new Board(trial, newSol, x + dx, y + dy));\n history.add(trial);\n }\n }\n }\n }\n return \"No solution\";\n }\n }\n\n return new Sokoban(level.split(\",\")).solve()\n}\n" + ], + "tests": [ + { + "text": "'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:", + "", + "Write a function that takes a 2D array as a parameter. The 2D array represents the puzzle. The elements can be:", + "