Skip to content

Instantly share code, notes, and snippets.

@trentbrooks
Created April 13, 2019 09:53
Show Gist options
  • Save trentbrooks/fe1310940c36036925c972eab2186c5b to your computer and use it in GitHub Desktop.
Save trentbrooks/fe1310940c36036925c972eab2186c5b to your computer and use it in GitHub Desktop.
dest = []
locs = []
def process(line):
global dest
inp_split = line.split(':')
dep = inp_split[0]
conn = inp_split[1]
dist = int(inp_split[2])
dest.append( (dep,conn,dist) )
locs.append(dep)
locs.append(conn)
if len(dest) > 1:
locs_sorted = sorted(locs, key=lambda x: locs.count(x), reverse=True)
connector = locs_sorted[0] # NYC is shared connector - all else are uniqeue
longest_sorted = sorted(dest, key=lambda x: x[2], reverse=True)
# ugly but works
d1 = longest_sorted[0]
d2 = longest_sorted[1]
d1_name = d1[1] if (d1[0] == connector) else d1[0]
d2_name = d2[1] if (d2[0] == connector) else d2[0]
out = str(d1[2] + d2[2]) + ":" + d2_name + ":" + connector + ":" + d1_name
return out
else:
return None
test = 'CHI:NYC:719' # none
test2 = 'NYC:LA:2414' #3133:CHI:NYC:LA
test3 = 'NYC:SEATTLE:2448' #4862:LA:NYC:SEATTLE
test4 = 'NYC:HAWAII:4924' #7372:HAWAII:NYC:SEATTLE
print(process(test))
print(process(test2))
print(process(test3))
print(process(test4))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment