File size: 4,052 Bytes
ca165c7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
import csv
import os
import random
def print_matrix(matrix):
for row in matrix:
print(','.join(map(str, row)))
def generate_random_values(num_values):
return [random.randint(0, 8) for _ in range(num_values)]
def randomize_l1(matrix, random_values, identical_positions, combination_positions):
for i in range(len(matrix)):
if (i, i + 1) in identical_positions or (i + 1, i) in identical_positions:
# Use the same random value for identical positions
matrix[i][0] = random_values[i]
matrix[i + 1][0] = random_values[i]
elif (i, i) in combination_positions:
# Use the same random value for each letter within the combination
matrix[i][0] = random_values[i]
else:
# Use a common random value for non-identical and non-combination positions
matrix[i][0] = random_values[0]
# Function to find positions of the same letters in the text
def find_same_letter_positions(text):
same_letter_positions = []
for i in range(len(text)):
for j in range(i + 1, len(text)):
if text[i] == text[j]:
same_letter_positions.append((i, j))
same_letter_positions.append((j, i)) # Include (j, i) as well
return same_letter_positions
# Function to find positions of the specified combinations in the text
def find_combination_positions(text, combinations):
combination_positions = []
for combination in combinations:
i = 0
while i < len(text) - len(combination) + 1:
if text[i:i+len(combination)] == combination:
for j in range(i, i+len(combination)):
combination_positions.append((j, j))
i += len(combination) # Move to the next position after the combination
else:
i += 1
return combination_positions
# Function to create a matrix from the text
def create_matrix_from_text(text):
num_rows = len(text)
num_columns = num_rows
# Initialize a matrix with zeros
matrix = [[0] * num_columns for _ in range(num_rows)]
# Fill the matrix with data
for i in range(num_rows):
matrix[i][0] = text[i] # Assign letters to the first column
return matrix
# Process all text files from text1.csv to text5.csv
for i in range(1, 20):
csv_filename = f'text{i}.csv'
combo_filename = f'text{i}combo1.csv'
# Read the text from the CSV file
with open(csv_filename, 'r') as file:
reader = csv.reader(file)
text = next(reader)[0]
# Find positions of the same letters
same_letter_positions = find_same_letter_positions(text)
print(f"\n{text} - Same Letter Positions:", same_letter_positions)
# Specify the combinations to find
combinations_to_find = ["bl", "wh", "sa", "wo", "no", "ve", "ab", "gro", "pu", "lo", "co", "bus", "pla", "ac", "at", "pr", "fa", "gr", "to", "or", "fa", "fr", "ki", "qu", "cl", "ok", "fig", "run", "ee", "BL", "WH", "SA", "WO", "NO", "VE", "AB", "GRO", "PU", "LO", "CO", "BUS", "PLA", "AC", "AT", "PR", "FA", "GR", "TO", "OR", "FA", "FR", "KI", "QU", "CL", "OK", "FIG", "RUN", "EE"]
# Find positions of the specified combinations
combination_positions = find_combination_positions(text, combinations_to_find)
print(f"{text} - Combination Positions:", combination_positions)
# Create a matrix based on the number of letters in the text
matrix = create_matrix_from_text(text)
# Generate random values for each letter position
random_values = generate_random_values(len(text))
# Randomly set values for the L1 column
randomize_l1(matrix, random_values, same_letter_positions, combination_positions)
# Print the matrix after the first modifications
print(f"{text} - Matrix After Randomization:")
print_matrix(matrix)
# Save the matrix to the combo file
with open(combo_filename, 'w', newline='') as csvfile:
writer = csv.writer(csvfile)
for row in matrix:
writer.writerow(row)
|