# Problem Description
This is a matrix transformation problem where we need to implement shifting operations on a matrix. The operations move all elements in four possible directions (Left, Right, Up, Down), with specific rules for filling empty spaces after movement.

# Visual Facts
1. The matrix is 3x3 in all examples shown
2. Matrix contains only binary values (0 and 1)
3. Four operations are demonstrated:
   - L (Left shift)
   - R (Right shift)
   - U (Up shift)
   - D (Down shift)
4. Each operation shows:
   - Initial state
   - Intermediate state with 'del' and 'fill' annotations
   - Final state
5. Blue-colored cells represent the values being moved
6. Dotted boxes marked 'del' show removed elements
7. Dotted boxes marked 'fill' show where zeros are added

# Visual Patterns
1. Movement Rules:
   - L: All values shift left (matrix[i][j - 1] = matrix[i][j]), rightmost column filled with zeros
   - R: All values shift right (matrix[i][j + 1] = matrix[i][j]), leftmost column filled with zeros
   - U: All values shift up (matrix[i - 1][j] = matrix[i][j]), bottom row filled with zeros
   - D: All values shift down (matrix[i + 1][j] = matrix[i][j]), top row filled with zeros


# Solution
from typing import List

def solution(matrix: List[List[int]], operations: List[str]) -> List[List[int]]:
    """
    Transforms a matrix by applying a sequence of operations.

    Args:
        matrix: A matrix represented as a list of lists, where each inner list is a row.
        operations: A list of operation codes as uppercase characters. ['L', 'R', 'U', 'D']

    Returns:
        The transformed matrix after applying all operations in sequence.
    """
    rows, cols = len(matrix), len(matrix[0])

    def move_left():
        """Move all 1s in the matrix one step to the left."""
        for i in range(rows):
            for j in range(1, cols):  # Start from the second column
                matrix[i][j - 1] = matrix[i][j]
            matrix[i][cols - 1] = 0

    def move_right():
        """Move all 1s in the matrix one step to the right."""
        for i in range(rows):
            for j in range(cols - 2, -1, -1):  # Start from the second to last column
                matrix[i][j + 1] = matrix[i][j]
            matrix[i][0] = 0


    def move_up():
        """Move all 1s in the matrix one step up."""
        for j in range(cols):
            for i in range(1, rows):  # Start from the second row
                matrix[i - 1][j] = matrix[i][j]
            matrix[rows - 1][j] = 0

    def move_down():
        """Move all 1s in the matrix one step down."""
        for j in range(cols):
            for i in range(rows - 2, -1, -1):  # Start from the second to last row
                matrix[i + 1][j] = matrix[i][j]
            matrix[0][j] = 0

    # Map operations to corresponding functions
    operations_map = {
        "L": move_left,
        "R": move_right,
        "U": move_up,
        "D": move_down
    }

    # Apply each operation in sequence
    for op in operations:
        if op in operations_map:
            operations_map[op]()

    return matrix


# Test Script
def test():
    test_cases = [
        {
            "input": ([[1, 1, 1], [0, 0, 1], [0, 0, 1]], ["R"]),
            "expected": [[0, 1, 1], [0, 0, 0], [0, 0, 0]]
        },
        {
            "input": ([[0, 1, 0], [0, 1, 1], [0, 0, 1]], ["U"]),
            "expected": [[0, 1, 1], [0, 0, 1], [0, 0, 0]]
        },
        {
            "input": ([[0, 1, 0], [0, 1, 1], [0, 0, 1]], ["D"]),
            "expected": [[0, 0, 0], [0, 1, 0], [0, 1, 1]]
        },
        {
            "input": ([[0, 1, 0], [0, 1, 1], [0, 0, 1]], ["L", "D"]),
            "expected": [[0, 0, 0], [1, 0, 0], [1, 1, 0]]
        },
        {
            "input": ([[0, 1, 0], [0, 1, 1], [0, 0, 1]], ["L", "U", "D", "D"]),
            "expected": [[0, 0, 0], [0, 0, 0], [1, 1, 0]]
        },
        {
            "input": ([[1, 1, 1], [0, 0, 1], [0, 0, 1]], ["L", "D", "U", "R", "L"]),
            "expected": [[1, 1, 0], [0, 1, 0], [0, 0, 0]]
        },
        {
            "input": ([[0, 1, 0, 1], [0, 0, 1, 1], [1, 0, 0, 1], [1, 1, 1, 1]], ["L", "U", "D", "D", "R", "R"]),
            "expected": [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 1], [0, 0, 0, 0]]
        },
        {
            "input": ([[0, 1, 0, 1], [0, 0, 1, 1], [1, 0, 0, 1], [1, 1, 1, 1]], ["L", "D", "R", "R"]),
            "expected": [[0, 0, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1], [0, 0, 0, 0]]
        },
        {
            "input": ([[0, 1, 0, 1], [0, 0, 1, 1], [1, 0, 0, 1], [1, 1, 1, 1]], ["L", "D", "L", "R"]),
            "expected": [[0, 0, 0, 0], [0, 0, 1, 0], [0, 1, 1, 0], [0, 0, 1, 0]]
        },
        {
            "input": ([[0, 1, 1, 1], [1, 0, 1, 1], [1, 0, 0, 1], [1, 1, 1, 1]], ["L", "U", "L", "R"]),
            "expected": [[0, 1, 1, 0], [0, 0, 1, 0], [0, 1, 1, 0], [0, 0, 0, 0]]
        },
    ]

    for i, test_case in enumerate(test_cases, start=1):
        matrix, operations = test_case["input"]
        expected = test_case["expected"]
        
        try:
            output = solution(matrix, operations)
        except Exception as e:
            error_message = (
                f"An exception occurred while running the test case:\n"
                f"`matrix`: {matrix}\n"
                f"`operations`: {operations}\n\n"
                f"Exception: '{str(e)}'"
            )
            assert False, error_message

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

test()