From 4455a4e6f498996f5023c2fb584d1e2b2fcc2b48 Mon Sep 17 00:00:00 2001 From: Cory Balaton Date: Tue, 1 Oct 2024 19:05:58 +0200 Subject: [PATCH] Add indexes_to_cities function --- common.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/common.py b/common.py index 03221a2..d5c787c 100644 --- a/common.py +++ b/common.py @@ -3,6 +3,7 @@ from typing import Dict, List, Tuple import matplotlib.pyplot as plt import numpy as np import numpy.typing as npt +from numpy._typing import NDArray # Data given by the assignment city_coords: Dict = { @@ -41,7 +42,7 @@ def plot_plan(city_order: List[str]) -> None: Args: city_order (List[str]): A list of cities in the order to be plotted. - Returns: + Returns: None """ @@ -72,6 +73,20 @@ def plot_plan(city_order: List[str]) -> None: plt.show() +def index_to_city(indexes: npt.NDArray, cities: npt.NDArray) -> npt.NDArray: + """Create an array of cities from indeces in a specific order. + + Args: + indexes (npt.NDArray): An array of city indexes. + cities (npt.NDArray): An array of cities. + + Returns: + npt.NDArray An array of cities in the same order as given in indexes. + """ + + return np.array([cities[i] for i in indexes]) + + def read_data(file_path: str) -> Tuple[npt.NDArray, npt.NDArray]: """Read the city data from a file given and return 2 arrays.