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 “[email protected]”, 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,...

  • 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...

  • 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...

  • Python help def palindrome_word: ''' (str) -> bool The parameter is a string consisting only of...

    Python help def palindrome_word: ''' (str) -> bool The parameter is a string consisting only of lowercase alphabetic letters. It may or may not be a palindrome. Return True if and only if the parameter is a palindrome. The empty string is considered to be a palindrome. ''' def palindrom_phrase: ''' (str) -> bool The parameter is a string that may or may not be a palindrome. Return True if and only if the parameter is a palindrome, independent of...

  • Define a Python function named borough_count that has one parameter. The parameter is a string representing...

    Define a Python function named borough_count that has one parameter. The parameter is a string representing the name of a CSV file. The CSV file is a subset of NYC's dataset of all film permits issued since April 2016. Each row in the CSV file has the format: Event Id, Police Precinct(s), Event Type, Borough, Category Your function must return a dictionary. The keys of this dictionary will be the boroughs read in from the file (boroughs are at index...

  • Python 3 Write a function named inverse that takes a single parameter, a dictionary. In this...

    Python 3 Write a function named inverse that takes a single parameter, a dictionary. In this dictionary each key is a student, represented by a string. The value of each key is a list of courses, each represented by a string, in which the student is enrolled. The function inverse should compute and return a dictionary in which each key is a course and the associated value is a list of students enrolled in that course. For example, the following...

  • Question #2 A DNA molecule can be specified using a string of the characters , ‘g...

    Question #2 A DNA molecule can be specified using a string of the characters , ‘g 'a', 't'. Each of these characters represents one of the four nucleobases Cytosine, Guanine, Adenine, and Thymine. Consult the wikipedia page on DNA for more details. A codon consists of a sequence of three DNA nucle- obases and can be represented by a string of length 3 consisting of characters from the set ‘c', ‘g', ‘a", ‘t'. So "cga" and "ttg" are examples of...

  • Write a function named vertical that accepts a string as its parameter and prints each letter...

    Write a function named vertical that accepts a string as its parameter and prints each letter of the string on separate lines. For example, a call of vertical("hey now") should produce the following output: h e y n o w

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
ADVERTISEMENT