|
1 |
| -/** |
2 |
| - * Write a function that takes a single character as an argument and |
3 |
| - * returns boolean value true if the character is an uppercase letter. |
4 |
| - * |
5 |
| - * @param char |
6 |
| - * @returns |
7 |
| - */ |
8 |
| -export function isUppercase(char: string): boolean { |
9 |
| - return false; |
10 |
| -} |
| 1 | +import { computeLexicographicDistance } from "./util.js"; |
11 | 2 |
|
12 | 3 | /**
|
13 |
| - * Determine if a person is eligible for a driving license (age and test passed). |
| 4 | + * Compares two strings lexicographically. |
14 | 5 | *
|
15 |
| - * @param age |
16 |
| - * @param passedTest |
17 |
| - * @returns |
| 6 | + * @param a The first `string` to compare. |
| 7 | + * @param b The second `string` to compare. |
| 8 | + * @return -1 if a is less than b, 1 if a is greater than b, and 0 otherwise. |
18 | 9 | */
|
19 |
| -export function canGetDriverLicense(age: number, passedTest: boolean): boolean { |
20 |
| - return false; |
21 |
| -} |
| 10 | +export function compareStrings(a: string, b: string): number { |
| 11 | + // The distance will be a number less than 0 if string `a` is lexicographically less than `b`, 1 |
| 12 | + // if it is greater, and 0 if the strings are equal. |
| 13 | + const distance = computeLexicographicDistance(a, b); |
22 | 14 |
|
23 |
| -/** |
24 |
| - * Check if a store is open based on the day and time. The store is open |
25 |
| - * Monday to Saturday from 9 AM to 9 PM. |
26 |
| - * |
27 |
| - * @param day |
28 |
| - * @param hour |
29 |
| - * @returns |
30 |
| - */ |
31 |
| -export function isStoreOpen(day: string, hour: number): boolean { |
32 |
| - return false; |
| 15 | + if (distance < 0) { |
| 16 | + return -1; |
| 17 | + } else if (distance > 0) { |
| 18 | + return 1; |
| 19 | + } else { |
| 20 | + return 0; |
| 21 | + } |
33 | 22 | }
|
| 23 | + /** |
| 24 | + * Computes the factorial of the given value of `n`. |
| 25 | + * |
| 26 | + * @param n The value for which to compute the factorial. |
| 27 | + * @return The factorial of n. |
| 28 | + */ |
| 29 | + export function computeFactorial(n: number): number { |
| 30 | + if (n === 0 || n === 1) { |
| 31 | + return 1; // Base case: 0! and 1! both equal 1 |
| 32 | + } |
| 33 | + return n * computeFactorial(n - 1); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Returns an array of the first `n` Fibonacci numbers starting from 1. |
| 38 | + * |
| 39 | + * @param n The first `n` of Fibonacci values to compute. |
| 40 | + * @return An array containing the first `n` Fibonacci values. |
| 41 | + */ |
| 42 | + export function getFirstNFibonacciNumbers(n: number): number[] { |
| 43 | + if (n <= 0) { |
| 44 | + return []; |
| 45 | + } |
| 46 | + else if (n === 1) { |
| 47 | + return [1]; |
| 48 | + } |
| 49 | + return []; |
| 50 | + } |
| 51 | + /** |
| 52 | + * Finds a value in an array of values. |
| 53 | + * |
| 54 | + * @param values The values to search. |
| 55 | + * @param start The left most index to search. |
| 56 | + * @param end The right most index to search. |
| 57 | + * @param value The value to look for. |
| 58 | + * @return The index of the value if found in the array and -1 otherwise. |
| 59 | + */ |
| 60 | + export function binarySearch( |
| 61 | + values: number[], |
| 62 | + start: number, |
| 63 | + end: number, |
| 64 | + value: number, |
| 65 | + ): number { |
| 66 | + if (end < start) { |
| 67 | + // The range is not valid so just return -1. |
| 68 | + return -1; |
| 69 | + } |
| 70 | + |
| 71 | + const pivotIndex = Math.floor((start + end) / 2); // The index in the middle of the array. |
| 72 | + if (values[pivotIndex] === value) { |
| 73 | + return pivotIndex; |
| 74 | + } else if (values[pivotIndex] > value) { |
| 75 | + return binarySearch(values, start, pivotIndex - 1, value); |
| 76 | + } else { |
| 77 | + return binarySearch(values, pivotIndex + 1, end, value); |
| 78 | + } |
| 79 | + |
| 80 | + // If values[pivotIndex] is equal to value then return `pivotIndex`. |
| 81 | + // Else if values[pivotIndex] is greater than the value, then |
| 82 | + // call `binarySearch(values, start, pivotIndex - 1, value)` and return its value; |
| 83 | + // Else call `binarySearch(values, pivotIndex + 1, end, value)` and return its value. |
| 84 | + } |
0 commit comments