from csv import reader
import datetime as dt
open_file = open('hacker_news.csv')
read_file = reader(open_file)
hn = list(read_file)
hn = hn[1:]
ask_posts = []
show_posts = []
other_posts = []
for row in hn:
title = row[1].lower()
if(title.startswith('ask hn')):
ask_posts.append(row)
elif(title.startswith('show hn')):
show_posts.append(row)
else:
other_posts.append(row)
print('Number of ask posts : ', len(ask_posts),
'\nNumber of show posts : ', len(show_posts),
'\nNumber of other posts : ', len(other_posts))
total_ask_comments = 0
total_show_comments = 0
for a in ask_posts:
total_ask_comments += int(row[4])
for s in show_posts:
total_show_comments += int(row[4])
print(total_ask_comments)
print(total_show_comments)
avg_ask_comments = total_ask_comments / len(ask_posts)
avg_show_comments = total_show_comments / len(show_posts)
print()
print('Average of ask comments : ', avg_ask_comments)
print('Average of show comments : ', avg_show_comments)
date_format = '%m/%d/%Y %H:%M'
result_list = []
counts_by_hour = {}
comments_by_hour = {}
for row in ask_posts:
result_list.append([row[6], int(row[4])])
for row in result_list:
# print(row[0], row[1])
dt_obj = dt.datetime.strptime(row[0], date_format)
if(dt_obj.hour in counts_by_hour):
counts_by_hour[dt_obj.hour] += 1
comments_by_hour[dt_obj.hour] += row[1]
else:
counts_by_hour[dt_obj.hour] = 1
comments_by_hour[dt_obj.hour] = row[1]
avg_by_hour = []
# print(counts_by_hour)
# print()
# print(comments_by_hour)
for hour in counts_by_hour:
avg_by_hour.append([hour, comments_by_hour[hour] / counts_by_hour[hour]])
# for avg in avg_by_hour:
# print(avg)
swap_avg_by_hour = []
for row in avg_by_hour:
swap_avg_by_hour.append([row[1], row[0]])
sorted_swap = sorted(swap_avg_by_hour, reverse=True)
# print(sorted_swap[:5])
hour_format = '%H'
for avg, hour in sorted_swap:
hour = dt.datetime.strptime(str(hour), hour_format)
print('{}: {:.2f} comments per post.'.format(hour.strftime('%H:%M'), avg))
15:00: 38.59 comments per post. 02:00: 23.81 comments per post. 20:00: 21.52 comments per post. 16:00: 16.80 comments per post. 21:00: 16.01 comments per post. 13:00: 14.74 comments per post. 10:00: 13.44 comments per post. 14:00: 13.23 comments per post. 18:00: 13.20 comments per post. 17:00: 11.46 comments per post. 01:00: 11.38 comments per post. 11:00: 11.05 comments per post. 19:00: 10.80 comments per post. 08:00: 10.25 comments per post. 05:00: 10.09 comments per post. 12:00: 9.41 comments per post. 06:00: 9.02 comments per post. 00:00: 8.13 comments per post. 23:00: 7.99 comments per post. 07:00: 7.85 comments per post. 03:00: 7.80 comments per post. 04:00: 7.17 comments per post. 22:00: 6.75 comments per post. 09:00: 5.58 comments per post.