Shared route stops
A transport desk wants a transfer-stop list for two bus routes. Read each route's space-separated stop codes. Print every stop that appears on both routes once, in the order it first appears on route A. If there is no transfer stop, print No transfer. Route A may list the same stop twice when a bus loops back, but the report must not repeat it.
Input
Two lines: stop codes for route A, then stop codes for route B. Each route has at least one stop.
Output
Shared stop codes on one line, separated by single spaces, in first-seen route-A order; otherwise No transfer.
Example 1
Input
depot park market park quay quay hill park
Output
park quay
Park and quay are shared; park appears twice on route A but is printed only once.
Constraints
- Each route contains 1 to 100 stop codes
- Each code has 1 to 20 lowercase letters
- The order of route B does not determine the output order
Hints
Hint 1 of 3
A set makes membership checks against route B quick.
Hint 2 of 3
A separate seen set prevents a looping stop from appearing twice.
Hint 3 of 3
Iterate route A directly; sorting would discard the required order.
Solution
Show a reference solution and explanation
route_a = input().split()
route_b = set(input().split())
seen = set()
shared = []
for stop in route_a:
if stop in route_b and stop not in seen:
shared.append(stop)
seen.add(stop)
print(' '.join(shared) if shared else 'No transfer')
Why it works
Convert route B to a set for average constant-time membership checks. Walk route A in its original order and append a stop only when it exists in route B and has not been emitted before. This keeps the transfer list stable even when a route loops. The work is O(A+B) on average and the extra space is proportional to the distinct stops.
Lesson for this exercise: Sets in Python