""" ################################################################################ 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: (15 points) ################################################################################ Write a function multiple_of_n(n, upper_limit) that will RETURN the largest positive integer divisible by n less than or equal to upper_limit, or RETURN -1 if such number does not exist. For example, multiple_of_n(5, 26) returns 25 because it is the largest positive integer less than or equal to 26 that is also divisible by 5. Similarly, multiple_of_n(13, 12) returns -1 because no positive integers less than or equal to 12 are divisible by 13. """ def multiple_of_n(n, upper_limit): while n<=upper_limit and upper_limit>0: if upper_limit%n==0: return upper_limit upper_limit -= 1 return -1 """ ################################################################################ Question 2: (15 points) ################################################################################ Write a function named triangle(t) to PRINT the following output on screen based on t, the given positive integer argument value. The following example shows the output for triangle(5): ..../\.... .../ \... ../ \.. ./ \. /________\ The following example shows the output for triangle(3): ../\.. ./ \. /____\ """ def triangle(t): for i in range(t-1, 0, -1): print(i * "." + "/" + 2*(t-1-i) * " " + "\\" + i * ".") print("/" + (t-1)*2*"_" + "\\") """ ################################################################################ Question 3: (20 points) ################################################################################ Write a function named scramble(s, subs) that RETURNS the following scrambled string: the returned string will be the same as input except for the substring subs that should be scrambled such that it is written in reverse. You may assume that subs will be the only such substring of s. For example, scramble("This is the string that needs to be scrambled", "scrambled") should print: This is the string that needs to be delbmarcs Similarly, scramble("Some test string here", "test string") should print: Some gnirts tset here """ def scramble(s, subs): r = "" for char in subs: r = char + r index = -1 for i in range(len(s) - len(subs) + 1): if s[i:(i + len(subs))] == subs: index = i return s[:index]+r+s[index+len(subs):] """ ################################################################################ Question 4: (10 points) ################################################################################ Write a function named power(base, pow) that takes two string arguments, converts them to integers and PRINTS the result of base^pow (base to the power of pow) formatted as shown below. For example, power("3", "4") will print: The result is 81. Similarly, power("100", "35") will print: The result is 10000000000000000000000000000000000000000000000000000000000000000000000. Important note: Do not forget the dot at the end! """ def power(base, pow): print("The result is " + str(int(base)**int(pow)) + ".") """ ################################################################################ Question 5: (20 points) ################################################################################ Write a function named is_valid_password(str) that RETURNs True if the given string argument is a valid password, and RETURNs False otherwise. For a string to be considered a valid password it must satisfy the following conditions: 1. It must be at least 8 characters long, 2. It must include at least one of these special characters: '.', '*', '-' 3. It should not include a blank space ' ' For example, is_valid_password("sif.rem") should return False because its length is 7, is_valid_password("sif.rem -") should return False because it has space in it, while is_valid_password("sif.rem-") should return True. """ def is_valid_password(str): has_mark = False if len(str) < 8: # return False if too short return False for c in str: # check for ., -, * if c == " ": # If space found, return False return False elif c == "-" or c == "." or c == "*": has_mark = True return has_mark """ ################################################################################ Question 6: (20 points) ################################################################################ Write a RECURSIVE function named A that takes two arguments x and y, and RETURNs the value of A(x,y) recursively with respect to the following formula: A(x,y) = A(x-1,y) + A(x,y-1) given that A(0,0) = 0, A(0,y) = 0 and A(x,0) = x """ def A(x,y): if x == 0: return 0 elif y == 0: return x else: return A(x-1, y) + A(x, y-1)