Question

In: Computer Science

#Write a function called average_word_length that takes as #input a string called my_string, and returns as...

#Write a function called average_word_length that takes as
#input a string called my_string, and returns as output the
#average length of the words in the string.

In writing this function, note the following:
#
# - You should account for consecutive spaces. A string like
# "Hi Lucy" is two words with an average length of 3.0
# characters.
# - You should not assume the string starts with a letter.
# A string like " David" has one word with an average
# length of 5.0 characters.
# - You should not count punctuation marks toward the
# length of a word. A string like "Hi, Lucy" has two
# words with an average length of 3.0 characters: the ,
# after "Hi" does not count as a character in the word.
# The only punctuation marks you need to handle are
# these: . , ! ?
# - You may assume the only characters in the string are
# letters, the punctuation marks listed above, and spaces.
# - If my_string is not a string, you should instead return
# the string, "Not a string".
# - If there are no words in my_string, you should instead
# return the string, "No words". This could happen for
# strings like "" (an empty string) and ".,!?" (a string
# of only punctuation marks). You may assume that any
# of these punctuation marks will always be followed by
# at least one space.
#
#Here are a few hints that might help you:
#
# - You can peak at the first character in my_string with
# my_string[0]. If my_string is "Hi, Lucy", then the value
# of my_string[0] is "H". You don't have to do this, but
# you can if you want.
# - There are lots of ways you can do this. If you're
# stuck, try taking a step back and thinking about the
# problem from a fresh perspective.
# - If you're still stuck, try counting words and letters
# separately, and worrying about average length only
# after both have been counted.
# - The word count should equal the number of letters that
# come immediately after a space or the start of the
# string. The character count should simply equal the
# number of characters besides spaces and punctuation
# marks. The average word length should be character
# count divided by word count.

#Write your function here!
average_word_length(my_string)


#When your function works, the following code should
#output:
#2.0
#3.0
#4.0
#Not a string
#No words

print(average_word_length("Hi"))
print(average_word_length("Hi, Lucy"))
print(average_word_length(" What big spaces you have!"))
print(average_word_length(True))
print(average_word_length("?!?!?! ... !"))

Solutions

Expert Solution

# do comment if any problem arises

# Code


# this function returns true if given string contains alphabets

def has_alpha(words):

    for char in words:

        if char.isalpha():

            return True

    return False


def average_word_length(words):

    # if input is not a string

    if not isinstance(words, str):

        return "Not a string"

    # if words doesnot contains alphabets

    if not has_alpha(words):

        return "No words"

    # remove punctuations

    words = words.replace('.', '')

    words = words.replace(',', '')

    words = words.replace('!', '')

    # strip

    words=words.lstrip()

    words=words.rstrip()

    # split words by spaces

    words = words.split(' ')

    length = 0

    # calculate total length of words

    for word in words:

        length += len(word)

    # return average length

    return length/len(words)


print(average_word_length("Hi"))

print(average_word_length("Hi, Lucy"))

print(average_word_length(" What big spaces you have!"))

print(average_word_length(True))

print(average_word_length("?!?!?! ... !"))

Screenshot of code:

Output:


Related Solutions

write an algorithm function called balance() in javascript that takes in a string and returns a...
write an algorithm function called balance() in javascript that takes in a string and returns a strinf with balanced parantheses only. the string should be able to contain only parantheses, numbers, and letters. include comments of each portion of your code balance(“()()”) should return “()()” balance(“())”) should return “()” balance(“a(b)c”) should return “a(b)c” balance(“a(b)c())”) should return “a(b)c()”
JAVA Arrays 4 Write a method called isPalindrome that takes a String as input and returns...
JAVA Arrays 4 Write a method called isPalindrome that takes a String as input and returns true if the String is a palindrome.
Write a C function called weighted_digit_sum that takes a single integer as input, and returns a...
Write a C function called weighted_digit_sum that takes a single integer as input, and returns a weighted sum of that numbers digits. The last digit of the number (the ones digit) has a weight of 1, so should be added to the sum "as is". The second from last digit (the tens digit) has a weight of 2, and so should be multiplied by 2 then added to the sum. The third from last digit should be multiplied by 1...
C++ write a function called divideBy that takes two integers as its input and returns the...
C++ write a function called divideBy that takes two integers as its input and returns the remainder. If the divisor is 0, the function should return -1, else it should return the remainder to the calling function.
Write a function that takes a C string as an input parameter and reverses the string.
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
Write a function that takes a number as input, and returns the character A if the...
Write a function that takes a number as input, and returns the character A if the input is 90 and above, B if it’s 80 and above but less than 90, C if it’s at least 70 but less than 80, D if it’s at least 60 but less than 70, and F if it’s less than 60. If the input is not a number or is negative, the function should exit 1 with an error (by calling the Matlab...
python 3 please Define a function voweliest that takes as input a string and returns as...
python 3 please Define a function voweliest that takes as input a string and returns as output a tuple where the string that has the most vowels in it is the first element and the second is the number of vowels in that string. Note: don't worry about ties or capital letters Hint: consider defining and using a separate function that counts the number of vowels in a given string
Python Implement function noVowel() that takes a string s as input and returns True if no...
Python Implement function noVowel() that takes a string s as input and returns True if no char- acter in s is a vowel, and False otherwise (i.e., some character in s is a vowel). >>> noVowel('crypt') True >>> noVowel('cwm') True >>> noVowel('car') False
Implement function noVowel() that takes a string s as input and returns True if no char-...
Implement function noVowel() that takes a string s as input and returns True if no char- acter in s is a vowel, and False otherwise (i.e., some character in s is a vowel). >>> noVowel('crypt') True >>> noVowel('cwm') True >>> noVowel('car') False Implement function allEven() that takes a list of integers and returns True if all integers in the list are even, and False otherwise. >>> allEven([8, 0, -2, 4, -6, 10]) True >>> allEven([8, 0, -1, 4, -6, 10])...
write a function that takes as input the root of a general tree and returns a...
write a function that takes as input the root of a general tree and returns a binary tree generated by the conversion process illustrated in java
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT