############################################################ # COMPUTER SCIENCE UNDERGRADUATE SCHOLARSHIP EXAM # UNIVERSITY OF WAIKATO # PYTHON PROGRAMMING LANGUAGE # JULIANO TAVARES (TAURANGA BOYS' COLLEGE YR 13) ############################################################ # Import modules import sys # Welcome user print("Welcome to Visualising Graphs") # Build the backend of the grid for the graph def main(): graph_grid = [[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], ] # Set variables max_x = 8 max_y = 24 # Set scale for the graph grid (list) scale = [4, 3, 2, 1, 0, -1, -2, -3, -4] # Draw the grid for the graph graph_grid = [[" " for col in range(max_y + 1)] for row in range(max_x + 1)] # Draw the vertical line on the first column for col in range(max_y + 1): graph_grid[4][col] = "_" # Draw the horizontal line on the last row for row in range(max_x + 1): graph_grid[row][0] = "|" # Reading info from text file file = open("files/simple_graph_1.txt", "r") coordinates = file.read() print("These are the coordinates: ", coordinates) file.close() # Draw x's on specific coordinates for col in range(1, max_x + 1): # [1,1] graph_grid[3][2] = "x" # [2,2] graph_grid[2][3] = "x" # [3,2] graph_grid[2][4] = "x" # [4,1] graph_grid[3][5] = "x" # [5,0] graph_grid[4][6] = "x" # [6,-1] graph_grid[5][7] = "x" # [7,-2] graph_grid[6][8] = "x" # [8,-2] graph_grid[6][9] = "x" # [9,-1] graph_grid[5][10] = "x" # [10,0] graph_grid[4][11] = "x" # Showing the scale on the side of the grid for row, scale_value in zip(graph_grid, scale): # Convert each item in row to a string and join them print(f"{scale_value:<5}{''.join(str(item) for item in row)}") # Stage H - Writing to the file # Writing a string to file f.write("There are some new coordinates added to the file.") # Writing multiple strings # at a time file.writelines(L) # Closing file file.close() # Checking if the data is # written to file or not file = open('myfile.txt', 'r') print(file.read()) file.close()