The assignments will be graded out of 100 points. Create a text document entitled answers.xxx (where you replace xxx with whatever extension is appropriate, depending on the file format you use). Acceptable file formats are plain text, Word document, OpenOffice document, and PDF. Put your name and UTA ID at the top. Your answers that are not part of a program will be added to this file. Each task below will instruct you where to put your answers.
What does this function do? What might it be used for? Note: s1
and s2
are strings. Your answer should be understandable by someone who does not know programming. Put your answer in answers.xxx.
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
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. Put your answer in answers.xxx.
def function1(z): # line 1 values = [1, 2, 3] # line 2 return values[z] # line 3 # line 4 def function2(z, y): # line 5 if y < 0: # line 6 val = 0 # line 7 elif y == 10: # line 8 val = 1 # line 9 else: # line 10 val = 2 # line 11 x = function1(val) # line 12 return x*z # line 13 # line 14 x = 10 # line 15 y = 2 # line 16 print(function2(y, x)) # line 17
Write a function named repeat_number(s)
that:
num
.
num
3 times.
None
.
For example:
None
, because the input argument is a string.
None
, because the input argument is a string.
.
None
, because the input argument is a float.
.
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
.
Save the function in file task3_functions.py, and code testing your function in file task3_main.py.
Write a function named divisors(num, limit)
that:
num
, and an integer limit
.
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.
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]
.
Save the function in file task4_functions.py, and code testing your function in file task4_main.py.
Write a function named is_prime(s)
that:
num
.
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.
False
if num
is not prime.
None
.
For example:
None
, because the input argument is a string.
None
, because the input argument is a string.
.
None
, because the input argument is a float.
.
True
.
True
.
True
.
False
(number 4 is divisible by 2).
Save the function in file task5_functions.py, and code testing your function in file task5_main.py.
Write a function named squares(s)
that:
num
.
num-3
and num
None
.
For example:
None
, because the input argument is a string.
None
, because the input argument is a string.
.
[4, 1, 0, 1]
, since the integers from num-3 to num are -2, -1, 0, 1.
[100, 121, 144, 169]
, since the integers from num-3 to num are 10, 12, 13.
Save the function in file task6_functions.py, and code testing your function in file task6_main.py.
Write a function named random_element(s)
that:
s
.
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 different things each time you pass it the same string, but (by chance) it may return the same answer multiple times in a row. 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. You do not # need this line each time you generate a random number. 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)
Save the function in file task7_functions.py, and code testing your function in file task7_main.py.
Write a function named reverse_vowels(s)
that:
s
.
s
, in REVERSE order.
For example:
Save the function in file task8_functions.py, and code testing your function in file task8_main.py.
The assignment should be submitted via Blackboard. Submit a ZIPPED directory called assignment5.zip (no other forms of compression accepted, contact the instructor or TA if you do not know how to produce .zip files). The zipped directory should contain your answers.xxx document and all the Python code files (task5.py, task6.py, etc).