diff options
| author | Dave Jacoby <jacoby.david@gmail.com> | 2024-09-18 19:56:42 -0400 |
|---|---|---|
| committer | Dave Jacoby <jacoby.david@gmail.com> | 2024-09-18 19:56:42 -0400 |
| commit | f86f5e2fec16020c1d86f9028fb0f61cfeac106e (patch) | |
| tree | 0fd388a696b51ffde5a7bfe8519a74e1caf42461 /challenge-285/lubos-kolouch/python/ch-1.py | |
| parent | ff8719c86653d5ad3121955e9494a0010527c2b9 (diff) | |
| parent | 0052ec63ca70eaa6d9ffb1926c294dbfd85f8c05 (diff) | |
| download | perlweeklychallenge-club-f86f5e2fec16020c1d86f9028fb0f61cfeac106e.tar.gz perlweeklychallenge-club-f86f5e2fec16020c1d86f9028fb0f61cfeac106e.tar.bz2 perlweeklychallenge-club-f86f5e2fec16020c1d86f9028fb0f61cfeac106e.zip | |
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
Diffstat (limited to 'challenge-285/lubos-kolouch/python/ch-1.py')
| -rw-r--r-- | challenge-285/lubos-kolouch/python/ch-1.py | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/challenge-285/lubos-kolouch/python/ch-1.py b/challenge-285/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..be24d53a49 --- /dev/null +++ b/challenge-285/lubos-kolouch/python/ch-1.py @@ -0,0 +1,52 @@ +from typing import List +import unittest + + +def find_destination(routes: List[List[str]]) -> str: + """ + Finds the destination with no further outgoing connection from a given list of routes. + + Each route is represented as a list containing a source and a destination city. + + Args: + routes (List[List[str]]): A list of routes. + + Returns: + str: The destination city with no outgoing connections. + + """ + sources = set() + destinations = set() + + for route in routes: + source, destination = route + sources.add(source) + destinations.add(destination) + + no_outgoing = destinations - sources + + if no_outgoing: + # Assuming there is only one such city + return no_outgoing.pop() + else: + return None # In case there is no such city + + +# Unit Tests +class TestNoConnection(unittest.TestCase): + + def test_example1(self): + routes = [["B", "C"], ["D", "B"], ["C", "A"]] + self.assertEqual(find_destination(routes), "A", 'Example 1') + + def test_example2(self): + routes = [["A", "Z"]] + self.assertEqual(find_destination(routes), "Z", 'Example 2') + + def test_no_destination(self): + routes = [["A", "B"], ["B", "A"]] + self.assertIsNone(find_destination(routes), 'No unique destination') + + +if __name__ == "__main__": + unittest.main() |
