Skip to content

[pull] master from TheAlgorithms:master #21

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 4 commits into from
Oct 11, 2023
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
85 changes: 85 additions & 0 deletions Conversions/RgbHslConversion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**
* Given a color in RGB format, convert it to HSL format.
*
* For more info: https://www.niwa.nu/2013/05/math-behind-colorspace-conversions-rgb-hsl/
*
* @param {number[]} colorRgb - One dimensional array of integers (RGB color format).
* @returns {number[]} - One dimensional array of integers (HSL color format).
*
* @example
* const colorRgb = [24, 98, 118]
*
* const result = rgbToHsl(colorRgb)
*
* // The function returns the corresponding color in HSL format:
* // result = [193, 66, 28]
*/

const checkRgbFormat = (colorRgb) => colorRgb.every((c) => c >= 0 && c <= 255)

const rgbToHsl = (colorRgb) => {
if (!checkRgbFormat(colorRgb)) {
throw new Error('Input is not a valid RGB color.')
}

let colorHsl = colorRgb

let red = Math.round(colorRgb[0])
let green = Math.round(colorRgb[1])
let blue = Math.round(colorRgb[2])

const limit = 255

colorHsl[0] = red / limit
colorHsl[1] = green / limit
colorHsl[2] = blue / limit

let minValue = Math.min(...colorHsl)
let maxValue = Math.max(...colorHsl)

let channel = 0

if (maxValue === colorHsl[1]) {
channel = 1
} else if (maxValue === colorHsl[2]) {
channel = 2
}

let luminance = (minValue + maxValue) / 2

let saturation = 0

if (minValue !== maxValue) {
if (luminance <= 0.5) {
saturation = (maxValue - minValue) / (maxValue + minValue)
} else {
saturation = (maxValue - minValue) / (2 - maxValue - minValue)
}
}

let hue = 0

if (saturation !== 0) {
if (channel === 0) {
hue = (colorHsl[1] - colorHsl[2]) / (maxValue - minValue)
} else if (channel === 1) {
hue = 2 + (colorHsl[2] - colorHsl[0]) / (maxValue - minValue)
} else {
hue = 4 + (colorHsl[0] - colorHsl[1]) / (maxValue - minValue)
}
}

hue *= 60

if (hue < 0) {
hue += 360
}

colorHsl[0] = Math.round(hue)
colorHsl[1] = Math.round(saturation * 100)
colorHsl[2] = Math.round(luminance * 100)

return colorHsl
}

export { rgbToHsl }
43 changes: 43 additions & 0 deletions Conversions/test/RgbHslConversion.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { rgbToHsl } from '../RgbHslConversion'
describe('RgbHslConversion', () => {
test.each([
[
[215, 19, 180],
[311, 84, 46]
],
[
[21, 190, 18],
[119, 83, 41]
],
[
[80, 100, 160],
[225, 33, 47]
],
[
[80, 1, 16],
[349, 98, 16]
],
[
[8, 20, 0],
[96, 100, 4]
],
[
[0, 0, 0],
[0, 0, 0]
],
[
[255, 255, 255],
[0, 0, 100]
]
])('Should return the color in HSL format.', (colorRgb, expected) => {
expect(rgbToHsl(colorRgb)).toEqual(expected)
})

test.each([
[[256, 180, 9], 'Input is not a valid RGB color.'],
[[-90, 46, 8], 'Input is not a valid RGB color.'],
[[1, 39, 900], 'Input is not a valid RGB color.']
])('Should return the error message.', (colorRgb, expected) => {
expect(() => rgbToHsl(colorRgb)).toThrowError(expected)
})
})
45 changes: 45 additions & 0 deletions Data-Structures/Linked-List/MergeTwoSortedLinkedLists.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { LinkedList } from './SinglyLinkedList.js'
/**
* A LinkedList-based solution for merging two sorted linked lists into one sorted list.
*
* @param {LinkedList} list1 - The the first sorted linked list.
* @param {LinkedList} list2 - The second sorted linked list.
* @returns {LinkedList} - The merged sorted linked list.
*
* @example
* const list1 = new LinkedList([1,2,4]);
*
* const list2 = new LinkedList([1,3,4]);
*
* const result = mergeLinkedLists(list1, list2);
* // Returns the merged linked list representing 1 -> 1 -> 2 -> 3 -> 4 -> 4
*/

