# Problem Description
This is a matrix transformation problem where:
- We have a 3x3 matrix with initial numbers
- Commands are given as letters corresponding to rows (D,E,F) or columns (A,B,C)
- Each command negates (multiplies by -1) the numbers in the specified row or column
- The goal is to calculate the final state of the matrix after applying all commands sequentially

# Visual Facts
1. The matrix is 3x3 with columns labeled A,B,C and rows labeled D,E,F
2. Initial matrix contains these numbers:
   ```
   8 6 1
   3 5 4
   9 7 2
   ```
3. Each command is a single letter
4. Commands shown are: "B", "E", "C", "D", "B"
5. Each command affects either an entire row or column
6. Affected cells are highlighted in pink/red after each transformation
7. Only the signs of numbers change, their absolute values remain the same

# Visual Patterns
1. Column Command Pattern (A,B,C):
   - When a column command is given, all numbers in that column are negated
   - Example: Command "B" changes [6,5,7] to [-6,-5,-7]

2. Row Command Pattern (D,E,F):
   - When a row command is given, all numbers in that row are negated
   - Example: Command "E" changes [3,5,4] to [-3,5,-4]

3. Sign Change Rules:
   - Each command toggles the signs of numbers in the affected row/column
   - If a number is positive, it becomes negative
   - If a number is negative, it becomes positive
   - Multiple operations on the same cell can cancel each other out


# Solution
from typing import List

def solution(commands: List[str], matrix: List[List[int]]) -> List[List[int]]:
    """
    Given a series of commands, transform the numbers of a matrix according to the rules shown in the image.

    Input:
    - command: A list of strings, where each string is a command that modifies the matrix.
               For example: ['A', 'B', 'E'].

    Output:
    - A 2d list of integers.
    """
    # Iterate through each command
    for command in commands:
        if command == "A":
            # Convert the first column numbers to their opposites
            for i in range(3):
                matrix[i][0] = -matrix[i][0]
        elif command == "B":
            # Convert the second column numbers to their opposites
            for i in range(3):
                matrix[i][1] = -matrix[i][1]
        elif command == "C":
            # Convert the third column numbers to their opposites
            for i in range(3):
                matrix[i][2] = -matrix[i][2]
        elif command == "D":
            # Convert the first row numbers to their opposites
            matrix[0] = [-x for x in matrix[0]]
        elif command == "E":
            # Convert the second row numbers to their opposites
            matrix[1] = [-x for x in matrix[1]]
        elif command == "F":
            # Convert the third row numbers to their opposites
            matrix[2] = [-x for x in matrix[2]]

    return matrix


# Test Script
def test():
    test_cases = [
        {
            'commands': ['B'],
            'matrix': [[7, 1, 8],[1, 1, 3],[7, 5, 9]],
            'expected': [[7, -1, 8], [1, -1, 3], [7, -5, 9]]
        },
        {
            'commands': ['E'],
            'matrix': [[3, 7, 2],[6, 9, 7],[4, 7, 8]],
            'expected': [[3, 7, 2], [-6, -9, -7], [4, 7, 8]]
        },
        {
            'commands': ['A'],
            'matrix': [[4, 9, 6],[8, 3, 1],[3, 6, 7]],
            'expected': [[-4, 9, 6], [-8, 3, 1], [-3, 6, 7]]
        },
        {
            'commands': ['C'],
            'matrix': [[4, 5, 6],[8, 1, 2],[3, 9, 7]],
            'expected': [[4, 5, -6], [8, 1, -2], [3, 9, -7]]
        },
        {
            'commands': ['B', 'E'],
            'matrix': [[1, 2, 3],[4, 5, 6],[7, 8, 9]],
            'expected': [[1, -2, 3], [-4, 5, -6], [7, -8, 9]]
        },
        {
            'commands': ['A', 'C', 'F'],
            'matrix': [[4, 5, 6], [8, 1, 2], [3, 9, 7]],
            'expected': [[-4, 5, -6], [-8, 1, -2], [3, -9, 7]]
        },
        {
            'commands': ['A', 'B', 'C', 'D', 'E', 'F'],
            'matrix': [[4, 5, 6], [8, 1, 2], [3, 9, 7]],
            'expected': [[4, 5, 6], [8, 1, 2], [3, 9, 7]]
        },
        {
            'commands': ['A', 'A'],
            'matrix': [[4, 5, 6], [8, 1, 2], [3, 9, 7]],
            'expected': [[4, 5, 6], [8, 1, 2], [3, 9, 7]]
        },
        {
            'commands': ['A', 'B', 'E', 'C', 'F'],
            'matrix': [[2, 3, 5], [7, 9, 1], [4, 6, 8]],
            'expected': [[-2, -3, -5], [7, 9, 1], [4, 6, 8]]
        },
        {
            'commands': ['A', 'B', 'E', 'F'],
            'matrix': [[2, 3, 5], [7, 9, 1], [4, 6, 8]],
            'expected': [[-2, -3, 5], [7, 9, -1], [4, 6, -8]]
        }
    ]
    for test_case in test_cases:
        try:
            output = solution(test_case['commands'], test_case['matrix'])
        except Exception as e:
            error_message = (
                f"An exception occurred while running the test case:\n"
                f"`commands`: {test_case['commands']}\n"
                f"`matrix`: {test_case['matrix']}\n\n"
                f"Exception: '{str(e)}'"
            )
            assert False, error_message

        if output != test_case['expected']:
            error_message = (
                f"The following Test Case failed:\n"
                f"`commands`: {test_case['commands']}\n" 
                f"`matrix`: {test_case['matrix']}\n\n"
                f"Actual Output: {output}\n"
                f"Expected Output: {test_case['expected']}"
            )
            assert False, error_message

test()