aboutsummaryrefslogtreecommitdiff
path: root/challenge-285/sgreen/python/ch-1.py
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-285/sgreen/python/ch-1.py')
-rwxr-xr-xchallenge-285/sgreen/python/ch-1.py35
1 files changed, 35 insertions, 0 deletions
diff --git a/challenge-285/sgreen/python/ch-1.py b/challenge-285/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..f0cbf0030e
--- /dev/null
+++ b/challenge-285/sgreen/python/ch-1.py
@@ -0,0 +1,35 @@
+#!/usr/bin/env python3
+
+import sys
+
+
+def no_connection(routes: list) -> str:
+ # Calculate a list of origins (first value) and destinations (second value)
+ origins = [v[0] for v in routes]
+ destinations = [v[1] for v in routes]
+
+ # Find route destinations that aren't also an origin
+ dead_ends = [d for d in destinations if d not in origins]
+
+ if len(dead_ends) > 1:
+ raise ValueError(
+ 'There are multiple routes with no outgoing connection')
+
+ if len(dead_ends) == 0:
+ raise ValueError('All routes have an outgoing connection')
+
+ return dead_ends[0]
+
+
+def main():
+ # Convert input into pairs. The first value is the start of route.
+ # The second value is the destination
+ routes = []
+ for i in range(1, len(sys.argv), 2):
+ routes.append([sys.argv[i], sys.argv[i+1]])
+ result = no_connection(routes)
+ print(result)
+
+
+if __name__ == '__main__':
+ main()