Skip to content

Commit 533e436

Browse files
authored
Merge pull request loiane#82 from loiane/third-edition
added tests
2 parents 89c8cc0 + eaeb188 commit 533e436

12 files changed

+157
-8
lines changed

src/ts/algorithms/math/find-divisors.ts

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ export const findDivisors = (num: number) => {
1212
}
1313
}
1414

15+
if (num >= 2 && !divisors.includes(num)) {
16+
divisors.push(num);
17+
}
18+
1519
divisors.sort((a, b) => a - b);
1620

1721
return divisors;

src/ts/algorithms/math/primality-test.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export const isPrime = (n: number) => {
44
}
55

66
const sqrt = Math.floor(Math.sqrt(n));
7-
for (let i = 2; i < sqrt; i++) {
7+
for (let i = 2; i <= sqrt; i++) {
88
if (n % i === 0) {
99
return false;
1010
}
@@ -33,4 +33,5 @@ export const testPrime = (n: number) => {
3333
return true;
3434
};
3535

36-
export const isPrime2 = (n: number) => ![...Array(n).keys()].slice(2).map(i => !(n % i)).includes(true) && ![0, 1].includes(n);
36+
// tslint:disable-next-line:max-line-length
37+
export const isPrime2 = (n: number) => (n >= 2) ? (![...Array(n).keys()].slice(2).map(i => !(n % i)).includes(true) && ![0, 1].includes(n)) : false;
+15-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
11
export const sieveOfEratosthenes = (n: number) => {
22

3-
const prime: boolean[] = [];
3+
const sieve: boolean[] = [];
4+
const primes: number[] = [];
45

5-
for (let i = 0; i < n; i++) {
6-
prime[i] = true;
6+
sieve[1] = false;
7+
8+
for (let i = 2; i <= n; i++) {
9+
sieve[i] = true;
710
}
811

912
for (let p = 2; p * p <= n; p++) {
10-
if (prime[p]) {
13+
if (sieve[p]) {
1114
for (let i = p * 2; i <= n; i += p) {
12-
prime[i] = false;
15+
sieve[i] = false;
1316
}
1417
}
1518
}
1619

17-
return prime.filter(num => num === true);
20+
sieve.forEach((value, index) => {
21+
if (value) {
22+
primes.push(index);
23+
}
24+
}, primes);
25+
26+
return primes;
1827
};

src/ts/index.ts

+9
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,15 @@ export { matrixChainOrder as matrixChainOrderGreedy } from './algorithms/greedy/
7676
export { ratInAMaze } from './algorithms/backtracking/rat-in-maze';
7777
export { sudokuSolver } from './algorithms/backtracking/sudoku-solver';
7878

79+
// others
80+
export { findDivisors } from './algorithms/math/find-divisors';
81+
export { gcd } from './algorithms/math/gcd';
82+
export { lcm } from './algorithms/math/lcm';
83+
export { greatestDifference } from './algorithms/math/greatest-difference';
84+
export { isPrime } from './algorithms/math/primality-test';
85+
export { testPrime } from './algorithms/math/primality-test';
86+
export { isPrime2 } from './algorithms/math/primality-test';
87+
export { sieveOfEratosthenes } from './algorithms/math/sieve-eratosthenes';
7988

8089

8190
/* import { hotPotato } from './others/hot-potato';

test/js/algorithms/search/search-algorithms-tests.js

+8
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ export function testSearchAlgorithm(
3333
expect(searchAlgorithm(array, SIZE)).to.equal(SIZE - 1);
3434
});
3535

36+
it('finds value at different positions', () => {
37+
const array = createSortedArray();
38+
39+
for (let value = 1; value <= SIZE; value++) {
40+
expect(searchAlgorithm(array, value)).to.equal(value - 1);
41+
}
42+
});
43+
3644
if (config.customEquals) {
3745
it('finds value with custom equals function', () => {
3846
const array = [{ key: 1 }, { key: 2 }, { key: 3 }];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import 'mocha';
2+
import { expect } from 'chai';
3+
import { findDivisors } from '../../../../src/ts/index';
4+
5+
describe('Find Divisors', () => {
6+
7+
it('returns the divisors of the number', () => {
8+
9+
expect(findDivisors(-1)).to.deep.equal([]);
10+
expect(findDivisors(0)).to.deep.equal([]);
11+
expect(findDivisors(1)).to.deep.equal([1]);
12+
expect(findDivisors(2)).to.deep.equal([1, 2]);
13+
expect(findDivisors(3)).to.deep.equal([1, 3]);
14+
expect(findDivisors(4)).to.deep.equal([1, 2, 4]);
15+
expect(findDivisors(100)).to.deep.equal([1, 2, 4, 5, 10, 20, 25, 50, 100 ]);
16+
});
17+
});

test/ts/algorithms/math/gcd.spec.ts

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import 'mocha';
2+
import { expect } from 'chai';
3+
import { gcd } from '../../../../src/ts/index';
4+
5+
describe('GCD', () => {
6+
7+
it('returns the gcd between two numbers', () => {
8+
9+
expect(gcd(1, 0)).to.equal(0);
10+
expect(gcd(1, 1)).to.equal(1);
11+
expect(gcd(2, 2)).to.equal(2);
12+
expect(gcd(2, 4)).to.equal(2);
13+
expect(gcd(2, 3)).to.equal(1);
14+
expect(gcd(10, 1000)).to.equal(10);
15+
});
16+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import 'mocha';
2+
import { expect } from 'chai';
3+
import { greatestDifference } from '../../../../src/ts/index';
4+
5+
describe('Greatest Difference', () => {
6+
7+
it('returns the gcd between two numbers', () => {
8+
9+
expect(greatestDifference([5, 6, 7, 2, 10])).to.equal(8);
10+
expect(greatestDifference([1, 2, 4])).to.equal(3);
11+
expect(greatestDifference([1, 3])).to.equal(2);
12+
13+
});
14+
});

test/ts/algorithms/math/lcm.spec.ts

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import 'mocha';
2+
import { expect } from 'chai';
3+
import { lcm } from '../../../../src/ts/index';
4+
5+
describe('LCM', () => {
6+
7+
it('returns the lcm between two numbers', () => {
8+
9+
expect(lcm(0, 0)).to.equal(0);
10+
expect(lcm(1, 1)).to.equal(1);
11+
expect(lcm(1, 2)).to.equal(2);
12+
expect(lcm(2, 4)).to.equal(4);
13+
expect(lcm(2, 3)).to.equal(6);
14+
15+
});
16+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import 'mocha';
2+
import { expect } from 'chai';
3+
import { isPrime, testPrime, isPrime2 } from '../../../../src/ts/index';
4+
5+
describe('Primality Tests', () => {
6+
7+
it('returns if the number is prime or not - isPrime', () => {
8+
testIsPrime(isPrime);
9+
});
10+
11+
it('returns if the number is prime or not - testPrime', () => {
12+
testIsPrime(testPrime);
13+
});
14+
15+
it('returns if the number is prime or not - isPrime2', () => {
16+
testIsPrime(isPrime2);
17+
});
18+
19+
function testIsPrime(primeFunction) {
20+
expect(primeFunction(-1)).to.equal(false);
21+
expect(primeFunction(0)).to.equal(false);
22+
expect(primeFunction(1)).to.equal(false);
23+
expect(primeFunction(2)).to.equal(true);
24+
expect(primeFunction(3)).to.equal(true);
25+
expect(primeFunction(4)).to.equal(false);
26+
expect(primeFunction(5)).to.equal(true);
27+
expect(primeFunction(10)).to.equal(false);
28+
expect(primeFunction(113)).to.equal(true);
29+
}
30+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import 'mocha';
2+
import { expect } from 'chai';
3+
import { sieveOfEratosthenes } from '../../../../src/ts/index';
4+
5+
describe('Sieve Of Eratosthene', () => {
6+
7+
it('returns the prime numbers', () => {
8+
9+
expect(sieveOfEratosthenes(0)).to.deep.equal([]);
10+
expect(sieveOfEratosthenes(1)).to.deep.equal([]);
11+
expect(sieveOfEratosthenes(2)).to.deep.equal([2]);
12+
expect(sieveOfEratosthenes(3)).to.deep.equal([2, 3]);
13+
expect(sieveOfEratosthenes(4)).to.deep.equal([2, 3]);
14+
expect(sieveOfEratosthenes(5)).to.deep.equal([2, 3, 5]);
15+
16+
});
17+
});

test/ts/algorithms/search/search-algorithms-tests.ts

+8
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ export function testSearchAlgorithm(
3939
expect(searchAlgorithm(array, SIZE)).to.equal(SIZE - 1);
4040
});
4141

42+
it('finds value at different positions', () => {
43+
const array = createSortedArray();
44+
45+
for (let value = 1; value <= SIZE; value++) {
46+
expect(searchAlgorithm(array, value)).to.equal(value - 1);
47+
}
48+
});
49+
4250
if (config.customEquals) {
4351
it('finds value with custom equals function', () => {
4452
const array = [{ key: 1 }, { key: 2 }, { key: 3 }];

0 commit comments

Comments
 (0)