Skip to content

Use mocha for tests #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions JavaScript/chapter01/1.1 - Is Unique/solution.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@ Input: takes an array of integers
Output: returns a deduped array of integers
*/

// Solution using Set
// Solution using Set
const isUnique = (arr) => [...new Set(arr)];

// Test Cases
console.log(isUnique([1,1,1,2,2,2,2,3,3,3,3]) === [1,2,3]);
const assert = require('assert');

describe(module.filename, () => {
it('should deduplicate array', () => {
assert.deepEqual(isUnique([1,1,1,2,2,2,2,3,3,3,3]), [1,2,3]);
});
});
23 changes: 16 additions & 7 deletions JavaScript/chapter01/1.2 - Check Perm/rroque98_sol.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,19 @@ const isPermutation = (str1, str2) => {
};

// Tests:
console.log(isPermutation('abc', 'abb') === false);
console.log(isPermutation('abb', 'abc') === false);
console.log(isPermutation('aaa', 'abc') === false);
console.log(isPermutation('abc', 'abcd') === false);
console.log(isPermutation('abc', 'bac') === true);
console.log(isPermutation('', '') === true);
console.log(isPermutation('12', '21') === true);
const assert = require('assert');

describe(module.filename, () => {
it('should handle positive cases', () => {
assert.equal(isPermutation('abc', 'bac'), true);
assert.equal(isPermutation('', ''), true);
assert.equal(isPermutation('12', '21'), true);
});

it('should handle negative cases', () => {
assert.equal(isPermutation('abc', 'abb'), false);
assert.equal(isPermutation('abb', 'abc'), false);
assert.equal(isPermutation('aaa', 'abc'), false);
assert.equal(isPermutation('abc', 'abcd'), false);
});
});
12 changes: 9 additions & 3 deletions JavaScript/chapter01/1.3 - URLify/rroque98_sol.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
const encodeSpaces = (string) => string.replace(/ /g, '%20');

// Tests:
console.log(encodeSpaces('Hello World') === 'Hello%20World');
console.log(encodeSpaces('') === '');
console.log(encodeSpaces('This is an example') === 'This%20is%20an%20example');
const assert = require('assert');

describe(module.filename, () => {
it('should replace spaces with %20', () => {
assert.equal(encodeSpaces('Hello World'), 'Hello%20World');
assert.equal(encodeSpaces(''), '');
assert.equal(encodeSpaces('This is an example'), 'This%20is%20an%20example');
});
});
23 changes: 12 additions & 11 deletions JavaScript/chapter01/1.3 - URLify/solution.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,32 @@
function URLify(arr, len) {
let spaces = 0;
for(let i = 0; i < len; i++) {
if(arr[i] === ' ') spaces++;
}
if(arr[i] === ' ') spaces++;
}
//last index
let index = len + spaces * 2 - 1;

for(let i = len - 1; i >= 0; i--) {
if(arr[i] === ' ') {
arr[index] = '0';
arr[index] = '0';
arr[index - 1] = '2';
arr[index - 2] = '%';
index -= 3
} else {
arr[index] = arr[i];
index--;
}
}
}
return arr;
}

//testing
let arr = ['M', 'r', ' ', 'J', 'o', 'h', 'n', ' ', 'S', 'm', 'i', 't', 'h', ' ', ' ', ' ', ' '];

//before
console.log(arr);
let ans = URLify(arr, 13);
const assert = require('assert');

//after
console.log(ans);
describe(module.filename, () => {
it('should correctly URLify', () => {
let arr = ['M', 'r', ' ', 'J', 'o', 'h', 'n', ' ', 'S', 'm', 'i', 't', 'h', ' ', ' ', ' ', ' '];
let expected = ['M', 'r', '%', '2', '0', 'J', 'o', 'h', 'n', '%', '2', '0', 'S', 'm', 'i', 't', 'h'];
assert.deepEqual(URLify(arr, 13), expected);
});
});
17 changes: 13 additions & 4 deletions JavaScript/chapter01/1.4 - PalinPerm/rroque98_sol.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,16 @@ const isPalindromePermutation = (str) => {
return true;
}

console.log(isPalindromePermutation('tact coa') === true);
console.log(isPalindromePermutation('tact cooa') === true);
console.log(isPalindromePermutation('tacr coa') === false);
console.log(isPalindromePermutation('tactr coa') === false);
const assert = require('assert');

