Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Pthon coding please. CSc 120: Classes: Anagrams Expected Behavior Write a Python

ID: 3723587 • Letter: P

Question

Pthon coding please.

CSc 120: Classes: Anagrams Expected Behavior Write a Python class Word that meets the following requirements: Attributes: . _word: the string that makes up the word; Methods: . init(self, word): initializes the_word attribute of the object with the string word supplied as the method's argument. . str_(self): returns the value of the object's_word attribute with all upper-case letters converted to lower-case. . A method that takes a Word object other as argument and returns True if other is an anagram of the word, False otherwise. Given two Word objects wi and w2, it should be possible to determine whether they are anagrams of each other (ignoring upper/lower case differences) using the expression Note: A word can need not contain just letters. For example, "Hi there! - is a legal word.

Explanation / Answer

class Word:
#constructor to set the attribute
def __init__(self, word):
self._word = word

def __str__(self):
return self._word.lower()

def __eq__(self, other):
if isinstance(other, Word):
return sorted(self._word.lower()) == sorted(other._word.lower())
return False

def is_anagram(self, other):
if isinstance(other, Word):
return self == other
return False
  
word1 = Word('post')
word2 = Word('stop')
print word1 == word2

word1 = Word('')
word2 = Word('')
print word1 == word2

word1 = Word('aBlE')
print str(word1)

word1 = Word('able')
word2 = Word('baker')
print word1 == word2

word1 = Word('Hi there! :-)')
word2 = Word('Hit here! :-)')
print word1 == word2

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote