import csv import numpy as np from tabulate import tabulate Adj_matrix_length_width = 10 def read_reconstruct_data_table(file_path, matrix_len_wid): # Read the dataset from the provided CSV file with open(file_path, 'r') as file: adjacency_reconstructed = [] reader = csv.reader(file) print('**************************************************************************') print('Dataset received') print('**************************************************************************') print("Index\tCity\tAdjacency Data") for idx, row in enumerate(reader): # Read the dataset from the CSV file city_name = row[1] adjacency_data = row[2:] adjacency_info = np.zeros((matrix_len_wid, matrix_len_wid), dtype=int) id_x = 0 for i in range(matrix_len_wid): for j in range(i + 1, matrix_len_wid): adjacency_info[i][j] = int(row[id_x + 2]) adjacency_info[j][i] = int(row[id_x + 2]) # Mirror along the diagonal id_x += 1 adjacency_reconstructed.append((city_name, adjacency_info)) print(f"{idx}\t{city_name}\t{', '.join(adjacency_data)}") return adjacency_reconstructed # Challenge: Add labels for the node points, let them be called A-J def add_labels(matrix): nodelabels = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'] matrix_labeled = np.empty((11, 11), dtype=object) matrix_labeled[0, 1:] = nodelabels matrix_labeled[1:, 0] = nodelabels matrix_labeled[1:, 1:] = matrix return matrix_labeled # Visualize the reconstructed matrices def visualize_matrices_cliPrint(matrices): for city_name, matrix in matrices: print('\n*******************************') print(f"{city_name} adjacency matrix:") print('*******************************\n') matrix_labeled = add_labels(matrix) print(tabulate(matrix_labeled, tablefmt='fancy_grid')) print("\n") # Main function def main(): 'Loading the dataset CSV file path' file_path = 'MatrixTask/data.csv' # Challenge: Read the dataset from a CSV file containing the database entries # Challenge: Reconstruct the adjacency matrices based on the provided data adjacency_reconst_data = read_reconstruct_data_table(file_path, Adj_matrix_length_width) # Challenge: Visualize the reconstructed matrices in a command-line interface (CLI) print format visualize_matrices_cliPrint(adjacency_reconst_data) # Main program starts from here if __name__ == "__main__": main()