asked 180k views
3 votes
Hello, I already posted this question but it was not fully answered, and part was incorrect. Please answer whole question as I have a test in a few days and I am really struggling. I will upvote immediately for correct answer, thank you!

Create a Python program that processes a text file that contains several arrays.
The text file would appear as shown below:
*START OF TEXT FILE*
A, 1,2,3
A, 4,5,6
B, 1
A, 3,4,4
B, 2
*END OF TEXT FILE*
The rows of the matrices can be interspersed. For example, the file contains an array A, 3, 3 and an array B, 2, 1.
There may be blank lines.
The program must work for each input file that respects the syntax described
The program must calculate the information required in the following points. For each point the program creates a text file called respectively 1.txt, 2.txt, 3.txt, 4.txt, 5.txt in which to write the answer.
At this point I call A the first matrix. Print all the matrices whose values are included in those of the A matrix
For each square matrix, swap the secondary diagonal with the first column
For each matrix, calculate the average of all its elements
Rearrange the rows of each matrix so that it goes from the highest sum to the lowest sum row
Print sudoku matrices (even non-square), ie those for which the sum of all rows, and all columns has the same value.

asked
User Jumah
by
8.0k points

1 Answer

0 votes

Answer:

To create a Python program that processes a text file containing several arrays, you can use the following code:

import numpy as np

import os

# Read input file

with open('input.txt', 'r') as f:

contents = f.readlines()

# Create dictionary to store matrices

matrices = {}

# Loop over lines in input file

for line in contents:

# Remove whitespace and split line into elements

elements = line.strip().split(',')

# Check if line is empty

if len(elements) == 0:

continue

# Get matrix name and dimensions

name = elements[0]

shape = tuple(map(int, elements[1:]))

# Get matrix data

data = np.zeros(shape)

for i in range(shape[0]):

line = contents.pop(0).strip()

while line == '':

line = contents.pop(0).strip()

row = list(map(int, line.split(',')))

data[i,:] = row

# Store matrix in dictionary

matrices[name] = data

# Create output files

output_dir = 'output'

if not os.path.exists(output_dir):

os.mkdir(output_dir)

for i in range(1, 6):

output_file = os.path.join(output_dir, str(i) + '.txt')

with open(output_file, 'w') as f:

# Check which point to process

if i == 1:

# Print matrices with values included in A matrix

A = matrices['A']

for name, matrix in matrices.items():

if np.all(np.isin(matrix, A)):

f.write(name + '\\')

f.write(str(matrix) + '\\\\')

elif i == 2:

# Swap secondary diagonal with first column in square matrices

for name, matrix in matrices.items():

if matrix.shape[0] == matrix.shape[1]:

matrix[:,[0,-1]] = matrix[:,[-1,0]] # Swap columns

matrix[:,::-1] = np.fliplr(matrix) # Flip matrix horizontally

f.write(name + '\\')

f.write(str(matrix) + '\\\\')

elif i == 3:

# Calculate average of all elements in each matrix

for name, matrix in matrices.items():

f.write(name + '\\')

f.write(str(np.mean(matrix)) + '\\\\')

elif i == 4

Step-by-step explanation:

answered
User Nickolodeon
by
8.1k points

No related questions found