Skip to content

Instantly share code, notes, and snippets.

@javi830810
Last active September 25, 2015 14:22
Show Gist options
  • Select an option

  • Save javi830810/fec1824edc1b1dcb3b06 to your computer and use it in GitHub Desktop.

Select an option

Save javi830810/fec1824edc1b1dcb3b06 to your computer and use it in GitHub Desktop.
from sys import stdin
#amount = int(stdin.readline())
class Node(object):
def __init__(self, id):
self.id = id
self.neighboors = []
class Edge(object):
def __init__(self, destination, time):
self.destination = destination
self.time = time
class Graph(object):
def __init__(self, root=None):
self.graph = {}
def add(self, source, destiny, time):
source_node = self.graph.get(source, None) or Node(source)
destiny_node = self.graph.get(destiny, None) or Node(destiny)
source_node.neighboors.append((Edge(destiny_node, time), destiny_node))
self.graph[source] = source_node
self.graph[destiny] = destiny_node
def __getitem__(self, key): return self.graph[key]
def dijkstra(self, source, destiny):
"""
I'm not going to have enough time to complete the ex
The idea is to do Dijkstra algorithm on the graph, and take into account modes
while also accounting for the time the plane have to wait
on each airport for the current mode to be OK
Below there is the rough Dijkstra algorithm.
Not taking into account the Timing for the Mode...
"""
MAX = 1000000
#Reset
for x in self.graph.keys():
if x == source:
self.graph[x].time = 0
else:
self.graph[x].time = MAX
self.graph[x].visited = False
unvisited_set = self.graph.values()
current_node = self.graph[source]
while len(unvisited_set):
small_distance_node = None
for neighboor in current_node.neighboors:
edge = neighboor[0]
node = neighboor[1]
if not node.visited and node.time > current_node.time + edge.time:
node.time = current_node.time + edge.time
if small_distance_node is None or small_distance_node.time > node.time:
small_distance_node = node
if node.id == destiny:
print node.time
# PRINTING RESULT OF Dijkstra HERE
return node.time
if small_distance_node.time == MAX:
print 0
# PRINTING INVALID ROUTE
return 0
current_node.visited = True
unvisited_set.remove(current_node)
current_node = small_distance_node
class Clock(object):
def __init__(self, initial_times):
self.initial_times = initial_times
def calculateShortestPath():
airports = stdin.readline().strip()
source_airport = airports.split(' ')[0].strip()
destiny_airport = airports.split(' ')[1].strip()
times_airways = stdin.readline().strip()
times_len = times_airways.split(' ')[0].strip()
airways_len = times_airways.split(' ')[1].strip()
#Get Initial Config
times = []
for x in xrange(0, int(times_len)):
line = stdin.readline().strip().split(' ')
times.append({
'mode': line[0].strip(),
'remaining': int(line[1].strip()),
'total_a': int(line[2].strip()),
'total_b': int(line[3].strip())
})
clock = Clock(times)
graph = Graph()
for x in xrange(0, int(airways_len)):
line = stdin.readline().strip().split(' ')
graph.add(line[0].strip(), line[1].strip(), int(line[2].strip()))
graph.dijkstra(source_airport, destiny_airport)
calculateShortestPath()
from sys import stdin
#amount = int(stdin.readline())
def OutputMostPopularDestination( count):
destinations = {}
for x in xrange(0, count):
destination = stdin.readline().strip()
if not destination in destinations:
destinations[destination] = 0
destinations[destination] += 1
print reduce(
lambda x,y: x if destinations[x] > destinations[y] else y,
destinations
)
_count = int(raw_input());
OutputMostPopularDestination(_count)
from sys import stdin
#amount = int(stdin.readline())
class Tree(object):
def __init__(self, root=None):
self.root = root
self.children = []
def add(self, tree):
self.children.append(tree)
def search(self, item):
if self.root == item:
return self
result = None
for child in self.children:
result = child.search(item)
if result:
break
return result
def update_org_chart(org_chart, line):
split_x = line.split(' ')
manager = split_x[0].strip()
employee = split_x[1].strip()
manager_tree = org_chart.search(manager)
if not manager_tree: # this is root manager
manager_tree = Tree(
root=manager
)
org_chart.add(manager_tree)
manager_tree.add(Tree(
root=employee
))
def find_common_parent(org_chart, employee1, employee2):
for children in org_chart.children:
employee1_tree = children.search(employee1)
employee2_tree = children.search(employee2)
if employee1_tree and employee2_tree:
result = find_common_parent(children, employee1, employee2)
return result or children
return None
def OutputCommonManager(count):
org_chart = Tree()
employee1 = stdin.readline().strip()
employee2 = stdin.readline().strip()
while 1:
line = stdin.readline().strip()
if not line:
break
else:
update_org_chart(org_chart, line)
print find_common_parent(org_chart, employee1, employee2).root
_count = int(raw_input());
OutputCommonManager(_count)
@javi830810
Copy link
Author

Max_destination: Find the max dest from a list of words. unconcluded like

Org_chart: Find the Closest Common Manager

Cities: Dijsktra with some other considerations. not finished

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