describe(module.filename, () => {
it('should handle positive cases', () => {
assert.equal(isPalindromePermutation('tact coa'), true);
assert.equal(isPalindromePermutation('tact cooa'), true);
});

it('should handle negative cases', () => {
assert.equal(isPalindromePermutation('tacr coa'), false);
assert.equal(isPalindromePermutation('tactr coa'), false);
});
});
64 changes: 35 additions & 29 deletions JavaScript/chapter01/1.5 - OneAway/rroque98_sol.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,38 +35,44 @@ const isOneAway = (str1, str2) => {
return true;
};

// ****** TESTS ******
const assert = require('assert');

function runTests(cases, expected) {
for (const [str1, str2] of cases) {
console.log(
isOneAway(str1, str2) === expected && isOneAway(str2, str1) === expected
);
assert.equal(isOneAway(str1, str2), expected);
assert.equal(isOneAway(str2, str1), expected);
}
}

runTests(
[
['pale', 'ple'], // deletion
['pale', 'opale'], // insertion in beginning
['pale', 'palse'], // insertion in middle
['pale', 'pales'], // insertion at end
['pale', 'bale'], // replacement
['p', 'b'],
['p', 'p'],
['p', ''],
['', '']
],
true
);
describe(module.filename, () => {
it('should handle positive cases', () => {
runTests(
[
['pale', 'ple'], // deletion
['pale', 'opale'], // insertion in beginning
['pale', 'palse'], // insertion in middle
['pale', 'pales'], // insertion at end
['pale', 'bale'], // replacement
['p', 'b'],
['p', 'p'],
['p', ''],
['', '']
],
true
);
});

runTests(
[
['pale', 'ae'], // greater than 1 deletions
['pale', 'ppalpe'], // greater than 1 insertions
['pale', 'bake'], // greater than 1 replacements
['pale', 'balpe'], // 1 insertion, 1 replacement
['pale', 'plo'], // 1 deletion, 1 replacement
['pale', 'ales'] // 1 deletion, 1 insertion
],
false
);
it('should handle negative cases', () => {
runTests(
[
['pale', 'ae'], // greater than 1 deletions
['pale', 'ppalpe'], // greater than 1 insertions
['pale', 'bake'], // greater than 1 replacements
['pale', 'balpe'], // 1 insertion, 1 replacement
['pale', 'plo'], // 1 deletion, 1 replacement
['pale', 'ales'] // 1 deletion, 1 insertion
],
false
);
});
});
22 changes: 15 additions & 7 deletions JavaScript/chapter01/1.6 - String Compression/rroque98_sol.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,18 @@ const stringCompression = (str) => {
return compStr;
}

// TESTS
console.log(stringCompression('aabcccccaaa') === 'a2b1c5a3');
console.log(stringCompression('cccccccc') === 'c8');
console.log(stringCompression('') === '');
console.log(stringCompression('AabccCccaaa') === 'AabccCccaaa');
// Explanation: 'A1a1b1c2C1c2a3' length is longer than original string so returns original string
console.log(stringCompression('x') === 'x');
const assert = require('assert');

describe(module.filename, () => {
it('should correctly compress longer strings', () => {
assert.equal(stringCompression('aabcccccaaa'), 'a2b1c5a3');
assert.equal(stringCompression('cccccccc'), 'c8');
});

it('should leave uncompressible strings unmodified', () => {
assert.equal(stringCompression(''), '');
// Explanation: 'A1a1b1c2C1c2a3' length is longer than original string so returns original string
assert.equal(stringCompression('AabccCccaaa'), 'AabccCccaaa');
assert.equal(stringCompression('x'), 'x');
});
});
20 changes: 13 additions & 7 deletions JavaScript/chapter01/1.7 - Rotate Matrix/rroque98_sol.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,16 @@ const rotateImage = nestedArr => {
};

// TESTS:
console.log(
JSON.stringify(rotateImage([[1, 2, 3], [4, 5, 6], [7, 8, 9]])) ===
JSON.stringify([[7, 4, 1], [8, 5, 2], [9, 6, 3]])
);
console.log(JSON.stringify(rotateImage([[1]])) === JSON.stringify([[1]]));
console.log(JSON.stringify(rotateImage([[]])) === JSON.stringify([[]]));
console.log(JSON.stringify(rotateImage([])) === JSON.stringify([]));
const assert = require('assert');

