Question
write a couple of Python functions that deal with two-dimensional lists. reverse_transpose , which takes a two-dimensional matrix of objects it doesn't matter what kind
write a couple of Python functions that deal with two-dimensional lists.
- reverse_transpose, which takes a two-dimensional matrix of objects it doesn't matter what kind of objects they are, but you can assume that the matrix is rectangular and that there's at least one row and at least one column and returns a new two-dimensional matrix that is the reverse transpose of the given one. So, what's the reverse transpose? It requires two kinds of changes:
- By "transpose," I mean that the resulting list uses columns where the original list uses rows, and uses rows where the original list uses columns. So, for example, if the original list had a row containing the values 1, 2, and 3, the new list would have a column containing the values 1, 2, and 3.
- By "reverse," I mean that the first row in the original list would become the last column in the new list, that the first column in the original list would become the last row in the new list, and so on.
- calculate_sums, which takes a two-dimensional list of numbers and modifies it, so that, for each cell a[i][j], it contains the sum of the values of all cells a[m][n] that were originally in the list, where m i and n j.
Here's one quick example of each function in action:
>>> a = [['x', 'y', 'z'], ['w', 'v', 't'], ['p', 'r', 's']] >>> b = reverse_transpose(a) >>> a [['x', 'y', 'z'], ['w', 'v', 't'], ['p', 'r', 's']] >>> b [['s', 't', 'z'], ['r', 'v', 'y'], ['p', 'w', 'x']] >>> c = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] >>> calculate_sums(c) >>> c [[45, 33, 18], [39, 28, 15], [24, 17, 9]]
Docstrings are not necessary, since we've already agreed on what problems are being solved here.
Testing your functions
As usual, you'll need to write automated tests, but since we've learned some new techniques for that this week, let's put them to use. Using the unittest module from Python's standard library, write a unit test class for each of your functions with the proper assert functions to test. No specific requirement with regard to how many tests you write as the goal when writing tests is a qualitative, rather than quantitative, one you'll want to be sure that you've separated different kinds of test cases into separate methods in your unit test classes, and that you've thought carefully about what tests would be interesting to include, rather than just writing as many tests as you can without considering whether they're meaningfully different from one another
add more tests in this format
# unit test class class Test(unittest.TestCase): # method testing reverse_transpose() def test_reverse_transpose(self): a = [['x', 'y', 'z'], ['w', 'v', 't'], ['p', 'r', 's']] b = reverse_transpose(a) # assertListEqual ensures that two lists have same contents in same order. self.assertListEqual(a, [['x', 'y', 'z'], ['w', 'v', 't'], ['p', 'r', 's']]) self.assertListEqual(b, [['s', 't', 'z'], ['r', 'v', 'y'], ['p', 'w', 'x']]) a = [[1, 2], [3, 4], [5, 6]] b = reverse_transpose(a) self.assertListEqual(a, [[1, 2], [3, 4], [5, 6]]) self.assertListEqual(b, [[6, 4, 2], [5, 3, 1]]) # add more tests here if you want # method testing calculate_sums() def test_calculate_sum(self): c = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] calculate_sums(c) self.assertListEqual(c, [[45, 33, 18], [39, 28, 15], [24, 17, 9]]) c = [[10, 20], [20, 10]] calculate_sums(c) self.assertListEqual(c, [[60, 30], [30, 10]]) # add more tests here if you want if __name__ == '__main__': unittest.main()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started