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. 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(x, 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 z = 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
For tasks 3, 4, and 5, you will be writing some useful string processing functions. For each function, you need to:
It is often useful to split a string at a specified character. As a programmer, you should not have to rely on the language
to provide a function to do so. Write a function named split(string, delimiter)
that:
string
and a string delimiter
(a single character) as argumentsstring
at each occurrence of delimiter
and puts the substrings in a listFor example, if I call split("hello:nice to see you:goodbye", ':')
, it will return
['hello', 'nice to see you', 'goodbye']
. As you can see, it split the string at ':'
and put each substring into a list.
For this task, delimiter
should be a single character. You are not allowed to any of Python's built-in
string methods (i.e. string.split()). Save your function in task3_functions.py and your test program as task3_main.py.
Another useful tool for string processing is the ability to remove certain characters from strings. Write a function
called strip(string, characters)
that:
string
and a string characters
as argumentscharacters
from the beginning and end of string
For both the left and right side of the string, your function will stop looking for characters to remove when
it hits the first character not in characters
. So, if I call strip(".$_hello_bye_&", ",&$_.")
, it will
return 'hello_bye'
, because it removes any of the characters in ",&$_."
from the beginning and end.
Notice that the function stops stripping on the left when it encounters 'h' and stops stripping on the
right when it encounters 'e', since they are not in the list of characters to remove, and leaves the '_' between the words.
You are not allowed to use any of Python's built-in string methods (i.e. string.strip(), string.lstrip(), string.rstrip(), etc)
in your code. Save your funtion as task4_functions.py and your test program as task4_main.py.
The final function you will write for string processing joins elements from a list into a string. Write a function
join(string_list, join_character)
that:
string_list
of strings and a string join_character
(which is a single character) as argumentsjoin_character
between each stringFor example, if I call join(['spam', 'eggs', 'bacon'], '*')
, it will return 'spam*eggs*bacon'
. You
are not allowed to use any of Python's built-in string methods (i.e. string.join()). Save your function in task5_functions.py
and your test program in task5_main.py.
Pay close attention to all specifications on this page, including file names and submission format. Even in cases where the program works correctly, points will be taken off for non-compliance with the instructions given on this page (such as wrong file names, wrong compression format for the submitted code, and so on). The reason is that non-compliance with the instructions makes the grading process significantly (and unnecessarily) more time consuming. Contact the instructor or TA if you have any questions.
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).