Question

Write a function named lengthDict with the following specifications: Input (parameters): t, a string Return: a...

Write a function named lengthDict with the following specifications:

Input (parameters): t, a string
Return: a dictionary in which each key is the length of a word in t and the corresponding value is the number of words in t with that length.

For example, the following would be correct output.
text = "it is what it is"
print(lengthDict(text)) {2: 4, 4: 1}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
def lengthDict(t):
    res = {}
    for x in t.split(" "):
        if not res.get(len(x)):
            res[len(x)] = 1
        else:
            res[len(x)] += 1
    return res

#Testing
text = "it is what it is"
print(lengthDict(text))

{2: 4, 4: 1}

Add a comment
Know the answer?
Add Answer to:
Write a function named lengthDict with the following specifications: Input (parameters): t, a string Return: 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
  • Python 3 Write a function named starCounter that takes three parameters: 1. a dictionary named starDictionary....

    Python 3 Write a function named starCounter that takes three parameters: 1. a dictionary named starDictionary. Each key in starDictionary is the name of a star (a string). Its value is a dictionary with two keys: the string 'distance' and the string 'type'. The value associated with key 'distance' is the distance from our solar system in light years. The value associated with key 'type' is a classification such as 'supergiant' or 'dwarf'. 2. a number, maxDistance 3. a string,...

  • help Question 12 (20 points) Write a function named inverse that takes a single parameter, a...

    help Question 12 (20 points) Write a function named inverse that takes a single parameter, a dictionary. In this 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. dictionary each 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...

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

  • Python 3.6 Question 12 (20 points) Write a function named wordLengths. The function wordLengths takes two...

    Python 3.6 Question 12 (20 points) Write a function named wordLengths. The function wordLengths takes two parameters: 1. inFile, a string that is the name of an input file 2. outFile, a string that is the name of an output file The input file exists when wordLengths is called; wordLengths must create the file outFile. The input file contains only letters and white space For each line of the input file, wordLengths should identify the length of the longest word...

  • Problem 3 Write a function named repeat_words that takes two string parameters: 1. in_file: the name...

    Problem 3 Write a function named repeat_words that takes two string parameters: 1. in_file: the name of an input file that exists before repeat_words is called 2. out_file: the name of an output file that repeat_words creates Assume that the input file is in the current working directory and write the output file to that directory. For each line of the input file, the function repeat_words should write to the output file all of the words that appear more than...

  • Program is in C++. Write a function named wordStatsPlus that accepts as its parameter a string...

    Program is in C++. Write a function named wordStatsPlus that accepts as its parameter a string holding a file name, opens that file and reads its contents as a sequence of words, and produces a particular group of statistics about the input. You should report: the total number of lines; total number of words; the number of unique letters used from A-Z, case-insensitively, and its percentage of the 26-letter alphabet; the average number of words per line (as an un-rounded...

  • Program is in C++.   Write a function named wordStatsPlus that accepts as its parameter a string...

    Program is in C++.   Write a function named wordStatsPlus that accepts as its parameter a string holding a file name, opens that file and reads its contents as a sequence of words, and produces a particular group of statistics about the input. You should report: the total number of lines; total number of words; the number of unique letters used from A-Z, case-insensitively, and its percentage of the 26-letter alphabet; the average number of words per line (as an un-rounded...

  • help ASAP 3. Write a string C++ function named UnsignedPartialSum() that takes two string parameters and...

    help ASAP 3. Write a string C++ function named UnsignedPartialSum() that takes two string parameters and an int parameter. If both string parameters represent binary numbers and the int parameter is equal to a positive number less than or equal to the length of the longest string parameter, the function should return a binary string whose length is equal to two times the length of the maximum length of the two string parameters whose value is equal to the sum...

  • in pyhton For example, the following would be correct input and output >» student_courses ('Elise:[1. Nelson :['cS1e0, MATH111']. 'Justin:t CS100']) >>> print (inve...

    in pyhton For example, the following would be correct input and output >» student_courses ('Elise:[1. Nelson :['cS1e0, MATH111']. 'Justin:t CS100']) >>> print (inverse(student_courses)) ('cs100': ['Nelson', Justin'], 'MATH111: [Nelson']) Question 13 (20 points) Write a function named filestats. The function filestats takes two parameters 1. 2. inFile, a string that is the name of an input file outFile, a string that is the name of an output file The function ftlestats should read and analyze each line of the input file...

  • Write a function named mostFrequent that takes two parameters: 1. inFile, a string that is the...

    Write a function named mostFrequent that takes two parameters: 1. inFile, a string that is the name of an input file 2. outFile, a string that is the name of an output file The input file inFile exists when mostFrequent is called; mostFrequent must create outFile. The input file contains only lower case letters and white space. The function mostFrequent identifies the letter(s) that appear most frequently on each line of inFile and writes them to a corresponding line of...

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