@@ -7,16 +7,15 @@ import { computeLexicographicDistance } from "./util.js";
7
7
* @param b The second `string` to compare.
8
8
* @return -1 if a is less than b, 1 if a is greater than b, and 0 otherwise.
9
9
*/
10
- export function compareStrings ( a : string , b : string ) : number {
11
- const distance = computeLexicographicDistance ( a , b ) ;
12
-
13
- if ( distance < 0 ) {
10
+ function compareStrings ( a : string , b : string ) : number {
11
+ if ( a < b ) {
14
12
return - 1 ;
15
- } else if ( distance > 0 ) {
13
+ } else if ( a > b ) {
16
14
return 1 ;
17
15
} else {
18
16
return 0 ;
19
17
}
18
+
20
19
}
21
20
22
21
/**
@@ -25,7 +24,7 @@ export function compareStrings(a: string, b: string): number {
25
24
* @param n The value for which to compute the factorial.
26
25
* @return The factorial of n.
27
26
*/
28
- export function computeFactorial ( n : number ) : number {
27
+ function computeFactorial ( n : number ) : number {
29
28
if ( n === 0 ) {
30
29
return 1 ;
31
30
}
@@ -44,21 +43,17 @@ export function computeFactorial(n: number): number {
44
43
* @param n The first `n` of Fibonacci values to compute.
45
44
* @return An array containing the first `n` Fibonacci values.
46
45
*/
47
- export function getFirstNFibonacciNumbers ( n : number ) : number [ ] {
48
- if ( n <= 0 ) return [ ] ;
49
-
50
- const fib : number [ ] = [ 1 ] ;
51
-
52
- if ( n === 1 ) return fib ;
46
+ function getFirstNFibonacciNumbers ( n : number ) : number [ ] {
47
+ let fib : number [ ] = [ ] ;
53
48
54
- fib . push ( 1 ) ; // Add the second 1
55
-
56
- for ( let i = 2 ; i < n ; i ++ ) {
57
- const next = fib [ i - 1 ] + fib [ i - 2 ] ;
58
- fib . push ( next ) ;
49
+ for ( let i = 0 ; i < n ; i ++ ) {
50
+ if ( i === 0 || i === 1 ) {
51
+ fib . push ( 1 ) ;
52
+ } else {
53
+ fib . push ( fib [ i - 1 ] + fib [ i - 2 ] ) ;
54
+ }
59
55
}
60
-
61
- return fib ;
56
+ return fib ;
62
57
}
63
58
64
59
/**
@@ -70,23 +65,16 @@ export function getFirstNFibonacciNumbers(n: number): number[] {
70
65
* @param value The value to look for.
71
66
* @return The index of the value if found in the array and -1 otherwise.
72
67
*/
73
- export function binarySearch (
68
+ function findValue (
74
69
values : number [ ] ,
75
70
start : number ,
76
71
end : number ,
77
- value : number ,
72
+ value : number
78
73
) : number {
79
- if ( end < start ) {
80
- return - 1 ; // Base case: value not found
81
- }
82
-
83
- const pivotIndex = Math . floor ( ( start + end ) / 2 ) ;
84
-
85
- if ( values [ pivotIndex ] === value ) {
86
- return pivotIndex ;
87
- } else if ( values [ pivotIndex ] > value ) {
88
- return binarySearch ( values , start , pivotIndex - 1 , value ) ;
89
- } else {
90
- return binarySearch ( values , pivotIndex + 1 , end , value ) ;
74
+ for ( let i + start ; i <= end ; i ++ ) {
75
+ if ( values [ i ] === value ) {
76
+ return i ;
77
+ }
91
78
}
79
+ return - 1 ;
92
80
}
0 commit comments