Question

Create a function named build_letter_distribution that has the same signature as build_dictionary. This function returns a...

Create a function named build_letter_distribution that has the same signature as build_dictionary. This function returns a dictionary; however, each key will be a letter and the value will be the count that represents how many times that letter was found. Essentially you will have a letter distribution over a sentence (which is a list of words). The letters should be case insensitive. See the lesson on "Working with Lists" on how to get the individual characters from a string Normalize letters by using the lower case (see the 'kitchen sink' lesson)

def build_word_counter(string): d = dict() for x in string: x = x.lower() if not d.get(x): d[x] = 1 else: d[x] += 1 return d

It did not past the test, saying that Traceback (most recent call last): File "/home/runner/unit_tests.py", line 159, in test_build_letter_distribution self.assertEquals(solution3, ans3) AssertionError: {'i': 5, 'l': 5, 'k': 4, 'e': 5, 'd': 10, '[73 chars]': 1} != {'I': 38, 'LIKE': 19, 'Dogs': 152, 'Black':[81 chars]: 19} + {'All': 19, - {'a': 4, - 'b': 2, - 'c': 1, - 'd': 10, ? ^ + 'Bad': 19, ? ++ ^ + 'Black': 19, + 'Dogs': 152, + 'Green': 19, - 'e': 5, - 'f': 1, - 'g': 9, - 'i': 5, - 'k': 4, - 'l': 5, - 'm': 1, - 'n': 3, ? ^ + 'I': 38, ? ^ + + 'LIKE': 19, + 'Mean': 19, + 'kinds': 19, + 'like': 19, - 'o': 9, ? ^ + 'of': 19} ? + + ^ - 'r': 1, - 's': 9}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The reason why your code is not working is because input to the function is a list of words and not a word. Your loop only traverses through the list and not each character inside the word. You need to add another loop inside the current loop to traverse through each letter inside the word.

Code:

def build_letter_distribution(string):

    d = dict()

    for x in string:

        for i in x.lower():

            if not d.get(i):

                d[i] = 1

            else:

                d[i] += 1

    return d

print(build_letter_distribution(["Like","test","DOGS"]))

Output:


def build_letter_distribution(string): d = dict() for x in string: for i in x.lower(): if not d.get(i): d[i] = 1 else: d[i] +

Add a comment
Know the answer?
Add Answer to:
Create a function named build_letter_distribution that has the same signature as build_dictionary. This function 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
  • 0. Modify or create the table(file) named Staff first used in assignment 5. There is no...

    0. Modify or create the table(file) named Staff first used in assignment 5. There is no change to the structure, and if you need to recreate it the column names, data types and lengths are described below: Table Name Column Name Data Type Length Comment/constraint Staff: id int name varchar office char fee decimal reviewDate date 3 primary key 15 not null 7 7,2 default 0.00 Initials have been added to the name column values, so data for an insert...

  • Hey guys I need help with this assignment. However it contains 7 sub-problems to solve but I figured only 4 of them can...

    Hey guys I need help with this assignment. However it contains 7 sub-problems to solve but I figured only 4 of them can be solved in one post so I posted the other on another question so please check them out as well :) Here is the questions in this assignment: Note: Two helper functions Some of the testing codes for the functions in this assignment makes use of the print_dict in_key_order (a dict) function which prints dictionary keyvalue pairs...

  • Using python Question 13 (20 points) Write a function named Linestats that finds the number of...

    Using python Question 13 (20 points) Write a function named Linestats that finds the number of consonant and vowel letters on each line of a file and writes those statistics to a corresponding line of a new file, separated by a space. Definitions: A vowel letter is one of a, e, i, o, u, along with the corresponding uppercase letters..A consonant letter is not a vowel letter. The function linestats takes two parameters: 1. inFile, a string, the name of...

  • For this question you will need to complete the methods to create a JavaFX GUI application...

    For this question you will need to complete the methods to create a JavaFX GUI application that implements two String analysis algorithms. Each algorithm is activated when its associated button is pressed. They both take their input from the text typed by the user in a TextField and they both display their output via a Text component at the bottom of the GUI, which is initialized to “Choose a string methods as indicated in the partially completed class shown after...

  • Two words or phrases in English are anagrams if their letters (and only their letters), rearranged,...

    Two words or phrases in English are anagrams if their letters (and only their letters), rearranged, are the same. We assume that upper and lower case are indistinguishable, and punctuation and spaces don't count. Two phrases are anagrams if they contain exactly the same number of exactly the same letters, e.g., 3 A's, 0 B's, 2 C's, and so forth. Some examples and non-examples of regular anagrams: * The eyes / they see (yes) * moo / mo (no) *...

  • LANGUAGE: PYTHON Write a function called: d_polybius(). The function applies the decryption scheme for the polybius...

    LANGUAGE: PYTHON Write a function called: d_polybius(). The function applies the decryption scheme for the polybius cipher scheme above. The start of the function call the get_polybius_square function to get the square as a string. The second scenario when the number of characters is not even, excluding ‘\n’. For instance: “71\n5” is an invalid cipher because there is no way that the last number correspond to a character (we need two numbers). A customized version of Polybius square will be...

  • 14.3: More Sentences Write a program that allows a user to enter a sentence and then...

    14.3: More Sentences Write a program that allows a user to enter a sentence and then the position of two characters in the sentence. The program should then report whether the two characters are identical or different. When the two characters are identical, the program should display the message: <char> and <char> are identical! Note that <char> should be replaced with the characters from the String. See example output below for more information. When the two characters are different, the...

  • JAVA PROJECT Part 1 - Normalize Text The first thing we will do is normalize the...

    JAVA PROJECT Part 1 - Normalize Text The first thing we will do is normalize the input message so that it’s easier to work with. Write a method called normalizeText which does the following: Removes all the spaces from your text Remove any punctuation (. , : ; ’ ” ! ? ( ) ) Turn all lower-case letters into upper-case letters Return the result. The call normalizeText(“This is some \“really\” great. (Text)!?”) should return “THISISSOMEREALLYGREATTEXT” Part 2 - Obfuscation...

  • 1. Create a String variable named menuChoice. 2. Display 3 options for your program. I recommend...

    1. Create a String variable named menuChoice. 2. Display 3 options for your program. I recommend using 3 System.out.println statements and a 4th System.out.print to show "Enter your Selection:". This needs to be inside the do -- while loop. A. Deposit Cash. B. Withdraw Cash X. Exit Enter your Selection: 3. Prompt for the value menuChoice. 4. If menuChoice is "A", display "Do Deposit" and redisplay the menu. If menuChoice is "B", display "Do withdrawal" and redisplay the menu. If...

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