Question

In: Computer Science

Who knows to do this topic with python code? Write a program to perform the following...

Who knows to do this topic with python code?
Write a program to perform the following two tasks:

1. The program will accept a string as input in which all of the words are run together, but the first character of each word is uppercase. Convert the string to a string in which the words are separated by spaces and only the first word starts with an uppercase letter. For example, the string "StopAndSmellTheRose" would be converted to "Stop and smell the rose". Display the result string.

2. Then the program will convert each word in the result string of task 1 into "Pig Latin" and display the result string. In one version of Pig Latin, you convert a word by removing the first letter, placing that letter at the end of the word, and then appending "ay" to the word.

For example, for the result string "Stop and smell the roses" in task 1, the Pig Latin string should be "topSay ndaay mellsay hetay osesray"


Hints:

1. In the first task, the first letter of a sentence should always be uppercase. Starting from the second letter to the last letter, if the letter is an uppercase (be cautious with I), that means it is the beginning of a new word. What you need to do is to insert a space before that uppercase letter, and change that uppercase letter to lowercase.

Python has isupper() function to check if a string or a character is uppercase, islower to check if a string or a character is lowercase

you may need a variable to remember the beginning index of a word and the ending index of a word. So you can use the two indexes to extract the word from the input phrase.

to add a space after a word, you can use the + operator as long as both operands are strings/characters

2. In the second task (pig Latin task), the result string of the first task should be passed as argument when you call the function you define for the second task.

You may need to use:

the index to access individual character (particularly the first character of each word in the sentence)

move the first character to the end of the word

add "ay" at the end of the word




Requirements:

*Algorithm must be modularized
*Python program (upload your .py file)

Solutions

Expert Solution

'''
Python version : 3.6
Python program to accept a string as input in which all of the words are run together, but the first character of each word is uppercase.
Convert the string to a string in which the words are separated by spaces and only the first word starts with an uppercase letter.
And then convert each word in the sentence to pig latin.
'''

# input the run together sentence
sentence = input('Enter the string: ')

# set resultant sentence to first character of sentence
format_sentence = sentence[0]

# set start_index to 1
start_index = 1
end_index = start_index # set end_index to start_index

# loop till end of string is reached
while end_index < len(sentence):
   # end_index character is an upper-case letter
   if sentence[end_index].isupper():
       # add the sentence from start_index to end_index to format_sentence followed by a space with the lowercase end_index character to resultant string
       format_sentence += sentence[start_index:end_index] + ' '+sentence[end_index].lower()
       # update start_index to end_index + 1
       start_index = end_index + 1
   # increment end_index by 1
   end_index += 1

# append the last word to the sentence
format_sentence += sentence[start_index:end_index]      

# display the formatted sentence
print('Formatted sentence: ',format_sentence)      
  
# Convert the format_sentence to Pig Latin  

# strip is used to remove leading and trailing spaces
# split is used to split the input string using whitespace as delimiter into list of strings
words = format_sentence.strip().split(" ")
  
pigLatin = "" # create an empty string for the resultant encrypted string
  
# loop over the list of words, converting each word to pig_latin and appending to encrypted followed by a space
for word in words:
   # copy the word from second character to end of word
   # appended by first character of word followed by ay and a space
   pigLatin += word[1:]+word[0]+"ay "

# display the pig latin sentence  
print('Pig Latin',pigLatin)  

#end of program

Code Screenshot:

Output:


Related Solutions

Does anyone know how to do pseudocode on this topic? Write a program to perform the...
Does anyone know how to do pseudocode on this topic? Write a program to perform the following two tasks: 1. The program will accept a string as input in which all of the words are run together, but the first character of each word is uppercase. Convert the string to a string in which the words are separated by spaces and only the first word starts with an uppercase letter. For example, the string "StopAndSmellTheRose" would be converted to "Stop...
Please write in Python code Write a program that stores the following data in a tuple:...
Please write in Python code Write a program that stores the following data in a tuple: 54,76,32,14,29,12,64,97,50,86,43,12 The program needs to display a menu to the user, with the following 4 options: 1 – Display minimum 2 – Display maximum 3 – Display total 4 – Display average 5 – Quit Make your program loop back to this menu until the user chooses option 5. Write code for all 4 other menu choices
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...
IN PYTHON Write a program to do the following: Solve the a set of equations as...
IN PYTHON Write a program to do the following: Solve the a set of equations as mentioned in "Zybook 5.20 LAB: Brute force equation solver". Instead of the arithmetic operators use your own function defined in a module named calc. You must provide two files (calc.py, brute_force_solver.py) :- 1) calc.py:- Add function named 'add' instead of using operator '+' [10pts] Add function named 'difference' instead of using operator '-' [10pts] Add function named 'product' instead of using operator '*' [10pts]...
Complete the following in syntactically correct Python code. Write a program, using a for loop, that...
Complete the following in syntactically correct Python code. Write a program, using a for loop, that calculates the amount of money a person would earn over a period of time if his or her salary is 1 penny for the first day, 2 pennies for the second day, 4 pennies for the third day, and continues to double each day. 1.      The program should ask the user for the number of days the employee worked. 2.      Display a table showing the salary...
please answer this in a simple python code 1. Write a Python program to construct the...
please answer this in a simple python code 1. Write a Python program to construct the following pattern (with alphabets in the reverse order). It will print the following if input is 5 that is, print z one time, y two times … v five times. The maximum value of n is 26. z yy xxx wwww vvvvvv
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:...
Reflection Write a program in Python or Matlab to perform reflection of an image about its...
Reflection Write a program in Python or Matlab to perform reflection of an image about its y-axis. Apply your program to an image of your choice to demonstrate the reflection. Do not use transpose library.
CODE IN PYTHON 1. Write a program that asks the user for the number of slices...
CODE IN PYTHON 1. Write a program that asks the user for the number of slices of pizza they want to order and displays the total number of dollar bills they owe given pizza costs 3 dollars a slice.  Note: You may print the value an integer value. 2. Assume that y, a and b have already been defined, display the value of x: x =   ya+b    3. The variable start_tees refers to the number of UD T-shirts at the start...
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:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT