""" ################################################################################ MIDTERM EXAM ASSIGNMENT RULES ################################################################################ - You are strictly forbidden to share answers with your classmates. - You are not allowed to search for answers on the Internet. You are not allowed to ask for answers in forums or other online communication systems. - Note that, when you find an answer on the web, somebody else will also find it and use it. This means, both of you will submit the exact same answer. This, then, means cheating, no matter you know the other person or not. - If your answers are similar to some other student's answers, you will enter manual cheat check process. We don't care whether you know the other person or not, we don't care whether he/she is your friend or not, we don't care who delivered and who received. Both parties get a discipline penalty if found guilty. - You are not allowed to import any packages. If there are any imports in your submission, we will remove them before grading. - If your file doesn't compile, then you receive 0. Make sure that your indentation and syntax is correct. - Do not change the below function definitions. In other words, DO NOT CHANGE the function names and the parameters of the functions. You are only allowed to write code within the function scope. - Do not print anything on the screen unless you are asked to. Some questions require you to RETURN a value. Printing that value on the screen is not equal to returning the value. - Do not submit any testing code. ################################################################################ DO NOT FORGET TO FILL THE LINES BELOW: My full name : _______________ My student ID : _______________ My department : _______________ """ """ ################################################################################ Question 1: (10 points) ################################################################################ Write a Python function triangle_area(base, height) that calculates and RETURNs the area of a triangle given its base and height. The parameters base and height can be given as integer, float or string. If any of the parameters is of type string, then RETURN -1. Else, do the computation and return the triangle area. Sample test cases: triangle_area(10, 3.5) should return 17.5 triangle_area("base", 8)) should return -1 (because one of the arguments is of type string) """ def triangle_area(base, height): # Remove the return line below before you answer this question. If you will NOT answer # this question, then do NOT remove this line below. return "I have not solved this question" """ ################################################################################ Question 2: (15 points) ################################################################################ Write a function named upside_down_triangle(n) to PRINT the following output on the screen based on n, the given positive integer argument value. Sample test cases: The following example shows the output when upside_down_triangle(5) is called: ********* |*******| ||*****|| |||***||| ||||*|||| The following example shows the output when upside_down_triangle(4) is called: ******* |*****| ||***|| |||*||| The following example shows the output when upside_down_triangle(1) is called: * """ def upside_down_triangle(n): # Remove the return line below before you answer this question. If you will NOT answer # this question, then do NOT remove this line below. return "I have not solved this question" """ ################################################################################ Question 3: (20 points) ################################################################################ Write a Python function count_upper_lower(s) that accepts a string as an argument, calculates the number of uppercase and lowercase letters in the string s, and then RETURNs the result of number of the lowercase letters divided by the number of the uppercase letters. Sample test cases: count_upper_lower("I can hear the voice of Wind") should return 10.0 because there are 20 lowercase letters and 2 uppercase letters in the given string. 20/2 = 10.0 count_upper_lower("I love walking in Ankara when it rains") should return 14.5 because there are 29 lowercase letters and 2 uppercase letters in the given string. 29/2 = 14.5 """ def count_upper_lower(s): # Remove the return line below before you answer this question. If you will NOT answer # this question, then do NOT remove this line below. return "I have not solved this question" """ ################################################################################ Question 4: (20 points) ################################################################################ Write a Python function calculator(num1, num2, operator) which is basically a calcualator. As the function signature shows, to perorm the calculation over two numbers, it expects three arguments: - two different integer numbers (i.e. num1, num2) - and one character symbol 'operator' (as string) that denotes the operation. The possible operations are: - addition 'a', - subtraction 's', - multiplication 'm', - division 'd' - and power 'p'. If the second argument num2 is 0 and the operation is 'd', the function must RETURN "Invalid division" string message. Moreover, if the operator parameter is not any of the given 'a','s','m','d' and 'p' operations, the function must RETURN "Invalid operator" string message. Otherwise, the function must RETURN the result of num1 operator num2 calculation. Sample test cases: calculator(2, 3, 'a') must return 5, because addition is selected with 'a' over numbers 2 and 3. 2 + 3 = 5 calculator(10, 4, 'd') must return 2.5, because division is selected with 'd' over numbers 10 and 4. 10/4 = 2.5 calculator(2, 0, 'd') must return "Invalid division" because the second argument is 0 and division by 0 is undefined. calculator(2, 3, 'x') must return "Invalid operator" because 'x' operator is not defined. """ def calculator(num1, num2, operator): # Remove the return line below before you answer this question. If you will NOT answer # this question, then do NOT remove this line below. return "I have not solved this question" """ ################################################################################ Question 5: (20 points) ################################################################################ You have been recruited by an intelligence agency to help them decrypt some secret messages exchanged between suspects. Write a function named decrypt(text, subtext) that RETURNS the decrypted text as follows. This function takes two string arguments: - text: is some string message that has an encrypted part as its substring e.g., in "This is a terces" the encrypted substring is "terces" - subtext: is a string that is believed to be the encrypted part of the message e.g., "secret" is the encrypted form of "terces" You may notice that the suspects encrypt their messages by REVERSING the substring they want to hide. Your function must RETURN the given message after decrypting the hidden substring. You may assume that there will be only one encrypted subtext in text. For the example above, the RETURN value of the function call decrypt("This is a terces", "secret") should be "This is a secret" More sample test cases: decrypt("Meet me at the egdirB epetyeB", "Beytepe Bridge") should return: "Meet me at the Beytepe Bridge" decrypt("My password is *876yttik, please don't share it.", "kitty678*") should return: "My password is kitty678*, please don't share it." """ def decrypt(text, subtext): # Remove the return line below before you answer this question. If you will NOT answer # this question, then do NOT remove this line below. return "I have not solved this question" """ ################################################################################ Question 6: (15 points) ################################################################################ Write a recursive function A(n) that computes and RETURNs the n-th value of the following recursive formula: A(n) = 3*A(n-1) - 2*A(n-2) A(0) = 0 A(1) = 1 Sample test cases: A(3) should return 7 A(5) should return 31 A(10) should return 1023 Important note: Implementing the solution without using recursion will result in -5 pts penalty! """ def A(n): # Remove the return line below before you answer this question. If you will NOT answer # this question, then do NOT remove this line below. return "I have not solved this question"