Skip to content

Commit 01f9e4e

Browse files
authored
Merge branch 'TheAlgorithms:master' into add-hexagonal-nums
2 parents 23ec55c + 0b8f3d3 commit 01f9e4e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+543
-30
lines changed

.github/ISSUE_TEMPLATE/bug_report.yml

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
name: Bug report
2+
description: "Create a report to help us improve"
3+
title: "[BUG]: "
4+
labels: ["bug"]
5+
body:
6+
- type: markdown
7+
attributes:
8+
value: "### Before you open an issue, please verify if a similar one already exists or has been closed before. More details about the process of contributing can be found in [CONTRIBUTING.md](https://github.com/TheAlgorithms/TypeScript/blob/master/CONTRIBUTING.md)."
9+
- type: textarea
10+
id: description
11+
attributes:
12+
label: Description
13+
description: Explain what the problem is.
14+
validations:
15+
required: true
16+
- type: textarea
17+
id: expectedbhv
18+
attributes:
19+
label: Expected Behavior
20+
description: Describe what was the expected behavior.
21+
validations:
22+
required: true
23+
- type: textarea
24+
id: actualbhv
25+
attributes:
26+
label: Actual Behavior
27+
description: Describe what actually happens.
28+
validations:
29+
required: true
30+
- type: textarea
31+
id: steps
32+
attributes:
33+
label: Steps to reproduce (if applicable)
34+
description: List steps to reproduce the behavior.
35+
placeholder: |
36+
1.
37+
2.
38+
3.
39+
4.
40+
validations:
41+
required: false
42+
- type: textarea
43+
id: specifications
44+
attributes:
45+
label: Specifications
46+
description: Specify version, platform, and web browser (if applicable).
47+
placeholder: |
48+
1. Version:
49+
2. Platform:
50+
3. Browser:
51+
validations:
52+
required: false
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: Feature request
2+
description: "Suggest features, propose improvements, discuss new ideas"
3+
title: "[FEATURE]: "
4+
labels: ["enhancement"]
5+
body:
6+
- type: markdown
7+
attributes:
8+
value: |
9+
## This issue template is not for requesting new algorithms. For new algorithms, PRs should be opened directly.
10+
## Make sure your issue isn't a duplicate and you follow our [contributing guidelines](https://github.com/TheAlgorithms/TypeScript/blob/master/CONTRIBUTING.md)
11+
- type: textarea
12+
id: description
13+
attributes:
14+
label: Motivation
15+
description: Describe what is the motivation behind this feature.
16+
validations:
17+
required: true
18+
- type: textarea
19+
id: examples
20+
attributes:
21+
label: Examples
22+
description: If possible, provide examples of how this feature can be used.
23+
validations:
24+
required: false
25+
- type: textarea
26+
id: workarounds
27+
attributes:
28+
label: Possible workarounds
29+
description: If possible, describes possible workarounds to this feature.
30+
validations:
31+
required: false

.github/workflows/ci.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ jobs:
1010
test:
1111
runs-on: ubuntu-22.04
1212
steps:
13-
- uses: actions/checkout@v2
14-
- uses: actions/setup-node@v2
13+
- uses: actions/checkout@v3
14+
- uses: actions/setup-node@v3
1515
with:
16-
node-version: "16.x"
16+
node-version: "18.x"
1717
- run: npm ci
1818
- run: npm test
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: Directory/Filename Formatter workflow
2+
on: [push, pull_request]
3+
4+
jobs:
5+
main:
6+
name: (Directory) Formatter
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@main
10+
- name: Setup Git configuration
11+
run: |
12+
git config --global user.name 'autoprettier'
13+
git config --global user.email '[email protected]'
14+
git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/$GITHUB_REPOSITORY
15+
- name: Filename Formatter
16+
run: |
17+
IFS=$'\n'
18+
for fname in `find . -type f -name '*.ts' -o -name '*.ts'`
19+
do
20+
echo "${fname}"
21+
new_fname=`echo ${fname} | tr ' ' '_'`
22+
echo " ${new_fname}"
23+
new_fname=`echo ${new_fname} | tr 'A-Z' 'a-z'`
24+
echo " ${new_fname}"
25+
new_fname=`echo ${new_fname} | tr '-' '_'`
26+
echo " ${new_fname}"
27+
if [ ${fname} != ${new_fname} ]
28+
then
29+
echo " ${fname} --> ${new_fname}"
30+
git "mv" "${fname}" ${new_fname}
31+
fi
32+
done
33+
git commit -am "Formatting filenames ${GITHUB_SHA::8}" || true
34+
- name: Update DIRECTORY.md
35+
run: |
36+
wget https://raw.githubusercontent.com/TheAlgorithms/scripts/main/build_directory_md.py
37+
python3 build_directory_md.py TypeScript . .ts jest.config.ts,sorts/test,search/test,maths/test,dynamic_programming/test,data_structures/test,ciphers/test > DIRECTORY.md
38+
39+
git diff
40+
git commit -m "Update DIRECTORY.md" DIRECTORY.md || true
41+
git push --force origin HEAD:$GITHUB_REF || true

