COGNIZANT Coding Question β Solved
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