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

A start up program is provided, which has a “MyString” class. This class has one

ID: 3818078 • Letter: A

Question

A start up program is provided, which has a “MyString” class. This class has one data attribute “__myString”, and two methods.

“countWord” method calculates how many words in “__myString”

“findMostFrequentChar” method finds the most frequent alphabetic character in “__myString” (case insensitive)

Main method is already provided. You need to implement “countWord” method and “findMostFrequentChar” method in “MyString” class.

For Reference:

class MyString:


  
def __init__(self, myString):
self.__myString = myString
  

def countWord(self):
#needs implementation

def findMostFrequentChar(self):
#needs implementation (case insensitive)

def main():
aString = MyString("This is a super long long long string. Please help count me")
print("There are", aString.countWord(), "words in the string.")
count, letter = aString.findMostFrequentChar()
print("The most frequent character is", letter, "which appeared", count, "times")

main()

  

Explanation / Answer

******** using collections. the program would also count ' ' (blank) as it is a chacter. used in method "findMostFrequentChar(self)" ************

import collections

class MyString:

  
def __init__(self, myString):
self.myString = myString
  
def countWord(self):
   return len(self.myString.split()) #return number of words in a sentence
## return len(nltk.word_tokenize(self.myString)) ## second way.

def findMostFrequentChar(self):
   maximum_letter = self.myString[0]
minimum_letter = self.myString[0]
maximum = self.myString.count(self.myString[0])
   minimum = self.myString.count(self.myString[0])
    for char in self.myString:
   if self.myString.count(char) > maximum:
   maximum_letter = char
   maximum = self.myString.count(char)
   return maximum, maximum_letter
  
def main():
aString = MyString("This is a super long long long string. Please help count me")
print("There are", aString.countWord(), "words in the string.")
count, letter = aString.findMostFrequentChar()
print("The most frequent character is", letter, "which appeared", count, "times")

main()

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