feat(interview-prep): Converting and transferring Rosetta problems (#35487)

* feat(interview-prep): Converting and transferring Rosetta problems

* feat(interview-prep): Fixing challenges

* fix(challenges): Added code tags

* Update knight's-tour.md

Moved text to instructions section and fixed a small typo.

* feat(interview-prep): Update description of Knight's tour

Co-Authored-By: NitronR <bhanur05@gmail.com>

* feat(interview-prep): Update description of Kaprekar numbers

Co-Authored-By: NitronR <bhanur05@gmail.com>

* feat(inteview-prep): Kaprekar numbers description

Added <code> tags.

* fix(interview-prep): Update description Kaprekar Numbers

Co-Authored-By: NitronR <bhanur05@gmail.com>

* fix(interview-prep): Added code tags Kaprekar numbers

Co-Authored-By: NitronR <bhanur05@gmail.com>
This commit is contained in:
Bhanu Pratap Singh Rathore
2019-03-21 11:52:35 +05:30
committed by The Coding Aviator
parent cab064a183
commit 79394d141f
9 changed files with 983 additions and 0 deletions

View File

@ -0,0 +1,353 @@
---
id: 5a23c84252665b21eecc7ecb
title: K-d tree
challengeType: 5
---
## Description
<section id='description'>
A k-d tree (short for <i>k</i>-dimensional tree) is a space-partitioning data structure for organizing points in a k-dimensional space.
k-d trees are a useful data structure for several applications, such as searches involving a multidimensional search key (e.g. range searches and nearest neighbor searches).
k-d trees are a special case of binary space partitioning trees. k-d trees are not suitable, however, for efficiently finding the nearest neighbor in high dimensional spaces. As a general rule, if the dimensionality is <i>k</i>, the number of points in the data, <i>N</i>, should be <i>N</i>2<sup><i>k</i></sup>.
Otherwise, when k-d trees are used with high-dimensional data, most of the points in the tree will be evaluated and the efficiency is no better than exhaustive search, and other methods such as approximate nearest-neighbor are used instead.
</section>
## Instructions
<section id='instructions'>
Write a function to perform a nearest neighbour search using k-d tree. The function takes two parameters: an array of k-dimensional points, and a single k-dimensional point whose nearest neighbour should be returned by the function. A k-dimensional point will be given as an array of k elements.
</section>
## Tests
<section id='tests'>
``` yml
tests:
- text: <code>kdNN</code> should be a function.
testString: assert(typeof kdNN == 'function', '<code>kdNN</code> should be a function.');
- text: <code>kdNN([[[2, 3], [5, 4], [9, 6], [4, 7], [8, 1], [7, 2]], [9, 2])</code> should return an array.
testString: assert(Array.isArray(kdNN([[2, 3], [5, 4], [9, 6], [4, 7], [8, 1], [7, 2]], [9, 2])), '<code>kdNN([[[2, 3], [5, 4], [9, 6], [4, 7], [8, 1], [7, 2]], [9, 2])</code> should return an array.');
- text: <code>kdNN([[[2, 3], [5, 4], [9, 6], [4, 7], [8, 1], [7, 2]], [9, 2])</code> should return <code>[ 8, 1 ]</code>.
testString: assert.deepEqual(kdNN([[2, 3], [5, 4], [9, 6], [4, 7], [8, 1], [7, 2]], [9, 2]), [8, 1], '<code>kdNN([[[2, 3], [5, 4], [9, 6], [4, 7], [8, 1], [7, 2]], [9, 2])</code> should return <code>[ 8, 1 ]</code>.');
- text: <code>kdNN([[[2, 3], [5, 4], [9, 6], [4, 7], [8, 1], [7, 2]], [7, 1])</code> should return <code>[ 8, 1 ]</code>.
testString: assert.deepEqual(kdNN([[2, 3], [5, 4], [9, 6], [4, 7], [8, 1], [7, 2]], [7, 1]), [8, 1], '<code>kdNN([[[2, 3], [5, 4], [9, 6], [4, 7], [8, 1], [7, 2]], [7, 1])</code> should return <code>[ 8, 1 ]</code>.');
- text: <code>kdNN([[[2, 3], [5, 4], [9, 6], [4, 7], [8, 1], [7, 2]], [3, 2])</code> should return <code>[ 2, 3 ]</code>.
testString: assert.deepEqual(kdNN([[2, 3], [5, 4], [9, 6], [4, 7], [8, 1], [7, 2]], [3, 2]), [2, 3], '<code>kdNN([[[2, 3], [5, 4], [9, 6], [4, 7], [8, 1], [7, 2]], [3, 2])</code> should return <code>[ 2, 3 ]</code>.');
- text: <code>kdNN([[2, 3, 1], [9, 4, 5], [4, 6, 7], [1, 2, 5], [7, 8, 9], [3, 6, 1]], [1, 2, 3])</code> should return <code>[ 1, 2, 5 ]</code>.
testString: assert.deepEqual(kdNN([[2, 3, 1], [9, 4, 5], [4, 6, 7], [1, 2, 5], [7, 8, 9], [3, 6, 1]], [1, 2, 3]), [1, 2, 5], '<code>kdNN([[2, 3, 1], [9, 4, 5], [4, 6, 7], [1, 2, 5], [7, 8, 9], [3, 6, 1]], [1, 2, 3])</code> should return <code>[ 1, 2, 5 ]</code>.');
- text: <code>kdNN([[2, 3, 1], [9, 4, 5], [4, 6, 7], [1, 2, 5], [7, 8, 9], [3, 6, 1]], [4, 5, 6])</code> should return <code>[ 4, 6, 7 ]</code>.
testString: assert.deepEqual(kdNN([[2, 3, 1], [9, 4, 5], [4, 6, 7], [1, 2, 5], [7, 8, 9], [3, 6, 1]], [4, 5, 6]), [4, 6, 7], '<code>kdNN([[2, 3, 1], [9, 4, 5], [4, 6, 7], [1, 2, 5], [7, 8, 9], [3, 6, 1]], [4, 5, 6])</code> should return <code>[ 4, 6, 7 ]</code>.');
- text: <code>kdNN([[2, 3, 1], [9, 4, 5], [4, 6, 7], [1, 2, 5], [7, 8, 9], [3, 6, 1]], [8, 8, 8])</code> should return <code>[ 7, 8, 9 ]</code>.
testString: assert.deepEqual(kdNN([[2, 3, 1], [9, 4, 5], [4, 6, 7], [1, 2, 5], [7, 8, 9], [3, 6, 1]], [8, 8, 8]), [7, 8, 9], '<code>kdNN([[2, 3, 1], [9, 4, 5], [4, 6, 7], [1, 2, 5], [7, 8, 9], [3, 6, 1]], [8, 8, 8])</code> should return <code>[ 7, 8, 9 ]</code>.');
```
</section>
## Challenge Seed
<section id='challengeSeed'>
<div id='js-seed'>
```js
function kdNN(fpoints, fpoint) {
// Good luck!
}
```
</div>
</section>
## Solution
<section id='solution'>
```js
function kdNN(fpoints, fpoint) {
function Node(obj, dimension, parent) {
this.obj = obj;
this.left = null;
this.right = null;
this.parent = parent;
this.dimension = dimension;
}
function kdTree(points, metric, dimensions) {
var self = this;
function buildTree(points, depth, parent) {
var dim = depth % dimensions.length,
median,
node;
if (points.length === 0) {
return null;
}
if (points.length === 1) {
return new Node(points[0], dim, parent);
}
points.sort(function (a, b) {
return a[dimensions[dim]] - b[dimensions[dim]];
});
median = Math.floor(points.length / 2);
node = new Node(points[median], dim, parent);
node.left = buildTree(points.slice(0, median), depth + 1, node);
node.right = buildTree(points.slice(median + 1), depth + 1, node);
return node;
}
this.root = buildTree(points, 0, null);
this.insert = function (point) {
function innerSearch(node, parent) {
if (node === null) {
return parent;
}
var dimension = dimensions[node.dimension];
if (point[dimension] < node.obj[dimension]) {
return innerSearch(node.left, node);
} else {
return innerSearch(node.right, node);
}
}
var insertPosition = innerSearch(this.root, null),
newNode,
dimension;
if (insertPosition === null) {
this.root = new Node(point, 0, null);
return;
}
newNode = new Node(point, (insertPosition.dimension + 1) % dimensions.length, insertPosition);
dimension = dimensions[insertPosition.dimension];
if (point[dimension] < insertPosition.obj[dimension]) {
insertPosition.left = newNode;
} else {
insertPosition.right = newNode;
}
};
this.nearest = function (point, maxNodes, maxDistance) {
var i,
result,
bestNodes;
bestNodes = new BinaryHeap(
function (e) { return -e[1]; }
);
function nearestSearch(node) {
var bestChild,
dimension = dimensions[node.dimension],
ownDistance = metric(point, node.obj),
linearPoint = {},
linearDistance,
otherChild,
i;
function saveNode(node, distance) {
bestNodes.push([node, distance]);
if (bestNodes.size() > maxNodes) {
bestNodes.pop();
}
}
for (i = 0; i < dimensions.length; i += 1) {
if (i === node.dimension) {
linearPoint[dimensions[i]] = point[dimensions[i]];
} else {
linearPoint[dimensions[i]] = node.obj[dimensions[i]];
}
}
linearDistance = metric(linearPoint, node.obj);
if (node.right === null && node.left === null) {
if (bestNodes.size() < maxNodes || ownDistance < bestNodes.peek()[1]) {
saveNode(node, ownDistance);
}
return;
}
if (node.right === null) {
bestChild = node.left;
} else if (node.left === null) {
bestChild = node.right;
} else {
if (point[dimension] < node.obj[dimension]) {
bestChild = node.left;
} else {
bestChild = node.right;
}
}
nearestSearch(bestChild);
if (bestNodes.size() < maxNodes || ownDistance < bestNodes.peek()[1]) {
saveNode(node, ownDistance);
}
if (bestNodes.size() < maxNodes || Math.abs(linearDistance) < bestNodes.peek()[1]) {
if (bestChild === node.left) {
otherChild = node.right;
} else {
otherChild = node.left;
}
if (otherChild !== null) {
nearestSearch(otherChild);
}
}
}
if (maxDistance) {
for (i = 0; i < maxNodes; i += 1) {
bestNodes.push([null, maxDistance]);
}
}
if (self.root)
nearestSearch(self.root);
result = [];
for (i = 0; i < Math.min(maxNodes, bestNodes.content.length); i += 1) {
if (bestNodes.content[i][0]) {
result.push([bestNodes.content[i][0].obj, bestNodes.content[i][1]]);
}
}
return result;
};
}
function BinaryHeap(scoreFunction) {
this.content = [];
this.scoreFunction = scoreFunction;
}
BinaryHeap.prototype = {
push: function (element) {
// Add the new element to the end of the array.
this.content.push(element);
// Allow it to bubble up.
this.bubbleUp(this.content.length - 1);
},
pop: function () {
// Store the first element so we can return it later.
var result = this.content[0];
// Get the element at the end of the array.
var end = this.content.pop();
// If there are any elements left, put the end element at the
// start, and let it sink down.
if (this.content.length > 0) {
this.content[0] = end;
this.sinkDown(0);
}
return result;
},
peek: function () {
return this.content[0];
},
size: function () {
return this.content.length;
},
bubbleUp: function (n) {
// Fetch the element that has to be moved.
var element = this.content[n];
// When at 0, an element can not go up any further.
while (n > 0) {
// Compute the parent element's index, and fetch it.
var parentN = Math.floor((n + 1) / 2) - 1,
parent = this.content[parentN];
// Swap the elements if the parent is greater.
if (this.scoreFunction(element) < this.scoreFunction(parent)) {
this.content[parentN] = element;
this.content[n] = parent;
// Update 'n' to continue at the new position.
n = parentN;
}
// Found a parent that is less, no need to move it further.
else {
break;
}
}
},
sinkDown: function (n) {
// Look up the target element and its score.
var length = this.content.length,
element = this.content[n],
elemScore = this.scoreFunction(element);
while (true) {
// Compute the indices of the child elements.
var child2N = (n + 1) * 2, child1N = child2N - 1;
// This is used to store the new position of the element,
// if any.
var swap = null;
// If the first child exists (is inside the array)...
if (child1N < length) {
// Look it up and compute its score.
var child1 = this.content[child1N],
child1Score = this.scoreFunction(child1);
// If the score is less than our element's, we need to swap.
if (child1Score < elemScore)
swap = child1N;
}
// Do the same checks for the other child.
if (child2N < length) {
var child2 = this.content[child2N],
child2Score = this.scoreFunction(child2);
if (child2Score < (swap == null ? elemScore : child1Score)) {
swap = child2N;
}
}
// If the element needs to be moved, swap it, and continue.
if (swap != null) {
this.content[n] = this.content[swap];
this.content[swap] = element;
n = swap;
}
// Otherwise, we are done.
else {
break;
}
}
}
};
var dims = []
for (var i = 0; i < fpoint.length; i++) dims.push(i)
var tree = new kdTree(fpoints, function (e1, e2) {
var d = 0;
var e3 = e1;
if (!Array.isArray(e1)) {
e3 = []
for (var key in e1)
e3.push(e1[key])
e1 = e3
}
e1.forEach(function (e, i) {
var sqd = (e1[i] - e2[i]);
d += sqd * sqd;
})
return d;
}, dims)
return tree.nearest(fpoint, 1, 1000)[0][0];
}
```
</section>

View File

@ -0,0 +1,82 @@
---
id: 5a23c84252665b21eecc7eca
title: Kaprekar numbers
challengeType: 5
---
## Description
<section id='description'>
A positive integer is a <a href="https://en.wikipedia.org/wiki/Kaprekar number">Kaprekar number</a> if:
<ul>
<li>It is 1, or,</li>
<li>The decimal representation of its square may be split once into two parts consisting of positive integers which sum to the original number. </li>
</ul>
Note that a split resulting in a part consisting purely of 0s is not valid, as 0 is not considered positive.Example
Kaprekar numbers:
<ul>
<li> <code>2223</code> is a Kaprekar number, as <code>2223 * 2223 = 4941729</code>, <code>4941729</code> may be split to <code>494</code> and <code>1729</code>, and <code>494 + 1729 = 2223</code></li>
<li>The series of Kaprekar numbers is known as <a href="http://rosettacode.org/wiki/oeis:A006886">A006886</a>, and begins as <code>1, 9, 45, 55, ...</code></li>
</ul>
</section>
## Instructions
<section id='instructions'>
Write a function that takes a number $n$, a base $bs$, and returns true if the number is a Kaprekar number for the given base. Otherwise, the function returns false.
</section>
## Tests
<section id='tests'>
``` yml
tests:
- text: <code>isKaprekar</code> should be a function.
testString: assert(typeof isKaprekar == 'function', '<code>isKaprekar</code> should be a function.');
- text: <code>isKaprekar(1, 10)</code> should return a boolean.
testString: assert(typeof isKaprekar(1, 10) == 'boolean', '<code>isKaprekar(1, 10)</code> should return a boolean.');
- text: <code>isKaprekar(1, 10)</code> should return <code>true</code>.
testString: assert.equal(isKaprekar(1, 10), true, '<code>isKaprekar(1, 10)</code> should return <code>true</code>.');
- text: <code>isKaprekar(9, 10)</code> should return <code>true</code>.
testString: assert.equal(isKaprekar(9, 10), true, '<code>isKaprekar(9, 10)</code> should return <code>true</code>.');
- text: <code>isKaprekar(2223, 10)</code> should return <code>true</code>.
testString: assert.equal(isKaprekar(2223, 10), true, '<code>isKaprekar(2223, 10)</code> should return <code>true</code>.');
- text: <code>isKaprekar(22823, 10)</code> should return <code>false</code>.
testString: assert.equal(isKaprekar(22823, 10), false, '<code>isKaprekar(22823, 10)</code> should return <code>false</code>.');
- text: <code>isKaprekar(9, 17)</code> should return <code>false</code>.
testString: assert.equal(isKaprekar(9, 17), false, '<code>isKaprekar(9, 17)</code> should return <code>false</code>.');
- text: <code>isKaprekar(225, 17)</code> should return <code>true</code>.
testString: assert.equal(isKaprekar(225, 17), true, '<code>isKaprekar(225, 17)</code> should return <code>true</code>.');
- text: <code>isKaprekar(999, 17)</code> should return <code>false</code>.
testString: assert.equal(isKaprekar(999, 17), false, '<code>isKaprekar(999, 17)</code> should return <code>false</code>.');
```
</section>
## Challenge Seed
<section id='challengeSeed'>
<div id='js-seed'>
```js
function isKaprekar(n, bs) {
// Good luck!
}
```
</div>
</section>
## Solution
<section id='solution'>
```js
function isKaprekar(n, bs) {
if (n < 1) return false;
if (n == 1) return true;
for (var a = n * n, b = 0, s = 1; a; s *= bs) {
b += a % bs * s;
a = Math.floor(a / bs);
if (b && a + b == n) return true;
} return false;
}
```
</section>

View File

@ -0,0 +1,141 @@
---
id: 5a23c84252665b21eecc7ed5
title: Knight's tour
challengeType: 5
---
## Description
<section id='description'>
<a href="https://en.wikipedia.org/wiki/Knight%27s_tour">Knight's Tour</a>Problem: You have an empty <code>w</code> * <code>h</code> chessboard, but for a single knight on some square. The knight must perform a sequence of legal moves that result in the knight visiting every square on the chessboard exactly once. Note that it is <i>not</i> a requirement that the tour be "closed"; that is, the knight need not end within a single move of its start position.
</section>
## Instructions
<section id='instructions'>
Write a function that takes <code>w</code> and <code>h</code> as parameters and returns the number of initial positions from where it is possible to achieve the task stated above.
</section>
## Tests
<section id='tests'>
``` yml
tests:
- text: <code>knightTour</code> should be a function.
testString: assert(typeof knightTour == 'function', '<code>knightTour</code> should be a function.');
- text: <code>knightTour(6, 6)</code> should return a number.
testString: assert(typeof knightTour(6, 6) == 'number', '<code>knightTour(6, 6)</code> should return a number.');
- text: <code>knightTour(6, 6)</code> should return <code>35</code>.
testString: assert.equal(knightTour(6, 6), 35, '<code>knightTour(6, 6)</code> should return <code>35</code>.');
- text: <code>knightTour(5, 6)</code> should return <code>20</code>.
testString: assert.equal(knightTour(5, 6), 20, '<code>knightTour(5, 6)</code> should return <code>20</code>.');
- text: <code>knightTour(4, 6)</code> should return <code>10</code>.
testString: assert.equal(knightTour(4, 6), 10, '<code>knightTour(4, 6)</code> should return <code>10</code>.');
- text: <code>knightTour(7, 3)</code> should return <code>4</code>.
testString: assert.equal(knightTour(7, 3), 4, '<code>knightTour(7, 3)</code> should return <code>4</code>.');
- text: <code>knightTour(8, 6)</code> should return <code>47</code>.
testString: assert.equal(knightTour(8, 6), 47, '<code>knightTour(8, 6)</code> should return <code>47</code>.');
```
</section>
## Challenge Seed
<section id='challengeSeed'>
<div id='js-seed'>
```js
function knightTour(w, h) {
// Good luck!
}
```
</div>
</section>
## Solution
<section id='solution'>
```js
function knightTour (w, h) {
var b, cnt=0;
var dx = [ -2, -2, -1, 1, 2, 2, 1, -1 ];
var dy = [ -1, 1, 2, 2, 1, -1, -2, -2 ];
function init_board()
{
var i, j, k, x, y;
// * b is board; a is board with 2 rows padded at each side
for(i=0;i<h;i++){
for(j=0;j<w;j++){
b[i][j]=255
}
}
for (i = 0; i < h; i++) {
for (j = 0; j < w; j++) {
for (k = 0; k < 8; k++) {
x = j + dx[k], y = i + dy[k];
if (b[i][j] == 255) b[i][j] = 0;
if(x >= 0 && x < w && y >= 0 && y < h) b[i][j]++;
}
}
}
}
function walk_board(x, y)
{
var i, nx, ny, least;
var steps = 0;
// printf(E"H"E"J"E"%d;%dH"E"32m[]"E"m", y + 1, 1 + 2 * x);
while (1) {
// * occupy cell
b[y][x] = 255;
// * reduce all neighbors' neighbor count
for (i = 0; i < 8; i++)
if(y+dy[i] >= 0 && x+dx[i] >= 0 && y+dy[i] < h && x+dx[i] < w)
b[ y + dy[i] ][ x + dx[i] ]--;
// find neighbor with lowest neighbor count
least = 255;
for (i = 0; i < 8; i++) {
if(y+dy[i] >= 0 && x+dx[i] >= 0 && y+dy[i] < h && x+dx[i] < w)
if (b[ y + dy[i] ][ x + dx[i] ] < least) {
nx = x + dx[i];
ny = y + dy[i];
least = b[ny][nx];
}
}
if (least > 7) {
return steps == w * h - 1;
}
steps++;
x = nx, y = ny;
}
}
function solve (x, y) {
b=new Array(h);
for(var i=0;i<h;i++)
b[i]=new Array(w)
init_board();
if (walk_board(x, y)) {
cnt++;
}
}
for(var i=0;i<h;i++){
for(var j=0;j<w;j++){
solve(j, i);
}
}
return cnt;
}
```
</section>

View File

@ -0,0 +1,74 @@
---
id: 5a23c84252665b21eecc7edb
title: Largest int from concatenated ints
challengeType: 5
---
## Description
<section id='description'>
Given a set of positive integers, write a function to order the integers in such a way that the concatenation of the numbers forms the largest possible integer and return this integer.
</section>
## Instructions
<section id='instructions'>
</section>
## Tests
<section id='tests'>
``` yml
tests:
- text: <code>maxCombine</code> should be a function.
testString: assert(typeof maxCombine == 'function', '<code>maxCombine</code> should be a function.');
- text: <code>maxCombine([1, 3, 3, 4, 55])</code> should return a number.
testString: assert(typeof maxCombine([1, 3, 3, 4, 55]) == 'number', '<code>maxCombine([1, 3, 3, 4, 55])</code> should return a number.');
- text: <code>maxCombine([1, 3, 3, 4, 55])</code> should return <code>554331</code>.
testString: assert.equal(maxCombine([1, 3, 3, 4, 55]), 554331, '<code>maxCombine([1, 3, 3, 4, 55])</code> should return <code>554331</code>.');
- text: <code>maxCombine([71, 45, 23, 4, 5])</code> should return <code>71545423</code>.
testString: assert.equal(maxCombine([71, 45, 23, 4, 5]), 71545423, '<code>maxCombine([71, 45, 23, 4, 5])</code> should return <code>71545423</code>.');
- text: <code>maxCombine([14, 43, 53, 114, 55])</code> should return <code>55534314114</code>.
testString: assert.equal(maxCombine([14, 43, 53, 114, 55]), 55534314114, '<code>maxCombine([14, 43, 53, 114, 55])</code> should return <code>55534314114</code>.');
- text: <code>maxCombine([1, 34, 3, 98, 9, 76, 45, 4])</code> should return <code>998764543431</code>.
testString: assert.equal(maxCombine([1, 34, 3, 98, 9, 76, 45, 4]), 998764543431, '<code>maxCombine([1, 34, 3, 98, 9, 76, 45, 4])</code> should return <code>998764543431</code>.');
- text: <code>maxCombine([54, 546, 548, 60])</code> should return <code>6054854654</code>.
testString: assert.equal(maxCombine([54, 546, 548, 60]), 6054854654, '<code>maxCombine([54, 546, 548, 60])</code> should return <code>6054854654</code>.');
```
</section>
## Challenge Seed
<section id='challengeSeed'>
<div id='js-seed'>
```js
function maxCombine(xs) {
// Good luck!
}
```
</div>
</section>
## Solution
<section id='solution'>
```js
function maxCombine (xs) {
return parseInt(
xs.sort(
function (x, y) {
var a = x.toString(),
b = y.toString(),
ab = parseInt(a + b),
ba = parseInt(b + a);
return ab > ba ? -1 : (ab < ba ? 1 : 0);
}
)
.join(''), 10
);
}
```
</section>

View File

@ -0,0 +1,76 @@
---
id: 5a23c84252665b21eecc7edc
title: Last Friday of each month
challengeType: 5
---
## Description
<section id='description'>
Write a function that returns the date of the last Friday of a given month for a given year.
</section>
## Instructions
<section id='instructions'>
</section>
## Tests
<section id='tests'>
``` yml
tests:
- text: <code>lastFriday</code> should be a function.
testString: assert(typeof lastFriday == 'function', '<code>lastFriday</code> should be a function.');
- text: <code>lastFriday(2018, 1)</code> should return a number.
testString: assert(typeof lastFriday(2018, 1) == 'number', '<code>lastFriday(2018, 1)</code> should return a number.');
- text: <code>lastFriday(2018, 1)</code> should return <code>26</code>.
testString: assert.equal(lastFriday(2018, 1), 26, '<code>lastFriday(2018, 1)</code> should return <code>26</code>.');
- text: <code>lastFriday(2017, 2)</code> should return <code>24</code>.
testString: assert.equal(lastFriday(2017, 2), 24, '<code>lastFriday(2017, 2)</code> should return <code>24</code>.');
- text: <code>lastFriday(2012, 3)</code> should return <code>30</code>.
testString: assert.equal(lastFriday(2012, 3), 30, '<code>lastFriday(2012, 3)</code> should return <code>30</code>.');
- text: <code>lastFriday(1900, 4)</code> should return <code>27</code>.
testString: assert.equal(lastFriday(1900, 4), 27, '<code>lastFriday(1900, 4)</code> should return <code>27</code>.');
- text: <code>lastFriday(2000, 5)</code> should return <code>26</code>.
testString: assert.equal(lastFriday(2000, 5), 26, '<code>lastFriday(2000, 5)</code> should return <code>26</code>.');
- text: <code>lastFriday(2006, 6)</code> should return <code>30</code>.
testString: assert.equal(lastFriday(2006, 6), 30, '<code>lastFriday(2006, 6)</code> should return <code>30</code>.');
- text: <code>lastFriday(2010, 7)</code> should return <code>30</code>.
testString: assert.equal(lastFriday(2010, 7), 30, '<code>lastFriday(2010, 7)</code> should return <code>30</code>.');
- text: <code>lastFriday(2005, 8)</code> should return <code>26</code>.
testString: assert.equal(lastFriday(2005, 8), 26, '<code>lastFriday(2005, 8)</code> should return <code>26</code>.');
```
</section>
## Challenge Seed
<section id='challengeSeed'>
<div id='js-seed'>
```js
function lastFriday(year, month) {
// Good luck!
}
```
</div>
</section>
## Solution
<section id='solution'>
```js
function lastFriday (year, month) {
var i, last_day;
i = 0;
while (true) {
last_day = new Date(year, month, i);
if (last_day.getDay() === 5) {
return last_day.getDate();
}
i -= 1;
}
};
```
</section>

View File

@ -0,0 +1,64 @@
---
id: 5a23c84252665b21eecc7ede
title: Leap year
challengeType: 5
---
## Description
<section id='description'>
Determine whether a given year is a leap year in the Gregorian calendar.
</section>
## Instructions
<section id='instructions'>
</section>
## Tests
<section id='tests'>
``` yml
tests:
- text: <code>isLeapYear</code> should be a function.
testString: assert(typeof isLeapYear == 'function', '<code>isLeapYear</code> should be a function.');
- text: <code>isLeapYear()</code> should return a boolean.
testString: assert(typeof isLeapYear(2018) == 'boolean', '<code>isLeapYear()</code> should return a boolean.');
- text: <code>isLeapYear(2018)</code> should return <code>false</code>.
testString: assert.equal(isLeapYear(2018), false, '<code>isLeapYear(2018)</code> should return <code>false</code>.');
- text: <code>isLeapYear(2016)</code> should return <code>true</code>.
testString: assert.equal(isLeapYear(2016), true, '<code>isLeapYear(2016)</code> should return <code>true</code>.');
- text: <code>isLeapYear(2000)</code> should return <code>true</code>.
testString: assert.equal(isLeapYear(2000), true, '<code>isLeapYear(2000)</code> should return <code>true</code>.');
- text: <code>isLeapYear(1900)</code> should return <code>false</code>.
testString: assert.equal(isLeapYear(1900), false, '<code>isLeapYear(1900)</code> should return <code>false</code>.');
- text: <code>isLeapYear(1996)</code> should return <code>true</code>.
testString: assert.equal(isLeapYear(1996), true, '<code>isLeapYear(1996)</code> should return <code>true</code>.');
- text: <code>isLeapYear(1800)</code> should return <code>false</code>.
testString: assert.equal(isLeapYear(1800), false, '<code>isLeapYear(1800)</code> should return <code>false</code>.');
```
</section>
## Challenge Seed
<section id='challengeSeed'>
<div id='js-seed'>
```js
function isLeapYear(year) {
// Good luck!
}
```
</div>
</section>
## Solution
<section id='solution'>
```js
function isLeapYear (year) {
return (year % 100 === 0) ? (year % 400 === 0) : (year % 4 === 0);
};
```
</section>

View File

@ -0,0 +1,72 @@
---
id: 5a23c84252665b21eecc7edf
title: Least common multiple
challengeType: 5
---
## Description
<section id='description'>
The least common multiple of 12 and 18 is 36, because 12 is a factor (12 &times; 3 = 36), and 18 is a factor (18 &times; 2 = 36), and there is no positive integer less than 36 that has both factors. As a special case, if either <i>m</i> or <i>n</i> is zero, then the least common multiple is zero.
One way to calculate the least common multiple is to iterate all the multiples of <i>m</i>, until you find one that is also a multiple of <i>n</i>.
If you already have <i>gcd</i> for <a href="http://rosettacode.org/wiki/greatest common divisor">greatest common divisor</a>, then this formula calculates <i>lcm</i>.
\( \operatorname{lcm}(m, n) = \frac{|m \times n|}{\operatorname{gcd}(m, n)} \)
</section>
## Instructions
<section id='instructions'>
Compute the least common multiple of an array of intergers.
Given <i>m</i> and <i>n</i>, the least common multiple is the smallest positive integer that has both <i>m</i> and <i>n</i> as factors.
</section>
## Tests
<section id='tests'>
``` yml
tests:
- text: <code>LCM</code> should be a function.
testString: assert(typeof LCM == 'function', '<code>LCM</code> should be a function.');
- text: <code>LCM([2, 4, 8])</code> should return a number.
testString: assert(typeof LCM([2, 4, 8]) == 'number', '<code>LCM([2, 4, 8])</code> should return a number.');
- text: <code>LCM([2, 4, 8])</code> should return <code>8</code>.
testString: assert.equal(LCM([2, 4, 8]), 8, '<code>LCM([2, 4, 8])</code> should return <code>8</code>.');
- text: <code>LCM([4, 8, 12])</code> should return <code>24</code>.
testString: assert.equal(LCM([4, 8, 12]), 24, '<code>LCM([4, 8, 12])</code> should return <code>24</code>.');
- text: <code>LCM([3, 4, 5, 12, 40])</code> should return <code>120</code>.
testString: assert.equal(LCM([3, 4, 5, 12, 40]), 120, '<code>LCM([3, 4, 5, 12, 40])</code> should return <code>120</code>.');
- text: <code>LCM([11, 33, 90])</code> should return <code>990</code>.
testString: assert.equal(LCM([11, 33, 90]), 990, '<code>LCM([11, 33, 90])</code> should return <code>990</code>.');
- text: <code>LCM([-50, 25, -45, -18, 90, 447])</code> should return <code>67050</code>.
testString: assert.equal(LCM([-50, 25, -45, -18, 90, 447]), 67050, '<code>LCM([-50, 25, -45, -18, 90, 447])</code> should return <code>67050</code>.');
```
</section>
## Challenge Seed
<section id='challengeSeed'>
<div id='js-seed'>
```js
function LCM(A) {
// Good luck!
}
```
</div>
</section>
## Solution
<section id='solution'>
```js
function LCM (A) {
var n = A.length, a = Math.abs(A[0]);
for (var i = 1; i < n; i++)
{ var b = Math.abs(A[i]), c = a;
while (a && b){ a > b ? a %= b : b %= a; }
a = Math.abs(c*A[i])/(a+b);
}
return a;
}
```
</section>

View File

@ -0,0 +1,89 @@
---
id: 5a23c84252665b21eecc7ee0
title: Left factorials
challengeType: 5
---
## Description
<section id='description'>
<b>Left factorials</b>, $ !n $, may refer to either <i>subfactorials</i> or to <i>factorial sums</i>. The same notation can be confusingly seen used for the two different definitions. Sometimes, <i>subfactorials</i> (also known as <i>derangements</i>) may use any of the notations:
<ul>
<li>$!n`$</li>
<li>$!n$</li>
<li>$n¡$</li>
</ul>
(It may not be visually obvious, but the last example uses an upside-down exclamation mark.) This task will be using this formula for <b>left factorial</b>:
$ !n = \sum_{k=0}^{n-1} k! $
where $!0 = 0$
</section>
## Instructions
<section id='instructions'>
Write a function to calculate the left factorial of a given number.
</section>
## Tests
<section id='tests'>
``` yml
tests:
- text: <code>leftFactorial</code> should be a function.
testString: assert(typeof leftFactorial == 'function', '<code>leftFactorial</code> should be a function.');
- text: <code>leftFactorial(0)</code> should return a number.
testString: assert(typeof leftFactorial(0) == 'number', '<code>leftFactorial(0)</code> should return a number.');
- text: <code>leftFactorial(0)</code> should return <code>0</code>.
testString: assert.equal(leftFactorial(0), 0, '<code>leftFactorial(0)</code> should return <code>0</code>.');
- text: <code>leftFactorial(1)</code> should return <code>1</code>.
testString: assert.equal(leftFactorial(1), 1, '<code>leftFactorial(1)</code> should return <code>1</code>.');
- text: <code>leftFactorial(2)</code> should return <code>2</code>.
testString: assert.equal(leftFactorial(2), 2, '<code>leftFactorial(2)</code> should return <code>2</code>.');
- text: <code>leftFactorial(3)</code> should return <code>4</code>.
testString: assert.equal(leftFactorial(3), 4, '<code>leftFactorial(3)</code> should return <code>4</code>.');
- text: <code>leftFactorial(10)</code> should return <code>409114</code>.
testString: assert.equal(leftFactorial(10), 409114, '<code>leftFactorial(10)</code> should return <code>409114</code>.');
- text: <code>leftFactorial(17)</code> should return <code>22324392524314</code>.
testString: assert.equal(leftFactorial(17), 22324392524314, '<code>leftFactorial(17)</code> should return <code>22324392524314</code>.');
- text: <code>leftFactorial(19)</code> should return <code>6780385526348314</code>.
testString: assert.equal(leftFactorial(19), 6780385526348314, '<code>leftFactorial(19)</code> should return <code>6780385526348314</code>.');
```
</section>
## Challenge Seed
<section id='challengeSeed'>
<div id='js-seed'>
```js
function leftFactorial(n) {
// Good luck!
}
```
</div>
</section>
## Solution
<section id='solution'>
```js
function leftFactorial(n) {
if (n == 0)
return 0
if (n == 1)
return 1;
// Note: for n>=20, the result may not be correct.
// This is because Javascript uses 53 bit integers and
// for n>=20 result becomes too large.
let res = 2, fact = 2;
for (var i = 2; i < n; i++) {
res += fact;
fact *= (i + 1);
}
return res;
}
```
</section>