Question

In: Computer Science

Hi there, please write code in Python 3 and show what input you used for the...

Hi there, please write code in Python 3 and show what input you used for the program. I've been stuck on this for hours!

(1) Prompt the user to enter a string of their choosing. Store the text in a string. Output the string. (1 pt)

Ex:

Enter a sample text:
we'll continue our quest in space.  there will be more shuttle flights and more shuttle crews and,  yes;  more volunteers, more civilians,  more teachers in space.  nothing ends here;  our hopes and our journeys continue!

You entered: we'll continue our quest in space.  there will be more shuttle flights and more shuttle crews and,  yes;  more volunteers, more civilians,  more teachers in space.  nothing ends here;  our hopes and our journeys continue!


(2) Implement a print_menu() function, which has a string as a parameter, outputs a menu of user options for analyzing/editing the string, and returns the user's entered menu option and the sample text string (which can be edited inside the print_menu() function). Each option is represented by a single character.

If an invalid character is entered, continue to prompt for a valid choice. Hint: Implement the Quit menu option before implementing other options. Call print_menu() in the main section of your code. Continue to call print_menu() until the user enters q to Quit. (3 pts)

Ex:

MENU
c - Number of non-whitespace characters
w - Number of words
f - Fix capitalization
r - Replace punctuation
s - Shorten spaces
q - Quit

Choose an option:


(3) Implement the get_num_of_non_WS_characters() function. get_num_of_non_WS_characters() has a string parameter and returns the number of characters in the string, excluding all whitespace. Call get_num_of_non_WS_characters() in the print_menu() function. (4 pts)

Ex:

Number of non-whitespace characters: 181


(4) Implement the get_num_of_words() function. get_num_of_words() has a string parameter and returns the number of words in the string. Hint: Words end when a space is reached except for the last word in a sentence. Call get_num_of_words() in the print_menu() function. (3 pts)

Ex:

Number of words: 35


(5) Implement the fix_capitalization() function. fix_capitalization() has a string parameter and returns an updated string, where lowercase letters at the beginning of sentences are replaced with uppercase letters. fix_capitalization() also returns the number of letters that have been capitalized. Call fix_capitalization() in the print_menu() function, and then output the the edited string followed by the number of letters capitalized. Hint 1: Look up and use Python functions .islower() and .upper() to complete this task. Hint 2: Create an empty string and use string concatenation to make edits to the string. (3 pts)

Ex:

Number of letters capitalized: 3
Edited text: We'll continue our quest in space.  There will be more shuttle flights and more shuttle crews and,  yes;  more volunteers, more civilians,  more teachers in space.  Nothing ends here;  our hopes and our journeys continue!


(6) Implement the replace_punctuation() function. replace_punctuation() has a string parameter and two keyword argument parameters exclamation_count and semicolon_count. replace_punctuation() updates the string by replacing each exclamation point (!) character with a period (.) and each semicolon (;) character with a comma (,). replace_punctuation() also counts the number of times each character is replaced and outputs those counts. Lastly, replace_punctuation() returns the updated string. Call replace_punctuation() in the print_menu() function, and then output the edited string. (3 pts)

Ex:

Punctuation replaced
exclamation_count: 1
semicolon_count: 2
Edited text: we'll continue our quest in space.  there will be more shuttle flights and more shuttle crews and,  yes,  more volunteers, more civilians,  more teachers in space.  nothing ends here,  our hopes and our journeys continue.


(7) Implement the shorten_space() function. shorten_space() has a string parameter and updates the string by replacing all sequences of 2 or more spaces with a single space. shorten_space() returns the string. Call shorten_space() in the print_menu() function, and then output the edited string. Hint: Look up and use Python function .isspace(). (3 pt)

Ex:

Edited text: we'll continue our quest in space. there will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. nothing ends here; our hopes and our journeys continue!

Solutions

Expert Solution

SOLUTION-
I have solve the problem in python code with comments and screenshot for easy understanding :)

CODE-

#python code

import string
import re
OUT = 0
IN = 1

def print_menu(userStr):
menuOp=' '
print('\nMENU\nc - Number of non white-space Characters\nw - Number of words\nf - fix capatilization\nr - Replace punctuation\ns - Shorten spaces\nq - Quit')
while True:
menuOp=input('\nChoose an option: ')
if(menuOp in 'cwfrsq'):
break
print()
if (menuOp=='q'):
return menuOp,userStr
elif(menuOp=='c'):
print('Numebr of non-whitespace Characters:',get_num_of_non_WS_characters(userStr))
elif(menuOp=='w'):
print('Numebr of words:',get_num_of_words(userStr))
elif(menuOp=='f'):
newstr,count=fix_capilliation(userStr)
print('Numbee of latter capatalized:',count)
print('Edited text:',newstr)
elif(menuOp=='r'):
newstr,exCount,semiCount=replace_punctuation(userStr)
print('Punctuation repalced')
print('exclamationCount:',exCount)
print('semicolonCount:',semiCount)
print('\nEdited text:',newstr)
elif(menuOp=='s'):
newstr=shorten_space(userStr)
print('Edited text:',newstr)
return menuOp,userStr

def get_num_of_non_WS_characters(userStr):
userStrLen=len(userStr)
noOfSpace=userStr.count(' ')
return userStrLen-noOfSpace

