COGNIZANT Coding Question – Solved

6 Live
Stemming is the process of extracting the base word from a word. For instance, the base for "worked" is "work". Use the following algorithm to stem a word: 1. If the word ends in 'ed', 'ly', or 'ing', remove the suffix. 2. If the resulting word is longer than 8 letters, keep the first 8 letters. Implement a function that takes a string of space-separated words and returns its stemmed counterpart. Example: text = 'an extremely dangerous dog is barking' 'an' does not end in one of the suffixes and is less than 8 letters. 'extremely' is 'extreme' after removing the suffix. 'extreme' is less than 8 letters. 'dangerous' is 9 letters long, so reduce it to 8 letters: 'dangerou'. 'dog' and 'is' are unchanged. 'barking' is 'bark' after removing the suffix, and is. Return 'an extreme dangerou dog is bark'. Function Description: Complete the function stemmer in the editor below. stemmer has the following parameter(s): string text: the input text Returns: string: the input text with each of the words replaced by its stem Constraints: Every character in text is either an English lowercase letter or a space character. text starts and ends with a letter. No two consecutive characters are spaces. text contains at most 100 words. No word is longer than 18 letters. Function Input Format Format for Custom Testing Sample Case 0 Sample Input 0 STDIN

Asked in: COGNIZANT

Image of the Question

Question Image Question Image

All Testcases Passed ✔



Passcode Image

Solution


def stemmer(text):
    # Write your code here
    answer_array = []
    words = [word for word in text.split()]
// ... rest of solution available after purchase

🔒 Please login to view the solution

Explanation


```
To solve the problem of stemming words in a given string according to specific rules, start by carefully understanding the requirements and the sequence of transformations that each word must undergo. The process is straightforward, but attention to detail is important to ensure all conditions are applied correctly and in the right order.

Begin by considering the input: a string containing multiple words separated by spaces. Your goal is to process each word individually and apply the stemming rules, then reconstruct the sentence with the transformed words.

Step one is to split the input text into individual words. Since the problem guarantees no leading, trailing, or consecutive spaces, splitting by spaces will reliably isolate each word for processing.

Next, focus on the stemming algorithm itself, which consists of two primary operations applied sequentially to each word:

1. **Suffix Removal:**
Check if the word ends with any of the suffixes "ed", "ly", or "ing". If so, remove that suffix. This step transforms words like "worked" to "work", "extremely" to "extreme", and "barking" to "bark".
When checking suffixes, it's important to consider the order in which you check them. Since "ing" has three characters and "ed" and "ly" have two, starting with the longest suffix first can avoid partial matches or errors.
Also, ensure that you only remove the suffix if it is exactly at the end of the word. Partial matches within the word should not trigger removal.
After removing the suffix, the new word length might change. This can impact the next step.

2. **Length Truncation:**
After suffix removal, examine the length of the resulting word. If the word is longer than 8 letters, truncate it to its first 8 characters. This means keeping only the substring from the beginning up to the eighth character.
This step ensures words like "dangerous" (9 letters) become "dangerou" (first 8 letters).
Note that truncation only happens after suffix removal, never before.

After applying these rules, the stemmed word is ready to be collected.

Repeat the above steps for every word in the original text. Keep track of all the stemmed words in order, maintaining their sequence, so the sentence can be reconstructed correctly.

Finally, join the processed words back into a single string separated by spaces. This final string is the output.

Keep in mind some additional considerations:

- If a word does not end with any of the specified suffixes, leave it unchanged except for checking the length and truncating if necessary.
- Be mindful that removing suffixes could shorten a word to fewer than or equal to 8 letters, in which case truncation won’t be needed.
- Since the input only contains lowercase English letters and spaces, case sensitivity is not a concern.
- The constraints specify a maximum of 100 words and word lengths up to 18 characters, which is manageable with straightforward string operations.
- Pay close attention to indexing and substring extraction to avoid off-by-one errors.
- Edge cases include words that are exactly 8 letters, words shorter than the suffix length, or words where suffix removal might result in an empty string (though the problem constraints imply this might not happen, it’s good to be mindful).
- Ensure the output preserves the order and number of words as in the input, just with their stemmed forms.

By thinking about the problem step-by-step like this, you break down the task into manageable parts: splitting the input, processing each word with clear rules, and then joining the results back together. This structured approach makes the implementation straightforward and less error-prone.
```


Related Questions