Use a dictionary to count the frequency of letters in the input string. Only letters should be counted, not blank spaces, numbers, or punctuation. Upper case should be considered the same as lower case. For example, count_letters("This is a sentence.") should return {'t': 2, 'h': 1, 'i': 2, 's': 3, 'a': 1, 'e': 3, 'n': 2, 'c': 1}.

Respuesta :

Answer:

def count_letters(text):

 result = {}

 # Go through each letter in the text

 convert_text = text.lower()

 for letter in convert_text:

   if letter.isalpha():

     if letter in result:

       result[letter] += 1

     else:

       result[letter] = 1

 return result

print(count_letters("AaBbCc"))

# Should be {'a': 2, 'b': 2, 'c': 2}

print(count_letters("Math is fun! 2+2=4"))

# Should be {'m': 1, 'a': 1, 't': 1, 'h': 1, 'i': 1, 's': 1, 'f': 1, 'u': 1, 'n': 1}

print(count_letters("This is a sentence."))

# Should be {'t': 2, 'h': 1, 'i': 2, 's': 3, 'a': 1, 'e': 3, 'n': 2, 'c': 1}

Explanation:

Following are the program to the given question:

Program Explanation:

  • Defining a method count_letters that takes a string variable "t" in the parameter.
  • Defining an empty dictionary "r".
  • Defining a "t" variable that converts "t" value into lower case, and define a for loop that counts string variable value.
  • Inside the loop an if block that counts string value and adds value into list and use return keyword that return the list value.

Program:

def count_letters(t):#defining a method count_letters that takes a string variable in the parameter

   r = {}#defining an empty list

   t = t.lower()#defining a t variable that converts t value into lower case

   for l in t:#defining a for loop that counts string variable

       if l.isalpha() and l not in r:#defining an if block that counts string value and adds value into list

           r[l] = t.lower().count(l)#adding value into list

   return r#return list value

print(count_letters("This is a sentence."))#defining a print method that calls the count_letters method by accepting string value

Output:

Please find the attached file.

Learn more:

brainly.com/question/14312083

Ver imagen codiepienagoya