ported to python3

master
Malar Kannan 2017-07-03 17:29:37 +05:30
parent b2cfa7297c
commit 27d7cae28a
2 changed files with 12 additions and 11 deletions

View File

@ -45,18 +45,19 @@ class Transporter(object):
@staticmethod
def read_contracts(c_file):
c_lines = open(c_file, 'r').readlines()
c_file_h = open(c_file, 'r')
c_lines = c_file_h.readlines()
c_dict = {}
for l in c_lines:
(c_id, src, dst, load) = l.split(' ')
if c_dict.has_key(c_id):
if c_id in c_dict:
route_id = len(c_dict[c_id])
c_dict[c_id].append(Route(route_id, int(c_id), load, src, dst))
else:
c_dict[c_id] = [Route(0, int(c_id), load, src, dst)]
contracts = [Contract(int(i), c_dict[i])
for i in sorted(c_dict.keys())]
c_file_h.close()
return contracts
def contract_routes(self):
@ -69,9 +70,9 @@ class Transporter(object):
def contracts_required(self):
routes = self.contract_routes()
unfulfilled = rem_routes(routes)
print "No of contracts required : ", len(unfulfilled)
print("No of contracts required : ", len(unfulfilled))
if len(unfulfilled) > 0:
print "Contracts from ", repr(unfulfilled)
print("Contracts from ", repr(unfulfilled))
def contract_corridors(self):
return rem_routes(self.contract_routes())

View File

@ -13,18 +13,18 @@ def rem_routes(routes):
while len(partial_routes) > 0:
lp = longest_path(partial_routes)
partial_paths.append((lp[-1].dst, lp[0].src))
partial_routes = filter(lambda x: x not in lp, partial_routes)
partial_routes = [x for x in partial_routes if x not in lp]
return partial_paths
def route_deps(routes):
routes_map = {}
for r in routes:
if routes_map.has_key(r.src):
if r.src in routes_map:
routes_map[r.src].append(r)
else:
routes_map[r.src] = [r]
if not routes_map.has_key(r.dst):
if r.dst not in routes_map:
routes_map[r.dst] = []
return routes_map
@ -34,13 +34,13 @@ def largest_loop(routes):
circ_paths = [find_loop(r.src, r.src, deps, set(), []) for r in routes]
circ, paths = max(circ_paths, key=lambda p: len(p[1]))
# print filter(lambda p:not p[0],circ_paths)
rem_paths = filter(lambda x: x not in paths, routes)
rem_paths = [x for x in routes if x not in paths]
return (circ, paths, rem_paths)
def longest_path(routes):
def route_path(r, deps):
if not deps.has_key(r.dst) or len(deps[r.dst]) == 0:
if r.dst not in deps or len(deps[r.dst]) == 0:
return [r]
all_dep_paths = [([r] + route_path(rt, deps)) for rt in deps[r.dst]]
rp = max(all_dep_paths, key=lambda p: len(p))
@ -53,7 +53,7 @@ def longest_path(routes):
def find_loop(elem, dep_elem, deps, checked, path):
found_loop = False
if dep_elem not in checked and deps.has_key(dep_elem):
if dep_elem not in checked and dep_elem in deps:
checked.add(dep_elem)
for c in deps[dep_elem]:
path.append(c)