Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lesson_06/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion lesson_06/expression/.env.test
Original file line number Diff line number Diff line change
@@ -1 +1 @@
HW_VERSION=your assigned version here
HW_VERSION=E
75 changes: 75 additions & 0 deletions lesson_06/expression/src/abbreviation.ts
Original file line number Diff line number Diff line change
@@ -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
20 changes: 17 additions & 3 deletions lesson_06/expression/src/expression_calculator.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
}