mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 08:16:50 +08:00
feat: Test running overhaul, switch to Prettier & reformat everything (#1407)
* chore: Switch to Node 20 + Vitest * chore: migrate to vitest mock functions * chore: code style (switch to prettier) * test: re-enable long-running test Seems the switch to Node 20 and Vitest has vastly improved the code's and / or the test's runtime! see #1193 * chore: code style * chore: fix failing tests * Updated Documentation in README.md * Update contribution guidelines to state usage of Prettier * fix: set prettier printWidth back to 80 * chore: apply updated code style automatically * fix: set prettier line endings to lf again * chore: apply updated code style automatically --------- Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Lars Müller <34514239+appgurueu@users.noreply.github.com>
This commit is contained in:
@ -16,13 +16,13 @@ const FibonacciIterative = (num) => {
|
||||
return sequence
|
||||
}
|
||||
|
||||
const FibonacciGenerator = function * (neg) {
|
||||
const FibonacciGenerator = function* (neg) {
|
||||
let a = 0
|
||||
let b = 1
|
||||
yield a
|
||||
while (true) {
|
||||
yield b;
|
||||
[a, b] = neg ? [b, a - b] : [b, a + b]
|
||||
yield b
|
||||
;[a, b] = neg ? [b, a - b] : [b, a + b]
|
||||
}
|
||||
}
|
||||
|
||||
@ -55,9 +55,11 @@ const FibonacciRecursiveDP = (stairs) => {
|
||||
if (stairs <= 1) return stairs
|
||||
|
||||
// Memoize stair count
|
||||
if (dict.has(stairs)) return (isNeg ? (-1) ** (stairs + 1) : 1) * dict.get(stairs)
|
||||
if (dict.has(stairs))
|
||||
return (isNeg ? (-1) ** (stairs + 1) : 1) * dict.get(stairs)
|
||||
|
||||
const res = FibonacciRecursiveDP(stairs - 1) + FibonacciRecursiveDP(stairs - 2)
|
||||
const res =
|
||||
FibonacciRecursiveDP(stairs - 1) + FibonacciRecursiveDP(stairs - 2)
|
||||
|
||||
dict.set(stairs, res)
|
||||
|
||||
@ -82,9 +84,7 @@ const FibonacciDpWithoutRecursion = (num) => {
|
||||
table.push(1)
|
||||
table.push(isNeg ? -1 : 1)
|
||||
for (let i = 2; i < num; ++i) {
|
||||
table.push(
|
||||
isNeg ? table[i - 1] - table[i] : table[i] + table[i - 1]
|
||||
)
|
||||
table.push(isNeg ? table[i - 1] - table[i] : table[i] + table[i - 1])
|
||||
}
|
||||
return table
|
||||
}
|
||||
@ -92,7 +92,7 @@ const FibonacciDpWithoutRecursion = (num) => {
|
||||
// Using Matrix exponentiation to find n-th fibonacci in O(log n) time
|
||||
|
||||
const copyMatrix = (A) => {
|
||||
return A.map(row => row.map(cell => cell))
|
||||
return A.map((row) => row.map((cell) => cell))
|
||||
}
|
||||
|
||||
const Identity = (size) => {
|
||||
@ -100,10 +100,14 @@ const Identity = (size) => {
|
||||
const ZERO = isBigInt ? 0n : 0
|
||||
const ONE = isBigInt ? 1n : 1
|
||||
size = Number(size)
|
||||
const I = Array(size).fill(null).map(() => Array(size).fill())
|
||||
return I.map((row, rowIdx) => row.map((_col, colIdx) => {
|
||||
return rowIdx === colIdx ? ONE : ZERO
|
||||
}))
|
||||
const I = Array(size)
|
||||
.fill(null)
|
||||
.map(() => Array(size).fill())
|
||||
return I.map((row, rowIdx) =>
|
||||
row.map((_col, colIdx) => {
|
||||
return rowIdx === colIdx ? ONE : ZERO
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
// A of size (l x m) and B of size (m x n)
|
||||
@ -117,7 +121,9 @@ const matrixMultiply = (A, B) => {
|
||||
const l = A.length
|
||||
const m = B.length
|
||||
const n = B[0].length // Assuming non-empty matrices
|
||||
const C = Array(l).fill(null).map(() => Array(n).fill())
|
||||
const C = Array(l)
|
||||
.fill(null)
|
||||
.map(() => Array(n).fill())
|
||||
for (let i = 0; i < l; i++) {
|
||||
for (let j = 0; j < n; j++) {
|
||||
C[i][j] = isBigInt ? 0n : 0
|
||||
@ -179,10 +185,7 @@ const FibonacciMatrixExpo = (num) => {
|
||||
]
|
||||
|
||||
const poweredA = matrixExpo(A, num - ONE) // A raised to the power n-1
|
||||
let F = [
|
||||
[ONE],
|
||||
[ZERO]
|
||||
]
|
||||
let F = [[ONE], [ZERO]]
|
||||
F = matrixMultiply(poweredA, F)
|
||||
return F[0][0] * (isNeg ? (-ONE) ** (num + ONE) : ONE)
|
||||
}
|
||||
@ -195,7 +198,7 @@ const sqrt5 = Math.sqrt(5)
|
||||
const phi = (1 + sqrt5) / 2
|
||||
const psi = (1 - sqrt5) / 2
|
||||
|
||||
const FibonacciUsingFormula = n => Math.round((phi ** n - psi ** n) / sqrt5)
|
||||
const FibonacciUsingFormula = (n) => Math.round((phi ** n - psi ** n) / sqrt5)
|
||||
|
||||
export { FibonacciDpWithoutRecursion }
|
||||
export { FibonacciIterative }
|
||||
|
Reference in New Issue
Block a user