# 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 rearranges numbers within the specified row or column
- The goal is to transform the matrix according to the rules shown in the sequence

# Visual Facts
1. The matrix is 3x3 with columns labeled A,B,C and rows labeled D,E,F
2. Initial matrix contains numbers 1-9
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 green after each transformation
7. Numbers only move within their designated row or column

# Visual Patterns
1. Column Command Pattern (A,B,C):
   - When a column command is given, numbers in that column are sorted in ascending order from top to bottom
   - Example: Command "B" transforms column B from [6,5,7] to [5,6,7]

2. Row Command Pattern (D,E,F):
   - When a row command is given, numbers in that row are sorted in ascending order from left to right
   - Example: Command "E" transforms row E from [3,6,4] to [3,4,6]

3. Sorting Rules:
   - Sorting is always in ascending order
   - For columns: top to bottom ascending
   - For rows: left to right ascending

4. Operation Rules:
   - Each command operates independently
   - Numbers only move within their assigned row or column
   - Original positions of unaffected cells remain unchanged
   - The order of commands matters as each transformation builds on the previous state


# 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":
            # Sort the first column in ascending order
            col = sorted([matrix[i][0] for i in range(3)])
            for i in range(3):
                matrix[i][0] = col[i]
        elif command == "B":
            # Sort the second column in ascending order
            col = sorted([matrix[i][1] for i in range(3)])
            for i in range(3):
                matrix[i][1] = col[i]
        elif command == "C":
            # Sort the third column in ascending order
            col = sorted([matrix[i][2] for i in range(3)])
            for i in range(3):
                matrix[i][2] = col[i]
        elif command == "D":
            # Sort the first row in ascending order
            matrix[0] = sorted(matrix[0])
        elif command == "E":
            # Sort the second row in ascending order
            matrix[1] = sorted(matrix[1])
        elif command == "F":
            # Sort the third row in ascending order
            matrix[2] = sorted(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, 7, 9], [4, 7, 8]]
        },
        {
            'commands': ['A'],
            'matrix': [[4, 9, 6],[8, 3, 1],[3, 6, 7]],
            'expected': [[3, 9, 6], [4, 3, 1], [8, 6, 7]]
        },
        {
            'commands': ['C'],
            'matrix': [[4, 5, 6],[8, 1, 2],[3, 9, 7]],
            'expected': [[4, 5, 2], [8, 1, 6], [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': [[3, 5, 2], [4, 1, 6], [7, 8, 9]]
        },
        {
            'commands': ['A', 'B', 'C', 'D', 'E', 'F'],
            'matrix': [[4, 5, 6], [8, 1, 2], [3, 9, 7]],
            'expected': [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
        },
        {
            'commands': ['A', 'A'],
            'matrix': [[4, 5, 6], [8, 1, 2], [3, 9, 7]],
            'expected': [[3, 5, 6], [4, 1, 2], [8, 9, 7]]
        },
        {
            'commands': ['A', 'B', 'E', 'C', 'F'],
            'matrix': [[2, 3, 5], [7, 9, 1], [4, 6, 8]],
            'expected': [[2, 3, 5], [1, 4, 6], [7, 8, 9]]
        },
        {
            'commands': ['A', 'B', 'E', 'F'],
            'matrix': [[2, 3, 5], [7, 9, 1], [4, 6, 8]],
            'expected': [[2, 3, 5], [1, 4, 6], [7, 8, 9]]
        }
    ]
    for test_case in test_cases:
        commands = test_case['commands']
        matrix = test_case['matrix']
        expected_output = test_case['expected']
        
        try:
            output = solution(commands, matrix)
        except Exception as e:
            error_message = (
                f"An exception occurred while running the test case:\n"
                f"`commands`: {commands}\n"
                f"`matrix`: {matrix}\n\n"
                f"Exception: '{str(e)}'"
            )
            assert False, error_message

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

test()