In: Computer Science
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!
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!!!!!!!!----------