158k views
4 votes
Def build_dictionary(string):

d = dict()
for x in string:
if not d.get(x):
d[x] = 1
else:
d[x] += 1
return d
Part 2:
Create a function named build_word_counter that has the same signature as build_dictionary. This function also returns a dictionary, however, it normalizes each word such that the case of the word is ignored (i.e. case insensitive). So the words LIKE, Like, like should be considered the same word (much like a regular dictionary would). You must use the same function add_item again (so don't make any modifications to it) Normalize words by using the lower case version of the word.
Part 3:
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.

User DarbyM
by
7.3k points

1 Answer

1 vote

Answer:

Follows are the code to this question:

def build_word_counter(string):#defining a method build_word_counter that accepts string as a parameter

d = dict()#defining d variable as a dictionary

for x in string:#defining for loop that use string

x = x.lower()#Use lower method

if not d.get(x):#defining if block that holds value in d variable

d[x] = 1#use dictionary to hold value in 1

else:#defining else block

d[x] += 1#use dictionary to increment value by 1

return d#return dictionary

print(build_word_counter(["Like","like","LIKE"]))#use print method to call method

def build_letter_distribution(string):#defining a method build_letter_distribution that accepts string variable

d = dict()#defining d variable as a dictionary

for i in string:#defining for loop to count string value

for j in i:#defining for loop to hold string as number

if not d.get(j):#defining if block that doesn't get value in dictionary

d[j] = 1#hold value in dictionary

else:#defining else block

d[j] += 1#use dictionary to increment the dictionary value

return d#return dictionary

print(build_letter_distribution(["Like","test","abcdEFG"]))#use print method to return its value

Output:

please find the attached file.

Step-by-step explanation:

In the first program a method, "build_word_counter" is defined that accepts a string variable in its parameter, and inside the method, a dictionary variable "d" is defined that uses the for loop to hold string value and convert all value into lower case.

In the next step, a conditional statement is used that holds value in the dictionary and count its word value.

In the second program a method, "build_letter_distribution" is defined that holds string variable in its parameter, and inside the method, a dictionary variable "d" is defined that uses the two for loop and in if the block, it holds value in the dictionary and returns its value, and use print method to print its return its value.

Def build_dictionary(string): d = dict() for x in string: if not d.get(x): d[x] = 1 else-example-1
User Rompetroll
by
6.2k points