function mergeLinkedLists(list1, list2) {
const mergedList = new LinkedList()

let current1 = list1.headNode
let current2 = list2.headNode

while (current1 || current2) {
if (!current1) {
mergedList.addLast(current2.data)
current2 = current2.next
} else if (!current2) {
mergedList.addLast(current1.data)
current1 = current1.next
} else {
if (current1.data < current2.data) {
mergedList.addLast(current1.data)
current1 = current1.next
} else {
mergedList.addLast(current2.data)
current2 = current2.next
}
}
}

return mergedList
}

export { mergeLinkedLists }
39 changes: 39 additions & 0 deletions Data-Structures/Linked-List/test/MergeTwoSortedLinkedLists.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { expect } from 'vitest'
import { mergeLinkedLists } from '../MergeTwoSortedLinkedLists.js'
import { LinkedList } from '../SinglyLinkedList.js'

describe('MergeTwoSortedLinkedLists', () => {
it('Merges two sorted linked lists', () => {
const list1 = new LinkedList([1, 2, 4])

const list2 = new LinkedList([1, 3, 4])

const expectedResult = new LinkedList([1, 1, 2, 3, 4, 4])

const result = mergeLinkedLists(list1, list2)

expect(result).toEqual(expectedResult)
})

it('Merges two empty linked lists', () => {
const list1 = new LinkedList()
const list2 = new LinkedList()

const expectedResult = new LinkedList()

const result = mergeLinkedLists(list1, list2)

expect(result).toEqual(expectedResult)
})

it('Merges one empty linked list with a non-empty one', () => {
const list1 = new LinkedList()
const list2 = new LinkedList([1])

const expectedResult = new LinkedList([1])

const result = mergeLinkedLists(list1, list2)

expect(result).toEqual(expectedResult)
})
})
78 changes: 78 additions & 0 deletions Maths/Determinant.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* Given a square matrix, find its determinant using Laplace Expansion.
* Time Complexity : O(n!)
*
* For more info: https://en.wikipedia.org/wiki/Determinant
*
* @param {number[[]]} matrix - Two dimensional array of integers.
* @returns {number} - An integer equal to the determinant.
*
* @example
* const squareMatrix = [
* [2,3,4,6],
* [5,8,9,0],
* [7,4,3,9],
* [4,0,2,1]
* ];
*
* const result = determinant(squareMatrix);
* // The function should return 858 as the resultant determinant.
*/

const subMatrix = (matrix, i, j) => {
let matrixSize = matrix[0].length
if (matrixSize === 1) {
return matrix[0][0]
}
let subMatrix = []
for (let x = 0; x < matrixSize; x++) {
if (x === i) {
continue
}
subMatrix.push([])
for (let y = 0; y < matrixSize; y++) {
if (y === j) {
continue
}
subMatrix[subMatrix.length - 1].push(matrix[x][y])
}
}
return subMatrix
}

const isMatrixSquare = (matrix) => {
let numRows = matrix.length
for (let i = 0; i < numRows; i++) {
if (numRows !== matrix[i].length) {
return false
}
}
return true
}

const determinant = (matrix) => {
if (
!Array.isArray(matrix) ||
matrix.length === 0 ||
!Array.isArray(matrix[0])
) {
throw new Error('Input is not a valid 2D matrix.')
}
if (!isMatrixSquare(matrix)) {
throw new Error('Square matrix is required.')
}
let numCols = matrix[0].length
if (numCols === 1) {
return matrix[0][0]
}
let result = 0
let setIndex = 0
for (let i = 0; i < numCols; i++) {
result +=
Math.pow(-1, i) *
matrix[setIndex][i] *
determinant(subMatrix(matrix, setIndex, i))
}
return result
}
export { determinant }
Loading