Last active
January 31, 2017 23:39
-
-
Save fitzk/0d0d78471cc96faca798cf00a95a6920 to your computer and use it in GitHub Desktop.
HackerRank: Python3 Agency List class and builder function.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AgencyList: | |
def __init__(self): | |
self.verticies = list() | |
self.edges = dict() | |
# dict of lists | |
def initEdge(self, v1, v2): | |
if not v1 in self.edges: | |
self.edges[v1] = list() | |
if not v2 in self.edges: | |
self.edges[v2] = list() | |
# add verticies, | |
# make sure edge verticies are in | |
# edges dict, add edges | |
def addEdge(self, v1, v2): | |
self.verticies.append(v1) | |
self.verticies.append(v2) | |
self.initEdge(v1, v2) | |
self.edges[v1].append(v2) | |
self.edges[v2].append(v1) | |
# helper function | |
def printEdges(self): | |
print(self.edges) | |
def build_agency_list(): | |
m, n = input().split(' ') | |
m = int(m) | |
n = int(n) | |
i=0 | |
# init AgencyList object | |
al = AgencyList() | |
# read input and build al | |
while i < n: | |
v1, v2 = input().split(' ') | |
al.addEdge(int(v1), int(v2)) | |
i = i + 1 | |
al.printEdges() | |
build_agency_list() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment