Untitled

mail@pastecode.io avatar
unknown
plain_text
4 months ago
1.5 kB
9
Indexable
def count_nearly_regular_crosses(matrix):
    def is_nearly_regular(matrix, row, col):
        cross_value = matrix[row][col]
        row_elements = matrix[row]
        col_elements = [matrix[i][col] for i in range(len(matrix))]
        
        # Count differences in the row and column
        row_diff_count = sum(1 for x in row_elements if x != cross_value)
        col_diff_count = sum(1 for x in col_elements if x != cross_value)
        
        # Check if all differences are only in the intersection point
        if row_diff_count == 0 and col_diff_count == 0:
            return False
        elif row_diff_count == 0 and col_diff_count == 1:
            return True
        elif row_diff_count == 1 and col_diff_count == 0:
            return True
        elif row_diff_count == 1 and col_diff_count == 1:
            # Ensure the intersection point is the only differing element
            intersection_differs = matrix[row][col] != cross_value
            return intersection_differs
        
        return False

    rows = len(matrix)
    cols = len(matrix[0])
    nearly_regular_crosses_count = 0

    for i in range(rows):
        for j in range(cols):
            if is_nearly_regular(matrix, i, j):
                nearly_regular_crosses_count += 1

    return nearly_regular_crosses_count


# Example usage:
matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 5, 9]
]

result = count_nearly_regular_crosses(matrix)
print(f"Number of nearly regular crosses: {result}")
Leave a Comment