Skip to content

Commit a180e8a

Browse files
committed
[String and Math Algorithms]
1 parent ab9a49e commit a180e8a

11 files changed

+241
-0
lines changed
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export const findDivisors = (num: number) => {
2+
const divisors = [];
3+
4+
const sqrt = Math.floor(Math.sqrt(num));
5+
6+
for (let i = 1; i <= sqrt; i++) {
7+
if (num % i === 0) {
8+
divisors.push(i);
9+
if (i !== sqrt) {
10+
divisors.push(Math.floor(num / i));
11+
}
12+
}
13+
}
14+
15+
divisors.sort((a, b) => a - b);
16+
17+
return divisors;
18+
};

src/ts/algorithms/math/gcd.ts

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
export const gcd = (num1: number, num2: number): number => {
2+
if (num1 === 0 || num2 === 0) {
3+
return 0;
4+
}
5+
if (num1 === num2) {
6+
return num1;
7+
}
8+
if (num1 > num2) {
9+
return gcd(num1 - num2, num2);
10+
}
11+
return gcd(num1, num2 - num1);
12+
};
13+
14+
export const gcdArray = (num: number[]) => {
15+
let result = num[0];
16+
17+
for (let i = 1; i < num.length; i++) {
18+
result = gcd(num[i], result);
19+
}
20+
21+
return result;
22+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export const greatestDifference = (numbers: number[]) => {
2+
let index = 0;
3+
let largest = numbers[0];
4+
const length = numbers.length;
5+
let number;
6+
let smallest = numbers[0];
7+
8+
for (index; index < length; index++) {
9+
number = numbers[index];
10+
11+
if (number > largest) {
12+
largest = number;
13+
}
14+
if (number < smallest) {
15+
smallest = number;
16+
}
17+
}
18+
19+
return largest - smallest;
20+
};

src/ts/algorithms/math/lcm.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { gcd } from './gcd';
2+
3+
export const lcm = (num1: number, num2: number) => {
4+
if (num1 === 0 || num2 === 0) {
5+
return 0;
6+
}
7+
num1 = Math.abs(num1);
8+
num2 = Math.abs(num2);
9+
return (num1 * num2) / gcd(num1, num2);
10+
};
11+
12+
export const lcmArray = (num: number[]) => {
13+
let result = num[0];
14+
15+
for (let i = 1; i < num.length; i++) {
16+
result = num[i] * result / gcd(num[i], result);
17+
}
18+
19+
return result;
20+
};
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
export const isPrime = (n: number) => {
2+
if (n <= 1) {
3+
return false;
4+
}
5+
6+
const sqrt = Math.floor(Math.sqrt(n));
7+
for (let i = 2; i < sqrt; i++) {
8+
if (n % i === 0) {
9+
return false;
10+
}
11+
}
12+
13+
return true;
14+
};
15+
16+
export const testPrime = (n: number) => {
17+
if (n <= 1) {
18+
return false;
19+
} else {
20+
if (n === 2 || n === 3) {
21+
return true;
22+
} else if (n % 2 === 0) {
23+
return false;
24+
} else {
25+
const sqrt = Math.floor(Math.sqrt(n));
26+
for (let i = 3; i <= sqrt; i += 2) {
27+
if (n % i === 0) {
28+
return false;
29+
}
30+
}
31+
}
32+
}
33+
return true;
34+
};
35+
36+
const isPrime2 = (n: number) => ![...Array(n).keys()].slice(2).map(i => !(n % i)).includes(true) && ![0, 1].includes(n);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export const sieveOfEratosthenes = (n: number) => {
2+
3+
const prime: boolean[] = [];
4+
5+
for (let i = 0; i < n; i++) {
6+
prime[i] = true;
7+
}
8+
9+
for (let p = 2; p * p <= n; p++) {
10+
if (prime[p]) {
11+
for (let i = p * 2; i <= n; i += p) {
12+
prime[i] = false;
13+
}
14+
}
15+
}
16+
17+
return prime.filter(num => num === true);
18+
};
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const stringSearch = (text: string, pattern: string) => {
2+
const n = text.length;
3+
const m = pattern.length;
4+
5+
if (m > n) {
6+
return -1;
7+
}
8+
9+
for (let i = 0; i < n; i++) {
10+
let j = 0;
11+
for (j = 0; j < m && (i + j) < n; j++) {
12+
if (text.charAt(i + j) !== pattern.charAt(j)) {
13+
break;
14+
}
15+
}
16+
if (j === m) {
17+
return i;
18+
}
19+
}
20+
21+
return -1;
22+
};

src/ts/algorithms/string/huffman.ts

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
const buildTable = (pattern: string) => {
2+
const length = pattern.length;
3+
const table = [];
4+
let position = 2;
5+
let cnd = 0;
6+
7+
table[0] = -1;
8+
table[1] = 0;
9+
10+
while (position < length) {
11+
if (pattern[position - 1] === pattern[cnd]) {
12+
table[position++] = ++cnd;
13+
} else if (cnd > 0) {
14+
cnd = table[cnd];
15+
} else {
16+
table[position++] = 0;
17+
}
18+
}
19+
20+
return table;
21+
};
22+
23+
const knuthMorrisPratt = (text: string, pattern: string) => {
24+
const textLength = text.length;
25+
const patternLength = pattern.length;
26+
let m = 0;
27+
let i = 0;
28+
const table = buildTable(pattern);
29+
30+
while (m + i < textLength) {
31+
if (pattern[i] === text[m + i]) {
32+
if (i === patternLength - 1) {
33+
return m;
34+
}
35+
i++;
36+
} else if (table[i] >= 0) {
37+
i = table[i];
38+
m = m + i - table[i];
39+
} else {
40+
i = 0;
41+
m++;
42+
}
43+
}
44+
45+
return textLength;
46+
};

src/ts/algorithms/string/longest-common-substring.ts

Whitespace-only changes.
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const base = 997;
2+
3+
const hash = (word: string) => {
4+
let h = 0;
5+
6+
for (let i = 0; i < word.length; i++) {
7+
h += word.charCodeAt(i) * Math.pow(base, word.length - i - 1);
8+
}
9+
10+
return h;
11+
};
12+
13+
const rabinKarp = (text: string, pattern: string) => {
14+
if (pattern == null || pattern.length === 0) {
15+
return 0;
16+
}
17+
18+
const hashPattern = hash(pattern);
19+
let currentSubstring = text.substring(0, pattern.length);
20+
let hashCurrentSubstring;
21+
22+
for (let i = pattern.length; i <= text.length; i++) {
23+
if (hashCurrentSubstring === undefined) {
24+
hashCurrentSubstring = hash(currentSubstring);
25+
} else {
26+
hashCurrentSubstring -= currentSubstring.charCodeAt(0) * Math.pow(base, pattern.length - 1);
27+
hashCurrentSubstring *= base;
28+
hashCurrentSubstring += text.charCodeAt(i);
29+
30+
currentSubstring = currentSubstring.substring(1) + text[i];
31+
}
32+
33+
if (hashPattern === hashCurrentSubstring && pattern === currentSubstring) {
34+
return i === pattern.length ? 0 : i - pattern.length + 1;
35+
}
36+
}
37+
38+
return -1;
39+
};

0 commit comments

Comments
 (0)