Validate string for propper brackets amount and order.
Empty string should return true
.
const validate = str => {
let acc = 0;
for (let i = 0; i < str.length; i += 1) {
acc = str[i] === '(' ? acc + 1 : acc - 1;
if (acc < 0) return false;
}
return acc === 0;
};
const validate = str => {
let acc = 0;
for (let i = 0; i < str.length; i += 1) {
acc = str[i] === '(' ? acc + 1 : acc - 1;
if (acc < 0) return false;
}
return acc === 0;
};
describe("Test cases", function() {
it("()", function() {
Test.assertSimilar(validate("()"), true);
});
it("(())", function() {
Test.assertSimilar(validate("(())"), true);
});
it("(()((((())))))", function() {
Test.assertSimilar(validate("(()((((())))))"), true);
});
it("(())(())", function() {
Test.assertSimilar(validate("(())(())"), true);
});
it("", function() {
Test.assertSimilar(validate(""), true);
});
it("((", function() {
Test.assertSimilar(validate("(("), false);
});
it("())(", function() {
Test.assertSimilar(validate("())("), false);
});
it("((())", function() {
Test.assertSimilar(validate("((())"), false);
});
it("(())())", function() {
Test.assertSimilar(validate("(())())"), false);
});
it("(()(()))))", function() {
Test.assertSimilar(validate("(()(()))))"), false);
});
it(")", function() {
Test.assertSimilar(validate(")"), false);
});
it("())(()", function() {
Test.assertSimilar(validate("())(()"), false);
});
});
Algorithms
Logic
const getDividors = n => { let result = []; for (let i = 1; i <= Math.floor(Math.sqrt(n)); i += 1) if (n % i === 0) { result.push(i); if (n / i !== i) result.push(n / i) } return result; // output array won't be sorted }
const getDividors = (n, result = []) => {- const getDividors = n => {
- let result = [];
for (let i = 1; i <= Math.floor(Math.sqrt(n)); i++)- for (let i = 1; i <= Math.floor(Math.sqrt(n)); i += 1)
- if (n % i === 0) { result.push(i); if (n / i !== i) result.push(n / i) }
- return result; // output array won't be sorted
- }
const getDividors = n => { let result = []; for (let i = 1; i <= Math.floor(Math.sqrt(n)); i += 1) if (n % i === 0) { result.push(i); if (n / i !== i) result.push(n / i) } return result.sort((a,b) => a - b); } describe("Test cases", function() { it("n = 200", function() { Test.assertSimilar(getDividors(200), [1, 2, 4, 5, 8, 10, 20, 25, 40, 50, 100, 200]); }); it("n = 560", function() { Test.assertSimilar(getDividors(560), [ 1, 2, 4, 5, 7, 8, 10, 14, 16, 20, 28, 35, 40, 56, 70, 80, 112, 140, 280, 560 ]); }); it("n = 755", function() { Test.assertSimilar(getDividors(755), [ 1, 5, 151, 755 ]); }); });
const getDividors = (n, result = []) => {- const getDividors = n => {
- let result = [];
for (let i = 1; i <= Math.floor(Math.sqrt(n)); i++)- for (let i = 1; i <= Math.floor(Math.sqrt(n)); i += 1)
- if (n % i === 0) { result.push(i); if (n / i !== i) result.push(n / i) }
- return result.sort((a,b) => a - b);
- }
- describe("Test cases", function() {
- it("n = 200", function() {
- Test.assertSimilar(getDividors(200), [1, 2, 4, 5, 8, 10, 20, 25, 40, 50, 100, 200]);
- });
- it("n = 560", function() {
- Test.assertSimilar(getDividors(560), [ 1, 2, 4, 5, 7, 8, 10, 14, 16, 20, 28, 35, 40, 56, 70, 80, 112, 140, 280, 560 ]);
- });
- it("n = 755", function() {
- Test.assertSimilar(getDividors(755), [ 1, 5, 151, 755 ]);
- });
- });
Algorithms
Logic
const flatten = arr => arr.reduce((acc, item) => acc.concat(Array.isArray(item) ? flatten(item) : item), []);
- const flatten = arr =>
arr.reduce((acc, item) => acc.concat(Array.isArray(item) ? flatten(item) : [item]), []);- arr.reduce((acc, item) => acc.concat(Array.isArray(item) ? flatten(item) : item), []);
const flatten = arr => arr.reduce((acc, item) => acc.concat(Array.isArray(item) ? flatten(item) : item), []); describe("Test cases", function() { it("[1, 2, [3, 5], [[4, 3], 2]]", function() { Test.assertSimilar(flatten([1, 2, [3, 5], [[4, 3], 2]]), [1, 2, 3, 5, 4, 3, 2]); }); it("[[1, [5], [], [[-3, 'hi']]], 'string', 10, [[[5]]]]", function() { Test.assertSimilar(flatten([[1, [5], [], [[-3, 'hi']]], 'string', 10, [[[5]]]]), [1, 5, -3, 'hi', 'string', 10, 5]); }); });
- const flatten = arr =>
arr.reduce((acc, item) => Array.isArray(item) ? [...acc, ...flatten(item)] : [...acc, item], []);- arr.reduce((acc, item) => acc.concat(Array.isArray(item) ? flatten(item) : item), []);
- describe("Test cases", function() {
- it("[1, 2, [3, 5], [[4, 3], 2]]", function() {
- Test.assertSimilar(flatten([1, 2, [3, 5], [[4, 3], 2]]), [1, 2, 3, 5, 4, 3, 2]);
- });
- it("[[1, [5], [], [[-3, 'hi']]], 'string', 10, [[[5]]]]", function() {
- Test.assertSimilar(flatten([[1, [5], [], [[-3, 'hi']]], 'string', 10, [[[5]]]]), [1, 5, -3, 'hi', 'string', 10, 5]);
- });
- });
Algorithms
Logic
const flatten = arr =>
arr.reduce((acc, item) => Array.isArray(item) ? [...acc, ...flatten(item)] : [...acc, item], []);
const flatten = arr =>
arr.reduce((acc, item) => Array.isArray(item) ? [...acc, ...flatten(item)] : [...acc, item], []);
describe("Test cases", function() {
it("[1, 2, [3, 5], [[4, 3], 2]]", function() {
Test.assertSimilar(flatten([1, 2, [3, 5], [[4, 3], 2]]), [1, 2, 3, 5, 4, 3, 2]);
});
it("[[1, [5], [], [[-3, 'hi']]], 'string', 10, [[[5]]]]", function() {
Test.assertSimilar(flatten([[1, [5], [], [[-3, 'hi']]], 'string', 10, [[[5]]]]), [1, 5, -3, 'hi', 'string', 10, 5]);
});
});
Algorithms
Logic
The fastest way to find all dividors of a number.
const getDividors = (n, result = []) => {
for (let i = 1; i <= Math.floor(Math.sqrt(n)); i++)
if (n % i === 0) { result.push(i); if (n / i !== i) result.push(n / i) }
return result; // output array won't be sorted
}
const getDividors = (n, result = []) => {
for (let i = 1; i <= Math.floor(Math.sqrt(n)); i++)
if (n % i === 0) { result.push(i); if (n / i !== i) result.push(n / i) }
return result.sort((a,b) => a - b);
}
describe("Test cases", function() {
it("n = 200", function() {
Test.assertSimilar(getDividors(200), [1, 2, 4, 5, 8, 10, 20, 25, 40, 50, 100, 200]);
});
it("n = 560", function() {
Test.assertSimilar(getDividors(560), [ 1, 2, 4, 5, 7, 8, 10, 14, 16, 20, 28, 35, 40, 56, 70, 80, 112, 140, 280, 560 ]);
});
it("n = 755", function() {
Test.assertSimilar(getDividors(755), [ 1, 5, 151, 755 ]);
});
});