python question using thonny
s Window Help % 49% Sat 7:39 PM alb COMP_208_Winter_2020 Assignment2_rev3.pdf (page 5 of 12) a Sanch Question 2 (25 points) Mr. Parr N. Thesis is having a problem with his parentheses. When writing, he replaces all his commas with an opening parenthesis 'C, and then inserts a closing parenthesis) at the end of the sentence. For example, instead of writing 'Hello, World! he writes 'Hello World!)'. He does this for every comma in the sentence. For example, the sentence 'Me, myself, and I' would become 'Mel myself and I))', and the sentence 'The first five letters of the alphabet are a, b, c, and d' would become 'The first five letters of the alphabet are al b( c( and d.))) In order to understand Mr. Thesis' writings, we will write code to help translate his sentences by replacing the parentheses with commas in the appropriate positions. We will do this by writing four functions as follows. (Note that there are other ways to do this question, but you must implement each of these functions as described; we will be testing each of your functions individually. Further, like in all other questions, you can only use concepts seen in class up to arld including February 5. That is, you may not use the string methods replace, index, find, rindex, rfind, partition or rpartition methods for this question.) Name: find first Parameters: A strings and character letter Return value: The first index of the character letter in the strings. (Hint: Loop over the string's indices, starting at 0.) If the character letter is not present in s, return the value None. Name: find last Parameters: A string s and character letter Return value: The last index of the character letter in the strings. (Hint: Use your code from find first but start at the last index.) If the character letter is not in s, return None. Name: get_comma phrase COMP_208 Winter_2020 Assignment 2 rev3.pdf (page 5 of 12) Q Search Return value: The last index of the character letter in the strings. (Hint: Use your code from find first but start at the last index.) If the character letter is not in s, return None. Name: get_comma phrase Parameters: A strings containing at least one opening '' and one closing parenthesis) Return value: The strings with the first opening parenthesis replaced by a comma, and the last closing parenthesis deleted. (Hint: Use the functions above to get the indices of the two parentheses, then use string slicing to get the parts of the string that are between the parentheses (3 parts in total), and put the parts back together with a comma inserted in the appropriate location.) Name: get_comma string Parameters: A strings Return value: The strings with all opening parentheses replaced by commas and all closing paren- theses deleted. (Hint: The function get_comma phrase returns a new string with the first and last parenthesis of the string deleted. If there are several sets of opening and closing parentheses in the string, then how many times should you call the function? A while loop will be useful (but you should figure out when to stop looping).) Dis Window Help * 49% Sat 7:39 PM COMP_208_Winter_2020 Assignment_2_rev3.pdf (page 6 of 12) Sear Filename You must write this program in a file called parentheses.py. In the file, the four functions are already defined for you, and at the bottom of the file there is some test code already present. It asks the user to type in a string, then prints out the result of the get.comma string function. Examples (as executed in Thonny) EXAMPLE 1: >>> %Run parentheses.py Enter text: Hello( World) Hello, World EXAMPLE 2: >>> %Run parentheses.py Enter text: Me( myself and I)) Me, myself, and I EXAMPLE 3: >>> Run parentheses.py Enter text: The first five letters of the alphabet are a b( c( and d.))) The first five letters of the alphabet are a, b, c, and d. EXAMPLE 4: >>> un parentheses Enter text: No commas in this sentence! No commas in this sentence! Thonny File Edit View Run Device Tools Help 6 Thonny - /Users/all OSO encryption.py parentheses.py 1 # Author: [your name here) 2 # McGill ID: your ID here] 3 # Assignment 1, Question 2 NA 5 def find_first(s, letter): pass # delete this line and add your code here 8 def find_last(s, letter): pass # delete this line and add your code here 11 def get_comma_phrase(s): pass # delete this line and add your code here 14 def get_comma_string(s): pass # delete this line and add your code here 17 S = input("Enter text: ") 18 print (get_comma_string(s)) Shell Python 3. bundle