Assignment 6 Due by Friday, April 17st 2015, midnight Instructions The assignment will be graded out of 100 points. Create a text document for your answers. Please use Python 3 (3.4 is the current version) Once you type your answers using your preferred word processor, generate a TEXT (.txt) file which has to have the named as name [yourname]_assignment6. The .txt has to include the following information on the first page: your first, last name and your UTA ID. The code submitted in the questions that require code to be written, should be exactly the same with the code you wrote in your python environment (i.e. IDLE). We will be copying-pasting your code from the text document to our python installation. Upload your file via the Blackboard. Task 1 (10 pts.) Explain what the following function will do. What might it be used for? Note: s1 and s2 are strings. def function1(s1, s2): s1_list = list(s1.lower()) s2_list = list(s2.lower()) s1_list.sort() s2_list.sort() if s1_list == s2_list: return True else: return False Task 2 (20 pts.) Describe the execution of this program: what lines are executed; what namespaces come into existence and when; what variables are in each namespace and what are their values? Your answer must explicitly show the state of each namespace after every execution of a line. def function1(z): values = [1, 2, 3] return values[z] def function2(z, y): if y < 0: val = 0 elif y == 10: val = 1 else: # # # # # # # # # # line line line line line line line line line line 1 2 3 4 5 6 7 8 9 10 val = 2 x = function1(val) return x*z x = 10 y = 2 print(function2(y, x)) # # # # # # line 11 line 12 line 13 line 14 line 15 line 16 # line 17 Task 3 (15 pts.) Write a function named repeat_n that: Takes one argument, an integer num. Returns a number that repeats the digits of num 3 times. If the argument is not an integer, the function should return None. For example: For input argument "hello!", it should return None, because the input argument is a string. For input argument "23", it should return None, because the input argument is a string. For input argument 12.34, it should return None, because the input argument is a float. For input argument 1, it should return 111. For input argument 241, it should return 241241241. IMPORTANT: make sure that, when the input argument is an integer, the output is also an integer. In other words, make sure that, if the input argument is of type int, the return value is also of type int. Task 4 (15 pts.) Write a function named divisors(num, limit) that: Takes two arguments, an integer num, and an integer limit. Returns the list of divisors of num that are less than or equal to limit. A divisor of num is a number X between 1 and num such that the remainder of dividing num by X is 0. If any argument is not an integer, the function should return None. For example: divisors("hello!", 5) should return None, because "hello" is a string. divisors("23", 5) should return None, because "23" is a string. . divisors(15, 12.34) should return None, because 12.34 is a float. . divisors(1, 5) should return [1]. divisors(13, 50) should return [1, 13]. divisors(101, 40) should return [1]. divisors(4, 2) should return [1, 2]. divisors(12, 5) should return [1, 2, 3, 4]. Task 5 (15 pts.) Write a function named is_prime(s) that: Takes one argument, an integer num. Returns True if num is prime. A number X is prime if it is only divisible by 1 and by X. You can consider that numbers 0 and 1 are prime. Returns False if num is not prime. If the argument is not an integer, the function should return None. For example: For input argument "hello!", it should return None, because the input argument is a string. For input argument 12.34, it should return None, because the input argument is a float. For input argument 1, it should return True. For input argument 13, it should return True. For input argument 4, it should return False (number 4 is divisible by 2). Task 6 (15 pts.) Write a function named random_element(s) that: Takes one argument, a string s. Returns a random letter from string s. For example, if I pass it the string "hello!", it may return "h", or "e", or any other of the letters in "hello", but it should not return "m" or "n", since those letters are not in "hello!". Your function will return random things each time you pass it the same string. To have Python choose a random integer from n to m, including both n and m, you can use the following code: # Place this line at the beginning of your file. from random import randint # This line generates a random integer from values n, n+1, ..., m, and stores it in random_index. random_index = randint(n, m) Task 7 (10 pts.) Write a function named reverse_vowels(s) that: Takes one argument, a string named s. Returns a string of all vowels in s, in REVERSE order. For example: For input argument "hello!", it should return "oe". For input argument "New Zealand", it should return "aaee". For input argument "TUESDAY", it should return "AEU".
© Copyright 2024