1-Do ASK HN or Show HN receive more comments on average? 2-Do post created at a certain time receive more comments on average?
from csv import reader
open_file=open('hacker_news.csv')
read=reader(open_file)
hn=list(read)
headers=hn[0]
hn=hn[1:]
print(headers)
['id', 'title', 'url', 'num_points', 'num_comments', 'author', 'created_at']
print(hn[:5])
[['12224879', 'Interactive Dynamic Video', 'http://www.interactivedynamicvideo.com/', '386', '52', 'ne0phyte', '8/4/2016 11:52'], ['10975351', 'How to Use Open Source and Shut the Fuck Up at the Same Time', 'http://hueniverse.com/2016/01/26/how-to-use-open-source-and-shut-the-fuck-up-at-the-same-time/', '39', '10', 'josep2', '1/26/2016 19:30'], ['11964716', "Florida DJs May Face Felony for April Fools' Water Joke", 'http://www.thewire.com/entertainment/2013/04/florida-djs-april-fools-water-joke/63798/', '2', '1', 'vezycash', '6/23/2016 22:20'], ['11919867', 'Technology ventures: From Idea to Enterprise', 'https://www.amazon.com/Technology-Ventures-Enterprise-Thomas-Byers/dp/0073523429', '3', '1', 'hswarna', '6/17/2016 0:01'], ['10301696', 'Note by Note: The Making of Steinway L1037 (2007)', 'http://www.nytimes.com/2007/11/07/movies/07stein.html?_r=0', '8', '2', 'walterbell', '9/30/2015 4:12']]
ask_posts=[]
show_posts=[]
other_posts=[]
for i in hn:
title=i[1]
if title.lower().startswith('ask hn'):
ask_posts.append(i)
elif title.lower().startswith('show hn'):
show_posts.append(i)
else:
other_posts.append(i)
print('Number of Ask_Posts',len(ask_posts))
print('Number of Show_Posts',len(show_posts))
print('Number of Other_Posts',len(other_posts))
Number of Ask_Posts 1744 Number of Show_Posts 1162 Number of Other_Posts 17194
print(ask_posts[:5])
[['12296411', 'Ask HN: How to improve my personal website?', '', '2', '6', 'ahmedbaracat', '8/16/2016 9:55'], ['10610020', 'Ask HN: Am I the only one outraged by Twitter shutting down share counts?', '', '28', '29', 'tkfx', '11/22/2015 13:43'], ['11610310', 'Ask HN: Aby recent changes to CSS that broke mobile?', '', '1', '1', 'polskibus', '5/2/2016 10:14'], ['12210105', 'Ask HN: Looking for Employee #3 How do I do it?', '', '1', '3', 'sph130', '8/2/2016 14:20'], ['10394168', 'Ask HN: Someone offered to buy my browser extension from me. What now?', '', '28', '17', 'roykolak', '10/15/2015 16:38']]
total_ask_comments=0
for i in ask_posts:
num_comments=i[4]
num_comments=int(num_comments)
total_ask_comments+=num_comments
print('Total Ask Comments',total_ask_comments)
avg_ask_comments=total_ask_comments/len(ask_posts)
print('Average Ask Comments',avg_ask_comments)
Total Ask Comments 24483 Average Ask Comments 14.038417431192661
total_show_comments=0
for i in show_posts:
num_comments=i[4]
num_comments=int(num_comments)
total_show_comments+=num_comments
print('Total Show Comments are:',total_show_comments)
avg_show_comments=total_show_comments/len(show_posts)
print('Average show comments',avg_show_comments)
Total Show Comments are: 11988 Average show comments 10.31669535283993
import datetime as dt
result_list=[]
counts_by_hour={}
comments_by_hour={}
for i in ask_posts:
created_at=i[6]
num_comments=i[4]
num_comments=int(num_comments)
result_list.append((created_at,num_comments))
#print(result_list)
for i in result_list:
date=dt.datetime.strptime(i[0], "%m/%d/%Y %H:%M")
date=date.strftime("%H")
comment=i[1]
if date not in counts_by_hour:
counts_by_hour[date]=1
comments_by_hour[date]=comment
else:
counts_by_hour[date]+=1
comments_by_hour[date]+=comment
print(comments_by_hour)
{'20': 1722, '19': 1188, '01': 683, '05': 464, '14': 1416, '06': 397, '17': 1146, '16': 1814, '12': 687, '22': 479, '02': 1381, '21': 1745, '18': 1439, '07': 267, '03': 421, '13': 1253, '10': 793, '04': 337, '00': 447, '11': 641, '15': 4477, '23': 543, '08': 492, '09': 251}
print(counts_by_hour)
{'20': 80, '19': 110, '01': 60, '05': 46, '14': 107, '06': 44, '17': 100, '16': 108, '12': 73, '22': 71, '02': 58, '21': 109, '18': 109, '07': 34, '03': 54, '13': 85, '10': 59, '04': 47, '00': 55, '11': 58, '15': 116, '23': 68, '08': 48, '09': 45}
avg_by_hour=[]
for i in comments_by_hour:
avg_by_hour.append([i,comments_by_hour[i]/counts_by_hour[i]])
print(avg_by_hour)
[['20', 21.525], ['19', 10.8], ['01', 11.383333333333333], ['05', 10.08695652173913], ['14', 13.233644859813085], ['06', 9.022727272727273], ['17', 11.46], ['16', 16.796296296296298], ['12', 9.41095890410959], ['22', 6.746478873239437], ['02', 23.810344827586206], ['21', 16.009174311926607], ['18', 13.20183486238532], ['07', 7.852941176470588], ['03', 7.796296296296297], ['13', 14.741176470588234], ['10', 13.440677966101696], ['04', 7.170212765957447], ['00', 8.127272727272727], ['11', 11.051724137931034], ['15', 38.5948275862069], ['23', 7.985294117647059], ['08', 10.25], ['09', 5.5777777777777775]]
swap_avg_by_hour=[]
for i in avg_by_hour:
swap_avg_by_hour.append((i[1],i[0]))
print(swap_avg_by_hour)
[(21.525, '20'), (10.8, '19'), (11.383333333333333, '01'), (10.08695652173913, '05'), (13.233644859813085, '14'), (9.022727272727273, '06'), (11.46, '17'), (16.796296296296298, '16'), (9.41095890410959, '12'), (6.746478873239437, '22'), (23.810344827586206, '02'), (16.009174311926607, '21'), (13.20183486238532, '18'), (7.852941176470588, '07'), (7.796296296296297, '03'), (14.741176470588234, '13'), (13.440677966101696, '10'), (7.170212765957447, '04'), (8.127272727272727, '00'), (11.051724137931034, '11'), (38.5948275862069, '15'), (7.985294117647059, '23'), (10.25, '08'), (5.5777777777777775, '09')]
sorted_swap=sorted(swap_avg_by_hour,reverse=True)
print(sorted_swap)
[(38.5948275862069, '15'), (23.810344827586206, '02'), (21.525, '20'), (16.796296296296298, '16'), (16.009174311926607, '21'), (14.741176470588234, '13'), (13.440677966101696, '10'), (13.233644859813085, '14'), (13.20183486238532, '18'), (11.46, '17'), (11.383333333333333, '01'), (11.051724137931034, '11'), (10.8, '19'), (10.25, '08'), (10.08695652173913, '05'), (9.41095890410959, '12'), (9.022727272727273, '06'), (8.127272727272727, '00'), (7.985294117647059, '23'), (7.852941176470588, '07'), (7.796296296296297, '03'), (7.170212765957447, '04'), (6.746478873239437, '22'), (5.5777777777777775, '09')]
for i,j in sorted_swap[:5]:
date=dt.datetime.strptime(j,'%H')
date=date.strftime('%H:%M')
print('{} {:.2f} average comments per post'.format(date,i))
15:00 38.59 average comments per post 02:00 23.81 average comments per post 20:00 21.52 average comments per post 16:00 16.80 average comments per post 21:00 16.01 average comments per post