CONTRIBUTING.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ should add unique value.
5656

5757
#### File Naming Convention
5858

59-
- Filenames should use the UpperCamelCase (PascalCase) style.
59+
- Filenames should use the snake_case style. This is needed for our workflows to work correctly.
6060
- There should be no spaces in filenames.
61-
- **Example:** `UserProfile.ts` is allowed. Do not use `userprofile.ts`, `Userprofile.ts`, `user-Profile.ts`, `userProfile.ts`; these naming conventions are discouraged.
61+
- **Example:** `user_profile.ts` is allowed. Do not use `userprofile.ts`, `Userprofile.ts`, `user-Profile.ts`, `userProfile.ts`, `UserProfile.ts`; these naming conventions are discouraged.
6262

6363
#### Commit Messages Formatting
6464

DIRECTORY.md

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
## Ciphers
3+
* [Xor Cipher](https://github.com/TheAlgorithms/TypeScript/blob/master/ciphers/xor_cipher.ts)
4+
5+
## Data Structures
6+
* [Stack](https://github.com/TheAlgorithms/TypeScript/blob/master/data_structures/stack.ts)
7+
8+
## Dynamic Programming
9+
* [Knapsack](https://github.com/TheAlgorithms/TypeScript/blob/master/dynamic_programming/knapsack.ts)
10+
11+
## Maths
12+
* [Absolute Value](https://github.com/TheAlgorithms/TypeScript/blob/master/maths/absolute_value.ts)
13+
* [Aliquot Sum](https://github.com/TheAlgorithms/TypeScript/blob/master/maths/aliquot_sum.ts)
14+
* [Armstrong Number](https://github.com/TheAlgorithms/TypeScript/blob/master/maths/armstrong_number.ts)
15+
* [Binary Convert](https://github.com/TheAlgorithms/TypeScript/blob/master/maths/binary_convert.ts)
16+
* [Calculate Mean](https://github.com/TheAlgorithms/TypeScript/blob/master/maths/calculate_mean.ts)
17+
* [Degrees To Radians](https://github.com/TheAlgorithms/TypeScript/blob/master/maths/degrees_to_radians.ts)
18+
* [Digit Sum](https://github.com/TheAlgorithms/TypeScript/blob/master/maths/digit_sum.ts)
19+
* [Factorial](https://github.com/TheAlgorithms/TypeScript/blob/master/maths/factorial.ts)
20+
* [Fibonacci](https://github.com/TheAlgorithms/TypeScript/blob/master/maths/fibonacci.ts)
21+
* [Find Min](https://github.com/TheAlgorithms/TypeScript/blob/master/maths/find_min.ts)
22+
* [Greatest Common Factor](https://github.com/TheAlgorithms/TypeScript/blob/master/maths/greatest_common_factor.ts)
23+
* [Is Divisible](https://github.com/TheAlgorithms/TypeScript/blob/master/maths/is_divisible.ts)
24+
* [Is Even](https://github.com/TheAlgorithms/TypeScript/blob/master/maths/is_even.ts)
25+
* [Is Leap Year](https://github.com/TheAlgorithms/TypeScript/blob/master/maths/is_leap_year.ts)
26+
* [Is Odd](https://github.com/TheAlgorithms/TypeScript/blob/master/maths/is_odd.ts)
27+
* [Lowest Common Multiple](https://github.com/TheAlgorithms/TypeScript/blob/master/maths/lowest_common_multiple.ts)
28+
* [Perfect Square](https://github.com/TheAlgorithms/TypeScript/blob/master/maths/perfect_square.ts)
29+
* [Radians To Degrees](https://github.com/TheAlgorithms/TypeScript/blob/master/maths/radians_to_degrees.ts)
30+
* [Sieve Of Eratosthenes](https://github.com/TheAlgorithms/TypeScript/blob/master/maths/sieve_of_eratosthenes.ts)
31+
32+
## Search
33+
* [Binary Search](https://github.com/TheAlgorithms/TypeScript/blob/master/search/binary_search.ts)
34+
* [Linear Search](https://github.com/TheAlgorithms/TypeScript/blob/master/search/linear_search.ts)
35+
36+
## Sorts
37+
* [Bubble Sort](https://github.com/TheAlgorithms/TypeScript/blob/master/sorts/bubble_sort.ts)
38+
* [Gnome Sort](https://github.com/TheAlgorithms/TypeScript/blob/master/sorts/gnome_sort.ts)
39+
* [Insertion Sort](https://github.com/TheAlgorithms/TypeScript/blob/master/sorts/insertion_sort.ts)
40+
* [Merge Sort](https://github.com/TheAlgorithms/TypeScript/blob/master/sorts/merge_sort.ts)
41+
* [Quick Sort](https://github.com/TheAlgorithms/TypeScript/blob/master/sorts/quick_sort.ts)

Ciphers/test/XORCipher.test.ts renamed to ciphers/test/xor_cipher.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { XORCipher } from '../XORCipher';
1+
import { XORCipher } from '../xor_cipher';
22

33
describe('Testing XORCipher function', () => {
44
it('passing a string & number as an argument', () => {
File renamed without changes.
File renamed without changes.

Data-Structures/test/Stack.test.ts renamed to data_structures/test/stack.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Stack } from '../Stack';
1+
import { Stack } from '../stack';
22

33
describe('Testing Stack data structure', () => {
44
it('push should add a new element to the stack', () => {

dynamic_programming/knapsack.ts

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* @function knapsack
3+
* @description Given weights and values of n (numberOfItems) items, put these items in a knapsack of capacity to get the maximum total value in the knapsack. In other words, given two integer arrays values[0..n-1] and weights[0..n-1] which represent values and weights associated with n items respectively. Also given an integer capacity which represents knapsack capacity, find out the maximum value subset of values[] such that sum of the weights of this subset is smaller than or equal to capacity. You cannot break an item, either pick the complete item or don’t pick it (0-1 property).
4+
* @Complexity_Analysis
5+
* Space complexity - O(1)
6+
* Time complexity (independent of input) : O(numberOfItems * capacity)
7+
*
8+
* @return maximum value subset of values[] such that sum of the weights of this subset is smaller than or equal to capacity.
9+
* @see [Knapsack](https://www.geeksforgeeks.org/0-1-knapsack-problem-dp-10/)
10+
* @example knapsack(3, 8, [3, 4, 5], [30, 50, 60]) = 90
11+
*/
12+
export const knapsack = (
13+
capacity: number,
14+
weights: number[],
15+
values: number[]
16+
) => {
17+
if (weights.length != values.length) {
18+
throw new Error(
19+
"weights and values arrays should have same number of elements"
20+
);
21+
}
22+
23+
const numberOfItems = weights.length;
24+
25+
// Declaring a data structure to store calculated states/values
26+
let dp: number[][] = new Array(numberOfItems + 1);
27+
28+
for (let i = 0; i < dp.length; i++) {
29+
// Placing an array at each index of dp to make it a 2d matrix
30+
dp[i] = new Array(capacity + 1);
31+
}
32+
33+
// Loop traversing each state of dp
34+
for (let i = 0; i < numberOfItems; i++) {
35+
for (let j = 0; j <= capacity; j++) {
36+
if (i == 0) {
37+
if (j >= weights[i]) {
38+
// grab the first item if it's weight is less than remaining weight (j)
39+
dp[i][j] = values[i];
40+
} else {
41+
// if weight[i] is more than remaining weight (j) leave it
42+
dp[i][j] = 0;
43+
}
44+
} else if (j < weights[i]) {
45+
// if weight of current item (weights[i]) is more than remaining weight (j), leave the current item and just carry on previous items
46+
dp[i][j] = dp[i - 1][j];
47+
} else {
48+
// select the maximum of (if current weight is collected thus adding it's value) and (if current weight is not collected thus not adding it's value)
49+
dp[i][j] = Math.max(
50+
dp[i - 1][j - weights[i]] + values[i],
51+
dp[i - 1][j]
52+
);
53+
}
54+
}
55+
}
56+
57+
// Return the final maximized value at last position of dp matrix
58+
return dp[numberOfItems - 1][capacity];
59+
};
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { knapsack } from "../knapsack";
2+
3+
const cases: [number, number[], number[], number][] = [
4+
[15, [6, 5, 6, 6, 3, 7], [5, 6, 4, 6, 5, 2], 17],
5+
[50, [10, 20, 30], [60, 100, 120], 220],
6+
[8, [3, 4, 5], [30, 50, 60], 90],
7+
[
8+
5,
9+
[1, 1, 1, 1, 1],
10+
[1000000000, 1000000000, 1000000000, 1000000000, 1000000000],
11+
5000000000,
12+
],
13+
];
14+
15+
describe("Knapsack Algorithm Test", () => {
16+
test.each(cases)(
17+
"given %p capacity available, with weights %p and values %p, knapsack should return %p",
18+
(capacity, weights, values, expectedResult) => {
19+
const result = knapsack(capacity, weights, values);
20+
expect(result).toBe(expectedResult);
21+
}
22+
);
23+
});
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

maths/lowest_common_multiple.ts

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* @function LowestCommonMultiple
3+
* @description Determine the lowest common multiple of a group of numbers.
4+
* @param {Number[]} nums - An array of numbers.
5+
* @return {Number} - The lowest common multiple.
6+
* @see https://www.mathsisfun.com/least-common-multiple.html
7+
* @example LowestCommonMultiple(3, 4) = 12
8+
* @example LowestCommonMultiple(8, 6) = 24
9+
* @example LowestCommonMultiple(5, 8, 3) = 120
10+
*/
11+
12+
import { greatestCommonFactor } from "./greatest_common_factor";
13+
14+
//A naive solution which requires no additional mathematical algorithm
15+
16+
export const naiveLCM = (nums: number[]): number => {
17+
if (nums.some((num) => num < 0)) {
18+
throw new Error("numbers must be positive to determine lowest common multiple");
19+
}
20+
21+
if (nums.length === 0) {
22+
throw new Error("at least one number must be passed in");
23+
}
24+
25+
const max_num = Math.max(...nums);
26+
let current_num = max_num;
27+
28+
while (true) {
29+
if (nums.every((num) => current_num % num === 0)){
30+
return current_num;
31+
} else {
32+
current_num += max_num;
33+
}
34+
}
35+
}
36+
37+
//A typically more efficient solution which requires prior knowledge of GCF
38+
//Note that due to utilizing GCF, which requires natural numbers, this method only accepts natural numbers.
39+
40+
export const binaryLCM = (a: number, b: number): number => {
41+
if (a < 0 || b < 0) {
42+
throw new Error("numbers must be positive to determine lowest common multiple");
43+
}
44+
45+
if (!Number.isInteger(a) || !Number.isInteger(b)) {
46+
throw new Error("this method, which utilizes GCF, requires natural numbers.");
47+
}
48+
49+
return a * b / greatestCommonFactor([a, b]);
50+
}
51+
52+
export const lowestCommonMultiple = (nums: number[]): number => {
53+
if (nums.length === 0) {
54+
throw new Error("at least one number must be passed in");
55+
}
56+
57+
return nums.reduce(binaryLCM);
58+
}
File renamed without changes.
File renamed without changes.

maths/sieve_of_eratosthenes.ts

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @function SieveOfEratosthenes
3+
* @description Find the prime numbers between 2 and n
4+
* @param {number} n - numbers set the limit that the algorithm needs to look to find the primes
5+
* @return {number[]} - List of prime numbers
6+
* @see https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes\
7+
* @example SieveOfErastosthenes(5) = [2,3,5]
8+
* @example SieveOfErastosthenes(10) = [2,3,5,7]
9+
*/
10+
11+
export function SieveOfEratosthenes(n: number): number[] {
12+
if (n < 0 || !Number.isInteger(n)) {
13+
throw new Error("Only natural numbers are supported");
14+
}
15+
const numbers = new Array<boolean>(n + 1)
16+
.fill(true)
17+
const primeNumbers: number[] = [];
18+
for (let i = 2; i <= n; i++) {
19+
if (numbers[i]) {
20+
primeNumbers.push(i);
21+
for (let j = i + i; j <= n; j += i) {
22+
numbers[j] = false;
23+
}
24+
}
25+
}
26+
return primeNumbers;
27+
}

Maths/test/AbsoluteValue.test.ts renamed to maths/test/absolute_value.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { AbsoluteValue } from "../AbsoluteValue";
1+
import { AbsoluteValue } from "../absolute_value";
22

33
describe("AbsoluteValue", () => {
44
it("should return the absolute value of zero", () => {

Maths/test/AliquotSum.test.ts renamed to maths/test/aliquot_sum.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { AliquotSum } from "../AliquotSum";
1+
import { AliquotSum } from "../aliquot_sum";
22

33
test.each([[15, 9], [18, 21], [28, 28], [100, 117], [169, 14], [1729, 511], [15625, 3906]])("Aliquot Sum of %i is %i", (num, expected) => {
44
expect(AliquotSum(num)).toBe(expected)

Maths/test/ArmstrongNumber.test.ts renamed to maths/test/armstrong_number.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ArmstrongNumber } from "../ArmstrongNumber"
1+
import { ArmstrongNumber } from "../armstrong_number"
22

33
test.each([[9, true], [-310, false], [0, false], [407, true], [420, false], [92727, true], [13579, false]])('i is an Armstrong number or not', (num, expected) => {
44
expect(ArmstrongNumber(num)).toBe(expected)

0 commit comments

Comments
 (0)