From 2bc48868e415f53d52dfdfb5d3e944f3e6c3b4e7 Mon Sep 17 00:00:00 2001 From: Hykilpikonna Date: Mon, 21 Mar 2022 00:48:53 -0400 Subject: [PATCH] [U] Variable names --- assignments/A3/a3_part1.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/assignments/A3/a3_part1.py b/assignments/A3/a3_part1.py index 64edb9b..fb045b5 100644 --- a/assignments/A3/a3_part1.py +++ b/assignments/A3/a3_part1.py @@ -273,17 +273,23 @@ def load_review_graph(reviews_file: str, book_names_file: str) -> Graph: True """ g = Graph() - mp = {} # maps book ID to book name + + # mp[book_id] = book_name + mp: dict[str, str] + + # Read book names file and create id-name mapping with open(book_names_file, 'r', newline='', encoding='UTF-8') as f: reader = csv.reader(f) - for row in reader: - mp[row[0]] = row[1] + mp = {book_id: name for book_id, name in reader} + + # Read user review file and link user and reviews in the graph with open(reviews_file, 'r', newline='', encoding='UTF-8') as f: reader = csv.reader(f) - for row in reader: - g.add_vertex(row[0], 'user') - g.add_vertex(mp[row[1]], 'book') - g.add_edge(row[0], mp[row[1]]) + for user_id, book_id, rating in reader: + g.add_vertex(user_id, 'user') + g.add_vertex(mp[book_id], 'book') + g.add_edge(user_id, mp[book_id]) + return g