In [3]:
def negate(matrix):
""" Function that accepts a list of lists of numbers and
returns a new list of lists with each of the numbers negated
"""
return [[-e for e in row] for row in matrix]
In [4]:
import unittest
class NegateTests(unittest.TestCase):
"""Tests for negate."""
def test_empty(self):
self.assertEqual(negate([[]]), [[]])
def test_single_item(self):
self.assertEqual(negate([[5]]), [[-5]])
def test_two_by_two_matrix(self):
inputs = [[1, 2], [3, 4]]
outputs = [[-1, -2], [-3, -4]]
self.assertEqual(negate(inputs), outputs)
def test_two_by_three_matrix(self):
inputs = [[1, 2, 3], [4, 5, 6]]
outputs = [[-1, -2, -3], [-4, -5, -6]]
self.assertEqual(negate(inputs), outputs)
def test_three_by_two_matrix(self):
inputs = [[1, 2], [3, 4], [5, 6]]
outputs = [[-1, -2], [-3, -4], [-5, -6]]
self.assertEqual(negate(inputs), outputs)
def test_three_by_three_matrix(self):
inputs = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
outputs = [[-1, -2, -3], [-4, -5, -6], [-7, -8, -9]]
self.assertEqual(negate(inputs), outputs)
def test_negative_numbers(self):
inputs = [[1, -2, 3], [-4, 5, -6], [7, -8, 9]]
outputs = [[-1, 2, -3], [4, -5, 6], [-7, 8, -9]]
self.assertEqual(negate(inputs), outputs)
if __name__ == "__main__":
unittest.main(argv=['first-arg-is-ignored'], exit=False)
In [ ]: