Generation

generate functionWed, 16 Nov 2022

Due to a certain reason, the government has to consider closing tube lines between immediate neighbouring stations as many as possible, while meeting the following condition: 1. A journey between any pair of stations should be still possible. For instance, even if the line between immediate neighbouring stations, Piccadilly Circus and Green Park, is closed, the journey between those two stations should be still possible on a different route. Find this alternate route, and print it. Use pandas and the london_underground_data.xlsx

import pandas as pd import numpy as np #read file df = pd.read_excel("london_underground_data.xlsx") #change to a dict format df = df.set_index("Line") df = df.to_dict() df = df["Stations"] #get all stations all_stations = [] for i in df: all_stations.extend(df[i]) #get all lines all_lines = set(all_stations) #create empty adjacency matrix adj_matrix = pd.DataFrame(np.zeros((len(all_stations), len(all_stations))), index=all_stations, columns=all_stations) #fill in all the lines for i in df: for j in range(len(df[i])): if j != len(df[i]) - 1: line = df[i][j] line_next = df[i

Questions about programming?Chat with your personal AI assistant