initial versions of solutions to harry potter challenges
[harrypotter-wikipedia-cdsw-solutions] / solution1.py
1 # Q: What are the most edited articles on Harry Potter on Wikipedia?
2
3 from csv import DictReader
4
5 # read in the input file and count by day
6 input_file = open("hp_wiki.tsv", 'r', encoding="utf-8")
7
8 edits_by_article = {}
9 for row in DictReader(input_file, delimiter="\t"):
10     title = row['title']
11
12     if title in edits_by_article:
13         edits_by_article[title] = edits_by_article[title] + 1
14     else:
15         edits_by_article[title] = 1
16
17 input_file.close()
18
19 # I used this answer here:
20 # https://stackoverflow.com/questions/613183/sort-a-python-dictionary-by-value
21
22 for title in sorted(edits_by_article, key=edits_by_article.get, reverse=True):
23     print(title)

Benjamin Mako Hill || Want to submit a patch?