describe(module.filename, () => {
it('should rotate matrices', () => {
assert.deepEqual(
rotateImage([[1, 2, 3], [4, 5, 6], [7, 8, 9]]),
[[7, 4, 1], [8, 5, 2], [9, 6, 3]]
);
assert.deepEqual(rotateImage([[1]]), [[1]]);
assert.deepEqual(rotateImage([[]]), [[]]);
assert.deepEqual(rotateImage([]), []);
});
});
61 changes: 28 additions & 33 deletions JavaScript/chapter01/1.8 - Zero Matrix/rroque98_sol.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,43 +34,38 @@ function checkForZeroIndex(nestArr) {
}

// **** TESTS ****:
let actual = zeroMatrix([[]]);
let expected = [[]];
isEqual(actual, expected);
const assert = require('assert');

actual = zeroMatrix([[3, 5, 6], [1, 0, 2], [4, 4, 5], [2, 2, 2]]);
expected = [[3, 0, 6], [0, 0, 0], [4, 0, 5], [2, 0, 2]];
isEqual(actual, expected);
describe(module.filename, () => {
it('should correctly zero out matrices', () => {
let actual = zeroMatrix([[]]);
let expected = [[]];
assert.deepEqual(actual, expected);

actual = zeroMatrix([[3, 5, 6], [1, 0, 2], [4, 4, 0], [2, 0, 2]]);
expected = [[3, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]];
isEqual(actual, expected);
actual = zeroMatrix([[3, 5, 6], [1, 0, 2], [4, 4, 5], [2, 2, 2]]);
expected = [[3, 0, 6], [0, 0, 0], [4, 0, 5], [2, 0, 2]];
assert.deepEqual(actual, expected);

actual = zeroMatrix([[3, 5, 6], [0, 0, 2], [4, 4, 0], [2, 0, 2]]);
expected = [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]];
isEqual(actual, expected);
actual = zeroMatrix([[3, 5, 6], [1, 0, 2], [4, 4, 0], [2, 0, 2]]);
expected = [[3, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]];
assert.deepEqual(actual, expected);

function isEqual(actual, expected) {
console.log(JSON.stringify(actual) === JSON.stringify(expected));
}

// ****HELPER FUNCTION TESTS ****:
actual = checkForZeroIndex([[1, 2, 0]]);
expected = { rows: { 0: true }, columns: { 2: true } };
testCheckForZeroIndex(actual, expected);
actual = zeroMatrix([[3, 5, 6], [0, 0, 2], [4, 4, 0], [2, 0, 2]]);
expected = [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]];
assert.deepEqual(actual, expected);
});

actual = checkForZeroIndex([[0]]);
expected = { rows: { 0: true }, columns: { 0: true } };
testCheckForZeroIndex(actual, expected);
it('should correctly find zero indices', () => {
actual = checkForZeroIndex([[1, 2, 0]]);
expected = { rows: { 0: true }, columns: { 2: true } };
assert.deepEqual(actual, expected);

actual = checkForZeroIndex([[1, 2, 3], [4, 0, 5], [6, 0, 8]]);
expected = { rows: { 1: true, 2: true }, columns: { 1: true } };
testCheckForZeroIndex(actual, expected);
actual = checkForZeroIndex([[0]]);
expected = { rows: { 0: true }, columns: { 0: true } };
assert.deepEqual(actual, expected);

function testCheckForZeroIndex(actual, expected) {
let rows = JSON.stringify(actual.rows);
let cols = JSON.stringify(actual.columns);
let expRows = JSON.stringify(expected.rows);
let expCols = JSON.stringify(expected.columns);
console.log(rows === expRows && cols === expCols);
}
actual = checkForZeroIndex([[1, 2, 3], [4, 0, 5], [6, 0, 8]]);
expected = { rows: { 1: true, 2: true }, columns: { 1: true } };
assert.deepEqual(actual, expected);
});
});
12 changes: 9 additions & 3 deletions JavaScript/chapter01/1.9 - String Rotation/solution.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@ call to isSubstring (e.g., "waterbottle" is a rotation of"erbottlewat") */

var StringRotate = function(string1, string2) {
if (string1.length !== string2.length ){
return false;
return false;
}
return ( string2 + string1 ).includes(string1); // one call of Substring
};

//Test
console.log(StringRotate('waterbottle', 'erbottlewat'), true);
//Test
const assert = require('assert');

describe(module.filename, () => {
it('should detect rotated substrings', () => {
assert.equal(StringRotate('waterbottle', 'erbottlewat'), true);
});
});
Loading