1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
luda_lava [24]
3 years ago
14

(count_word_lengths_test: 2 points). Write a function count_word_lengths(s) that, given a string consisting of words separated b

y spaces, returns a list containing the length of each word. Words will consist of lowercase alphabetic characters, and they may be separated by multiple consecutive spaces. If a string is empty or has no spaces, the function should return an empty list.
Computers and Technology
1 answer:
olga_2 [115]3 years ago
6 0

Answer:

The solution code is writing in Python.

  1. import re  
  2. def count_word_lengths(s):
  3.    processed_str = re.sub(' +', ' ', s)
  4.    
  5.    count = 0
  6.    word_list = processed_str.split(" ")
  7.    word_length_list = []
  8.    if(len(word_list) == 1):
  9.        return []
  10.    else:
  11.        for x in word_list:
  12.            word_length_list.append(len(x))
  13.    return word_length_list  
  14. print(count_word_lengths("i    have a dream"))

Explanation:

Since this program is expected to handle multiple consecutive spaces, we need to import Python regular expression module, <em>re </em>(Line 1). The usage of <em>re</em> is to use its<em> sub </em>method to substitute those multiple spaces with single space ' ' and assigned the modified string to variable <em>processed_str</em> (Line 4).

Next, use the string <em>split() </em>method to transform the processed_str into a list of individual words using single space " " as separator and assign the list to variable word_list (Line 7).

If the <em>word_list </em>contains only one word, this means the input string is either empty or no spaces and therefore it return empty list (Line 10). Otherwise, the program will proceed to a for loop that traverse through each word in the word_list and use len() method to get the length of the individual word and add the length value to the word_length_list (Line 13-14). At last, return the word_length_list as output (Line 16).

We can test the function using a sample string as in Line 19 and the output is [1, 4, 1, 5].

You might be interested in
A type of address translation in which a gateway has a pool of public ip addresses that it is free to assign to a local host whe
krok68 [10]
<span>Dynamic Network Address Translation (DNAT)</span>
7 0
3 years ago
Forwarded events can only be recorded when systems ADMINISTRATORS have de-established an event subscription. TRUE or FALSE
weeeeeb [17]

Answer:

True

Explanation:

Forwarded events can only be recorded when systems administrators have de-established an event subscription.

8 0
3 years ago
What does the W in WAN represent
Ilya [14]
WAN stands for Wide Area Network.
7 0
4 years ago
Read 2 more answers
You have been hired by an educational software company to create a program that automatically calculates the sum of each place-v
Vinvika [58]

Hi, you haven't provided the programing language, therefore, we will use python but you can extend it to any programing language by reading the code and the explanation.

Answer:

n1 = int(input("First numeber: "))

n2 = int(input("Second numeber: "))

for i in range(5):

   r1 = n1%10

   r2 = n2%10

   print(r1+r2)

   n1 = n1//10

   n2 = n2//10

 

Explanation:

  1. First, we ask for the user input n1 and n2
  2. We create a for-loop to calculate the sum of each place-value of two numbers
  3. We obtain the last number in n1 by using the mod operator (%) an the number ten this way we can always take the last value, we make the same for n2
  4. Then we print the result of adding the last two numbers (place value)
  5. Finally, we get rid of the last value and overwrite n1 and n2 to continue with the process

4 0
3 years ago
What best describes the difference between careers and industries?
Rasek [7]

Answer:

c

Explanation:

big brain

3 0
4 years ago
Other questions:
  • The Internet and World Wide Web allow almost any business to be global. What are two important results of this process?
    8·1 answer
  • A token is combination of hardware and software that acts as a gatekeeper and prevents unauthorized users from accessing private
    11·1 answer
  • All data process by a computer must be in 1. binary form 2. Hexadecimal form 3. Duodecimal form 4. Unitary form?
    11·2 answers
  • When you are printing handouts, which of these can you do?
    6·1 answer
  • What is syntax?
    15·1 answer
  • I can't solve this <br> Python loop with while statement
    15·2 answers
  • Where can you find the sizing handles for a graphic, shape, or text box? Check all that apply.
    10·2 answers
  • When you check your hard drive to see how much space is available, you are checking your
    15·1 answer
  • The average numbers of shares a piece of content receives is known as its:
    7·1 answer
  • How can you prevent someone with access to a mobile phone from circumventing access controls for the entire device
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!