Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can someone please help me with this python code? I don't know why I keep getting a traceback. def count_word_lengths(s): assert all([x.isalpha() or x ==

Can someone please help me with this python code? I don't know why I keep getting a traceback.

def count_word_lengths(s):

assert all([x.isalpha() or x == ' ' for x in s])

assert type(s) is str

x = list(map(len, s.split()))

return(x)

from random import choice

def random_letter_or_space(pr_space=0.15):

from random import choice, random

is_space = (random() <= pr_space)

if is_space:

return ' '

return random_letter()

S_LEN = 40

W_SPACE = 1 / 6

rand_str = random_string(S_LEN, fun=random_letter_or_space)

rand_lens = count_word_lengths(rand_str)

print("Test 2: count_word_lengths('{}') == '{}'".format(rand_str, rand_lens))

c = 0

while c < len(rand_str) and rand_str[c] == ' ':

c += 1

for k in rand_lens:

print("=> '{}'".format (rand_str[c:c+k]))

assert (c+k) == len(rand_str) or rand_str[c+k] == ' '

c += k

while c < len(rand_str) and rand_str[c] == ' ':

c += 1

print(" (goal!)")

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Programming questions