![potatogirl9300](/avatars/34604.jpg)
potatogirl9300
18.08.2020 •
Mathematics
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'.
Solved
Show answers
More tips
- F Food and Cooking Is it Really Possible to Cook Tasty Colored Cauliflower?...
- O Other What happens if you get scared half to death twice?...
- F Family and Home What s That Noise When a Kettle Boils? The Science of Water and Steam...
- L Leisure and Entertainment History of International Women s Day: When Did the Celebration of March 8th Begin?...
- W Work and Career How much does an honest traffic police officer earn in a day?...
- F Food and Cooking Red Caviar: How to Choose the Best?...
- S Style and Beauty How to Get Rid of a Bruise: Tips and Tricks...
- H Health and Medicine Is Massage Necessary? Facts and Opinions...
- L Leisure and Entertainment Should You Buy a Ceramic Knife?...
- C Computers and Internet Best Antivirus: How to Choose Protection for Your Computer?...
Answers on questions: Mathematics
- M Mathematics Plz solve question number 5 a (trigonometric equations) #salute u if u solved it...
- M Mathematics What is the inverse function of d(x)=-2x-6?...
- M Mathematics What is the distance between the points (5,7) and (-3, 4) on the coordinate plane?...
- M Mathematics The difference between two positive numbers is 7 and the square of their sum is 289. find the two numbers ....
- M Mathematics If megan is swimming, then it is warm outside...
- M Mathematics Can either p, q, or w be a point on j? justify your answer...
- M Mathematics Which point represents −238 on the number line?...
- M Mathematics You are going camping with your family for a family reunion. you need to know how many sleeping bags will fit inside the tent you will be using. the floor of the...
- M Mathematics Is the positive integer p a multiple of 50? p is a multiple of 10 p is a multiple of 5 statement (1) alone is sufficient, but statement (2) alone is not sufficient...
- M Mathematics On earth day a local community wants to distribute 800 flyers 300 banners 250 badges to some volunteers.if identical set of materials are to be given to each volunteer...
Ответ:
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.
Ответ:
D
Step-by-step explanation: