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..73dafdc70 100644 --- a/lesson_07/conditionals/src/lesson7.ts +++ b/lesson_07/conditionals/src/lesson7.ts @@ -1,69 +1,105 @@ import { computeLexicographicDistance } from "./util.js"; + /** - * Compares two strings lexicographically. - * - * @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. - */ +* Compares two strings lexicographically. +* +* @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 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); + // 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); - // TODO(you): Finish this method. - return 0; + // TODO(you): Finish this method. + 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. - */ +* 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 { - return 0; + if (n < 0) { + return 0; + } else if (n === 0) { + return 1; + } + let ans = 1; + for (let i = 1; i <= n; i++) { + ans *= i; + } + return ans; } + /** - * 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. - */ +* 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[] { - return []; + const fib = []; + for (let i = 0; i < 2 && i < n; i++) { + fib.push(1); + } + for (let i = 2; i < n; i++) { + fib.push(fib[i - 1] + fib[i - 2]); + } + return fib; } + /** - * 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. - */ +* 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, + values: number[], + start: number, + end: number, + value: number, ): number { - if (end < start) { - // The range is not valid so just return -1. - return -1; - } + 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. + + + // TODO(you): Finish implementing this algorithm + - const pivotIndex = Math.floor((start + end) / 2); // The index in the middle of the array. + // 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. - // TODO(you): Finish implementing this algorithm - // 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. - return -1; + const mid = values[pivotIndex]; + if (mid > value){return binarySearch(values, start, pivotIndex - 1, value) + } + else if (mid < value){ + + return binarySearch(values, pivotIndex + 1, end, value) + } + else{ + return pivotIndex + } } diff --git a/lesson_07/conditionals/src/part_e.ts b/lesson_07/conditionals/src/part_e.ts index d25631c23..5f3ca17b9 100644 --- a/lesson_07/conditionals/src/part_e.ts +++ b/lesson_07/conditionals/src/part_e.ts @@ -1,33 +1,35 @@ /** - * 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 - */ +* 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; + return (char.toUpperCase() === char && /[a-zA-Z]/.test(char)); } + /** - * Determine if a person is eligible for a driving license (age and test passed). - * - * @param age - * @param passedTest - * @returns - */ +* 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 { - return false; + return age >= 18 && passedTest; } + /** - * 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 - */ +* 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; + return day.toLowerCase() !== "sunday" && (hour >=9 && hour <21); }