Registry Synced

Jan 2026 - Python - Week 10 - GrPA 2

800 words
4 min read

Graded Assignment

Course: Jan 2026 - Python
GrPA 2
Submission deadline has passed for this assignment
Due Apr 22, 2026 at 11:59 PM IST
Instructions
Use "Test Run" to verify your code with public test cases.
Press "Submit" to have your assignment evaluated.
You can submit your assignment multiple times up until the deadline.
Make sure to submit your final code by the deadline to receive your score.
Summary
100 out of100
Score
Public Tests
2/2 Passed
Submitted on Apr 22, 2026 at 7:11 PM IST
Private Tests
5/5 Passed
Submitted on Apr 22, 2026 at 7:11 PM IST

Create a class named StringManipulation that has the following specification:
Attributes
words: list of strings, all of which will be in lower case
Methods
(1) __init__: accept a list words as argument and assign it to the corresponding attribute (2) total_words: return the total number of words in words (3) count: accept an argument named some_word and return the number of times this word occurs in the list words (4) words_of_length: accept a positive integer length as argument and return a list of all the words in the list words that have a length equal to length (5) words_start_with: accept a character char as argument and return the list of all the words in words that start with char (6) longest_word: return the longest word in the list words; if there are multiple words that satisfy this condition, return the first such occurrence (7) palindromes: return a list of all the words that are palindromes in words

(1) For those methods where you are expected to return a list of words, make sure that the words in the returned list appear in the order in which they are present in the attribute words.
(2) Each test case corresponds to one or more method calls. We will use S to denote the name of the object.
(3) You do not have to accept input from the user or print output to the console. You just have to define the class based on the specifications given in the question.

Public Tests ( 2/2 )

Case 1

Input:
text
public
StringManipulation(['a', 'an', 'the', 'mom', 'that'])
S.words_of_length(3)
Expected Output:
text
['the', 'mom']
Actual Output:
text
['the', 'mom']

Case 2

Input:
text
public
StringManipulation(['a', 'mom', 'the', 'an'])
S.palindromes()
Expected Output:
text
['a', 'mom']
Actual Output:
text
['a', 'mom']

Private Tests ( 5/5 )

Case 1

Input:
text
private
StringManipulation(['this', 'wonderful', 'good', 'to', 'see', 'you'])
S.total_words()
Expected Output:
text
6

You have defined the class correctly.
Actual Output:
text
6

You have defined the class correctly.

Case 2

Input:
text
private
StringManipulation(['this', 'wonderful', 'good', 'to', 'see', 'you'])
S.longest_word()
Expected Output:
text
wonderful

You have defined the class correctly.
Actual Output:
text
wonderful

You have defined the class correctly.

Case 3

Input:
text
private
StringManipulation(['the', 'this', 'though', 'thou', 'many', 'mom', 'malayalam', 'manorama', 'was', 'wisdom'])
S.words_start_with('m')
Expected Output:
text
['many', 'mom', 'malayalam', 'manorama']

You have defined the class correctly.
Actual Output:
text
['many', 'mom', 'malayalam', 'manorama']

You have defined the class correctly.

Case 4

Input:
text
private
StringManipulation(['the', 'this', 'though', 'thou', 'many', 'mom', 'malayalam', 'manorama', 'was', 'wisdom'])
S.palindromes()
Expected Output:
text
['mom', 'malayalam']

You have defined the class correctly.
Actual Output:
text
['mom', 'malayalam']

You have defined the class correctly.

Case 5

Input:
text
private
StringManipulation(['a', 'the', 'a', 'on', 'a', 'many'])
S.count('a')
Expected Output:
text
3

You have defined the class correctly.
Actual Output:
text
3

You have defined the class correctly.

💻 IITM Official Solution

python
class StringManipulation:
    def __init__(self, words):
        self.words = words
    def total_words(self):
        return len(self.words)
    def count(self, some_word):
        n = 0
        for word in self.words:
            if word == some_word:
                n += 1
        return n
    def words_of_length(self, length):
        L = [ ]
        for word in self.words:
            if len(word) == length:
                L.append(word)
        return L
    def words_start_with(self, char):
        L = [ ]
        for word in self.words:
            if word[0] == char:
                L.append(word)
        return L
    def longest_word(self):
        maxword = self.words[0]
        for word in self.words:
            if len(word) > len(maxword):
                maxword = word
        return maxword
    def palindromes(self):
        L = [ ]
        for word in self.words:
            reverse = ''
            for char in word:
                reverse = char + reverse
            if word == reverse:
                L.append(word)
        return L


💻 My Submitted Code

python
class StringManipulation:
    def __init__(self, words):
        self.words = words
    def total_words(self):
        return len(self.words)
    def count(self, some_word):
        return self.words.count(some_word)
    def words_of_length(self, length):
        return [w for w in self.words if len(w) == length]
    def words_start_with(self, char):
        return [w for w in self.words if w.startswith(char)]
    def longest_word(self):
        return max(self.words, key=len)
    def palindromes(self):
        return [w for w in self.words if w == w[::-1]]

Document Outline
Table of Contents
System Normal // Awaiting Context

Intelligence Hub

Navigate the knowledge graph to generate context. The Hub adapts dynamically to surface backlinks, related notes, and metadata insights.