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? Do not simply give the output from running it with a sample string, but try to figure out what its purpose is. Your answer should be understandable by someone who does not know any programming.
def function1(s): s = s.lower() vowels = 'aeiou' for x in s: if x in vowels: return x return None
How does this function differ? What does it do? Again, Your answer should be understandable by someone who does not know any programming.
def function2(s): s = s.lower() vowels = 'aeiou' for x in s: if x in vowels: return True return False
Put your answer in answers.xxx.
Write a function named compute_differences(n1, n2, n3)
that:
n1, n2, n3
, that are expected to be numbers (integers or floats).
None
.
n1, n2, n3
are numbers, the function returns the list [n2 - n1, n3 - n2]
, i.e., a list of two elements, the first element being the difference between n2 and n1, and the second element being the difference between n3 and n2.
For example:
compute_differences(15, 2.5, 30)
should return [-12.5, 27.5]
.
compute_differences(5, 10, 30)
should return [5, 20]
.
Save the function in file task2_functions.py, and code testing your function in file task2_main.py.
Write a function named list_powers(n1, n2)
that:
n1, n2
.
None
.
None
.
n1, n2
are integers greater than or equal to 0, the function returns the list [n1**0, n1**1, n**2, ..., n1**n2]
.
For example:
list_powers(15, -1)
should return None
.
list_powers('hello', 1)
should return None
.
list_powers(2, 0)
should return [1]
.
list_powers(3, 1)
should return [1, 3]
.
list_powers(3, 4)
should return [1, 3, 9, 27, 81]
.
Save the function in file task3_functions.py, and code testing your function in file task3_main.py.
Write a function named foo(num)
that:
num
.
num
is not an integer or float, it returns None
.
num
is a number, it returns 3*num + 1.
For example:
foo(10)
should return 31
.
foo(2.5)
should return 8.5
.
Save the function in file task4_functions.py, and code testing your function in file task4_main.py.
Write a function named foo(num)
that:
num
.
num
is not an integer or float, it returns None
.
num
is an integer, it returns 3*num + 1.
num
is a float, it returns num - 1.
For example:
None
, since the argument is a string.
31.14
, since the argument is a float.
31
, the argument is an integer.
9.0
, since the argument is a float.
Save the function in file task5_functions.py, and code testing your function in file task5_main.py.
In this task, you will implement some functions that can help decide if a string is a float number, and can be safely converted to such a number using the float
function.
Part a (10 points).
Implement a function check_float_chars
with the following specifications:
text
.
True
if each character in text
is either white space (you can use the isspace
method of strings for that), a dot (character "."), a minus sign (character "-"), or a digit (0, 1, 2, 3, 4, 5, 6, 7, 8, 9).
False
otherwise, i.e., if any character in text
is neither white space, nor a dot, nor a digit.
True
.
True
.
False
, since it contains letters.
False
, since it contains punctuation.
Save the function in file task6a_functions.py, and code testing your function in file task6a_main.py.
Part b (10 points).
Implement a function check_dots
with the following specifications:
text
.
True
if the string has zero or one dot characters ("." characters).
False
otherwise, i.e., if text
contains two or more dots.
For example:
True
.
True
.
True
.
False
, since it contains two dots.
Part c (10 points).
Implement a function check_spaces
with the following specifications:
text
.
True
if the string has no white space characters between non-white-space characters.
False
otherwise, i.e., if text
contains one or more white-space characters between non-white-space characters (again, you can use the string method isspace
to check if a character is white space).
For example:
True
.
True
, since all white space characters are at the beginning or the end.
True
, since all white space characters are at the beginning or the end.
False
, since there is white space between "32" and "14".
False
, since there is white space between "number" and "32.14"
False
, since there are several cases where white space appears between non-white-space characters.
Part d (10 points).
Implement a function check_float
with the following specifications:
text
.
True
if the string contains a valid float number, and can be safely be cast to a float using the float
function.
False
otherwise, i.e., if the string does not contains a valid float number, and float(text)
produces an error message.
For example:
True
.
True
.
False
, since float(" a.32!#.14 ")
gives an error message.
False
, since float(" 32 14 ")
gives an error message.
False
, since float("number 32.14")
gives an error message.
False
, since float(" 44.21.12 ")
gives an error message.
The assignment should be submitted via Blackboard. Submit a ZIPPED directory called assignment4.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).