Skip to content

Commit 9351838

Browse files
committed
feat: justin lesson 07
1 parent 872b4b0 commit 9351838

File tree

3 files changed

+125
-36
lines changed

3 files changed

+125
-36
lines changed

lesson_07/conditionals/.env.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
HW_VERSION=your homework version here
1+
HW_VERSION=E

lesson_07/conditionals/src/lesson7.ts

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,33 @@ import { computeLexicographicDistance } from "./util.js";
1010
export function compareStrings(a: string, b: string): number {
1111
// The distance will be a number less than 0 if string `a` is lexicographically less than `b`, 1
1212
// if it is greater, and 0 if the strings are equal.
13-
const distance = computeLexicographicDistance(a, b);
13+
const distance = computeLexicographicDistance(a, b);
1414

15-
// TODO(you): Finish this method.
16-
17-
return 0;
15+
if (distance < 0) {
16+
return -1;
17+
} else if (distance > 0) {
18+
return 1;
19+
} else {
20+
return 0;
21+
}
1822
}
19-
2023
/**
2124
* Computes the factorial of the given value of `n`.
2225
*
2326
* @param n The value for which to compute the factorial.
2427
* @return The factorial of n.
2528
*/
2629
export function computeFactorial(n: number): number {
27-
return 0;
30+
if (n === 0 ) {
31+
return 1; // Base case: 0! and 1! both equal 1
32+
}
33+
else if (n === 1) {
34+
return 1;
35+
}
36+
else if (n < 0) {
37+
return 0;
38+
}
39+
return n * computeFactorial(n - 1);
2840
}
2941

3042
/**
@@ -34,6 +46,22 @@ export function computeFactorial(n: number): number {
3446
* @return An array containing the first `n` Fibonacci values.
3547
*/
3648
export function getFirstNFibonacciNumbers(n: number): number[] {
49+
for (let i = 0; i < n; i++) {
50+
if (n === 0) {
51+
return [];
52+
}
53+
else if (n === 1) {
54+
return [1];
55+
}
56+
else if (n === 2) {
57+
return [1, 1];
58+
}
59+
else {
60+
const fib = getFirstNFibonacciNumbers(n - 1);
61+
fib.push(fib[fib.length - 1] + fib[fib.length - 2]);
62+
return fib;
63+
}
64+
}
3765
return [];
3866
}
3967

@@ -57,9 +85,19 @@ export function binarySearch(
5785
return -1;
5886
}
5987

60-
const pivotIndex = Math.floor((start + end) / 2); // The index in the middle of the array.
6188

62-
// TODO(you): Finish implementing this algorithm
89+
const pivotIndex = Math.floor((start + end) / 2); // The index in the middle of the array.
90+
if (values[pivotIndex] === value) {
91+
return pivotIndex;
92+
} else if (values[pivotIndex] > value) {
93+
return binarySearch(values, start, pivotIndex - 1, value);
94+
} else {
95+
return binarySearch(values, pivotIndex + 1, end, value);
96+
return -1;
97+
}
98+
99+
100+
63101

64102
// If values[pivotIndex] is equal to value then return `pivotIndex`.
65103
// Else if values[pivotIndex] is greater than the value, then

lesson_07/conditionals/src/part_e.ts

Lines changed: 78 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,84 @@
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";
112

123
/**
13-
* Determine if a person is eligible for a driving license (age and test passed).
4+
* Compares two strings lexicographically.
145
*
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.
189
*/
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);
2214

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+
}
3322
}
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

Comments
 (0)