def get_num_of_words(userStr):
state = OUT
wc = 0
# Scan all characters one by one
for i in range(len(userStr)):
# If next character is a separator, set the
# state as OUT
if (userStr[i] == ' ' or userStr[i] == '\n' or
userStr[i] == '\t'):
state = OUT
# If next character is not a word separator
# and state is OUT, then set the state as
# IN and increment word count
elif state == OUT:
state = IN
wc+=1
# Return the number of words
return wc
def fix_capilliation(user_input):
char_count=0
output_string = ''
line_terminator = ['.', '!']
end_of_line = False
for i in user_input:
if output_string == '':
char_count += 1
output_string += i.upper()
elif end_of_line == False and i not in line_terminator:
output_string = output_string + i
elif i in line_terminator:
end_of_line = True
output_string = output_string + i
elif end_of_line == True and i == ' ':
output_string = output_string + i
elif end_of_line == True and i != ' ':
output_string = output_string + i.upper()
char_count += 1
end_of_line = False
return output_string, char_count
def replace_punctuation(userStr,exclamationCount=0,semicolonCount=0):
newstr=' '
for ch in userStr:
if(ch=='!'):
newstr+='.'
exclamationCount+=1
elif(ch==';'):
newstr+=','
semicolonCount+=1
else:
newstr+=ch
return newstr,exclamationCount,semicolonCount
def shorten_space(userStr):
return re.sub(' +',' ',userStr)

def main():
print("Enter a simple text:")
userStr=input()
print('\nYou Entered:',userStr)

while True:
userChoie,userStr=print_menu(userStr)
if(userChoie=='q'):
break
if __name__=='__main__':
main()

SCREENSHOT-

CODE -

IF YOU HAVE ANY DOUBT PLEASE COMMENT DOWN BELOW I WILL SOLVE IT FOR YOU:)
----------------PLEASE RATE THE ANSWER-----------THANK YOU!!!!!!!!----------


Related Solutions

​​​​​​​ASAP PLEASE Write a python code that prompts a user to input a 10-digit phone number....
​​​​​​​ASAP PLEASE Write a python code that prompts a user to input a 10-digit phone number. The output of your code is the phone number’s  area code, prefix and line number separated with a space on a single line e.g INPUT 2022745512 OUTPUT 202 274 5512 write a code that will prompt a user to input 5 integers and output its largest value. PYTHON e.g INPUT 2 4 6 1 3 OUTPUT Largest value is: 6
Please write in beginner level PYTHON code! Your job is to write a Python program that...
Please write in beginner level PYTHON code! Your job is to write a Python program that asks the user to make one of two choices: destruct or construct. - If the user chooses to destruct, prompt them for an alternade, and then output the 2 words from that alternade. - If the user chooses construct, prompt them for 2 words, and then output the alternade that would have produced those words. - You must enforce that the users enter real...
Please provide Python code that does the following: 3) Write a program that takes a string...
Please provide Python code that does the following: 3) Write a program that takes a string as input, checks to see if it is comprised entirely of letters, and if all those letters are lower case. The output should be one of three possible messages: Your string is comprised entirely of lower case letters. Your string is comprised entirely of letters but some or all are upper case. Your string is not comprised entirely of letters. Your program may NOT:...
Write a python code to Design and implement a function with no input parameter which reads...
Write a python code to Design and implement a function with no input parameter which reads a number from input (like 123). Only non-decimal numbers are valid (floating points are not valid). The number entered by the user should not be divisible by 10 and if the user enters a number that is divisible by 10 (like 560), it is considered invalid and the application should keep asking until the user enters a valid input. Once the user enters a...
1. Please program the following in Python 3 code. 2. Please share your code. 3. Please...
1. Please program the following in Python 3 code. 2. Please share your code. 3. Please show all outputs. Instructions: Run Python code  List as Stack  and verify the following calculations; submit screen shots in a single file. Postfix Expression                Result 4 5 7 2 + - * = -16 3 4 + 2  * 7 / = 2 5 7 + 6 2 -  * = 48 4 2 3 5 1 - + * + = 18   List as Stack Code: """...
Please show the Rstudio code used in markdown. thank you! 3. Hypothesis Testing on Two Proportions...
Please show the Rstudio code used in markdown. thank you! 3. Hypothesis Testing on Two Proportions The Organization for Economic Cooperation and Development (OECD) summarizes data on labor-force participation rates in a publication called OECD in Figures. Independent simple random samples were taken of 300 U.S. women and 250 Canadian women. Of the U.S. women, 215 were found to be in the labor force; of the Canadian women, 186 were found to be in the labor force. a) Compute the...
Please write the code in c++ Write a function with one input parameter that is a...
Please write the code in c++ Write a function with one input parameter that is a vector of strings. The function should count and return the number of strings in the vector that have either an 'x' or a 'z' character in them. For example, when the function is called, if the vector argument contains the 6 string values, "enter", "exit", "zebra", "tiger", "pizza", "zootaxy" the function should return a count of 4. ("exit", "zebra", "pizza", and "zootaxy" all have...
Please write in Python code please: Write a program that asks the user to enter 5...
Please write in Python code please: Write a program that asks the user to enter 5 test scores between 0 and 100. The program should display a letter grade for each score and the average test score. You will need to write the following functions, including main: calc_average – The function should accept a list of 5 test scores as an input argument, and return the average of the scores determine_grade – The function should accept a test score as...
can you please convert this python code into java? Python code is as shown below: #...
can you please convert this python code into java? Python code is as shown below: # recursive function def row_puzzle_rec(row, pos, visited):    # if the element at the current position is 0 we have reached our goal    if row[pos] == 0:        possible = True    else:        # make a copy of the visited array        visited = visited[:]        # if the element at the current position has been already visited then...
hi i do not know what is wrong with my python code. this is the class:...
hi i do not know what is wrong with my python code. this is the class: class Cuboid: def __init__(self, width, length, height, colour): self.__width = width self.__length = length self.__height = height self.__colour = colour self.surface_area = (2 * (width * length) + 2 * (width * height) + 2 * (length * height)) self.volume = height * length * width def get_width(self): return self.__width def get_length(self): return self.__length def get_height(self): return self.__height def get_colour(self): return self.__colour def set_width(self,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT