# 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 moves all values to the left, move the leftmost column to the rightmost.
   - R moves all values to the right, move the rightmost column to the leftmost. 
   - U moves all values up, move the top row to the bottom.
   - D moves all values down, move the bottom row to the top.
4. Each operation shows:
   - Initial state
   - Intermediate state with '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 moves all values to the left, move the leftmost column to the rightmost.
   - R moves all values to the right, move the rightmost column to the leftmost. 
   - U moves all values up, move the top row to the bottom.
   - D moves all values down, move the bottom row to the top.


# 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 nested lists, where each inner list represents a row.
        operations: A sequence of operation codes, each being one of the four possible uppercase characters.

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

    def move_left():
        """Move all elements in the matrix one step to the left with cyclic movement."""
        for i in range(rows):
            # Store the first element to be moved to the end
            first_element = matrix[i][0]
            for j in range(cols - 1):
                matrix[i][j] = matrix[i][j + 1]
            matrix[i][cols - 1] = first_element

    def move_right():
        """Move all elements in the matrix one step to the right with cyclic movement."""
        for i in range(rows):
            # Store the last element to be moved to the beginning
            last_element = matrix[i][cols - 1]
            for j in range(cols - 1, 0, -1):
                matrix[i][j] = matrix[i][j - 1]
            matrix[i][0] = last_element

    def move_up():
        """Move all elements in the matrix one step up with cyclic movement."""
        for j in range(cols):
            # Store the first row element to be moved to the end
            first_element = matrix[0][j]
            for i in range(rows - 1):
                matrix[i][j] = matrix[i + 1][j]
            matrix[rows - 1][j] = first_element

    def move_down():
        """Move all elements in the matrix one step down with cyclic movement."""
        for j in range(cols):
            # Store the last row element to be moved to the beginning
            last_element = matrix[rows - 1][j]
            for i in range(rows - 1, 0, -1):
                matrix[i][j] = matrix[i - 1][j]
            matrix[0][j] = last_element

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