1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
zubka84 [21]
3 years ago
7

Write a function $\verb#most_common_letter(string)#$ that determines the most commonly occurring letter in the input string. (If

more than one letter is tied, it doesn't matter which one you return.) You should consider upper and lower case as the same letter. For example, $\verb#most_common_letter('This is a test of the function I have written')#$ should return 't', because 't' occurs 7 times, more than any other letter -- it occurs once as 'T' and 6 times as 't'.

Mathematics
1 answer:
likoan [24]3 years ago
6 0

Answer:

I am writing a Python program:

def most_common_letter (string):  #function that takes a string as argument and returns the most commonly occurring letter in string

   string = string.lower()  # converts the string into lower case

   inp_string="".join(string.split())  #splits the string in to a list and joins the elements of the list

   maximum = 0  #sets the value of maximum to 0

   letter = ""  #stores the most commonly occurring letter

   length = len(inp_string)  #returns the length of the input string

   counter = 0  #counts the occurrences of the letter in the string

   for i in range(0, length):  # iterates through the length of string        

       j = 0  #initializes j to 0

       char = inp_string[i]  #  holds letter of input string at index position i

       while length > 0:  # iterates until the length of string exceeds 0

           if (char == inp_string[j]):  # if letter in char is equal to letter at index j of the string

               counter += 1  #adds 1 to the count

           j += 1  #increments j by 1

           length -= 1    #decrements value of length by 1

           if (maximum <= counter):  #if maximum value is less than counter

               maximum = counter  #sets the maximum number of occurrences of a letter to maximum

               letter = char #sets the most occuring letter in string to letter

   return letter  #returns the most commonly occurring letter in the input string

#in order to check if the function works properly use following statement

print(most_common_letter("This is a test of the function I have written")) #calls most_common_letter method by passing a string to it

   

Step-by-step explanation:

The program works as follows:

I will explain this with the help of an example. Suppose the string is:

string = "hello worLd"

first this string is converted to lowercase using lower() method. So the string becomes:

string = "hello world"

Next the string is split into a list using split() method. The string becomes:

['hello', 'world']

Then using join() this string is joined together on the basis of "" empty space

So the string becomes

helloworld

This string is assigned to the inp_string variable. Hence

inp_string = "helloworld"

The value of maximum is initialized to 0 and variable letter is also declared

which holds the most commonly occurring letter in the inp_string

len function is used to get the length of the inp_string

counter is initializes to 0. This counts the number of times the letter occurs in a string

The for loop iterates through the inp_string

Inside the loop the statement char = inp_string[i] sets the letter at the i-th index of inp_string to char.

i is initialized to 0 so inp_string[i] is inp_string[0] which is the first element of the string i.e. "h".

The program control then moves to the while loop. As length>0 so the program moves to the body of while loop which has an if statement:            if (char == inp_string[j]):      

This checks if the letter stored in char is equal to the letter at j-th index of the string. Now as j is initialized to 0. So

if (char == inp_string[0]):   this evaluates to true and value of counter is incremented to 1. Next value of j also incremented to 1 and length of string is decremented to 1 Hence

counter = 1

j = 1

length = 9

Next if (maximum <= counter): condition checks if value of maximum is less than or equal to counter. It is true because maximum=0 and counter =1

So    maximum = counter  assigns counter value to maximum and letter = char assigns char to letter which was initially empty.

  maximum = 1

  letter = 'h'

At occurrence each iteration each letter in a string is counted and the letter that occurs the most in the string is returned by the function. For the above example hello world, letter l appears 3 times in the string and it is the most commonly occurring letter in the input string. So letter "l" is returned by this function. Hence the output of this program is l.

You might be interested in
Help help help.. please
Anestetic [448]

I think it’s everything above 12 not including 12

6 0
4 years ago
Sarah is robbing a jewelry store, and she needs to get to the safe. To bypass the security, she needs to enter the 8-digit code
yawa3891 [41]
3/8 is her probability
5 0
3 years ago
The table below represents the number of math problems Jana completed as a function of the number of minutes since she began doi
andreyandreev [35.5K]

Answer:

Its D It represents a non-linear function because there is not a constant rate of change.

Step-by-step explanation:

I got then answer from Edge

7 0
3 years ago
Read 2 more answers
(4, 3) is a solution to the following system of equations:
sasho [114]
The first answer is true and the second answer is true as for how this actually is just plug in the numbers

x=4, y=3

2(4)=8 8+3=11

4-1=3
4 0
4 years ago
Which information could not be used to show that triangle FGH=IJK by SAS?
daser333 [38]
The answer is option D
7 0
3 years ago
Read 2 more answers
Other questions:
  • What are the zeros for the quadratic equation 2x2 - 9x-5 = 0?
    15·1 answer
  • Which function has a range of<br><br> f(x)≥7
    11·1 answer
  • Please help ASAP! will give brainlist.
    9·1 answer
  • PLEASE PLEASE PLEASE HELP PLEASE!!!!!!!!PLEASE SHOW YOUR WORK PLEASE!!!!!
    7·1 answer
  • Which of the following could not be true for a function?
    15·2 answers
  • 24. Anton borrowed $460 for a new bicycle. He made 9 monthly payments to repay the loan. He paid $60 interest. What is his simpl
    13·1 answer
  • Which expression is equivalent to 5a+20?
    10·2 answers
  • PLZZZ HELP!, answer number 3
    8·1 answer
  • Woman has a new fitness watch.
    8·1 answer
  • Miranda needs to buy a bottle of honey. She notices that she can pay $4 for a
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!