TIP102 Unit 1 Session 1 Advanced (Click for link to problem statements)
Understand what the interviewer is asking for by using test cases and questions about the problem.
Q: What is the input to the function?
word
that may contain substrings "t", "i", "gg", or "er" that need to be removed.Q: What is the expected output of the function?
Q: How should the function handle case sensitivity?
Q: What if the string is empty or does not contain any of the specified substrings?
The function tiggerfy()
should take a string word and return a new string with the substrings t, i, gg, and er removed. The function should be case insensitive.
HAPPY CASE
Input: `Trigger`
Expected Output: `r`
Explanation: Removing `t`, `i`, `gg`, and `er` from `Trigger` results in `r`.
Input: `eggplant`
Expected Output: `eplan`
Explanation: Removing `g` from `eggplant` results in `eplan`.
EDGE CASE
Input: `Choir`
Expected Output: `Choir`
Explanation: No specified substrings are present in `Choir`, so it remains unchanged.
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Convert the string to lowercase to ensure case insensitivity, then iteratively replace each specified substring with an empty string.
1. Define the function `tiggerfy(word)`.
2. Convert the `word` to lowercase to handle case insensitivity.
3. Replace the substrings `t`, `i`, `gg`, and `er` with an empty string in the `word`.
4. Return the modified `word`.
⚠️ Common Mistakes
Implement the code to solve the algorithm.
def tiggerfy(word):
# Convert the word to lowercase to handle case insensitivity
word_lower = word.lower()
# Replace the specified substrings with an empty string
word_lower = word_lower.replace('t', '')
word_lower = word_lower.replace('i', '')
word_lower = word_lower.replace('gg', '')
word_lower = word_lower.replace('er', '')
return word_lower