Skip to content

Instantly share code, notes, and snippets.

@christianp
Created March 9, 2015 10:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christianp/b9fc07fcc6ebad9d9401 to your computer and use it in GitHub Desktop.
Save christianp/b9fc07fcc6ebad9d9401 to your computer and use it in GitHub Desktop.
Shortest route between stations coverign all the vowels with their names
from itertools import combinations
stations = ["Airport", "Bank Foot", "Bede", "Benton", "Brockley Whins", "Byker", "Callerton Parkway", "Central Station", "Chichester", "Chillingham Road", "Cullercoats", "East Boldon", "Fawdon", "Fellgate", "Felling", "Four Lane Ends", "Gateshead", "Gateshead Stadium", "Hadrian Road", "Haymarket", "Hebburn", "Heworth", "Howdon", "Ilford Road", "Jarrow", "Jesmond", "Kingston Park", "Longbenton", "Manors", "Meadow Well", "Millfield", "Monkseaton", "Monument", "North Shields", "Northumberland Park", "Pallion", "Palmersville", "Park Lane", "Pelaw", "Percy Main", "Regent Centre", "Seaburn", "Shiremoor", "Simonside", "South Gosforth", "South Hylton", "South Shields", "St James", "St Peter's", "Stadium of Light", "Sunderland", "Tyne Dock", "Tynemouth", "University", "Walkergate", "Wallsend", "Wansbeck Road", "West Jesmond", "West Monkseaton", "Whitley Bay"]
green_line = ["Airport","Callerton Parkway","Bank Foot","Kingston Park","Fawdon","Wansbeck Road","Regent Centre","South Gosforth","Ilford Road","West Jesmond","Jesmond","Haymarket","Monument","Central Station","Gateshead","Gateshead Stadium","Felling","Heworth","Pelaw","Fellgate","Brockley Whins","East Boldon","Seaburn","Stadium of Light","St Peter's","Sunderland","Park Lane","University","Millfield","Pallion","South Hylton"]
yellow_line = ["St James","Monument","Manors","Byker","Chillingham Road","Walkergate","Wallsend","Hadrian Road","Howdon","Percy Main","Meadow Well","North Shields","Tynemouth","Cullercoats","Whitley Bay","Monkseaton","West Monkseaton","Shiremoor","Northumberland Park","Palmersville","Benton","Four Lane Ends","Longbenton","South Gosforth","Ilford Road","West Jesmond","Jesmond","Haymarket","Monument","Central Station","Gateshead","Gateshead Stadium","Felling","Heworth","Pelaw","Hebburn","Jarrow","Bede","Simonside","Tyne Dock","Chichester","South Shields"]
lines = [green_line,yellow_line]
vowels = 'aeiou'
def num_vowels(s):
s = s.lower()
return sum(1 if v in s else 0 for v in vowels)
def distance(a,b):
for line in lines:
if a in line:
line_a = line
if b in line:
line_b = line
if a in line and b in line:
return abs(line.index(a)-line.index(b))
both = [s for s in stations if s in line_a and s in line_b]
c_distances = [distance(a,x)+distance(x,b) for x in both]
return min(c_distances)
print(min(((a,b) for a,b in combinations(stations,2) if num_vowels(a+b)==5),key=lambda a: distance(*a)))
@christianp
Copy link
Author

Output: (Central station, Monument)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment