# 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 ones are added

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


# 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, and fill the rightmost column with 1."""
        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] = 1  # Fill the rightmost column with 1

    def move_right():
        """Move all 1s in the matrix one step to the right, and fill the leftmost column with 1."""
        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] = 1  # Fill the leftmost column with 1

    def move_up():
        """Move all 1s in the matrix one step up, and fill the bottom row with 1."""
        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] = 1  # Fill the bottom row with 1

    def move_down():
        """Move all 1s in the matrix one step down, and fill the top row with 1."""
        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] = 1  # Fill the top row with 1

    # 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": [[1, 1, 1], [1, 0, 0], [1, 0, 0]]
        },
        {
            "input": ([[0, 1, 0], [0, 1, 1], [0, 0, 1]], ["U"]),
            "expected": [[0, 1, 1], [0, 0, 1], [1, 1, 1]]
        },
        {
            "input": ([[0, 1, 0], [0, 1, 1], [0, 0, 1]], ["D"]),
            "expected": [[1, 1, 1], [0, 1, 0], [0, 1, 1]]
        },
        {
            "input": ([[0, 1, 0], [0, 1, 1], [0, 0, 1]], ["L", "D"]),
            "expected": [[1, 1, 1], [1, 0, 1], [1, 1, 1]]
        },
        {
            "input": ([[0, 1, 0], [0, 1, 1], [0, 0, 1]], ["L", "U", "D", "D"]),
            "expected": [[1, 1, 1], [1, 1, 1], [1, 1, 1]]
        },
        {
            "input": ([[1, 1, 1], [0, 0, 1], [0, 0, 1]], ["L", "D", "U", "R", "L"]),
            "expected": [[1, 1, 1], [0, 1, 1], [1, 1, 1]]
        },
        {
            "input": ([[0, 1, 0, 1], [0, 0, 1, 1], [1, 0, 0, 1], [1, 1, 1, 1]], ["L", "U", "D", "D", "R", "R"]),
            "expected": [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 0, 1], [1, 1, 0, 0]]
        },
        {
            "input": ([[0, 1, 0, 1], [0, 0, 1, 1], [1, 0, 0, 1], [1, 1, 1, 1]], ["L", "D", "R", "R"]),
            "expected": [[1, 1, 1, 1], [1, 1, 1, 0], [1, 1, 0, 1], [1, 1, 0, 0]]
        },
        {
            "input": ([[0, 1, 0, 1], [0, 0, 1, 1], [1, 0, 0, 1], [1, 1, 1, 1]], ["L", "D", "L", "R"]),
            "expected":[[1, 1, 1, 1], [1, 0, 1, 1], [1, 1, 1, 1], [1, 0, 1, 1]]
        },
        {
            "input": ([[0, 1, 1, 1], [1, 0, 1, 1], [1, 0, 0, 1], [1, 1, 1, 1]], ["L", "U", "L", "R"]),
            "expected": [[1, 1, 1, 1], [1, 0, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]
        },
    ]

    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()