diff --git a/curriculum/challenges/_meta/rosetta-code/meta.json b/curriculum/challenges/_meta/rosetta-code/meta.json index d57fdfc966..9e2996f9f2 100644 --- a/curriculum/challenges/_meta/rosetta-code/meta.json +++ b/curriculum/challenges/_meta/rosetta-code/meta.json @@ -100,6 +100,18 @@ "59713da0a428c1a62d7db430", "Cramer's rule" ], + [ + "5a23c84252665b21eecc7e03", + "Cumulative standard deviation" + ], + [ + "5a23c84252665b21eecc7e05", + "CUSIP" + ], + [ + "5a23c84252665b21eecc7e06", + "Cut a rectangle" + ], [ "59669d08d75b60482359409f", "Date format" @@ -132,6 +144,10 @@ "59f4eafba0343628bb682785", "Discordian date" ], + [ + "5a23c84252665b21eecc7e1e", + "Dot product" + ], [ "599c333915e0ea32d04d4bec", "Element-wise operations" diff --git a/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/cumulative-standard-deviation.md b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/cumulative-standard-deviation.md new file mode 100644 index 0000000000..9e175f4cb0 --- /dev/null +++ b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/cumulative-standard-deviation.md @@ -0,0 +1,71 @@ +--- +id: 5a23c84252665b21eecc7e03 +title: Cumulative standard deviation +challengeType: 5 +--- + +## Description +
+Write a function that takes an array of numbers as parameter and returns the standard deviation of the series. +
+ +## Instructions +
+ +
+ +## Tests +
+ +``` yml +tests: + - text: standardDeviation should be a function. + testString: assert(typeof standardDeviation == 'function', 'standardDeviation should be a function.'); + - text: standardDeviation([2, 4, 4, 4, 5, 5, 7, 9]) should return a number. + testString: assert(typeof standardDeviation([2, 4, 4, 4, 5, 5, 7, 9]) == 'number', 'standardDeviation([2, 4, 4, 4, 5, 5, 7, 9]) should return a number.'); + - text: standardDeviation([2, 4, 4, 4, 5, 5, 7, 9]) should return 2. + testString: assert.equal(standardDeviation([2, 4, 4, 4, 5, 5, 7, 9]), 2, 'standardDeviation([2, 4, 4, 4, 5, 5, 7, 9]) should return 2.'); + - text: standardDeviation([600, 470, 170, 430, 300]) should return 147.323. + testString: assert.equal(standardDeviation([600, 470, 170, 430, 300]), 147.323, 'standardDeviation([600, 470, 170, 430, 300]) should return 147.323.'); + - text: standardDeviation([75, 83, 96, 100, 121, 125]) should return 18.239. + testString: assert.equal(standardDeviation([75, 83, 96, 100, 121, 125]), 18.239, 'standardDeviation([75, 83, 96, 100, 121, 125]) should return 18.239.'); + - text: standardDeviation([23, 37, 45, 49, 56, 63, 63, 70, 72, 82]) should return 16.87. + testString: assert.equal(standardDeviation([23, 37, 45, 49, 56, 63, 63, 70, 72, 82]), 16.87, 'standardDeviation([23, 37, 45, 49, 56, 63, 63, 70, 72, 82]) should return 16.87.'); + - text: standardDeviation([271, 354, 296, 301, 333, 326, 285, 298, 327, 316, 287, 314]) should return 22.631. + testString: assert.equal(standardDeviation([271, 354, 296, 301, 333, 326, 285, 298, 327, 316, 287, 314]), 22.631, 'standardDeviation([271, 354, 296, 301, 333, 326, 285, 298, 327, 316, 287, 314]) should return 22.631.'); +``` + +
+ +## Challenge Seed +
+
+ +```js +function standardDeviation (arr) { + // Good luck! +} +``` + +
+
+ +## Solution +
+ +```js +function standardDeviation (arr) { + var sum = 0, + sum_sq = 0, + n = arr.length; + arr.forEach(function(e) { + sum += e; + sum_sq += e * e; + }) + + var std_dev=Math.sqrt((sum_sq / n) - Math.pow(sum / n, 2)) + return Math.round(std_dev*1000)/1000; +} +``` + +
\ No newline at end of file diff --git a/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/cusip.md b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/cusip.md new file mode 100644 index 0000000000..d70096b2b1 --- /dev/null +++ b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/cusip.md @@ -0,0 +1,92 @@ +--- +id: 5a23c84252665b21eecc7e05 +title: CUSIP +challengeType: 5 +--- + +## Description +
+A CUSIP is a nine-character alphanumeric code that identifies a North American financial security for the purposes of facilitating clearing and settlement of trades. The CUSIP was adopted as an American National Standard under Accredited Standards X9.6. +Write a function that takes a string as a parameter and checks if the string is valid CUSIP. +
+ +## Instructions +
+ +
+ +## Tests +
+ +``` yml +tests: + - text: isCusip should be a function. + testString: assert(typeof isCusip == 'function', 'isCusip should be a function.'); + - text: isCusip("037833100") should return a boolean. + testString: assert(typeof isCusip("037833100") == 'boolean', 'isCusip("037833100") should return a boolean.'); + - text: isCusip("037833100") should return true. + testString: assert.equal(isCusip("037833100"), true, 'isCusip("037833100") should return true.'); + - text: isCusip("17275R102") should return true. + testString: assert.equal(isCusip("17275R102"), true, 'isCusip("17275R102") should return true.'); + - text: isCusip("38259P50a") should return false. + testString: assert.equal(isCusip("38259P50a"), false, 'isCusip("38259P50a") should return false.'); + - text: isCusip("38259P508") should return true. + testString: assert.equal(isCusip("38259P508"), true, 'isCusip("38259P508") should return true.'); + - text: isCusip("38259P50#") should return false. + testString: assert.equal(isCusip("38259P50#"), false, 'isCusip("38259P50#") should return false.'); + - text: isCusip("68389X105") should return true. + testString: assert.equal(isCusip("68389X105"), true, 'isCusip("68389X105") should return true.'); + - text: isCusip("68389X106") should return false. + testString: assert.equal(isCusip("68389X106"), false, 'isCusip("68389X106") should return false.'); + - text: isCusip("5949181") should return false. + testString: assert.equal(isCusip("5949181"), false, 'isCusip("5949181") should return false.'); +``` + +
+ +## Challenge Seed +
+
+ +```js +function isCusip (s) { + // Good luck! +} +``` + +
+
+ +## Solution +
+ +```js +function isCusip (s) { + if (s.length != 9) return false; + var sum = 0; + var ASCII = x => x.charCodeAt(0); + for (var i = 0; i < 7; i++) { + var c = s.charCodeAt(i); + + var v; + if (c >= ASCII('0') && c <= ASCII('9')) { + v = c - 48; + } else if (c >= ASCII('A') && c <= ASCII('Z')) { + v = c - 64; // lower case letters apparently invalid + } else if (c == ASCII('*')) { + v = 36; + } else if (c == ASCII('@')) { + v = 37; + } else if (c == ASCII('#')) { + v = 38; + } else { + return false; + } + if (i % 2 == 1) v *= 2; // check if odd as using 0-based indexing + sum += Math.floor(v / 10) + v % 10; + } + return s.charCodeAt(8) - 48 == (10 - (sum % 10)) % 10; +} +``` + +
\ No newline at end of file diff --git a/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/cut-a-rectangle.md b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/cut-a-rectangle.md new file mode 100644 index 0000000000..61737ed21d --- /dev/null +++ b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/cut-a-rectangle.md @@ -0,0 +1,113 @@ +--- +id: 5a23c84252665b21eecc7e06 +title: Cut a rectangle +challengeType: 5 +--- + +## Description +
+A given rectangle is made from m × n squares. If m and n are not both odd, then it is possible to cut a path through the rectangle along the square edges such that the rectangle splits into two connected pieces with the same shape (after rotating one of the pieces by 180°). All such paths for 2 × 2 and 4 × 3 rectangles are shown below. +file:rect-cut.svg +Write a function that calculates the number of different ways to cut an m × n rectangle. +
+ +## Instructions +
+ +
+ +## Tests +
+ +``` yml +tests: + - text: cutRectangle should be a function. + testString: assert(typeof cutRectangle == 'function', 'cutRectangle should be a function.'); + - text: cutRectangle(2, 2) should return a number. + testString: assert(typeof cutRectangle(2, 2) == 'number', 'cutRectangle(2, 2) should return a number.'); + - text: cutRectangle(2, 2) should return 2. + testString: assert.equal(cutRectangle(2, 2), 2, 'cutRectangle(2, 2) should return 2.'); + - text: cutRectangle(4, 3) should return 9. + testString: assert.equal(cutRectangle(4, 3), 9, 'cutRectangle(4, 3) should return 9.'); + - text: cutRectangle(4, 4) should return 22. + testString: assert.equal(cutRectangle(4, 4), 22, 'cutRectangle(4, 4) should return 22.'); + - text: cutRectangle(8, 3) should return 53. + testString: assert.equal(cutRectangle(8, 3), 53, 'cutRectangle(8, 3) should return 53.'); + - text: cutRectangle(7, 4) should return 151. + testString: assert.equal(cutRectangle(7, 4), 151, 'cutRectangle(7, 4) should return 151.'); +``` + +
+ +## Challenge Seed +
+
+ +```js +function cutRectangle (w, h) { + // Good luck! +} +``` + +
+
+ +## Solution +
+ +```js +function cutRectangle (w, h) { + if (w % 2 == 1 && h % 2 == 1) + return; + + var dirs = [[0, -1], [-1, 0], [0, 1], [1, 0]]; + + var grid = new Array(h); for (var i = 0; i < grid.length; i++) grid[i]=new Array(w); + var stack = []; + + var half = Math.floor((w * h) / 2); + var bits = Math.pow(2, half) - 1; + var result=0; + for (; bits > 0; bits -= 2) { + + for (var i = 0; i < half; i++) { + var r = Math.floor(i / w); + var c = i % w; + grid[r][c] = (bits & (1 << i)) != 0 ? 1 : 0; + grid[h - r - 1][w - c - 1] = 1 - grid[r][c]; + } + + stack.push(0); + grid[0][0] = 2; + var count = 1; + while (stack.length!=0) { + + var pos = stack.pop(); + var r = Math.floor(pos / w); + var c = pos % w; + + for (var dir of dirs) { + var nextR = r + dir[0]; + var nextC = c + dir[1]; + + if (nextR >= 0 && nextR < h && nextC >= 0 && nextC < w) { + + if (grid[nextR][nextC] == 1) { + stack.push(nextR * w + nextC); + grid[nextR][nextC] = 2; + count++; + } + } + } + } + + if (count == half) { + result++; + } + } + + return result; +} +``` + +
\ No newline at end of file diff --git a/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/dot-product.md b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/dot-product.md new file mode 100644 index 0000000000..73a1e3a94b --- /dev/null +++ b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/dot-product.md @@ -0,0 +1,65 @@ +--- +id: 5a23c84252665b21eecc7e1e +title: Dot product +challengeType: 5 +--- + +## Description +
+Create a function, to compute the dot product, also known as the scalar product of two vectors. +
+ +## Instructions +
+ +
+ +## Tests +
+ +``` yml +tests: + - text: dotProduct should be a function. + testString: assert(typeof dotProduct == 'function', 'dotProduct should be a function.'); + - text: dotProduct([1, 3, -5], [4, -2, -1]) should return a number. + testString: assert(typeof dotProduct([1, 3, -5], [4, -2, -1]) == 'number', 'dotProduct([1, 3, -5], [4, -2, -1]) should return a number.'); + - text: dotProduct([1, 3, -5], [4, -2, -1]) should return 3. + testString: assert.equal(dotProduct([1, 3, -5], [4, -2, -1]), 3, 'dotProduct([1, 3, -5], [4, -2, -1]) should return 3.'); + - text: dotProduct([1, 2, 3, 4, 5], [6, 7, 8, 9, 10]) should return 130. + testString: assert.equal(dotProduct([1, 2, 3, 4, 5], [6, 7, 8, 9, 10]), 130, 'dotProduct([1, 2, 3, 4, 5], [6, 7, 8, 9, 10]) should return 130.'); + - text: dotProduct([5, 4, 3, 2], [7, 8, 9, 6]) should return 106. + testString: assert.equal(dotProduct([5, 4, 3, 2], [7, 8, 9, 6]), 106, 'dotProduct([5, 4, 3, 2], [7, 8, 9, 6]) should return 106.'); + - text: dotProduct([-5, 4, -3, 2], [-7, -8, 9, -6]) should return -36. + testString: assert.equal(dotProduct([-5, 4, -3, 2], [-7, -8, 9, -6]), -36, 'dotProduct([-5, 4, -3, 2], [-7, -8, 9, -6]) should return -36.'); + - text: dotProduct([17, 27, 34, 43, 15], [62, 73, 48, 95, 110]) should return 10392. + testString: assert.equal(dotProduct([17, 27, 34, 43, 15], [62, 73, 48, 95, 110]), 10392, 'dotProduct([17, 27, 34, 43, 15], [62, 73, 48, 95, 110]) should return 10392.'); +``` + +
+ +## Challenge Seed +
+
+ +```js +function dotProduct (ary1, ary2) { + // Good luck! +} +``` + +
+
+ +## Solution +
+ +```js +function dotProduct (ary1, ary2) { + var dotprod = 0; + for (var i = 0; i < ary1.length; i++) + dotprod += ary1[i] * ary2[i]; + return dotprod; +} +``` + +
\ No newline at end of file