Skip to content

Commit

Permalink
Comments
Browse files Browse the repository at this point in the history
  • Loading branch information
micknudsen committed Dec 10, 2023
1 parent 63e2da1 commit 03a2ca1
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions 2015/09/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@


class Map:
"""Class representing a map of cities and all the pairwise
distances between them stored in a symmetric matrix implemented
as a dictionary of dictionaries."""

def __init__(
self,
distances: Dict[str, Dict[str, int]],
Expand Down Expand Up @@ -37,6 +41,7 @@ def distance(
return self._distances[source][destination]

def route_lengths(self) -> Iterator[int]:
"""Iterates through all possible routes and yileds their lengths."""
for route in permutations(self._distances.keys()):
yield (
sum(
Expand All @@ -46,9 +51,11 @@ def route_lengths(self) -> Iterator[int]:
)

def shortest_route(self) -> int:
"""Returns the length of the shortest route."""
return min(self.route_lengths())

def longest_route(self) -> int:
"""Returns the length of the longest route."""
return max(self.route_lengths())


Expand Down

0 comments on commit 03a2ca1

Please sign in to comment.