def test_distance_1(self): # Arrange from_house = House(0, 0) to_house = House(1, 3) # Act distance = from_house.distance(to_house) # Assert self.assertEqual(distance, 4)
def test_distance_2(self): # Arrange from_house = House(3, 2) to_house = House(2, 4) # Act distance = from_house.distance(to_house) # Assert self.assertEqual(distance, 3)
def test_instructions_6(self): # Arrange from_house = House(2, 5) to_house = House(4, 1) # Act instructions = from_house.instructions(to_house) text_instructions = convert_to_text_instructions(instructions) # Assert self.assertEqual(text_instructions, "EESSSSD")
def test_instructions_3(self): # Arrange from_house = House(2, 0) to_house = House(0, 0) # Act instructions = from_house.instructions(to_house) text_instructions = convert_to_text_instructions(instructions) # Assert self.assertEqual(text_instructions, "WWD")
def __find_path(self, houses: list['House']) -> list['House']: """Finds the path using the nearest neighbor algorithm""" current = House(0, 0) visited = set() path = [current] for house in range(len(houses)): nearest_index = self.__find_nearest(current, houses, visited) visited.add(nearest_index) current = houses[nearest_index] path.append(current) return path
def __parse_input(self, text_input: str) -> list['House']: """Converts the input string into a list of houses""" # remove all spaces for simpler/smaller regex text_input = text_input.replace(" ", "") # regex explanation: # - first part ^\d+x\d+: find exactly one NUMBERxNUMBER pattern in the begining # - second part (\(\d+\,\d+\))+$: after that find one or more (NUMBER,NUMBER) patterns till the end whole_regex = "^\d+x\d+(\(\d+\,\d+\))+$" grid_regex = "^\d+x\d+" coordinates_regex = "(\d+\,\d+)" match = re.search(whole_regex, text_input) if not match: raise BadInputFormatError( "Wrong format, respect the following format: \"NUMxNUM (NUM, NUM) (NUM, NUM)\"" ) grid_input = re.findall(grid_regex, text_input)[0].split("x") grid_size = [int(el) for el in grid_input] houses_input = re.findall(coordinates_regex, text_input) houses = [] for house in houses_input: house_coords = [int(el) for el in house.split(",")] if (house_coords[0] > grid_size[0]) or (house_coords[1] > grid_size[1]): raise BadHouseCoordinatesError( "One of the houses is outside of the grid, all of them must be inside the grid." ) houses.append(House(house_coords[0], house_coords[1])) return houses