Asked Dec 8, 2019

Find the most frequent character in a sentence and print the letter and then the frequency.

, not just ['e' , 5]


Expert Answer

Step 1

The python program and approach are given below:


  • Defining the value of size variable as per the ASCII table.
  • Defining the function mostfrequentchar(s) which returns the most frequent string and its count.
  • Declaring the variables as per the requirement (use of each variable is mentioned as comments in the program).
  • First for loop will traverse the string and update the list with the ASCII values + 1 at every character.
  • Second, for loop will traverse the string and if the statement will check whether the value of num[ord(x)] is greater than If max is less than the value of max will update.
  • Most frequent character in the string will store in the variable “y”.
  • Returns y and max.
  • Displays the result by calling the function "mostfrequentchar()" inside the print() function.    
Step 2


#setting the limit as per the ASCII table 

size = 128

#defining the function which returns

#most frequent character and its count

def mostfrequentchar(s):

    #setting the size of list

    num = [0] * size

    #to store the count of most frequent character

    max = -1

    #empty string

    y = ''

    #list is update with the ASCII values + 1 at every character

    for x in s:


    #traversing the string

    for x in s:

        #if the value of max is greater than num[ord(x)]

        #than store it t...

Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.
Tagged in


Computer Science


Related Computer Science Q&A

Find answers to questions asked by student like you
Show more Q&A

Q: Simplify the following Boolean function , using three- variable maps: a) F(x,y,z) = Σ (0,2,6,7) b) F...

A: Note: Since we only answer up to 3 sub-parts, we’ll answer the first 3. Please resubmit the question...


Q: A class Climber has been defined, and the variable cat refers to an instance of Climber. Call the me...

A: Method calling in pythonIn python, a class climber has been defined. Variable cat refers to an insta...


Q: a. Explain what the CPU should do when an interrupt occurs. Include in your answer the method the CP...

A: Since we only answer up to 3 sub-parts, we’ll answer the first 3. Please resubmit the question and s...


Q: |(1st part) You are going to draw flowers a certain integral locations on a wall in your room. You h...

A: Program Instructions: In main method, call the function draw with length and location array.In funct...


Q: Python Coding-While Loops  (In PyCharm Community edition)   Create a while loop that SUMS up ALL the...

A: Prompt user to enter initial numberPrompt user to enter ending numberUsing while loop sums up all th...


Q: write a c++ program that sssign numItems' address to numItemsPtr, then print the shown text followed...

A: Program Code:


Q: Write some MYSQL code to select three different sets of data and display them on the screen. Also, d...

A: Creating Table and Inserting then values into it-The Create Table statement is used to create a tabl...


Q: Let's say we have the attached table :   Find the year in which employees hired with minimum salary ...

A: Answer:Select HIREDATE from EMP where SAL > 1000;Here, the above query will display the employees...


Q: How do I creat a switch statement in java for directions? Ex. North, South, East, West.

A: Switch statement: In Java, the switch statement is used to select a single statement out of multiple...