Question: * use python * A startup program is provided, which has a MyString class. This class has one data attribute __myString, and two methods. countWord
*use python * A startup 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 character in __myString (case insensitive)
You need to implement countWord method and findMostFrequentChar method in MyString class
Change findMostFrequentCharacter function and use a dictionary instead?
show indendation **for your reference**
class MyString: def __init__(self, mystring): self.__myString = mystring def countWord(self): contents = open(self.__myString, 'r')# getting info from main contents = contents.read()# reading file, in string format contents = contents.split(" ")# seperating words with spaces return len(contents) # returing length of the content
def findMostFrequentChar(self): contents = open(self.__myString, 'r') # opening file contents = contents.read() contents = contents.upper()# converting to upper case count_char_list = [0 for _ in range(0, 27)]
for char in contents: # Ordinal numbers, converting char into counting position if ord(char) > 64 and ord(char) < 91: count_char_list[ord(char) - 65] += 1 #finding max in the char list char_with_higest_count = count_char_list.index(max(count_char_list)) return (65 + char_with_higest_count)# returning values
if __name__ == "__main__":# comparison try:# try / except block info= input("Please enter anything you like: ") stringFile = open('TextFile.txt','w')# writing info in file stringFile.write(info) stringFile.close() except IOError: print("Error opening/writing file") except Exception as err: print(err) InfoFile = MyString("TextFile.txt")# creating object wordcount = InfoFile.countWord() print("The number of words you entered in",info, "are", wordcount) Fc= InfoFile.findMostFrequentChar() print("The most frequent character is", chr(Fc))
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
