Sequence Types: Strings and Lists ISTA 350 Hw1,

Introduction. This homework is intended to review and extend your knowledge of two of Python’s sequence types – strings and lists and how to go back and forth between them. Strings and lists share many functions, methods, and operations, such as len, index, and slicing. Each also has unique builtin functionality. In part of thisassignment, you will extend some of string’s unique functionality to lists and vice versa.

It is also intended to reinforce your understanding of mutability and immutability. Strings are immutable and cannot be changed in place, while lists are mutable and can be changed in place. Therefore, any function that does something to a string must return a new string, while list functions may return a new list or modify a list passed as an argument in place.
Instructions. Create a module named hw1.py. Below is the spec for ten functions. Implement them and upload your module to the D2L dropbox.

Testing. Download hw1_test.py and put it in the same folder as your hw1.py module. Run it from the command line to see your current correctness score. Each of the
10 functions is worth 10% of your correctness score. You can examine the test module in a text editor to understand better what your code should do. The test module is part of the spec. The test file we will use to grade your program will be different and may uncover failings in your work not evident upon testing with the provided file. Add
any necessary tests to make sure your code works in all cases.

Documentation. Your module must contain a header docstring containing your name, your section leader’s name, the date, ISTA 350 Hw1, and a brief summary of the module. Each function must contain a docstring. Each function docstring should include a description of the function’s purpose, the name, type, and purpose of each
parameter, and the type and meaning of the function’s return value.

Grading. Your module will be graded on correctness, documentation, and coding style. Code should be clear and concise. You will only lose style points if your code
is a real mess. Include inline comments to explain tricky lines and summarize sections of code (not necessary on this assignment).

This function is right off last semester’s 130 final. sort_int_string takes one string parameter. Assume the string will be a series of integers separated by spaces. Ignore any extra whitespace. The empty string or a whitespace string return the empty string.
Otherwise, the function returns a string with the argument’s integers separated by spaces but now in sorted order.
Do not check for invalid strings. For instance, if the argument is "43 -1 17", the
function returns "-1 17 43".
dash_reverse: This function takes one string argument and returns a new string with the same characters as in the argument but in reverse order and separated by dashes.

xslice_replace: Python has extended slice functionality for lists, including assignment. Here’s an example showing what it looks like:

my_list = [777, 777, 777,
88, 777, 777] my_list[0:5:2] = [-2, 14, -22]

my_list

[-2, 777, 14, 88, -22, 777]



Your job is to mimic the functionality of extended slices for lists with the string function
xslice_replace(start_string, start, end, step, replacement_string). The integer parameter start corresponds to 0 in the above example, end to 5, and step to 2.
Your function will mimic Python’s extended slice assignment functionality for lists with the following exception:
• Create and return a new string since you cannot alter one in place.

Hint: you do not need to know anything about how extended slice assignment
for lists works beyond the basic syntax. If you do not see the trick, this function will kill you.
element_ip_replace:
The “ip” stands for “in-place”. This list function mimics Python’s replace
method for strings. It takes three arguments: a list to be altered, an item to be searched for, and a replacement value that will replace the item wherever it is found in the list. The replacement parameter has a default argument of None. Replace every occurrence of the item in the list with the replacement value.
element_nl_replace:
The “nl” stands for “new list”. This list function mimics Python’s replace method for strings. It takes three arguments: the original list, an item to be searched for, and a replacement value that will replace the item wherever it is found in the list. The replacement parameter has a default argument of None. Make and return a new list that is the same as the original list, except with every instance of the item replaced with the replacement value.
lreplace: (short for list replace) The previous two functions replace all of the
elements with a given value with a different value. This function searches for a sequence of values and replaces them with a different sequence of values. The first parameter is the list to be altered. The second is a list containing the sequence of values to be searched for, and the third is a list containing the replacement sequence of values. The third parameter has a default argument of the empty list. list_lt: This function takes two lists as arguments and returns a new list. If the two arguments are different lengths, return None. Otherwise, create a new list. Every entry in the new list will be either True or False. The entry in the new list at a given index
will be True if the element at that index in the first list is less than the corresponding
element in the second list, False otherwise. sum_of_powers: This is the hard list function, made harder because I am insisting that you use nested while loops in order to practice while loops. Perhaps do the next two functions first and save this one for last. This function has two list parameters. The first parameter is a list of bases; the second is a list of exponents. It returns a new list. The first entry in the new list is the sum of every entry in the first argument raised to the first entry in the second
list. The second entry is the sum of every entry in the first argument raised to the second entry in the second list. For example, if the first argument is [1, 2,3] and the second argument is [2], then the function returns [14], calculated 12 + 22 + 32. See the test file for more examples. trace: The trace
of a square matrix is the sum of its diagonal entries. This function takes one argument – a list of lists representing the matrix and returns the trace of that matrix. For instance, if the matrix is [[15, 777, -12], [ 5, 4, 13], [11, 7, -1]] then the trace is 15 + 4 – 1 = 18. You may assume that the argument is a nonempty square matrix of numbers. str_by_twos: This function takes a string argument and returns a list of each pair of adjacent characters in order. For example, str_by_twos(“abcd”) returns [‘ab’, ‘bc’, ‘cd’]. See the test module for more examples.
Powered by