Question

project-8a Write a function named count_letters that takes as a parameter a string and returns a...

project-8a

Write a function named count_letters that takes as a parameter a string and returns a dictionary that tabulates how many of each letter is in that string. The string can contain characters other than letters, but only the letters should be counted. The string could even be the empty string. Lower-case and upper-case versions of a letter should be part of the same count. The keys of the dictionary should be the upper-case letters. If a letter does not appear in the string, then it would not get added to the dictionary. For example, if the string is

"AaBb"

then the dictionary that is returned should contain these key-value pairs:

{'A': 2, 'B': 2}

And no, we haven't covered the isalpha() funciton, so you can't use it.

The file must be named: count_letters.py

1 0
Add a comment Improve this question Transcribed image text
Answer #1
def count_letters(string):
    d = dict()
    for x in string.upper():
        if (x>='a' and x<='z') or (x>='A' and x<='Z'):
            if not d.get(x):
                d[x] = 1
            else:
                d[x] += 1
    return d

# Testing
print(count_letters('AaBb'))

Output:

{A: 2, B: 2} Process finished with exit code 0

Note: Please comment below if you have any doubts. upuote the solution if it helped. Thanks!

Add a comment
Know the answer?
Add Answer to:
project-8a Write a function named count_letters that takes as a parameter a string and returns a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • write a function firstLetterWords(words) that takes as a parameter a list of strings named words and...

    write a function firstLetterWords(words) that takes as a parameter a list of strings named words and returns a dictionary with lower case letters as keys. But now associate with each key the list of the words in words that begin with that letter. For example, if the list is ['ant', 'bee', 'armadillo', 'dog', 'cat'], then your function should return the dictionary {'a': ['ant', 'armadillo'], 'b': ['bee'], 'c': ['cat'], 'd': ['dog']}.

  • Write a method in java named isValidEmail that takes a string as input parameter, and returns...

    Write a method in java named isValidEmail that takes a string as input parameter, and returns true if that string represents a valid email address, or false otherwise. An email address is considered valid if it follows this format “user123@domain.ext”, where:  user123 represents a sequence of word characters (i.e., letters, digits, or underscore) whose length is between 1 and 10 (inclusive), but the first character must be a letter  domain represents a sequence of alphanumeric characters (i.e., letters...

  • Write a function named words_in_both that takes two strings as parameters and returns a set of...

    Write a function named words_in_both that takes two strings as parameters and returns a set of the words contained in both strings. You can assume all characters are letters or spaces. Capitalization shouldn't matter: "to", "To", "tO", and "TO" should all count as the same word. The words in the set should be all lower-case. For example, if one string contains "To", and the other string contains "TO", then the set should contain "to". You can use Python's split() funciton,...

  • c++ Write a function Count_m_z that takes a string as an input parameter argument and counts...

    c++ Write a function Count_m_z that takes a string as an input parameter argument and counts the number of m, n, o, ... z characters in it. The function returns an integer value for the number of times those 14 lowercase letters appear in the input string. Your function should be named Count_m_z Your function should take one string parameter Your function should return the number of m, n, o, ... z characters as an integer Your function should not...

  • Python 5. Write a function named grade_components that has one parameter, a dictionary. The keys for...

    Python 5. Write a function named grade_components that has one parameter, a dictionary. The keys for the dictionary are strings and the values are lists of numbers. The function should create a new dictionary where the keys are the same strings as the original dictionary and the values are tuples. The first entry in each tuple should be the weighted average of the non-negative values in the list (where the weight was the number in the 0 position of the...

  • Define a function ValidRoom() that passes in a string parameter and returns true if the string...

    Define a function ValidRoom() that passes in a string parameter and returns true if the string parameter obeys all rules for a building's room address, else returns false. The rules are: (1) Must be two or three characters long. (2) First two characters must be digits, (3) If third character exists, must be a letter (upper or lowercase). Examples of strings yielding true: "65", "00", "21L". Examples of strings yielding false: "356", "7", "A23", " 65", "45AB". Recall some available...

  • In Javascript: Write a function named "categorize" that takes a string as a parameter and returns...

    In Javascript: Write a function named "categorize" that takes a string as a parameter and returns "short" if the input has less than 6 characters, "medium" if the input has greater than or equal to 6 but less than 7, and returns "long" if the input has greater than or equal to 7 characters. Thanks

  • Write a function named words_in_both that takes two strings as parameters and returns a set of...

    Write a function named words_in_both that takes two strings as parameters and returns a set of the words contained in both strings. You can assume all characters are letters or spaces. Capitalization shouldn't matter: "to", "To", "tO", and "TO" should all count as the same word. The words in the set should be all lower-case. For example, if one string contains "To", and the other string contains "TO", then the set should contain "to". The file must be named: words_in_both.py...

  • ** In Python ** Write a function named "tweets" that takes a string as a parameter...

    ** In Python ** Write a function named "tweets" that takes a string as a parameter and returns the number of tweets required to tweet the input to the world. Note: The maximum length for a single tweet is 280 characters Please provided an explanation.

  • javascript-Write a function named "json_average" that takes a JSON formatted string as a parameter in the...

    javascript-Write a function named "json_average" that takes a JSON formatted string as a parameter in the format of an array of objects where each object has keys "mass", "density", "temperature", and "velocity" and each key maps to a floating point number. This function should return the average "velocity" of all the objects in the array as a JSON string in the format {"velocity": }

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT