Question

In: Computer Science

Program in Python Problem Statement Write a program with the following functions:  wordCount. This function...

Program in Python

Problem Statement
Write a program with the following functions:
 wordCount. This function should accept a string as a parameter and return the
number of words contained in the string.
 mostFrequentWord. This function accepts a string as a parameter and returns the
word that occurs the most frequently in the string.
 replaceWord. This function accepts three strings as parameters, let’s call them
string1, string2, and string3. It searches string1 for all occurrences of string2. When it finds an occurrence of string2, it replaces it with string3.
For example, suppose the three parameters have the following values:
string1: string2: string3:
“the cat jumped up to reach the ball” “the”
“that”
With these three, the function would return a string with the value “that cat jumped up to reach that ball”
In the main function, ask the user to enter a sentence, and demonstrate each of the above functions.

Sample Output
Enter a sentence: the cat jumped up to reach the ball There are 8 words in the sentence
The most frequent word in the sentence is: the
Enter oldString, then newString you want to replace, separate them with a space: the that The new sentence after replacement is: that cat jumped up to reach that ball

Solutions

Expert Solution

Python code:

def wordCount(string):
li = string.split(" ")
return len(li)
  
def mostFrequentWord(string):
li = string.split(" ")
s = list(set(li))
frequency = li.count(s[0])
ind = 0
for i in range(1,len(s)):
if li.count(s[i]) > frequency:
frequency = li.count(s[i])
ind = i
return s[ind]
  
def replaceWord(string1,string2,string3):
li = string1.split(" ")
s1 = " "
for i in range(len(li)):
if(li[i]==string2):
li[i] = string3
return s1.join(li)
  
  
def main():
string = input("Enter a sentence :")
cwords = wordCount(string)
print("There are "+str(cwords)+" words in the sentence")
print("The most frequent word in the sentence is : "+mostFrequentWord(string))
print("Enter old string,then new string you want to replace,seperate them with a space :")
temp = input().split(" ")
string2 = temp[0]
string3 = temp[1]
print("The new sentence after replacement is :"+replaceWord(string,string2,string3))
  
if __name__ =="__main__":
main()
  
Execution screenshots:

Note:please like the answer.Thank you.Have a nice day.


Related Solutions

PYTHON Program Problem Statement: Write a Python program that processes information related to a rectangle and...
PYTHON Program Problem Statement: Write a Python program that processes information related to a rectangle and prints/displays the computed values. The program will behave as in the following example. Note that in the two lines, Enter length and Enter width, the program does not display 10.0 or 8.0. They are values typed in by the user and read in by the program. The first two lines are text displayed by the program informing the user what the program does. This...
Write a program in PYTHON which: *Defines the following 5 functions: whoamI() This function prints out...
Write a program in PYTHON which: *Defines the following 5 functions: whoamI() This function prints out your name and PRINTS OUT any programming course you have taken prior to this course. isEven(number) This function accepts one parameter, a number, and PRINTS OUT a message telling if the numberer is even or odd. Keep in mind that a number is even if there is no remainder when the number is divided by two. printEven(number) This function accepts one parameter, a number,...
Write a Python program that uses function(s) for the following problem: A nutritionist who works for...
Write a Python program that uses function(s) for the following problem: A nutritionist who works for a fitness club helps members by evaluating their diets. As part of her evaluation, she asks members for the number of fat grams and carbohydrate grams that they consumed in a day (two inputs, one for number of fat grams and the other for number of carb grams). Then, she calculates the number of calories that result from the fat, using the following formula:...
Language Python with functions and one main function Write a program that converts a color image...
Language Python with functions and one main function Write a program that converts a color image to grayscale. The user supplies the name of a file containing a GIF or PPM image, and the program loads the image and displays the file. At the click of the mouse, the program converts the image to grayscale. The user is then prompted for a file name to store the grayscale image in.
Write the following easy Python functions: 1) Write the function named roundDollars(). The function has one...
Write the following easy Python functions: 1) Write the function named roundDollars(). The function has one input, a String named amountStr which consists of a dollar-formatted amount, such as "$ 1,256.86". The returned value is an int representing the number of rounded "dollars" in the amount, (1257 in the sample shown here). You will need to scrub, format and parse the input, then use arithmetic to determine how many rounded dollars the amount contains. roundDollars("$ 1,256.86") → 1257 roundDollars("$ 0.42")...
Write a complete and syntactically correct Python program to solve the following problem: Write a program...
Write a complete and syntactically correct Python program to solve the following problem: Write a program for your professor that allows him to keep a record of the students’ average grade in his class. The program must be written in accordance with the following specs: 1. The input must be interactive from the keyboard. You will take input for 12 students. 2. You will input the students’ name and an average grade. The student cannot enter an average below zero...
Python Problem Problem 1: In this problem you are asked to write a Python program to...
Python Problem Problem 1: In this problem you are asked to write a Python program to find the greatest and smallest elements in the list. The user gives the size of the list and its elements (positive and negative integers) as the input. Sample Output: Enter size of the list: 7 Enter element: 2 Enter element: 3 Enter element: 4 Enter element: 6 Enter element: 8 Enter element: 10 Enter element: 12 Greatest element in the list is: 12 Smallest...
PYTHON PYTHON Recursive Functions. In this problem, you are asked to write three recursive functions. Implement...
PYTHON PYTHON Recursive Functions. In this problem, you are asked to write three recursive functions. Implement all functions in a module called problem1.py. (10 points) Write a recursive function called remove char with two parameters: a string astr and a character ch. The function returns a string in which all occurrences of ch in astr are removed. For example, remove char("object oriented", ’e’) returns the string "objct orintd". Your implementation should not contain any loops and may use only the...
Write functions in Python IDLE that do the following: i) A function that takes 2 arguments...
Write functions in Python IDLE that do the following: i) A function that takes 2 arguments and adds them. The result returned is the sum of the parameters. ii) A function that takes 2 arguments and returns the difference, iii) A function that calls both functions in i) and ii) and prints the product of the values returned by both.
In python please write the following code the problem. Write a function called play_round that simulates...
In python please write the following code the problem. Write a function called play_round that simulates two people drawing cards and comparing their values. High card wins. In the case of a tie, draw more cards. Repeat until someone wins the round. The function has two parameters: the name of player 1 and the name of player 2. It returns a string with format '<winning player name> wins!'. For instance, if the winning player is named Rocket, return 'Rocket wins!'.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT