Skip to content

Commit

Permalink
fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
appgurueu committed Aug 3, 2024
1 parent 5bb6396 commit 1fe60c4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 33 deletions.
34 changes: 17 additions & 17 deletions search/fibonacci_search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,40 +18,40 @@ export const fibonacciSearch = (
target: number
): number | null => {
const arrayLength = array.length
let a = 0; // (n-2)'th Fibonacci No.
let b = 1; // (n-1)'th Fibonacci No.
let c = a + b; // n'th Fibonacci
let a = 0 // (n-2)'th Fibonacci No.
let b = 1 // (n-1)'th Fibonacci No.
let c = a + b // n'th Fibonacci

while (c < arrayLength) {
a = b;
b = c;
c = a + b;
a = b
b = c
c = a + b
}

let offset = -1

while (c > 1) {
let i = Math.min(offset + a, arrayLength - 1);
let i = Math.min(offset + a, arrayLength - 1)

if (array[i] < target) {
c = b;
b = a;
a = c - b;
offset = i;
c = b
b = a
a = c - b
offset = i
} else if (array[i] > target) {
c = a;
b = b - a;
a = c - b;
c = a
b = b - a
a = c - b
} else {
// Element found then return index
return i;
return i
}
}

if (b && array[offset + 1] === target) {
return offset + 1;
return offset + 1
}

// Element not found then return null
return null;
return null
}
32 changes: 16 additions & 16 deletions search/test/fibonacci_search.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { fibonacciSearch } from "../fibonacci_search";
import { fibonacciSearch } from '../fibonacci_search'

describe('Fibonacci search', () => {
test.each([
[[1, 2, 3], 2, 1],
[[4, 5, 6], 2, null],
[[10, 22, 35, 40, 45, 50, 80, 82, 85, 90, 100], 85, 8],
[[], 1, null],
[[1], 1, 0],
[[1, 3, 5, 7, 9, 11, 13], 11, 5],
[[1, 3, 5, 7, 9, 11, 13], 8, null]
])(
'of %o, searching for %o, expected %i',
(array: number[], target: number, expected: number | null) => {
expect(fibonacciSearch(array, target)).toBe(expected);
}
);
});
test.each([
[[1, 2, 3], 2, 1],
[[4, 5, 6], 2, null],
[[10, 22, 35, 40, 45, 50, 80, 82, 85, 90, 100], 85, 8],
[[], 1, null],
[[1], 1, 0],
[[1, 3, 5, 7, 9, 11, 13], 11, 5],
[[1, 3, 5, 7, 9, 11, 13], 8, null]
])(
'of %o, searching for %o, expected %i',
(array: number[], target: number, expected: number | null) => {
expect(fibonacciSearch(array, target)).toBe(expected)
}
)
})

0 comments on commit 1fe60c4

Please sign in to comment.