From 9351838add6fe9d21834d7f329742d501d6fa6fb Mon Sep 17 00:00:00 2001 From: JEKLUND251 Date: Mon, 24 Mar 2025 20:37:54 +0000 Subject: [PATCH 1/2] feat: justin lesson 07 --- lesson_07/conditionals/.env.test | 2 +- lesson_07/conditionals/src/lesson7.ts | 54 +++++++++++-- lesson_07/conditionals/src/part_e.ts | 105 +++++++++++++++++++------- 3 files changed, 125 insertions(+), 36 deletions(-) diff --git a/lesson_07/conditionals/.env.test b/lesson_07/conditionals/.env.test index de8c44249..9fd9f468b 100644 --- a/lesson_07/conditionals/.env.test +++ b/lesson_07/conditionals/.env.test @@ -1 +1 @@ -HW_VERSION=your homework version here \ No newline at end of file +HW_VERSION=E \ No newline at end of file diff --git a/lesson_07/conditionals/src/lesson7.ts b/lesson_07/conditionals/src/lesson7.ts index cc44be44c..915f35e44 100644 --- a/lesson_07/conditionals/src/lesson7.ts +++ b/lesson_07/conditionals/src/lesson7.ts @@ -10,13 +10,16 @@ import { computeLexicographicDistance } from "./util.js"; export function compareStrings(a: string, b: string): number { // The distance will be a number less than 0 if string `a` is lexicographically less than `b`, 1 // if it is greater, and 0 if the strings are equal. - const distance = computeLexicographicDistance(a, b); + const distance = computeLexicographicDistance(a, b); - // TODO(you): Finish this method. - - return 0; + if (distance < 0) { + return -1; + } else if (distance > 0) { + return 1; + } else { + return 0; +} } - /** * Computes the factorial of the given value of `n`. * @@ -24,7 +27,16 @@ export function compareStrings(a: string, b: string): number { * @return The factorial of n. */ export function computeFactorial(n: number): number { - return 0; + if (n === 0 ) { + return 1; // Base case: 0! and 1! both equal 1 + } + else if (n === 1) { + return 1; + } + else if (n < 0) { + return 0; + } + return n * computeFactorial(n - 1); } /** @@ -34,6 +46,22 @@ export function computeFactorial(n: number): number { * @return An array containing the first `n` Fibonacci values. */ export function getFirstNFibonacciNumbers(n: number): number[] { + for (let i = 0; i < n; i++) { + if (n === 0) { + return []; + } + else if (n === 1) { + return [1]; + } + else if (n === 2) { + return [1, 1]; + } + else { + const fib = getFirstNFibonacciNumbers(n - 1); + fib.push(fib[fib.length - 1] + fib[fib.length - 2]); + return fib; + } + } return []; } @@ -57,9 +85,19 @@ export function binarySearch( return -1; } - const pivotIndex = Math.floor((start + end) / 2); // The index in the middle of the array. - // TODO(you): Finish implementing this algorithm + const pivotIndex = Math.floor((start + end) / 2); // The index in the middle of the array. + if (values[pivotIndex] === value) { + return pivotIndex; + } else if (values[pivotIndex] > value) { + return binarySearch(values, start, pivotIndex - 1, value); + } else { + return binarySearch(values, pivotIndex + 1, end, value); + return -1; + } + + + // If values[pivotIndex] is equal to value then return `pivotIndex`. // Else if values[pivotIndex] is greater than the value, then diff --git a/lesson_07/conditionals/src/part_e.ts b/lesson_07/conditionals/src/part_e.ts index d25631c23..9b3a69305 100644 --- a/lesson_07/conditionals/src/part_e.ts +++ b/lesson_07/conditionals/src/part_e.ts @@ -1,33 +1,84 @@ -/** - * Write a function that takes a single character as an argument and - * returns boolean value true if the character is an uppercase letter. - * - * @param char - * @returns - */ -export function isUppercase(char: string): boolean { - return false; -} +import { computeLexicographicDistance } from "./util.js"; /** - * Determine if a person is eligible for a driving license (age and test passed). + * Compares two strings lexicographically. * - * @param age - * @param passedTest - * @returns + * @param a The first `string` to compare. + * @param b The second `string` to compare. + * @return -1 if a is less than b, 1 if a is greater than b, and 0 otherwise. */ -export function canGetDriverLicense(age: number, passedTest: boolean): boolean { - return false; -} +export function compareStrings(a: string, b: string): number { + // The distance will be a number less than 0 if string `a` is lexicographically less than `b`, 1 + // if it is greater, and 0 if the strings are equal. + const distance = computeLexicographicDistance(a, b); -/** - * Check if a store is open based on the day and time. The store is open - * Monday to Saturday from 9 AM to 9 PM. - * - * @param day - * @param hour - * @returns - */ -export function isStoreOpen(day: string, hour: number): boolean { - return false; + if (distance < 0) { + return -1; + } else if (distance > 0) { + return 1; + } else { + return 0; + } } + /** + * Computes the factorial of the given value of `n`. + * + * @param n The value for which to compute the factorial. + * @return The factorial of n. + */ + export function computeFactorial(n: number): number { + if (n === 0 || n === 1) { + return 1; // Base case: 0! and 1! both equal 1 + } + return n * computeFactorial(n - 1); + } + + /** + * Returns an array of the first `n` Fibonacci numbers starting from 1. + * + * @param n The first `n` of Fibonacci values to compute. + * @return An array containing the first `n` Fibonacci values. + */ + export function getFirstNFibonacciNumbers(n: number): number[] { + if (n <= 0) { + return []; + } + else if (n === 1) { + return [1]; + } + return []; + } + /** + * Finds a value in an array of values. + * + * @param values The values to search. + * @param start The left most index to search. + * @param end The right most index to search. + * @param value The value to look for. + * @return The index of the value if found in the array and -1 otherwise. + */ + export function binarySearch( + values: number[], + start: number, + end: number, + value: number, + ): number { + if (end < start) { + // The range is not valid so just return -1. + return -1; + } + + const pivotIndex = Math.floor((start + end) / 2); // The index in the middle of the array. + if (values[pivotIndex] === value) { + return pivotIndex; + } else if (values[pivotIndex] > value) { + return binarySearch(values, start, pivotIndex - 1, value); + } else { + return binarySearch(values, pivotIndex + 1, end, value); + } + + // If values[pivotIndex] is equal to value then return `pivotIndex`. + // Else if values[pivotIndex] is greater than the value, then + // call `binarySearch(values, start, pivotIndex - 1, value)` and return its value; + // Else call `binarySearch(values, pivotIndex + 1, end, value)` and return its value. + } From 7de5ed86dd7b5645d2b3339a47f648c0f898281e Mon Sep 17 00:00:00 2001 From: JEKLUND251 Date: Tue, 25 Mar 2025 13:07:04 +0000 Subject: [PATCH 2/2] readded work into lesson 7 --- lesson_07/conditionals/src/part_e.ts | 112 +++++++++------------------ 1 file changed, 36 insertions(+), 76 deletions(-) diff --git a/lesson_07/conditionals/src/part_e.ts b/lesson_07/conditionals/src/part_e.ts index 9b3a69305..76e7f7a41 100644 --- a/lesson_07/conditionals/src/part_e.ts +++ b/lesson_07/conditionals/src/part_e.ts @@ -1,84 +1,44 @@ -import { computeLexicographicDistance } from "./util.js"; - /** - * Compares two strings lexicographically. + * Write a function that takes a single character as an argument and + * returns boolean value true if the character is an uppercase letter. * - * @param a The first `string` to compare. - * @param b The second `string` to compare. - * @return -1 if a is less than b, 1 if a is greater than b, and 0 otherwise. + * @param char + * @returns */ -export function compareStrings(a: string, b: string): number { - // The distance will be a number less than 0 if string `a` is lexicographically less than `b`, 1 - // if it is greater, and 0 if the strings are equal. - const distance = computeLexicographicDistance(a, b); - - if (distance < 0) { - return -1; - } else if (distance > 0) { - return 1; - } else { - return 0; - } -} - /** - * Computes the factorial of the given value of `n`. - * - * @param n The value for which to compute the factorial. - * @return The factorial of n. - */ - export function computeFactorial(n: number): number { - if (n === 0 || n === 1) { - return 1; // Base case: 0! and 1! both equal 1 - } - return n * computeFactorial(n - 1); +export function isUppercase(char: string): boolean { + if (char >= "A" && char <= "Z") { + return true; } - /** - * Returns an array of the first `n` Fibonacci numbers starting from 1. - * - * @param n The first `n` of Fibonacci values to compute. - * @return An array containing the first `n` Fibonacci values. - */ - export function getFirstNFibonacciNumbers(n: number): number[] { - if (n <= 0) { - return []; - } - else if (n === 1) { - return [1]; - } - return []; - } - /** - * Finds a value in an array of values. - * - * @param values The values to search. - * @param start The left most index to search. - * @param end The right most index to search. - * @param value The value to look for. - * @return The index of the value if found in the array and -1 otherwise. - */ - export function binarySearch( - values: number[], - start: number, - end: number, - value: number, - ): number { - if (end < start) { - // The range is not valid so just return -1. - return -1; - } + return false; +} - const pivotIndex = Math.floor((start + end) / 2); // The index in the middle of the array. - if (values[pivotIndex] === value) { - return pivotIndex; - } else if (values[pivotIndex] > value) { - return binarySearch(values, start, pivotIndex - 1, value); - } else { - return binarySearch(values, pivotIndex + 1, end, value); - } +/** + * Determine if a person is eligible for a driving license (age and test passed). + * + * @param age + * @param passedTest + * @returns + */ +export function canGetDriverLicense(age: number, passedTest: boolean): boolean { + if (age >= 18 && passedTest) { + return true; + } + return false; +} - // If values[pivotIndex] is equal to value then return `pivotIndex`. - // Else if values[pivotIndex] is greater than the value, then - // call `binarySearch(values, start, pivotIndex - 1, value)` and return its value; - // Else call `binarySearch(values, pivotIndex + 1, end, value)` and return its value. +/** + * Check if a store is open based on the day and time. The store is open + * Monday to Saturday from 9 AM to 9 PM. + * + * @param day + * @param hour + * @returns + */ +export function isStoreOpen(day: string, hour: number): boolean { + if (day === "Sunday" || hour < 9 || hour >= 21) { + return false; + } else { + return true; } +}