def run(input_file_name, output_file_name): # Load data set input_file = parsing.CSV().from_file(input_file_name) header = input_file.read_line(sep=" ") for i in range(len(header)): header[i] = int(header[i]) rides = input_file.read_n_line(int(header[3]), " ") for i in range(len(rides)): rides[i].append(i) for i in range(len(rides)): for j in range(len(rides[0])): rides[i][j] = int(rides[i][j]) rides = sort.by_time_lastest(rides, header[3]) # Create fleet cars_1 = fleet.create(header[2]) cars_2 = fleet.create(header[2]) rides_2 = rides nb_ride_failed_1 = my_try_run(rides, cars_1) rides_2.reverse() nb_ride_failed_2 = my_try_run(rides_2, cars_2) # Export output_file = exporting.CSV().from_file(output_file_name) if nb_ride_failed_1 < nb_ride_failed_2: for car in cars_1: output_file.write_line(car.export(), sep=" ") else: for car in cars_2: output_file.write_line(car.export(), sep=" ") print("End of", input_file_name)
def test_get(self): file = parsing.CSV() file_name, stream = file.get() if file_name != "" or stream is not None: self.fail() file.from_file("sample.csv") file_name, stream = file.get() if file_name != "sample.csv" or stream is None: self.fail() file.close()
def test_write_line(self): file = exporting.CSV().from_file("sample.csv") file.write_line(['M', 'T', 'M']) file.close() file = parsing.CSV().from_file("sample.csv") line = file.read_line() if len(line) != 3: self.fail() if line[0] != 'M' or line[1] != 'T' or line[2] != 'M': self.fail() file.close()
def test_read_line(self): file = parsing.CSV().from_file("sample.csv") header = file.read_line() if len(header) != 4: self.fail() if header[0] != "col_1" or header[1] != "col_2" or header[2] != "col_3" or header[3] != "col_4": self.fail() first_line = file.read_line() if len(first_line) != 4: self.fail() if first_line[0] != "1" or first_line[1] != "2" or first_line[2] != "3" or first_line[3] != "4": self.fail() file.close()
def test_write_n_line(self): file = exporting.CSV().from_file("sample.csv") file.write_n_line([['M', 'T', 'M'], ['T', 'M', 'T'], ['M', 'T', 'M']], 2) file.close() file = parsing.CSV().from_file("sample.csv") lines = file.read_n_line(n=2) if len(lines) != 2: self.fail() if lines[0][0] != 'M' or lines[0][1] != 'T' or lines[0][2] != 'M': self.fail() if lines[1][0] != 'T' or lines[1][1] != 'M' or lines[1][2] != 'T': self.fail() file.close()
def test_read_all_line(self): file = parsing.CSV().from_file("sample.csv") header = file.read_line() if len(header) != 4: self.fail() if header[0] != "col_1" or header[1] != "col_2" or header[2] != "col_3" or header[3] != "col_4": self.fail() lines = file.read_all_line() if len(lines) != 3: self.fail() if len(lines[0]) != 4: self.fail() for i in range(12): if lines[i // 4][i % 4] != str(i + 1): self.fail() file.close()