diff --git a/lesson_06/README.md b/lesson_06/README.md index 2dd23c143..f696f7e5e 100644 --- a/lesson_06/README.md +++ b/lesson_06/README.md @@ -58,7 +58,7 @@ function isValidAlphaAbbreviation(word: string, abbr: string): boolean Example 1: ``` -Input: word = "internationalization", abbr = "irzdn" +Input: word = "internationalization", abbr = "imzdn" Output: true Explanation: - "internationalization" can be abbreviated as "i m z d n" diff --git a/lesson_06/expression/.env.test b/lesson_06/expression/.env.test index 56cf4a06f..9fd9f468b 100644 --- a/lesson_06/expression/.env.test +++ b/lesson_06/expression/.env.test @@ -1 +1 @@ -HW_VERSION=your assigned version here \ No newline at end of file +HW_VERSION=E \ No newline at end of file diff --git a/lesson_06/expression/src/abbreviation.ts b/lesson_06/expression/src/abbreviation.ts new file mode 100644 index 000000000..d8b2d0d18 --- /dev/null +++ b/lesson_06/expression/src/abbreviation.ts @@ -0,0 +1,75 @@ +/** + * Utility class for checking alphabet-based abbreviations. + * + * An abbreviation is considered valid if: + * - It starts and ends with the same character as the word. + * - It correctly skips letters based on alphabetical offsets. + */ +export class Abbreviation { + /** + * Determines whether a given abbreviation is valid for a given word. + * + * The abbreviation alternates between: + * - a "number" step: an abbreviation character represents a skip of positions in the word. + * - a "letter" step: the abbreviation character must directly match the word. + * + * @param {string} word - The full word to validate against. + * @param {string} abbr - The abbreviation to check. + * @return {boolean} True if the abbreviation is valid for the word, false otherwise. + */ + isValidAlphaAbbreviation(word: string, abbr: string): boolean { + if (word.length < abbr.length) { + return false; + } + + // First and last characters must match. + if ( + word[0] !== abbr[0] || + word[word.length - 1] !== abbr[abbr.length - 1] + ) { + return false; + } + + let num = true; + let wordIndex = 1; + let abbrIndex = 1; + + while (wordIndex < word.length && abbrIndex < abbr.length) { + // If num is true, treat the abbreviation character as a number. + // If num is false, treat it as a letter. + if (num) { + // Interpret abbreviation character as a skip. + const skip = abbr[abbrIndex].charCodeAt(0) - "a".charCodeAt(0) + 1; + const newWordIndex = wordIndex + skip; + // If out of bounds, check if it matches the last character. + if (newWordIndex >= word.length) { + if (word[wordIndex] !== abbr[abbrIndex]) { + return false; + } + wordIndex++; + } else { + wordIndex = newWordIndex; + num = false; + } + abbrIndex++; + } else { + // Match letters if num isn't true. + if (word[wordIndex] !== abbr[abbrIndex]) { + return false; + } + wordIndex++; + abbrIndex++; + num = true; + } + } + + return wordIndex === word.length && abbrIndex === abbr.length; + } +} + +// Example usage: +const abbrChecker = new Abbreviation(); +console.log(abbrChecker.isValidAlphaAbbreviation("internationalization", "irn"),); // true +console.log(abbrChecker.isValidAlphaAbbreviation("internationalization", "imzdn"),); // true +console.log(abbrChecker.isValidAlphaAbbreviation("apple", "abe")); // false +console.log(abbrChecker.isValidAlphaAbbreviation("bags", "bas")); // false diff --git a/lesson_06/expression/src/expression_calculator.ts b/lesson_06/expression/src/expression_calculator.ts index 76b215c42..1d9cab668 100644 --- a/lesson_06/expression/src/expression_calculator.ts +++ b/lesson_06/expression/src/expression_calculator.ts @@ -1,11 +1,25 @@ export class ExpressionCalculator { /** Returns a calculation involving a, b, c, d, and e */ calculate(a: number, b: number, c: number, d: number, e: number): number { - // Implement your code here to return the correct value. - return 0; + return this.multiply(this.pow(this.divide(this.add(a,b), c), d),e); } + add(a: number, b: number): number { + return a + b; + } + subtract(a: number, b: number): number { + return a - b; + } + multiply(a: number, b: number): number { + return a * b; + } + divide(a: number, b: number): number { + if (b === 0) { + throw new Error("Division by zero is not allowed."); + } + return a / b; + } pow(base: number, exponent: number): number { return Math.pow(base, exponent); } -} +} \ No newline at end of file