From 21930a8c1b2af5d93196741dbde3b246d46e8089 Mon Sep 17 00:00:00 2001 From: Bhanu Pratap Singh Rathore Date: Tue, 19 Jun 2018 07:11:16 +0530 Subject: [PATCH] feat(interview-prep): Porting Rosetta problems (#17537) * feat(interview-prep): Porting Rosetta problems * Objects formatted and description updated. * Update rosetta-code.json * Update rosetta-code.json * Fixed syntax for Identity matrix description array --- .../rosetta-code.json | 468 ++++++++++++++++++ 1 file changed, 468 insertions(+) diff --git a/challenges/08-coding-interview-prep/rosetta-code.json b/challenges/08-coding-interview-prep/rosetta-code.json index 123f9fc23..df0fc8c4b 100644 --- a/challenges/08-coding-interview-prep/rosetta-code.json +++ b/challenges/08-coding-interview-prep/rosetta-code.json @@ -5293,6 +5293,474 @@ } } }, + { + "title": "I before E except after C", + "description": [ + "The phrase \"I before E, except after C\" is a widely known mnemonic which is supposed to help when spelling English words.", + "Using the words provided, check if the two sub-clauses of the phrase are plausible individually:", + "
  1. \"I before E when not preceded by C\".
  2. \"E before I when preceded by C\".
", + "If both sub-phrases are plausible then the original phrase can be said to be plausible.", + "Write a function that accepts a word and check if the word follows this rule. The function should return true if it follows the rule otherwise false." + ], + "solutions": [ + "function IBeforeExceptC (word)\n{\n\tif(word.indexOf(\"c\")==-1 && word.indexOf(\"ie\")!=-1)\n\t\treturn true;\n\telse if(word.indexOf(\"cei\")!=-1)\n\t\treturn true;\n\treturn false;\n}\n" + ], + "tests": [ + { + "text": "'IBeforeExceptC should be a function.'", + "testString": "assert(typeof IBeforeExceptC=='function','IBeforeExceptC should be a function.');" + }, + { + "text": "'IBeforeExceptC(\"receive\") should return a boolean.'", + "testString": "assert(typeof IBeforeExceptC(\"receive\")=='boolean','IBeforeExceptC(\"receive\") should return a boolean.');" + }, + { + "text": "'IBeforeExceptC(\"receive\") should return true.'", + "testString": "assert.equal(IBeforeExceptC(\"receive\"),true,'IBeforeExceptC(\"receive\") should return true.');" + }, + { + "text": "'IBeforeExceptC(\"science\") should return false.'", + "testString": "assert.equal(IBeforeExceptC(\"science\"),false,'IBeforeExceptC(\"science\") should return false.');" + }, + { + "text": "'IBeforeExceptC(\"imperceivable\") should return true.'", + "testString": "assert.equal(IBeforeExceptC(\"imperceivable\"),true,'IBeforeExceptC(\"imperceivable\") should return true.');" + }, + { + "text": "'IBeforeExceptC(\"inconceivable\") should return true.'", + "testString": "assert.equal(IBeforeExceptC(\"inconceivable\"),true,'IBeforeExceptC(\"inconceivable\") should return true.');" + }, + { + "text": "'IBeforeExceptC(\"insufficient\") should return false.'", + "testString": "assert.equal(IBeforeExceptC(\"insufficient\"),false,'IBeforeExceptC(\"insufficient\") should return false.');" + }, + { + "text": "'IBeforeExceptC(\"omniscient\") should return false.'", + "testString": "assert.equal(IBeforeExceptC(\"omniscient\"),false,'IBeforeExceptC(\"omniscient\") should return false.');" + } + ], + "id": "5a23c84252665b21eecc7eb0", + "challengeType": 5, + "releasedOn": "June 8, 2018", + "files": { + "indexjs": { + "key": "indexjs", + "ext": "js", + "name": "index", + "contents": [ + "function IBeforeExceptC (word) {", + " // Good luck!", + "}" + ], + "head": [], + "tail": [] + } + } + }, + { + "title": "IBAN", + "description": [ + "The International Bank Account Number (IBAN) is an internationally agreed means of identifying bank accounts across national borders with a reduced risk of propagating transcription errors.", + "The IBAN consists of up to 34 alphanumeric characters:", "", + "The check digits enable a sanity check of the bank account number to confirm its integrity even before submitting a transaction.", + "Write a function that takes IBAN string as parameter. If it is valid return true. Otherwise, return false." + ], + "solutions": [ + "function isValid (iban) {\n var ibanLen = {\n \tNO:15, BE:16, DK:18, FI:18, FO:18, GL:18, NL:18, MK:19,\n \tSI:19, AT:20, BA:20, EE:20, KZ:20, LT:20, LU:20, CR:21,\n \tCH:21, HR:21, LI:21, LV:21, BG:22, BH:22, DE:22, GB:22,\n \tGE:22, IE:22, ME:22, RS:22, AE:23, GI:23, IL:23, AD:24,\n \tCZ:24, ES:24, MD:24, PK:24, RO:24, SA:24, SE:24, SK:24,\n \tVG:24, TN:24, PT:25, IS:26, TR:26, FR:27, GR:27, IT:27,\n \tMC:27, MR:27, SM:27, AL:28, AZ:28, CY:28, DO:28, GT:28,\n \tHU:28, LB:28, PL:28, BR:29, PS:29, KW:30, MU:30, MT:31\n }\n\tiban = iban.replace(/\\s/g, '')\n\tif (!iban.match(/^[\\dA-Z]+$/)) return false\n\tvar len = iban.length\n\tif (len != ibanLen[iban.substr(0,2)]) return false\n\tiban = iban.substr(4) + iban.substr(0,4)\n\tfor (var s='', i=0; iisValid should be a function.'", + "testString": "assert(typeof isValid=='function','isValid should be a function.');" + }, + { + "text": "'isValid(\"'+tests[0]+'\") should return a boolean.'", + "testString": "assert(typeof isValid(tests[0])=='boolean','isValid(\"'+tests[0]+'\") should return a boolean.');" + }, + { + "text": "'isValid(\"'+tests[0]+'\") should return true.'", + "testString": "assert.equal(isValid(tests[0]),true,'isValid(\"'+tests[0]+'\") should return true.');" + }, + { + "text": "'isValid(\"'+tests[1]+'\") should return false.'", + "testString": "assert.equal(isValid(tests[1]),false,'isValid(\"'+tests[1]+'\") should return false.');" + }, + { + "text": "'isValid(\"'+tests[2]+'\") should return false.'", + "testString": "assert.equal(isValid(tests[2]),false,'isValid(\"'+tests[2]+'\") should return false.');" + }, + { + "text": "'isValid(\"'+tests[3]+'\") should return false.'", + "testString": "assert.equal(isValid(tests[3]),false,'isValid(\"'+tests[3]+'\") should return false.');" + }, + { + "text": "'isValid(\"'+tests[4]+'\") should return true.'", + "testString": "assert.equal(isValid(tests[4]),true,'isValid(\"'+tests[4]+'\") should return true.');" + } + ], + "id": "5a23c84252665b21eecc7eaf", + "challengeType": 5, + "releasedOn": "June 8, 2018", + "files": { + "indexjs": { + "key": "indexjs", + "ext": "js", + "name": "index", + "contents": [ + "function isValid (iban) {", + " // Good luck!", + "}" + ], + "head": [], + "tail": [ + "let tests=[", + "'GB82 WEST 1234 5698 7654 32',", + "'GB82 WEST 1.34 5698 7654 32',", + "'GB82 WEST 1234 5698 7654 325',", + "'GB82 TEST 1234 5698 7654 32',", + "'SA03 8000 0000 6080 1016 7519'", + "]" + ] + } + } + }, + { + "title": "Identity matrix", + "description": [ + "An identity matrix is a square matrix of size \\( n \\times n \\),", + "where the diagonal elements are all 1s (ones),", + "and all the other elements are all 0s (zeroes).", + "\\begin{bmatrix} 1 & 0 & 0 \\cr 0 & 1 & 0 \\cr 0 & 0 & 1 \\cr \\end{bmatrix}", + "Write a function that takes a number 'n' as a parameter and returns the identity matrix of order n x n." + ], + "solutions": [ + "function idMatrix (n) {\n\treturn Array.apply(null, new Array(n)).map(function (x, i, xs) {\n\t\treturn xs.map(function (_, k) {\n\t\t\treturn i === k ? 1 : 0;\n\t\t})\n\t});\n}\n" + ], + "tests": [ + { + "text": "'idMatrix should be a function.'", + "testString": "assert(typeof idMatrix=='function','idMatrix should be a function.');" + }, + { + "text": "'idMatrix(1) should return an array.'", + "testString": "assert(Array.isArray(idMatrix(1)),'idMatrix(1) should return an array.');" + }, + { + "text": "'idMatrix(1) should return '+JSON.stringify(results[0])+'.'", + "testString": "assert.deepEqual(idMatrix(1),results[0],'idMatrix(1) should return '+JSON.stringify(results[0])+'.');" + }, + { + "text": "'idMatrix(2) should return '+JSON.stringify(results[1])+'.'", + "testString": "assert.deepEqual(idMatrix(2),results[1],'idMatrix(2) should return '+JSON.stringify(results[1])+'.');" + }, + { + "text": "'idMatrix(3) should return '+JSON.stringify(results[2])+'.'", + "testString": "assert.deepEqual(idMatrix(3),results[2],'idMatrix(3) should return '+JSON.stringify(results[2])+'.');" + }, + { + "text": "'idMatrix(4) should return '+JSON.stringify(results[3])+'.'", + "testString": "assert.deepEqual(idMatrix(4),results[3],'idMatrix(4) should return '+JSON.stringify(results[3])+'.');" + } + ], + "id": "5a23c84252665b21eecc7eb1", + "challengeType": 5, + "releasedOn": "June 8, 2018", + "files": { + "indexjs": { + "key": "indexjs", + "ext": "js", + "name": "index", + "contents": [ + "function idMatrix (n) {", " // Good luck!", "}" + ], + "head": [], + "tail": [ + "let results=[[ [ 1 ] ],", + "[ [ 1, 0 ], [ 0, 1 ] ],", + "[ [ 1, 0, 0 ], [ 0, 1, 0 ], [ 0, 0, 1 ] ],", + "[ [ 1, 0, 0, 0 ], [ 0, 1, 0, 0 ], [ 0, 0, 1, 0 ], [ 0, 0, 0, 1 ] ]]" + ] + } + } + }, + { + "title": "Iterated digits squaring", + "description": [ + "If you add the square of the digits of a Natural number (an integer bigger than zero), you always end with either 1 or 89:", + "
15 -> 26 -> 40 -> 16 -> 37 -> 58 -> 89",
+        "7 -> 49 -> 97 -> 130 -> 10 -> 1
", + "Write a function that takes a number as a parameter and returns 1 or 89 after performing the mentioned process." + ], + "solutions": [ + "function iteratedSquare (n) {\n\tvar total;\n\twhile (n != 89 && n != 1) {\n\t\ttotal = 0;\n\t\twhile (n > 0) {\n\t\t\ttotal += Math.pow(n % 10, 2);\n\t\t\tn = Math.floor(n/10);\n\t\t}\n\t\tn = total;\n\t}\n\treturn n;\n}\n" + ], + "tests": [ + { + "text": "'iteratedSquare should be a function.'", + "testString": "assert(typeof iteratedSquare=='function','iteratedSquare should be a function.');" + }, + { + "text": "'iteratedSquare(4) should return a number.'", + "testString": "assert(typeof iteratedSquare(4)=='number','iteratedSquare(4) should return a number.');" + }, + { + "text": "'iteratedSquare(4) should return 89.'", + "testString": "assert.equal(iteratedSquare(4),89,'iteratedSquare(4) should return 89.');" + }, + { + "text": "'iteratedSquare(7) should return 1.'", + "testString": "assert.equal(iteratedSquare(7),1,'iteratedSquare(7) should return 1.');" + }, + { + "text": "'iteratedSquare(15) should return 89.'", + "testString": "assert.equal(iteratedSquare(15),89,'iteratedSquare(15) should return 89.');" + }, + { + "text": "'iteratedSquare(20) should return 89.'", + "testString": "assert.equal(iteratedSquare(20),89,'iteratedSquare(20) should return 89.');" + }, + { + "text": "'iteratedSquare(70) should return 1.'", + "testString": "assert.equal(iteratedSquare(70),1,'iteratedSquare(70) should return 1.');" + }, + { + "text": "'iteratedSquare(100) should return 1.'", + "testString": "assert.equal(iteratedSquare(100),1,'iteratedSquare(100) should return 1.');" + } + ], + "id": "5a23c84252665b21eecc7ec1", + "challengeType": 5, + "releasedOn": "June 8, 2018", + "files": { + "indexjs": { + "key": "indexjs", + "ext": "js", + "name": "index", + "contents": [ + "function iteratedSquare (n) {", + " // Good luck!", + "}" + ], + "head": [], + "tail": [] + } + } + }, + { + "title": "Jaro distance", + "description": [ + "The Jaro distance is a measure of similarity between two strings. The higher the Jaro distance for two strings is, the more similar the strings are. The score is normalized such that 0 equates to no similarity and 1 is an exact match. Definition The Jaro distance \\( d_j \\) of two given strings \\(s_1\\) and \\(s_2\\) is", + "\\begin{align}d_j = \\begin{cases}0& & \\text{if }m=0 \\\\\\\\{\\frac {1}{3}}\\left({\\frac {m}{|s_{1}|}}+{\\frac {m}{|s_{2}|}}+{\\frac {m-t}{m}}\\right)& & \\text{otherwise}\\end{cases}\\end{align}", + " Where: ", + "Two characters from \\(s_1\\) and \\(s_2\\) respectively, are considered matching only if they are the same and not farther than \\(\\left\\lfloor\\frac{\\max(|s_1|,|s_2|)}{2}\\right\\rfloor-1\\).", + "Each character of \\(s_1\\) is compared with all its matching characters in \\(s_2\\) . The number of matching (but different sequence order) characters divided by 2 defines the number of transpositions.", + "Example", + "Given the strings \\(s_1\\) DWAYNE and \\(s_2\\) DUANE we find:", + "", + "We find a Jaro score of: \\(d_j = \\frac{1}{3}\\left(\\frac{4}{6} + \\frac{4}{5} + \\frac{4-0}{4}\\right) = 0.822\\).", + "Write a function a that takes two strings as parameters and returns the associated Jaro distance." + ], + "solutions": [ + "function jaro (s, t) {\n var s_len = s.length;\n var t_len = t.length;\n\n if (s_len == 0 && t_len == 0) return 1;\n\n var match_distance = Math.max(s_len, t_len) / 2 - 1;\n\n var s_matches = new Array(s_len);\n var t_matches = new Array(t_len);\n\n var matches = 0;\n var transpositions = 0;\n\n for (var i = 0; i < s_len; i++) {\n var start = Math.max(0, i - match_distance);\n var end = Math.min(i + match_distance + 1, t_len);\n\n for (var j = start; j < end; j++) {\n if (t_matches[j]) continue;\n if (s.charAt(i) != t.charAt(j)) continue;\n s_matches[i] = true;\n t_matches[j] = true;\n matches++;\n break;\n }\n }\n\n if (matches == 0) return 0;\n\n var k = 0;\n for (var i = 0; i < s_len; i++) {\n if (!s_matches[i]) continue;\n while (!t_matches[k]) k++;\n if (s.charAt(i) != t.charAt(k)) transpositions++;\n k++;\n }\n\n return ((matches / s_len) +\n (matches / t_len) +\n ((matches - transpositions / 2.0) / matches)) / 3.0;\n}\n" + ], + "tests": [ + { + "text": "'jaro should be a function.'", + "testString": "assert(typeof jaro=='function','jaro should be a function.');" + }, + { + "text": "'jaro(\"'+tests[0][0]+'\",\"'+tests[0][1]+'\") should return a number.'", + "testString": "assert(typeof jaro(tests[0][0],tests[0][1])=='number','jaro() should return a number.');" + }, + { + "text": "'jaro(\"'+tests[0][0]+'\",\"'+tests[0][1]+'\") should return '+results[0]+'.'", + "testString": "assert.equal(jaro(tests[0][0],tests[0][1]),results[0],'jaro(\"'+tests[0][0]+'\",\"'+tests[0][1]+'\") should return '+results[0]+'.');" + }, + { + "text": "'jaro(\"'+tests[1][0]+'\",\"'+tests[1][1]+'\") should return '+results[1]+'.'", + "testString": "assert.equal(jaro(tests[1][0],tests[1][1]),results[1],'jaro(\"'+tests[1][0]+'\",\"'+tests[1][1]+'\") should return '+results[1]+'.');" + }, + { + "text": "'jaro(\"'+tests[2][0]+'\",\"'+tests[2][1]+'\") should return '+results[2]+'.'", + "testString": "assert.equal(jaro(tests[2][0],tests[2][1]),results[2],'jaro(\"'+tests[2][0]+'\",\"'+tests[2][1]+'\") should return '+results[2]+'.');" + }, + { + "text": "'jaro(\"'+tests[3][0]+'\",\"'+tests[3][1]+'\") should return '+results[3]+'.'", + "testString": "assert.equal(jaro(tests[3][0],tests[3][1]),results[3],'jaro(\"'+tests[3][0]+'\",\"'+tests[3][1]+'\") should return '+results[3]+'.');" + }, + { + "text": "'jaro(\"'+tests[4][0]+'\",\"'+tests[4][1]+'\") should return '+results[4]+'.'", + "testString": "assert.equal(jaro(tests[4][0],tests[4][1]),results[4],'jaro(\"'+tests[4][0]+'\",\"'+tests[4][1]+'\") should return '+results[4]+'.');" + } + ], + "id": "5a23c84252665b21eecc7ec2", + "challengeType": 5, + "releasedOn": "June 9, 2018", + "files": { + "indexjs": { + "key": "indexjs", + "ext": "js", + "name": "index", + "contents": [ + "function jaro (s, t) {", + " // Good luck!", + "}" + ], + "head": [], + "tail": [ + "let tests=[", + "[\"MARTHA\", \"MARHTA\"],", + "[\"DIXON\", \"DICKSONX\"],", + "[\"JELLYFISH\", \"SMELLYFISH\"],", + "[\"HELLOS\", \"CHELLO\"],", + "[\"ABCD\", \"BCDA\"]", + "]", + "let results=[", + "0.9444444444444445,", + "0.7666666666666666,", + "0.8962962962962964,", + "0.888888888888889,", + "0.8333333333333334", + "]" + ] + } + } + }, + { + "title": "JortSort", + "description": [ + "jortSort is a sorting toolset that makes the user do the work and guarantees efficiency because you don't have to sort ever again. It was originally presented by Jenn \"Moneydollars\" Schiffer at the prestigious JSConf.", + "jortSort is a function that takes a single array of comparable objects as its argument. It then sorts the array in ascending order and compares the sorted array to the originally provided array. If the arrays match (i.e. the original array was already sorted), the function returns true. If the arrays do not match (i.e. the original array was not sorted), the function returns false." + ], + "solutions": [ + "function jortsort (array) {\n // sort the array\n var originalArray = array.slice(0);\n array.sort( function(a,b){return a - b} );\n\n // compare to see if it was originally sorted\n for (var i = 0; i < originalArray.length; ++i) {\n if (originalArray[i] !== array[i]) return false;\n }\n\n return true;\n};\n" + ], + "tests": [ + { + "text": "'jortsort should be a function.'", + "testString": "assert(typeof jortsort=='function','jortsort should be a function.');" + }, + { + "text": "'jortsort('+JSON.stringify(tests[0])+') should return a boolean.'", + "testString": "assert(typeof jortsort(tests[0].slice())=='boolean','jortsort('+JSON.stringify(tests[0])+') should return a boolean.');" + }, + { + "text": "'jortsort('+JSON.stringify(tests[0])+') should return true.'", + "testString": "assert.equal(jortsort(tests[0].slice()),true,'jortsort('+JSON.stringify(tests[0])+') should return true.');" + }, + { + "text": "'jortsort('+JSON.stringify(tests[1])+') should return false.'", + "testString": "assert.equal(jortsort(tests[1].slice()),false,'jortsort('+JSON.stringify(tests[1])+') should return false.');" + }, + { + "text": "'jortsort('+JSON.stringify(tests[2])+') should return false.'", + "testString": "assert.equal(jortsort(tests[2].slice()),false,'jortsort('+JSON.stringify(tests[2])+') should return false.');" + }, + { + "text": "'jortsort('+JSON.stringify(tests[3])+') should return true.'", + "testString": "assert.equal(jortsort(tests[3].slice()),true,'jortsort('+JSON.stringify(tests[3])+') should return true.');" + }, + { + "text": "'jortsort('+JSON.stringify(tests[4])+') should return false.'", + "testString": "assert.equal(jortsort(tests[4].slice()),false,'jortsort('+JSON.stringify(tests[4])+') should return false.');" + }, + { + "text": "'jortsort('+JSON.stringify(tests[5])+') should return true.'", + "testString": "assert.equal(jortsort(tests[5].slice()),true,'jortsort('+JSON.stringify(tests[5])+') should return true.');" + } + ], + "id": "5a23c84252665b21eecc7ec4", + "challengeType": 5, + "releasedOn": "June 9, 2018", + "files": { + "indexjs": { + "key": "indexjs", + "ext": "js", + "name": "index", + "contents": [ + "function jortsort (array) {", + " // Good luck!", + "}" + ], + "head": [], + "tail": [ + "let tests=[[1,2,3,4,5],", + "[1,2,13,4,5],", + "[12,4,51,2,4],", + "[1,2],", + "[5,4,3,2,1],", + "[1,1,1,1,1]]" + ] + } + } + }, + { + "title": "Josephus problem", + "description": [ + "Josephus problem is a math puzzle with a grim description: $n$ prisoners are standing on a circle, sequentially numbered from $0$ to $n-1$.", + "An executioner walks along the circle, starting from prisoner $0$, removing every $k$-th prisoner and killing him.", + "As the process goes on, the circle becomes smaller and smaller, until only one prisoner remains, who is then freed.", + "For example, if there are $n=5$ prisoners and $k=2$, the order the prisoners are killed in (let's call it the \"killing sequence\") will be 1, 3, 0, and 4, and the survivor will be #2.", + "Given any $n, k > 0$, find out which prisoner will be the final survivor.", + "In one such incident, there were 41 prisoners and every 3rd prisoner was being killed ($k=3$).", + "Among them was a clever chap name Josephus who worked out the problem, stood at the surviving position, and lived on to tell the tale.", + "Which number was he?", + "Write a function that takes the initial number of prisoners and 'k' as parameter and returns the number of the prisoner that survives." + ], + "solutions": [ + "function josephus (init, kill) {\n var Josephus = {\n init: function(n) {\n this.head = {};\n var current = this.head;\n for (var i = 0; i < n - 1; i++) {\n current.label = i + 1;\n current.next = {\n prev: current\n };\n current = current.next;\n }\n current.label = n;\n current.next = this.head;\n this.head.prev = current;\n return this;\n },\n kill: function(spacing) {\n var current = this.head;\n while (current.next !== current) {\n for (var i = 0; i < spacing - 1; i++) {\n current = current.next;\n }\n current.prev.next = current.next;\n current.next.prev = current.prev;\n current = current.next;\n }\n return current.label;\n }\n }\n\n return Josephus.init(init).kill(kill)\n}\n\n" + ], + "tests": [ + { + "text": "'josephus should be a function.'", + "testString": "assert(typeof josephus=='function','josephus should be a function.');" + }, + { + "text": "'josephus(30,3) should return a number.'", + "testString": "assert(typeof josephus(30,3)=='number','josephus(30,3) should return a number.');" + }, + { + "text": "'josephus(30,3) should return 29.'", + "testString": "assert.equal(josephus(30,3),29,'josephus(30,3) should return 29.');" + }, + { + "text": "'josephus(30,5) should return 3.'", + "testString": "assert.equal(josephus(30,5),3,'josephus(30,5) should return 3.');" + }, + { + "text": "'josephus(20,2) should return 9.'", + "testString": "assert.equal(josephus(20,2),9,'josephus(20,2) should return 9.');" + }, + { + "text": "'josephus(17,6) should return 2.'", + "testString": "assert.equal(josephus(17,6),2,'josephus(17,6) should return 2.');" + }, + { + "text": "'josephus(29,4) should return 2.'", + "testString": "assert.equal(josephus(29,4),2,'josephus(29,4) should return 2.');" + } + ], + "id": "5a23c84252665b21eecc7ec5", + "challengeType": 5, + "releasedOn": "June 9, 2018", + "files": { + "indexjs": { + "key": "indexjs", + "ext": "js", + "name": "index", + "contents": [ + "function josephus (init, kill) {", + " // Good luck!", + "}" + ], + "head": [], + "tail": [] + } + } + }, { "title": "Sailors, coconuts and a monkey problem", "description": [