Skip to content

Commit 83a24cb

Browse files
authored
Update graphs/graph_list.py (#3813)
- update naming style to snake_case - add type hints
1 parent 686d837 commit 83a24cb

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

graphs/graph_list.py

+20-20
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,34 @@
77

88
class AdjacencyList:
99
def __init__(self):
10-
self.List = {}
10+
self.adj_list = {}
1111

12-
def addEdge(self, fromVertex, toVertex):
12+
def add_edge(self, from_vertex: int, to_vertex: int) -> None:
1313
# check if vertex is already present
14-
if fromVertex in self.List.keys():
15-
self.List[fromVertex].append(toVertex)
14+
if from_vertex in self.adj_list:
15+
self.adj_list[from_vertex].append(to_vertex)
1616
else:
17-
self.List[fromVertex] = [toVertex]
17+
self.adj_list[from_vertex] = [to_vertex]
1818

19-
def printList(self):
20-
for i in self.List:
21-
print((i, "->", " -> ".join([str(j) for j in self.List[i]])))
19+
def print_list(self) -> None:
20+
for i in self.adj_list:
21+
print((i, "->", " -> ".join([str(j) for j in self.adj_list[i]])))
2222

2323

2424
if __name__ == "__main__":
2525
al = AdjacencyList()
26-
al.addEdge(0, 1)
27-
al.addEdge(0, 4)
28-
al.addEdge(4, 1)
29-
al.addEdge(4, 3)
30-
al.addEdge(1, 0)
31-
al.addEdge(1, 4)
32-
al.addEdge(1, 3)
33-
al.addEdge(1, 2)
34-
al.addEdge(2, 3)
35-
al.addEdge(3, 4)
36-
37-
al.printList()
26+
al.add_edge(0, 1)
27+
al.add_edge(0, 4)
28+
al.add_edge(4, 1)
29+
al.add_edge(4, 3)
30+
al.add_edge(1, 0)
31+
al.add_edge(1, 4)
32+
al.add_edge(1, 3)
33+
al.add_edge(1, 2)
34+
al.add_edge(2, 3)
35+
al.add_edge(3, 4)
36+
37+
al.print_list()
3838

3939
# OUTPUT:
4040
# 0 -> 1 -> 4

0 commit comments

Comments
 (0)