|
| 1 | +import inspect |
| 2 | + |
| 3 | + |
| 4 | +def transpose(matrix: list) -> list: |
| 5 | + """ |
| 6 | + Transpose a 2D matrix |
| 7 | +
|
| 8 | + Args: |
| 9 | + matrix: list: A 2D list |
| 10 | +
|
| 11 | + Returns: |
| 12 | + list: A 2D list that is the transpose of the input matrix |
| 13 | +
|
| 14 | + Raises: |
| 15 | + TypeError: If the input is not a list |
| 16 | + ValueError: If the input is an empty list or not a 2D list or rows have different lengths |
| 17 | + """ |
| 18 | + if type(matrix) is not list: |
| 19 | + raise TypeError("Input must be a list") |
| 20 | + if len(matrix) == 0: |
| 21 | + raise ValueError("Input must not be empty") |
| 22 | + if not all([type(row) is list for row in matrix]): |
| 23 | + raise ValueError("Input must be a 2D list") |
| 24 | + if not all([len(row) == len(matrix[0]) for row in matrix]): |
| 25 | + raise ValueError("All rows must have the same length") |
| 26 | + return [[matrix[j][i] for j in range(len(matrix))] for i in range(len(matrix[0]))] |
| 27 | + |
| 28 | + |
| 29 | +def main(): |
| 30 | + total_points = 0 # Total points for the test cases: get 5 points for each test case |
| 31 | + # Test cases for transpose function |
| 32 | + try: |
| 33 | + # test if we have a callable function named transpose |
| 34 | + assert callable(transpose) |
| 35 | + print("PASS: You have a function named transpose") |
| 36 | + total_points += 5 |
| 37 | + except AssertionError: |
| 38 | + print("FAIL: You need a function named transpose") |
| 39 | + |
| 40 | + try: |
| 41 | + # test if the function transpose takes one argument |
| 42 | + assert len(inspect.signature(transpose).parameters) == 1 |
| 43 | + print("PASS: The function takes one argument") |
| 44 | + total_points += 5 |
| 45 | + except AssertionError: |
| 46 | + print("FAIL: The function should take one argument") |
| 47 | + |
| 48 | + try: |
| 49 | + # test if the function has annotation for the argument |
| 50 | + assert inspect.signature(transpose).parameters["matrix"].annotation == list |
| 51 | + print("PASS: The function has type annotation for the argument") |
| 52 | + total_points += 5 |
| 53 | + except AssertionError: |
| 54 | + print("FAIL: The function should have type annotation for the argument") |
| 55 | + |
| 56 | + try: |
| 57 | + # test if the function has annotation for the return value |
| 58 | + assert inspect.signature(transpose).return_annotation == list |
| 59 | + print("PASS: The function has type annotation for the return value") |
| 60 | + total_points += 5 |
| 61 | + except AssertionError: |
| 62 | + print("FAIL: The function should have type annotation for the return value") |
| 63 | + |
| 64 | + try: |
| 65 | + # test if the function has a docstring |
| 66 | + assert transpose.__doc__ is not None |
| 67 | + print("PASS: The function has a docstring") |
| 68 | + total_points += 5 |
| 69 | + except AssertionError: |
| 70 | + print("FAIL: The function should have a docstring") |
| 71 | + |
| 72 | + try: |
| 73 | + transpose(1) |
| 74 | + except TypeError as e: |
| 75 | + assert str(e) == "Input must be a list" |
| 76 | + print("PASS: The function raises TypeError for non-list input") |
| 77 | + total_points += 5 |
| 78 | + else: |
| 79 | + print("FAIL: The function should raise TypeError for non-list input") |
| 80 | + |
| 81 | + try: |
| 82 | + transpose([]) |
| 83 | + except ValueError as e: |
| 84 | + assert str(e) == "Input must not be empty" |
| 85 | + print("PASS: The function raises ValueError for empty input") |
| 86 | + total_points += 5 |
| 87 | + else: |
| 88 | + print("FAIL: The function should raise ValueError for empty input") |
| 89 | + |
| 90 | + try: |
| 91 | + transpose([[1, 2], 3]) |
| 92 | + except ValueError as e: |
| 93 | + assert str(e) == "Input must be a 2D list" |
| 94 | + print("PASS: The function raises ValueError for non-2D list input") |
| 95 | + total_points += 5 |
| 96 | + else: |
| 97 | + print("FAIL: The function should raise ValueError for non-2D list input") |
| 98 | + |
| 99 | + try: |
| 100 | + transpose([[1, 2], [3, 4, 5]]) |
| 101 | + except ValueError as e: |
| 102 | + assert str(e) == "All rows must have the same length" |
| 103 | + print("PASS: The function raises ValueError for rows of different lengths") |
| 104 | + total_points += 5 |
| 105 | + else: |
| 106 | + print( |
| 107 | + "FAIL: The function should raise ValueError for rows of different lengths" |
| 108 | + ) |
| 109 | + |
| 110 | + try: |
| 111 | + result = transpose([[1, 2], [3, 4]]) |
| 112 | + assert result == [[1, 3], [2, 4]] |
| 113 | + print("PASS: The function returns the correct result for a 2x2 matrix") |
| 114 | + total_points += 5 |
| 115 | + except AssertionError: |
| 116 | + print("FAIL: The function should return the correct result for a 2x2 matrix") |
| 117 | + |
| 118 | + try: |
| 119 | + result = transpose([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) |
| 120 | + assert result == [[1, 4, 7], [2, 5, 8], [3, 6, 9]] |
| 121 | + print("PASS: The function returns the correct result for a 3x3 matrix") |
| 122 | + total_points += 5 |
| 123 | + except AssertionError: |
| 124 | + print("FAIL: The function should return the correct result for a 3x3 matrix") |
| 125 | + |
| 126 | + try: |
| 127 | + result = transpose([[1, 2, 3], [4, 5, 6]]) |
| 128 | + assert result == [[1, 4], [2, 5], [3, 6]] |
| 129 | + print("PASS: The function returns the correct result for a 2x3 matrix") |
| 130 | + total_points += 5 |
| 131 | + except AssertionError: |
| 132 | + print("FAIL: The function should return the correct result for a 2x3 matrix") |
| 133 | + |
| 134 | + print("Total points: ", total_points) |
| 135 | + print("All test cases passed") |
| 136 | + return None |
| 137 | + |
| 138 | + |
| 139 | +if __name__ == "__main__": |
| 140 | + main() |
0 commit comments