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/expression/.env.test
Original file line number Diff line number Diff line change
@@ -1 +1 @@
HW_VERSION=your assigned version here
HW_VERSION=A
94 changes: 94 additions & 0 deletions lesson_06/expression/src/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# Stretch Assignment

```typescript
/**
* Validates whether a given alphabetic abbreviation matches a word using a special encoding system.
*
* In this system:
* - Numbers in abbreviations are replaced by their corresponding alphabet letters (a=1, b=2, ..., z=26)
* - The abbreviation follows the same rules as standard abbreviations but uses letters instead of digits
* - Letters cannot have leading zeros (e.g., no 'aa' for 01)
* - Adjacent abbreviated substrings are not allowed
* - Empty substrings cannot be abbreviated
*
* @param word - The original word to match against (1-25 characters, lowercase English letters)
* @param abbr - The alphabetic abbreviation to validate (1-15 characters, lowercase English letters)
* @returns true if the abbreviation is valid for the given word, false otherwise
*
* @example
* // Example 1: Valid abbreviation
* isValidAlphaAbbreviation("internationalization", "irzdn")
* // Returns: true
* // Explanation: i + (r=18 chars) + z + (d=4 chars) + n = "internationalization"
*/

function isValidAlphaAbbreviation(word: string, abbr: string): boolean {
/**
* Hashmap mapping every letter to its numerical value in the encoding system.
* Used to convert letters to their corresponding skip counts.
* @type {Record<string, number>}
*/
const letterToNumberMap: Record<string, number> = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Next time, consider converting the char code so that you don't have to use extra memory with a dictionary.

a: 1,
b: 2,
c: 3,
d: 4,
e: 5,
f: 6,
g: 7,
h: 8,
i: 9,
j: 10,
k: 11,
l: 12,
m: 13,
n: 14,
o: 15,
p: 16,
q: 17,
r: 18,
s: 19,
t: 20,
u: 21,
v: 22,
w: 23,
x: 24,
y: 25,
z: 26,
};

// Input validation: abbreviation cannot be longer than the original word
if (word.length < abbr.length) {
return false; // Abbreviation cannot be longer than the word
}

// Input validation: both strings must be non-empty
if (word.length == 0 || abbr.length == 0) {
return false; // Both word and abbreviation must be non-empty
}

let wordIndex = 0; // Current position in the original word

// Process each character in the abbreviation
for (
let abbreviationIndex = 0;
abbreviationIndex < abbr.length;
abbreviationIndex++
) {
// Pattern: even indices are literal characters, odd indices are skip counts
if (abbreviationIndex % 2 == 0) {
// Even index: treat as a literal character that must match the word
if (abbr[abbreviationIndex] !== word[wordIndex]) {
return false; // Invalid character in abbreviation
} else {
wordIndex++; // Move to next character in word
}
} else {
// Odd index: treat as a letter representing a number of characters to skip
wordIndex += letterToNumberMap[abbr[abbreviationIndex]];
}
}
// Verify that we've consumed the entire word (no extra characters left)
return wordIndex === word.length; // Ensure we have matched the entire word
}
```
16 changes: 15 additions & 1 deletion lesson_06/expression/src/expression_calculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,24 @@ 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.divide(this.multiply(this.add(a, b), c), this.pow(d, e));
}

pow(base: number, exponent: number): number {
return Math.pow(base, exponent);
}

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 {
return a / b;
}
}