mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 16:26:47 +08:00
Remove live code & console.log, leave examples as comments (Geometry, Graphs, Maths).
This commit is contained in:
@ -2,7 +2,8 @@
|
|||||||
* Author: Arnab Ray
|
* Author: Arnab Ray
|
||||||
* ConvexHull using Graham Scan
|
* ConvexHull using Graham Scan
|
||||||
* Wikipedia: https://en.wikipedia.org/wiki/Graham_scan
|
* Wikipedia: https://en.wikipedia.org/wiki/Graham_scan
|
||||||
* Given a set of points in the plane. The Convex hull of the set is the smallest convex polygon that contains all the points of it.
|
* Given a set of points in the plane. The Convex hull of the set is the smallest
|
||||||
|
* convex polygon that contains all the points of it.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function compare (a, b) {
|
function compare (a, b) {
|
||||||
@ -27,7 +28,7 @@ function orientation (a, b, c) {
|
|||||||
function convexHull (points) {
|
function convexHull (points) {
|
||||||
const pointsLen = points.length
|
const pointsLen = points.length
|
||||||
if (pointsLen <= 2) {
|
if (pointsLen <= 2) {
|
||||||
console.log('Minimum of 3 points is required to form closed polygon!')
|
throw new Error('Minimum of 3 points is required to form closed polygon!')
|
||||||
}
|
}
|
||||||
|
|
||||||
points.sort(compare)
|
points.sort(compare)
|
||||||
@ -65,18 +66,22 @@ function convexHull (points) {
|
|||||||
for (let i = lowerPoints.length - 1; i >= 0; i--) {
|
for (let i = lowerPoints.length - 1; i >= 0; i--) {
|
||||||
hull.push(lowerPoints[i])
|
hull.push(lowerPoints[i])
|
||||||
}
|
}
|
||||||
console.log('The Convex Hull found is: \n')
|
|
||||||
console.log(hull)
|
return hull
|
||||||
}
|
}
|
||||||
|
|
||||||
const points = [
|
export { convexHull }
|
||||||
{ x: 0, y: 3 },
|
|
||||||
{ x: 1, y: 1 },
|
|
||||||
{ x: 2, y: 2 },
|
|
||||||
{ x: 4, y: 4 },
|
|
||||||
{ x: 0, y: 0 },
|
|
||||||
{ x: 1, y: 2 },
|
|
||||||
{ x: 3, y: 1 },
|
|
||||||
{ x: 3, y: 3 }]
|
|
||||||
|
|
||||||
convexHull(points)
|
// Example
|
||||||
|
|
||||||
|
// const points = [
|
||||||
|
// { x: 0, y: 3 },
|
||||||
|
// { x: 1, y: 1 },
|
||||||
|
// { x: 2, y: 2 },
|
||||||
|
// { x: 4, y: 4 },
|
||||||
|
// { x: 0, y: 0 },
|
||||||
|
// { x: 1, y: 2 },
|
||||||
|
// { x: 3, y: 1 },
|
||||||
|
// { x: 3, y: 3 }]
|
||||||
|
|
||||||
|
// convexHull(points)
|
||||||
|
@ -45,12 +45,12 @@ class GraphUnweightedUndirectedAdjacencyList {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function main () {
|
export { GraphUnweightedUndirectedAdjacencyList }
|
||||||
const graph = new GraphUnweightedUndirectedAdjacencyList()
|
|
||||||
graph.addEdge(1, 2) // Component 1
|
|
||||||
graph.addEdge(3, 4) // Component 2
|
|
||||||
graph.addEdge(3, 5) // Component 2
|
|
||||||
console.log(graph.connectedComponents())
|
|
||||||
}
|
|
||||||
|
|
||||||
main()
|
// Example
|
||||||
|
|
||||||
|
// const graph = new GraphUnweightedUndirectedAdjacencyList()
|
||||||
|
// graph.addEdge(1, 2) // Component 1
|
||||||
|
// graph.addEdge(3, 4) // Component 2
|
||||||
|
// graph.addEdge(3, 5) // Component 2
|
||||||
|
// const components = graph.connectedComponents()
|
||||||
|
@ -8,4 +8,5 @@ function density (numberOfNodes, numberOfEdges, isDirected = false) {
|
|||||||
return (multi * numberOfEdges) / (numberOfNodes * (numberOfNodes - 1))
|
return (multi * numberOfEdges) / (numberOfNodes * (numberOfNodes - 1))
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(density(10, 2))
|
export { density }
|
||||||
|
|
||||||
|
@ -38,14 +38,14 @@ class GraphUnweightedUndirected {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function main () {
|
export { GraphUnweightedUndirected }
|
||||||
const graph = new GraphUnweightedUndirected()
|
|
||||||
graph.addEdge(1, 2)
|
|
||||||
graph.addEdge(2, 3)
|
|
||||||
graph.addEdge(2, 4)
|
|
||||||
graph.addEdge(3, 5)
|
|
||||||
console.log(graph.DFSIterative(5, 1))
|
|
||||||
console.log(graph.DFSIterative(5, 100))
|
|
||||||
}
|
|
||||||
|
|
||||||
main()
|
// Example
|
||||||
|
|
||||||
|
// const graph = new GraphUnweightedUndirected()
|
||||||
|
// graph.addEdge(1, 2)
|
||||||
|
// graph.addEdge(2, 3)
|
||||||
|
// graph.addEdge(2, 4)
|
||||||
|
// graph.addEdge(3, 5)
|
||||||
|
// graph.DFSIterative(5, 1)
|
||||||
|
// graph.DFSIterative(5, 100)
|
||||||
|
@ -33,14 +33,12 @@ class GraphUnweightedUndirected {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function main () {
|
export { GraphUnweightedUndirected }
|
||||||
const graph = new GraphUnweightedUndirected()
|
|
||||||
graph.addEdge(1, 2)
|
|
||||||
graph.addEdge(2, 3)
|
|
||||||
graph.addEdge(2, 4)
|
|
||||||
graph.addEdge(3, 5)
|
|
||||||
console.log(graph.DFSRecursive(5, 1))
|
|
||||||
console.log(graph.DFSRecursive(5, 100))
|
|
||||||
}
|
|
||||||
|
|
||||||
main()
|
// const graph = new GraphUnweightedUndirected()
|
||||||
|
// graph.addEdge(1, 2)
|
||||||
|
// graph.addEdge(2, 3)
|
||||||
|
// graph.addEdge(2, 4)
|
||||||
|
// graph.addEdge(3, 5)
|
||||||
|
// graph.DFSRecursive(5, 1)
|
||||||
|
// graph.DFSRecursive(5, 100)
|
||||||
|
@ -47,30 +47,30 @@ function djikstra (graph, V, src) {
|
|||||||
return dist
|
return dist
|
||||||
}
|
}
|
||||||
|
|
||||||
const V = 9
|
export { createGraph, djikstra }
|
||||||
const E = [
|
|
||||||
[0, 1, 4],
|
|
||||||
[0, 7, 8],
|
|
||||||
[1, 7, 11],
|
|
||||||
[1, 2, 8],
|
|
||||||
[7, 8, 7],
|
|
||||||
[6, 7, 1],
|
|
||||||
[2, 8, 2],
|
|
||||||
[6, 8, 6],
|
|
||||||
[5, 6, 2],
|
|
||||||
[2, 5, 4],
|
|
||||||
[2, 3, 7],
|
|
||||||
[3, 5, 14],
|
|
||||||
[3, 4, 9],
|
|
||||||
[4, 5, 10]
|
|
||||||
]
|
|
||||||
|
|
||||||
const graph = createGraph(V, E)
|
// const V = 9
|
||||||
const distances = djikstra(graph, V, 0)
|
// const E = [
|
||||||
|
// [0, 1, 4],
|
||||||
|
// [0, 7, 8],
|
||||||
|
// [1, 7, 11],
|
||||||
|
// [1, 2, 8],
|
||||||
|
// [7, 8, 7],
|
||||||
|
// [6, 7, 1],
|
||||||
|
// [2, 8, 2],
|
||||||
|
// [6, 8, 6],
|
||||||
|
// [5, 6, 2],
|
||||||
|
// [2, 5, 4],
|
||||||
|
// [2, 3, 7],
|
||||||
|
// [3, 5, 14],
|
||||||
|
// [3, 4, 9],
|
||||||
|
// [4, 5, 10]
|
||||||
|
// ]
|
||||||
|
|
||||||
|
// const graph = createGraph(V, E)
|
||||||
|
// const distances = djikstra(graph, V, 0)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The first value in the array determines the minimum distance and the
|
* The first value in the array determines the minimum distance and the
|
||||||
* second value represents the parent node from which the minimum distance has been calculated
|
* second value represents the parent node from which the minimum distance has been calculated
|
||||||
*/
|
*/
|
||||||
|
|
||||||
console.log(distances)
|
|
||||||
|
@ -39,28 +39,31 @@ function solve (graph, s) {
|
|||||||
|
|
||||||
return solutions
|
return solutions
|
||||||
}
|
}
|
||||||
// create graph
|
|
||||||
const graph = {}
|
|
||||||
|
|
||||||
const layout = {
|
export { solve }
|
||||||
R: ['2'],
|
|
||||||
2: ['3', '4'],
|
|
||||||
3: ['4', '6', '13'],
|
|
||||||
4: ['5', '8'],
|
|
||||||
5: ['7', '11'],
|
|
||||||
6: ['13', '15'],
|
|
||||||
7: ['10'],
|
|
||||||
8: ['11', '13'],
|
|
||||||
9: ['14'],
|
|
||||||
10: [],
|
|
||||||
11: ['12'],
|
|
||||||
12: [],
|
|
||||||
13: ['14'],
|
|
||||||
14: [],
|
|
||||||
15: []
|
|
||||||
}
|
|
||||||
|
|
||||||
// convert uni-directional to bi-directional graph
|
// // create graph
|
||||||
|
// const graph = {}
|
||||||
|
|
||||||
|
// const layout = {
|
||||||
|
// R: ['2'],
|
||||||
|
// 2: ['3', '4'],
|
||||||
|
// 3: ['4', '6', '13'],
|
||||||
|
// 4: ['5', '8'],
|
||||||
|
// 5: ['7', '11'],
|
||||||
|
// 6: ['13', '15'],
|
||||||
|
// 7: ['10'],
|
||||||
|
// 8: ['11', '13'],
|
||||||
|
// 9: ['14'],
|
||||||
|
// 10: [],
|
||||||
|
// 11: ['12'],
|
||||||
|
// 12: [],
|
||||||
|
// 13: ['14'],
|
||||||
|
// 14: [],
|
||||||
|
// 15: []
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // convert uni-directional to bi-directional graph
|
||||||
// let graph = {
|
// let graph = {
|
||||||
// a: {e:1, b:1, g:3},
|
// a: {e:1, b:1, g:3},
|
||||||
// b: {a:1, c:1},
|
// b: {a:1, c:1},
|
||||||
@ -72,26 +75,22 @@ const layout = {
|
|||||||
// h: {f:1}
|
// h: {f:1}
|
||||||
// };
|
// };
|
||||||
|
|
||||||
for (const id in layout) {
|
// for (const id in layout) {
|
||||||
if (!graph[id]) { graph[id] = {} }
|
// if (!graph[id]) { graph[id] = {} }
|
||||||
layout[id].forEach(function (aid) {
|
// layout[id].forEach(function (aid) {
|
||||||
graph[id][aid] = 1
|
// graph[id][aid] = 1
|
||||||
if (!graph[aid]) { graph[aid] = {} }
|
// if (!graph[aid]) { graph[aid] = {} }
|
||||||
graph[aid][id] = 1
|
// graph[aid][id] = 1
|
||||||
})
|
// })
|
||||||
}
|
// }
|
||||||
|
|
||||||
// choose start node
|
// // choose start node
|
||||||
const start = '10'
|
// const start = '10'
|
||||||
// get all solutions
|
// // get all solutions
|
||||||
const solutions = solve(graph, start)
|
// const solutions = solve(graph, start)
|
||||||
|
|
||||||
console.log("From '" + start + "' to")
|
// // for s in solutions..
|
||||||
// display solutions
|
// ' -> ' + s + ': [' + solutions[s].join(', ') + '] (dist:' + solutions[s].dist + ')'
|
||||||
for (const s in solutions) {
|
|
||||||
if (!solutions[s]) continue
|
|
||||||
console.log(' -> ' + s + ': [' + solutions[s].join(', ') + '] (dist:' + solutions[s].dist + ')')
|
|
||||||
}
|
|
||||||
|
|
||||||
// From '10' to
|
// From '10' to
|
||||||
// -> 2: [7, 5, 4, 2] (dist:4)
|
// -> 2: [7, 5, 4, 2] (dist:4)
|
||||||
|
@ -23,26 +23,26 @@ const FloydWarshall = (dist) => {
|
|||||||
return dist
|
return dist
|
||||||
}
|
}
|
||||||
|
|
||||||
const main = () => {
|
export { FloydWarshall }
|
||||||
// For the following graph (edge weights are shown in brackets)
|
|
||||||
// 4 1 dist[1][2] = dist[2][1] = 1
|
// For the following graph (edge weights are shown in brackets)
|
||||||
// \ (2)/ \ dist[1][3] = dist[3][1] = 2
|
// 4 1 dist[1][2] = dist[2][1] = 1
|
||||||
// \ / \(1) dist[1][4] = dist[4][1] = Infinity
|
// \ (2)/ \ dist[1][3] = dist[3][1] = 2
|
||||||
// (1)\ / \ dist[3][4] = dist[4][3] = 1
|
// \ / \(1) dist[1][4] = dist[4][1] = Infinity
|
||||||
// 3 2 dist[2][4] = dist[4][2] = Infinity
|
// (1)\ / \ dist[3][4] = dist[4][3] = 1
|
||||||
// dist[2][3] = dist[3][2] = Infinity
|
// 3 2 dist[2][4] = dist[4][2] = Infinity
|
||||||
// Output should be:
|
// dist[2][3] = dist[3][2] = Infinity
|
||||||
// [ [0, 1, 2, 3],
|
// Output should be:
|
||||||
// [1, 0, 3, 4],
|
// [ [0, 1, 2, 3],
|
||||||
// [2, 3, 0, 1],
|
// [1, 0, 3, 4],
|
||||||
// [3, 4, 1, 0] ]
|
// [2, 3, 0, 1],
|
||||||
console.log(FloydWarshall(
|
// [3, 4, 1, 0] ]
|
||||||
[[0, 1, 2, Infinity],
|
|
||||||
[1, 0, Infinity, Infinity],
|
// FloydWarshall(
|
||||||
[2, Infinity, 0, 1],
|
// [[0, 1, 2, Infinity],
|
||||||
[Infinity, Infinity, 1, 0]
|
// [1, 0, Infinity, Infinity],
|
||||||
]
|
// [2, Infinity, 0, 1],
|
||||||
))
|
// [Infinity, Infinity, 1, 0]
|
||||||
}
|
// ]
|
||||||
|
// )
|
||||||
|
|
||||||
main()
|
|
||||||
|
@ -101,15 +101,12 @@ class GraphWeightedUndirectedAdjacencyList {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function main () {
|
export { GraphWeightedUndirectedAdjacencyList }
|
||||||
const graph = new GraphWeightedUndirectedAdjacencyList()
|
|
||||||
graph.addEdge(1, 2, 1)
|
|
||||||
graph.addEdge(2, 3, 2)
|
|
||||||
graph.addEdge(3, 4, 1)
|
|
||||||
graph.addEdge(3, 5, 100) // Removed in MST
|
|
||||||
graph.addEdge(4, 5, 5)
|
|
||||||
console.log(graph)
|
|
||||||
console.log(graph.KruskalMST())
|
|
||||||
}
|
|
||||||
|
|
||||||
main()
|
// const graph = new GraphWeightedUndirectedAdjacencyList()
|
||||||
|
// graph.addEdge(1, 2, 1)
|
||||||
|
// graph.addEdge(2, 3, 2)
|
||||||
|
// graph.addEdge(3, 4, 1)
|
||||||
|
// graph.addEdge(3, 5, 100) // Removed in MST
|
||||||
|
// graph.addEdge(4, 5, 5)
|
||||||
|
// graph.KruskalMST()
|
||||||
|
@ -30,11 +30,11 @@ class Graph {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
(() => {
|
export { Graph }
|
||||||
const graph = new Graph()
|
|
||||||
graph.addEdge(1, 2)
|
// const graph = new Graph()
|
||||||
graph.addEdge(2, 3)
|
// graph.addEdge(1, 2)
|
||||||
graph.addEdge(3, 5)
|
// graph.addEdge(2, 3)
|
||||||
graph.addEdge(1, 5)
|
// graph.addEdge(3, 5)
|
||||||
console.log(graph.nodeNeighbors(1))
|
// graph.addEdge(1, 5)
|
||||||
})()
|
// graph.nodeNeighbors(1)
|
||||||
|
@ -46,12 +46,6 @@ Pseudocode:
|
|||||||
|
|
||||||
Return the count
|
Return the count
|
||||||
*/
|
*/
|
||||||
const grid = [
|
|
||||||
['1', '1', '0', '0', '0'],
|
|
||||||
['1', '1', '0', '0', '0'],
|
|
||||||
['0', '0', '1', '0', '0'],
|
|
||||||
['0', '0', '0', '1', '1']
|
|
||||||
]
|
|
||||||
|
|
||||||
const islands = (matrixGrid) => {
|
const islands = (matrixGrid) => {
|
||||||
const matrix = matrixGrid
|
const matrix = matrixGrid
|
||||||
@ -83,4 +77,12 @@ const islands = (matrixGrid) => {
|
|||||||
}
|
}
|
||||||
return counter
|
return counter
|
||||||
}
|
}
|
||||||
console.log(islands(grid))
|
|
||||||
|
export { islands }
|
||||||
|
|
||||||
|
// islands(
|
||||||
|
// ['1', '1', '0', '0', '0'],
|
||||||
|
// ['1', '1', '0', '0', '0'],
|
||||||
|
// ['0', '0', '1', '0', '0'],
|
||||||
|
// ['0', '0', '0', '1', '1']
|
||||||
|
// )
|
||||||
|
@ -197,14 +197,12 @@ class GraphWeightedUndirectedAdjacencyList {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function main () {
|
export { GraphWeightedUndirectedAdjacencyList }
|
||||||
const graph = new GraphWeightedUndirectedAdjacencyList()
|
|
||||||
graph.addEdge(1, 2, 1)
|
|
||||||
graph.addEdge(2, 3, 2)
|
|
||||||
graph.addEdge(3, 4, 1)
|
|
||||||
graph.addEdge(3, 5, 100) // Removed in MST
|
|
||||||
graph.addEdge(4, 5, 5)
|
|
||||||
console.log(graph.PrimMST(1))
|
|
||||||
}
|
|
||||||
|
|
||||||
main()
|
// const graph = new GraphWeightedUndirectedAdjacencyList()
|
||||||
|
// graph.addEdge(1, 2, 1)
|
||||||
|
// graph.addEdge(2, 3, 2)
|
||||||
|
// graph.addEdge(3, 4, 1)
|
||||||
|
// graph.addEdge(3, 5, 100) // Removed in MST
|
||||||
|
// graph.addEdge(4, 5, 5)
|
||||||
|
// graph.PrimMST(1)
|
||||||
|
@ -10,6 +10,6 @@ export const isDivisible = (num1, num2) => {
|
|||||||
return num1 % num2 === 0
|
return num1 % num2 === 0
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(isDivisible(10, 5)) // returns true
|
// isDivisible(10, 5) // returns true
|
||||||
console.log(isDivisible(123498175, 5)) // returns true
|
// isDivisible(123498175, 5) // returns true
|
||||||
console.log(isDivisible(99, 5)) // returns false
|
// isDivisible(99, 5) // returns false
|
||||||
|
@ -45,7 +45,7 @@ const MatMult = (matA, matB) => {
|
|||||||
return matC
|
return matC
|
||||||
}
|
}
|
||||||
|
|
||||||
const MatrixExponentiationRecursive = (mat, m) => {
|
export const MatrixExponentiationRecursive = (mat, m) => {
|
||||||
// Input: mat: 2D Array of Numbers of size n x n
|
// Input: mat: 2D Array of Numbers of size n x n
|
||||||
// Output: mat^n: 2D Array of Numbers of size n x n
|
// Output: mat^n: 2D Array of Numbers of size n x n
|
||||||
// Complexity: O(n^3 log m)
|
// Complexity: O(n^3 log m)
|
||||||
@ -65,20 +65,16 @@ const MatrixExponentiationRecursive = (mat, m) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const main = () => {
|
// const mat = [[1, 0, 2], [2, 1, 0], [0, 2, 1]]
|
||||||
const mat = [[1, 0, 2], [2, 1, 0], [0, 2, 1]]
|
|
||||||
|
|
||||||
// mat ^ 0 = [ [ 1, 0, 0 ], [ 0, 1, 0 ], [ 0, 0, 1 ] ]
|
// // mat ^ 0 = [ [ 1, 0, 0 ], [ 0, 1, 0 ], [ 0, 0, 1 ] ]
|
||||||
console.log(MatrixExponentiationRecursive(mat, 0))
|
// MatrixExponentiationRecursive(mat, 0)
|
||||||
|
|
||||||
// mat ^ 1 = [ [ 1, 0, 2 ], [ 2, 1, 0 ], [ 0, 2, 1 ] ]
|
// // mat ^ 1 = [ [ 1, 0, 2 ], [ 2, 1, 0 ], [ 0, 2, 1 ] ]
|
||||||
console.log(MatrixExponentiationRecursive(mat, 1))
|
// MatrixExponentiationRecursive(mat, 1)
|
||||||
|
|
||||||
// mat ^ 2 = [ [ 1, 4, 4 ], [ 4, 1, 4 ], [ 4, 4, 1 ] ]
|
// // mat ^ 2 = [ [ 1, 4, 4 ], [ 4, 1, 4 ], [ 4, 4, 1 ] ]
|
||||||
console.log(MatrixExponentiationRecursive(mat, 2))
|
// MatrixExponentiationRecursive(mat, 2)
|
||||||
|
|
||||||
// mat ^ 5 = [ [ 1, 4, 4 ], [ 4, 1, 4 ], [ 4, 4, 1 ] ]
|
// // mat ^ 5 = [ [ 1, 4, 4 ], [ 4, 1, 4 ], [ 4, 4, 1 ] ]
|
||||||
console.log(MatrixExponentiationRecursive(mat, 5))
|
// MatrixExponentiationRecursive(mat, 5)
|
||||||
}
|
|
||||||
|
|
||||||
main()
|
|
||||||
|
@ -10,7 +10,7 @@ const matrixCheck = (matrix) => {
|
|||||||
if (index === 0) {
|
if (index === 0) {
|
||||||
columnNumb = matrix[index].length
|
columnNumb = matrix[index].length
|
||||||
} else if (matrix[index].length !== columnNumb) {
|
} else if (matrix[index].length !== columnNumb) {
|
||||||
console.log('The columns in this array are not equal')
|
// The columns in this array are not equal
|
||||||
} else {
|
} else {
|
||||||
return columnNumb
|
return columnNumb
|
||||||
}
|
}
|
||||||
@ -21,7 +21,7 @@ const matrixCheck = (matrix) => {
|
|||||||
const twoMatricesCheck = (first, second) => {
|
const twoMatricesCheck = (first, second) => {
|
||||||
const [firstRowLength, secondRowLength, firstColLength, secondColLength] = [first.length, second.length, matrixCheck(first), matrixCheck(second)]
|
const [firstRowLength, secondRowLength, firstColLength, secondColLength] = [first.length, second.length, matrixCheck(first), matrixCheck(second)]
|
||||||
if (firstRowLength !== secondColLength || secondRowLength !== firstColLength) {
|
if (firstRowLength !== secondColLength || secondRowLength !== firstColLength) {
|
||||||
console.log('These matrices do not have a common side')
|
// These matrices do not have a common side
|
||||||
return false
|
return false
|
||||||
} else {
|
} else {
|
||||||
return true
|
return true
|
||||||
@ -44,7 +44,7 @@ const initiateEmptyArray = (first, second) => {
|
|||||||
// Finally, `matrixMult` uses `Array.prototype.push()`, multiple layers of nested `for` loops, the addition assignment `+=` operator and multiplication operator `*` to perform the dot product between two matrices of differing sizes.
|
// Finally, `matrixMult` uses `Array.prototype.push()`, multiple layers of nested `for` loops, the addition assignment `+=` operator and multiplication operator `*` to perform the dot product between two matrices of differing sizes.
|
||||||
// Dot product, takes the row of the first matrix and multiplies it by the column of the second matrix, the `twoMatricesCheck` tested to see if they were the same size already.
|
// Dot product, takes the row of the first matrix and multiplies it by the column of the second matrix, the `twoMatricesCheck` tested to see if they were the same size already.
|
||||||
// The dot product for each iteration is then saved to its respective index into `multMatrix`.
|
// The dot product for each iteration is then saved to its respective index into `multMatrix`.
|
||||||
const matrixMult = (firstArray, secondArray) => {
|
export const matrixMult = (firstArray, secondArray) => {
|
||||||
const multMatrix = initiateEmptyArray(firstArray, secondArray)
|
const multMatrix = initiateEmptyArray(firstArray, secondArray)
|
||||||
for (let rm = 0; rm < firstArray.length; rm++) {
|
for (let rm = 0; rm < firstArray.length; rm++) {
|
||||||
const rowMult = []
|
const rowMult = []
|
||||||
@ -66,26 +66,26 @@ const matrixMult = (firstArray, secondArray) => {
|
|||||||
return multMatrix
|
return multMatrix
|
||||||
}
|
}
|
||||||
|
|
||||||
const firstMatrix = [
|
// const firstMatrix = [
|
||||||
[1, 2],
|
// [1, 2],
|
||||||
[3, 4]
|
// [3, 4]
|
||||||
]
|
// ]
|
||||||
|
|
||||||
const secondMatrix = [
|
// const secondMatrix = [
|
||||||
[5, 6],
|
// [5, 6],
|
||||||
[7, 8]
|
// [7, 8]
|
||||||
]
|
// ]
|
||||||
|
|
||||||
console.log(matrixMult(firstMatrix, secondMatrix)) // [ [ 19, 22 ], [ 43, 50 ] ]
|
// matrixMult(firstMatrix, secondMatrix) // [ [ 19, 22 ], [ 43, 50 ] ]
|
||||||
|
|
||||||
const thirdMatrix = [
|
// const thirdMatrix = [
|
||||||
[-1, 4, 1],
|
// [-1, 4, 1],
|
||||||
[7, -6, 2]
|
// [7, -6, 2]
|
||||||
]
|
// ]
|
||||||
const fourthMatrix = [
|
// const fourthMatrix = [
|
||||||
[2, -2],
|
// [2, -2],
|
||||||
[5, 3],
|
// [5, 3],
|
||||||
[3, 2]
|
// [3, 2]
|
||||||
]
|
// ]
|
||||||
|
|
||||||
console.log(matrixMult(thirdMatrix, fourthMatrix)) // [ [ 21, 16 ], [ -10, -28 ] ]
|
// matrixMult(thirdMatrix, fourthMatrix) // [ [ 21, 16 ], [ -10, -28 ] ]
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
/*
|
/*
|
||||||
author: Theepag
|
author: Theepag
|
||||||
*/
|
*/
|
||||||
const factorialize = (num) => {
|
export const factorialize = (num) => {
|
||||||
// Step 1. variable result to store num
|
// Step 1. variable result to store num
|
||||||
let result = num
|
let result = num
|
||||||
// If num = 0 OR 1, the factorial will return 1
|
// If num = 0 OR 1, the factorial will return 1
|
||||||
@ -14,6 +14,3 @@ const factorialize = (num) => {
|
|||||||
// Step 3. Return the factorial
|
// Step 3. Return the factorial
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
// test
|
|
||||||
console.log(factorialize(5))
|
|
||||||
console.log(factorialize(4))
|
|
||||||
|
@ -4,14 +4,7 @@
|
|||||||
* Return the result.
|
* Return the result.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const decimalIsolate = (number) => {
|
export const decimalIsolate = (number) => {
|
||||||
const ans = parseFloat((number + '').replace(/^[-\d]+./, '.'))
|
const ans = parseFloat((number + '').replace(/^[-\d]+./, '.'))
|
||||||
return isNaN(ans) === true ? 0 : ans
|
return isNaN(ans) === true ? 0 : ans
|
||||||
}
|
}
|
||||||
|
|
||||||
// testing
|
|
||||||
console.log(decimalIsolate(35.345))
|
|
||||||
console.log(decimalIsolate(56.879))
|
|
||||||
console.log(decimalIsolate(89.5643))
|
|
||||||
console.log(decimalIsolate(38.00))
|
|
||||||
console.log(decimalIsolate(33))
|
|
||||||
|
@ -4,10 +4,6 @@
|
|||||||
* else false
|
* else false
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const isOdd = (value) => {
|
export const isOdd = (value) => {
|
||||||
return !!((value & 1))
|
return !!((value & 1))
|
||||||
}
|
}
|
||||||
|
|
||||||
// testing
|
|
||||||
console.log(isOdd(2))
|
|
||||||
console.log(isOdd(3))
|
|
||||||
|
Reference in New Issue
Block a user