|
import csv |
|
import numpy as np |
|
import random |
|
|
|
def change_fourth_occurrence(matrix): |
|
for col in range(len(matrix[0])): |
|
count = 1 |
|
for row in range(1, len(matrix)): |
|
if matrix[row][col] == matrix[row - 1][col]: |
|
count += 1 |
|
else: |
|
count = 1 |
|
|
|
if count == 4: |
|
current_number = matrix[row][col] |
|
|
|
|
|
next_number = random.choice([current_number - 1, current_number + 1]) |
|
|
|
matrix[row][col] = next_number |
|
count = 1 |
|
|
|
|
|
matrix[matrix < 0] = 0 |
|
|
|
|
|
input_matrix = [] |
|
with open('matrix2.csv', 'r') as file: |
|
csv_reader = csv.reader(file) |
|
for row in csv_reader: |
|
input_matrix.append([int(num) for num in row]) |
|
|
|
|
|
input_matrix = np.array(input_matrix) |
|
|
|
|
|
change_fourth_occurrence(input_matrix) |
|
|
|
|
|
with open('matrix5.csv', 'w', newline='') as file: |
|
csv_writer = csv.writer(file) |
|
csv_writer.writerows(input_matrix) |
|
|