Respuesta :
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.
![Ver imagen mahamnasir](https://us-static.z-dn.net/files/d2a/3082f701c463ca7e3e779b880e7a6347.png)