Exploring Hacker News Posts

Hacker News is a site started by the startup incubator Y Combinator, where user-submitted stories (known as "posts") are voted and commented upon, similar to reddit.

We're specifically interested in posts whose titles begin with either Ask HN or Show HN. Users submit Ask HN posts to ask the Hacker News community a specific question. Likewise, users submit Show HN posts to show the Hacker News community a project, product, or just generally something interesting.

We'll compare these two types of posts to determine the following:

  • Do Ask HN or Show HN receive more comments on average?
  • Do posts created at a certain time receive more comments on average?
In [1]:
# import reader to open the data set file
from csv import reader
opened_file = open('hacker_news.csv')
read_file = reader(opened_file)
data_set = list(read_file)
headers = data_set[0]
hn = data_set[1:]

To make it easier to explore the two data sets, we'll first write a function named explore_data() that we can use repeatedly to explore rows in a more readable way. We'll also add an option for our function to show the number of rows and columns for any data set.

In [2]:
# define function to explore the data set
def explore_data(dataset, start, end, rows_and_columns=False):
    dataset_slice = dataset[start:end]
    for row in dataset_slice:
        print(row)
        print('\n')
        
    if rows_and_columns==True:
        print('Number of rows is ', len(dataset))
        print('Number of colums is ', len(dataset[0]))

#explore the first 5 rows of our data set 
print(headers)
print('\n')
explore_data(hn, 0, 5, True)
['id', 'title', 'url', 'num_points', 'num_comments', 'author', 'created_at']


['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']


Number of rows is  20100
Number of colums is  7

Calculating the Average Number of Comments for Ask HN and Show HN posts

Since we're only concerned with post titles beginning with Ask HN or Show HN, we'll separate posts beginning with Ask HN and Show HN (and case variations) into two different lists next.

In [3]:
#create 3 list for different post types
ask_posts = []
show_posts = []
other_posts = []

#loop over the data set and append rows for ask_posts, show_posts and other_posts 
#to the lists respectively
for row in hn:
    title = row[1]
    title = title.lower()
    if title.startswith('ask hn') == True:
        ask_posts.append(row)
    elif title.startswith('show hn') == True:
        show_posts.append(row)
    else:
        other_posts.append(row)

#create a function which will return the number of posts in each category
def num_posts(posts):
    number = 0
    for row in posts:
        number += 1
    
    return number

num_ask = num_posts(ask_posts)
num_show = num_posts(show_posts)
num_others = num_posts(other_posts)

print('The number of posts in ask_posts is ', num_ask )
print('The number of posts in show_posts is ', num_show)
print('The number of posts in other_posts is ', num_others)
The number of posts in ask_posts is  1744
The number of posts in show_posts is  1162
The number of posts in other_posts is  17194

Now when we separated defferent types of posts, we'll determine if ask posts or show posts receive more comments on average.

In [4]:
#compute the number of ask HN comments
total_ask_comments = 0
#loop over the ask_posts list to calculate the number of comments 
for row in ask_posts:
    total_ask_comments += int(row[4])
    
avg_ask_comments = total_ask_comments/num_ask
print(avg_ask_comments)
14.038417431192661

We've computed that on average a post in Ask HN has ~14 comments. Now we'll rake a look at Show HN posts.

In [5]:
#compute the number of Show HN comments
total_show_comments = 0
#loop over show_post lists and to calculate the number of comments
for row in show_posts:
    total_show_comments += int(row[4])
    
avg_show_comments = total_show_comments/num_show
print(avg_show_comments)
10.31669535283993

We've computed that on average a post in Show HN gets ~ 10 comments.

We've figured out that Ask HN posts receive more comments than Show HN. We can assume that people are more willing to ask questions than discuss, critisize, or praise whatether other users showed in the Sho HN post type.

Finding the Amount of Ask Posts and Comments by Hour Created

Since ask posts are more likely to receive comments, we'll focus our remaining analysis just on these posts.

Next, we'll determine if ask posts created at a certain time are more likely to attract comments.

In [6]:
#import thr datetime class useing an alias dt
from datetime import datetime as dt

#append the date in row[6] and the number of comments in row[4] to the result list
result_list = []
for row in ask_posts:
    a_list = []
    a_list.append(row[6])
    a_list.append(int(row[4]))
    result_list.append(a_list)

"""create dictionaries counts_by_hour for the number of post created at each other
and comments_by_hour for a number of comments left 

"""
counts_by_hour = {}
comments_by_hour = {}

#loop over the result list and append data to the dictionaries
for row in result_list:
    time_str = row[0]
    datetime_dt = dt.strptime(time_str, "%m/%d/%Y %H:%M")
    time_str = datetime_dt
    hour_dt = time_str.hour
    
    if hour_dt not in counts_by_hour:
        counts_by_hour[hour_dt] = 1
        comments_by_hour[hour_dt] = row[1]
    else:
        counts_by_hour[hour_dt] += 1
        comments_by_hour[hour_dt] += row[1]
 

print(counts_by_hour)
print(comments_by_hour)
        
{0: 55, 1: 60, 2: 58, 3: 54, 4: 47, 5: 46, 6: 44, 7: 34, 8: 48, 9: 45, 10: 59, 11: 58, 12: 73, 13: 85, 14: 107, 15: 116, 16: 108, 17: 100, 18: 109, 19: 110, 20: 80, 21: 109, 22: 71, 23: 68}
{0: 447, 1: 683, 2: 1381, 3: 421, 4: 337, 5: 464, 6: 397, 7: 267, 8: 492, 9: 251, 10: 793, 11: 641, 12: 687, 13: 1253, 14: 1416, 15: 4477, 16: 1814, 17: 1146, 18: 1439, 19: 1188, 20: 1722, 21: 1745, 22: 479, 23: 543}

We have created two dictionaries: one, containing the number of posts for each our of the day, and the other containing the corresponding number of comments ask posts received. Now we are ready to calculate the average number of comments for posts created during each hour of the day.

Calculating the Average Number of Comments for Ask HN Posts by Hour

In [7]:
avg_by_hour = []

#compute the average number of comments, append the result to the avg_by_hour list
for key in counts_by_hour:
    a_list = []
    a_list.append(key)
    avg = comments_by_hour[key]/counts_by_hour[key]
    a_list.append(avg)
    avg_by_hour.append(a_list)
    
for a_list in avg_by_hour:
    print(a_list)
[0, 8.127272727272727]
[1, 11.383333333333333]
[2, 23.810344827586206]
[3, 7.796296296296297]
[4, 7.170212765957447]
[5, 10.08695652173913]
[6, 9.022727272727273]
[7, 7.852941176470588]
[8, 10.25]
[9, 5.5777777777777775]
[10, 13.440677966101696]
[11, 11.051724137931034]
[12, 9.41095890410959]
[13, 14.741176470588234]
[14, 13.233644859813085]
[15, 38.5948275862069]
[16, 16.796296296296298]
[17, 11.46]
[18, 13.20183486238532]
[19, 10.8]
[20, 21.525]
[21, 16.009174311926607]
[22, 6.746478873239437]
[23, 7.985294117647059]
In [8]:
swap_avg_by_hour = []

#swap the time and the average number of comments and append the list to the swap_avg_by_time list
for row in avg_by_hour:
    a_list = []
    a_list.append(row[1])
    a_list.append(row[0])
    swap_avg_by_hour.append(a_list)
    
for a_list in swap_avg_by_hour:
    print(a_list)
[8.127272727272727, 0]
[11.383333333333333, 1]
[23.810344827586206, 2]
[7.796296296296297, 3]
[7.170212765957447, 4]
[10.08695652173913, 5]
[9.022727272727273, 6]
[7.852941176470588, 7]
[10.25, 8]
[5.5777777777777775, 9]
[13.440677966101696, 10]
[11.051724137931034, 11]
[9.41095890410959, 12]
[14.741176470588234, 13]
[13.233644859813085, 14]
[38.5948275862069, 15]
[16.796296296296298, 16]
[11.46, 17]
[13.20183486238532, 18]
[10.8, 19]
[21.525, 20]
[16.009174311926607, 21]
[6.746478873239437, 22]
[7.985294117647059, 23]
In [9]:
#implement the sorted function to the list with swapped comment and time, reverse it
sorted_swap = sorted(swap_avg_by_hour, reverse=True)

string = 'Top 5 Hours for Ask Posts Comments'
print(string)
for row in sorted_swap[:5]:
    string1 = dt.strptime(str(row[1]), '%H')
    string1 = dt.strftime(string1, '%H:%M')
    output = "{0}: {1:.2f} average comments per post".format(string1, row[0])
    print(output)
Top 5 Hours for Ask Posts Comments
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

According to the result, the best hour to create a post is 15:00 (3pm). That time you are more likely to receive comments. The next is 2:00 (2am). The difference between average comments at 3pm and 2am is big (~15 comments). You can receive an average of 21 comments at 20:00 (8pm) and the difference between posts at 2am is small (~2 comments).

In the next step we are going to calculate the average number of comments for a Show post.

Calculating the Average Number of Comments for a Show HN Post by Hour

In [10]:
#create a list with time in row[6] and number of comments in row[4]
show_result_list = []
for row in show_posts:
    a_list1 = []
    a_list1.append(row[6])
    a_list1.append(int(row[4]))
    show_result_list.append(a_list1)

"""create two empty dictionaries, s_counts_by_hour for the number of show posts for each hour, and
s_comments_by_hour for the number of comments for each hour
""" 
s_counts_by_hour = {}
s_comments_by_hour = {}

#loop through the show_result_list and append data to the dictionaries
for row in show_result_list:
    s_time_str = row[0]
    s_datetime_dt = dt.strptime(s_time_str, "%m/%d/%Y %H:%M")
    s_hour_dt = s_datetime_dt.hour
    
    if s_hour_dt not in s_counts_by_hour:
        s_counts_by_hour[s_hour_dt] = 1
        s_comments_by_hour[s_hour_dt] = row[1]
    else:
        s_counts_by_hour[s_hour_dt] += 1
        s_comments_by_hour[s_hour_dt] += row[1]
 

print(s_counts_by_hour)
print(s_comments_by_hour)
{0: 31, 1: 28, 2: 30, 3: 27, 4: 26, 5: 19, 6: 16, 7: 26, 8: 34, 9: 30, 10: 36, 11: 44, 12: 61, 13: 99, 14: 86, 15: 78, 16: 93, 17: 93, 18: 61, 19: 55, 20: 60, 21: 47, 22: 46, 23: 36}
{0: 487, 1: 246, 2: 127, 3: 287, 4: 247, 5: 58, 6: 142, 7: 299, 8: 165, 9: 291, 10: 297, 11: 491, 12: 720, 13: 946, 14: 1156, 15: 632, 16: 1084, 17: 911, 18: 962, 19: 539, 20: 612, 21: 272, 22: 570, 23: 447}
In [11]:
s_avg_by_hour = []

"""compute the average number of comments and append a list with time 
and the average comments to the s_avg_by_hour list
"""
for key in s_counts_by_hour:
    s_a_list = []
    s_a_list.append(key)
    s_avg = s_comments_by_hour[key]/s_counts_by_hour[key]
    s_a_list.append(s_avg)
    s_avg_by_hour.append(s_a_list)
    
for s_a_list in s_avg_by_hour:
    print(s_a_list)
[0, 15.709677419354838]
[1, 8.785714285714286]
[2, 4.233333333333333]
[3, 10.62962962962963]
[4, 9.5]
[5, 3.0526315789473686]
[6, 8.875]
[7, 11.5]
[8, 4.852941176470588]
[9, 9.7]
[10, 8.25]
[11, 11.159090909090908]
[12, 11.80327868852459]
[13, 9.555555555555555]
[14, 13.44186046511628]
[15, 8.102564102564102]
[16, 11.655913978494624]
[17, 9.795698924731182]
[18, 15.770491803278688]
[19, 9.8]
[20, 10.2]
[21, 5.787234042553192]
[22, 12.391304347826088]
[23, 12.416666666666666]
In [12]:
s_swap_avg_by_hour = []

#swap the index of time and comments and append the result to s_swap_avg_by_hour
for row in s_avg_by_hour:
    sa_list = []
    sa_list.append(row[1])
    sa_list.append(row[0])
    s_swap_avg_by_hour.append(sa_list)
    
for a_list in s_swap_avg_by_hour:
    print(a_list)
[15.709677419354838, 0]
[8.785714285714286, 1]
[4.233333333333333, 2]
[10.62962962962963, 3]
[9.5, 4]
[3.0526315789473686, 5]
[8.875, 6]
[11.5, 7]
[4.852941176470588, 8]
[9.7, 9]
[8.25, 10]
[11.159090909090908, 11]
[11.80327868852459, 12]
[9.555555555555555, 13]
[13.44186046511628, 14]
[8.102564102564102, 15]
[11.655913978494624, 16]
[9.795698924731182, 17]
[15.770491803278688, 18]
[9.8, 19]
[10.2, 20]
[5.787234042553192, 21]
[12.391304347826088, 22]
[12.416666666666666, 23]
In [13]:
#implement the sorted function to sort the result 
s_sorted_swap = sorted(s_swap_avg_by_hour, reverse=True)

s_string = 'Top 5 Hours for Show Posts Comments'
print(s_string)
for row in s_sorted_swap[:5]:
    sstring1 = dt.strptime(str(row[1]), '%H')
    sstring1 = dt.strftime(sstring1, '%H:%M')
    s_output = "{0}: {1:.2f} average comments per post".format(sstring1, row[0])
    print(s_output)
Top 5 Hours for Show Posts Comments
18:00: 15.77 average comments per post
00:00: 15.71 average comments per post
14:00: 13.44 average comments per post
23:00: 12.42 average comments per post
22:00: 12.39 average comments per post

We can see that the best time for creating a Show post is 18:00 (6pm). On average this post receives almost the same number of comments as the one posted at 00:00 (12pm). The third best time for posting is 14:00 (2pm). The difference between the average number of posts is vague.

Now we'll calculate an average comments number for other posts.

In [14]:
# create a list for post in other cateory, loop through other_posts, 
#append time (row[6]) and number of comments (row[4])
other_result_list = []
for row in other_posts:
    o_list1 = []
    o_list1.append(row[6])
    o_list1.append(int(row[4]))
    other_result_list.append(o_list1)

"""create empty dictionaries:
o_counts_by_hour for the number of post at a given hour
o_comments_by_hour number of comments for posts of a given hour
"""
o_counts_by_hour = {}
o_comments_by_hour = {}

#loop over other_result_list, append data to the dictionaries
for row in other_result_list:
    o_time_str = row[0]
    o_datetime_dt = dt.strptime(o_time_str, "%m/%d/%Y %H:%M")
    o_hour_dt = o_datetime_dt.hour
    
    if o_hour_dt not in o_counts_by_hour:
        o_counts_by_hour[o_hour_dt] = 1
        o_comments_by_hour[o_hour_dt] = row[1]
    else:
        o_counts_by_hour[o_hour_dt] += 1
        o_comments_by_hour[o_hour_dt] += row[1]
 

print(o_counts_by_hour)
print(o_comments_by_hour)
{0: 611, 1: 500, 2: 441, 3: 407, 4: 454, 5: 388, 6: 408, 7: 448, 8: 496, 9: 534, 10: 591, 11: 660, 12: 789, 13: 918, 14: 958, 15: 1040, 16: 1101, 17: 1169, 18: 1084, 19: 980, 20: 911, 21: 874, 22: 758, 23: 674}
{0: 16544, 1: 11536, 2: 12254, 3: 10918, 4: 10953, 5: 9768, 6: 8714, 7: 12010, 8: 13405, 9: 14732, 10: 15728, 11: 19532, 12: 23944, 13: 28363, 14: 30973, 15: 30700, 16: 27959, 17: 32727, 18: 29186, 19: 26167, 20: 21080, 21: 20635, 22: 17635, 23: 16592}
In [15]:
o_avg_by_hour = []

#compute the average number of comments and append it with the time to a new list
for key in o_counts_by_hour:
    o_a_list = []
    o_a_list.append(key)
    o_avg = o_comments_by_hour[key]/o_counts_by_hour[key]
    o_a_list.append(o_avg)
    o_avg_by_hour.append(o_a_list)
    
for o_a_list in o_avg_by_hour:
    print(o_a_list)
[0, 27.076923076923077]
[1, 23.072]
[2, 27.786848072562357]
[3, 26.825552825552826]
[4, 24.125550660792953]
[5, 25.175257731958762]
[6, 21.357843137254903]
[7, 26.808035714285715]
[8, 27.026209677419356]
[9, 27.588014981273407]
[10, 26.612521150592215]
[11, 29.593939393939394]
[12, 30.34727503168568]
[13, 30.896514161220043]
[14, 32.33089770354906]
[15, 29.51923076923077]
[16, 25.394187102633968]
[17, 27.99572284003422]
[18, 26.924354243542435]
[19, 26.701020408163266]
[20, 23.13940724478595]
[21, 23.60983981693364]
[22, 23.265171503957784]
[23, 24.617210682492583]
In [16]:
o_swap_avg_by_hour = []

#swap the index of time and the number of comments
for row in o_avg_by_hour:
    oa_list = []
    oa_list.append(row[1])
    oa_list.append(row[0])
    o_swap_avg_by_hour.append(oa_list)
    
for o_list in o_swap_avg_by_hour:
    print(o_list)
[27.076923076923077, 0]
[23.072, 1]
[27.786848072562357, 2]
[26.825552825552826, 3]
[24.125550660792953, 4]
[25.175257731958762, 5]
[21.357843137254903, 6]
[26.808035714285715, 7]
[27.026209677419356, 8]
[27.588014981273407, 9]
[26.612521150592215, 10]
[29.593939393939394, 11]
[30.34727503168568, 12]
[30.896514161220043, 13]
[32.33089770354906, 14]
[29.51923076923077, 15]
[25.394187102633968, 16]
[27.99572284003422, 17]
[26.924354243542435, 18]
[26.701020408163266, 19]
[23.13940724478595, 20]
[23.60983981693364, 21]
[23.265171503957784, 22]
[24.617210682492583, 23]
In [17]:
#implement the sorted function to creaate a top-list
o_sorted_swap = sorted(o_swap_avg_by_hour, reverse=True)

o_string = 'Top 5 Hours for Other Posts Comments'
print(o_string)
for row in o_sorted_swap[:5]:
    ostring1 = dt.strptime(str(row[1]), '%H')
    ostring1 = dt.strftime(ostring1, '%H:%M')
    ooutput = "{0}: {1:.2f} average comments per post".format(ostring1, row[0])
    print(ooutput)
Top 5 Hours for Other Posts Comments
14:00: 32.33 average comments per post
13:00: 30.90 average comments per post
12:00: 30.35 average comments per post
11:00: 29.59 average comments per post
15:00: 29.52 average comments per post

We found out that other post created at 14:00 (2pm) receive more comments. Howether the average numbers are distributed almost evenly, so you will get more comments if you create a post between 11:00 (11am) and 16:00 (4pm).

Now let's compare the results for all three post types.

In [18]:
#print all the results from different posts categories

print(string)
for row in sorted_swap[:5]:
    string1 = dt.strptime(str(row[1]), '%H')
    string1 = dt.strftime(string1, '%H:%M')
    output = "{0}: {1:.2f} average comments per post".format(string1, row[0])
    print(output)
    
print('\n')

print(s_string)
for row in s_sorted_swap[:5]:
    sstring1 = dt.strptime(str(row[1]), '%H')
    sstring1 = dt.strftime(sstring1, '%H:%M')
    s_output = "{0}: {1:.2f} average comments per post".format(sstring1, row[0])
    print(s_output)

print('\n')

print(o_string)
for row in o_sorted_swap[:5]:
    ostring1 = dt.strptime(str(row[1]), '%H')
    ostring1 = dt.strftime(ostring1, '%H:%M')
    ooutput = "{0}: {1:.2f} average comments per post".format(ostring1, row[0])
    print(ooutput)
Top 5 Hours for Ask Posts Comments
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


Top 5 Hours for Show Posts Comments
18:00: 15.77 average comments per post
00:00: 15.71 average comments per post
14:00: 13.44 average comments per post
23:00: 12.42 average comments per post
22:00: 12.39 average comments per post


Top 5 Hours for Other Posts Comments
14:00: 32.33 average comments per post
13:00: 30.90 average comments per post
12:00: 30.35 average comments per post
11:00: 29.59 average comments per post
15:00: 29.52 average comments per post

The result

  • Ask Posts on average get more comments throughout the day. The most commented ones are at 15:00 (3pm), 2:00 (2am), 20:00 (8pm). Then users activity declines. So the best way to get more comments is to create an Ask post at 15:00 (3pm)

  • Other posts receive less comments. The most commented ones are at 14:00 (2pm), 13:00 (1pm), and 12:00 (12pm). The difference in comments within the top-list is not striking, so you will get many comments posting between 12:00 and 16:00

  • The least amount of comments is received by the Show posts. The most commented ones are at 18:00 (6pm), 00:00 (12pm), and 14:00 (2pm).

Determining whether Show or Ask Posts Receive More Points on Average

Karma points are calculated as the number of upvotes a given user's content has received minus the number of downvotes. We want to know which type of post gets more upvotes.

In [19]:
show_points = 0

#iterate over the show_post, compute the number of points
for row in show_posts:
    points = int(row[3])
    show_points += points

#find the average by dividing show_points into num_show
avg_show_points = show_points/num_show
print(avg_show_points)
27.555077452667813
In [20]:
ask_points = 0

#iterate over the ask_posts to compute the number of points
for row in ask_posts:
    points = int(row[3])
    ask_points += points

#find the average ask points by dividing ask_points into num_ask
avg_ask_points = ask_points/num_ask
print(avg_ask_points)
15.061926605504587

We can see, that show posts receive more points on average. That means that creating a Show HN post can get you much more upvotes.

Next we'll figure out if post created at a certain time are more likely to receive more points.

Determining if Posts Created at a Certain Time are More Likely to Receive More Points

In [38]:
ask_result_list = []

#loop through ask_post, append time(row[6]) and number of points(row[3]) to the list
for row in ask_posts:
    a_list = []
    a_list.append(row[6])
    a_list.append(int(row[3]))
    ask_result_list.append(a_list)

"""create two empty dictionaries:
counts_ask_by_hour for a number of posts created at a given hour
ask_points_by_hour for a number of points for posts at a given hour
"""
counts_ask_by_hour = {}
ask_points_by_hour = {}

#loop through ask_result_list, append the data 
for row in ask_result_list:
    time_str = row[0]
    datetime_dt = dt.strptime(time_str, "%m/%d/%Y %H:%M")
    time_str = datetime_dt
    hour_dt = time_str.hour
    
    if hour_dt not in ask_points_by_hour:
        ask_points_by_hour[hour_dt] = row[1]
        counts_ask_by_hour[hour_dt] = 1
    else:
        ask_points_by_hour[hour_dt] += row[1]
        counts_ask_by_hour[hour_dt] +=1
        
average_by_hour = []

#find out the average by deviding a number of points (ask_points_by_hour) into a number of comments
for key in counts_ask_by_hour:
    a_list = []
    a_list.append(key)
    avg1 = ask_points_by_hour[key]/counts_ask_by_hour[key]
    a_list.append(avg1)
    average_by_hour.append(a_list)
    
swap_average_by_hour = []

#swap the index of time and number of posts, append the list to swap_average_by_hour
for row in average_by_hour:
    av_list = []
    av_list.append(row[1])
    av_list.append(row[0])
    swap_average_by_hour.append(av_list)
    
#implement the sorted function to  swap_average_by_hour to create a top-list   
sor_ask_average = sorted(swap_average_by_hour, reverse=True)

string2 = 'Top 5 Hours for Ask Posts Points:'
print(string2)
for row in sor_ask_average[:5]:
    string3 = dt.strptime(str(row[1]), '%H')
    string3 = dt.strftime(string3, '%H:%M')
    output = "{0}: {1:.2f} point per post".format(string3, row[0])
    print(output)
     
Top 5 Hours for Ask Posts Points:
15:00: 29.99 point per post
13:00: 24.26 point per post
16:00: 23.35 point per post
17:00: 19.41 point per post
10:00: 18.68 point per post

We can see, that Ask HN posts created at 15:00 (3pm) get considerably more points that those created at any other time.

Now we'll analyse the Show HN posts.

In [39]:
show_result_list = []

#loop through the show_posts and append the time(row[6]) and number of points(row[3]) to show_result_list
for row in show_posts:
    c_list = []
    c_list.append(row[6])
    c_list.append(int(row[3]))
    show_result_list.append(c_list)

"""create two emoty dictionaries:
counts_show_by_hour for the number of posts at a given hour
show_points_by_hour for the number of points for these posts
"""
counts_show_by_hour = {}
show_points_by_hour = {}

#loop through show_result_list, append the number of points and calculate the number of posts
for row in show_result_list:
    time_str1 = row[0]
    datetime_dt1 = dt.strptime(time_str1, "%m/%d/%Y %H:%M")
    hour_dt1 = datetime_dt1.hour
    
    if hour_dt1 not in show_points_by_hour:
        show_points_by_hour[hour_dt1] = row[1]
        counts_show_by_hour[hour_dt1] = 1
    else:
        show_points_by_hour[hour_dt1] += row[1]
        counts_show_by_hour[hour_dt1] += 1
        
show_average_by_hour = []

#compute the average number of points per post, append to show_average_by_hour
for key in counts_show_by_hour:
    a_list = []
    a_list.append(key)
    avg2 = show_points_by_hour[key]/counts_show_by_hour[key]
    a_list.append(avg2)
    show_average_by_hour.append(a_list)
    
swap_showp_by_hour = []

#swap the index of time and number of points and append to the swap_showp_by_hour
for row in show_average_by_hour:
    v_list = []
    v_list.append(row[1])
    v_list.append(row[0])
    swap_showp_by_hour.append(v_list)

#implement the sorted function to create a top-list
sor_show_pointsbh = sorted(swap_showp_by_hour, reverse=True)

string4 = 'Top 5 Hours for Show Posts Points:'
print(string4)

for row in sor_show_pointsbh[:5]:
    string5 = dt.strptime(str(row[1]), '%H')
    string5 = dt.strftime(string5, '%H:%M')
    output = '{0}: {1:.2f} points per post'.format(string5, row[0])
    print(output)
Top 5 Hours for Show Posts Points:
23:00: 42.39 points per post
12:00: 41.69 points per post
22:00: 40.35 points per post
00:00: 37.84 points per post
18:00: 36.31 points per post

We can see that the difference between the number of Show HN posts received every hour is small. Points are distributed evenly. It doesn't matter much when exactly you create a Show HN post if it's in the period:

  • from 22:00 (10pm) to 00:00 (12pm)
  • at 12:00 (12am)

Now we will compute the average number of points other posts receive.

In [40]:
other_result_list = []

#loop through other_posts, append the time (row[6]) and the number of points (row[3]) to the other_result_list
for row in other_posts:
    oth_list = []
    oth_list.append(row[6])
    oth_list.append(int(row[3]))
    other_result_list.append(oth_list)

"""create two empty dictionaries:
counts_other_by_hour - for the number of posts at a given hour
other_points_by_hour - for the number of points for these posts
"""
counts_other_by_hour = {}    
other_points_by_hour = {}

#loop through other_result_list, append time and calculate the number of post for each hour
for row in other_result_list:
    time_str3 = row[0]
    datetime_dt3 = dt.strptime(time_str3, "%m/%d/%Y %H:%M")
    hour_dt3 = datetime_dt3.hour
    
    if hour_dt3 not in other_points_by_hour:
        other_points_by_hour[hour_dt3] = row[1]
        counts_other_by_hour[hour_dt3] = 1
    else:
        other_points_by_hour[hour_dt3] += row[1]
        counts_other_by_hour[hour_dt3] += 1
        
av_other_points = []

#compute the average of points per hour, append to av_other_points
for key in counts_other_by_hour:
    f_list = []
    f_list.append(key)
    avg_f = other_points_by_hour[key]/counts_other_by_hour[key]
    f_list.append(avg_f)
    av_other_points.append(f_list)
    
swap_other_points = []

#swap the index of the time and number of points
for row in av_other_points:
    v_list = []
    v_list.append(row[1])
    v_list.append(row[0])
    swap_other_points.append(v_list)

#implement the sorted function on swap_other_points  to create a top-list
sor_other_pointsbh = sorted(swap_other_points , reverse=True)

string7 = 'Top 5 Hours for Other Posts Points:'
print(string7)

for row in sor_other_pointsbh[:5]:
    string8 = dt.strptime(str(row[1]), '%H')
    string8 = dt.strftime(string8, '%H:%M')
    oth_output = '{0}: {1:.2f} points per post'.format(string8, row[0])
    print(oth_output)
Top 5 Hours for Other Posts Points:
13:00: 62.53 points per post
14:00: 61.79 points per post
15:00: 60.54 points per post
10:00: 60.48 points per post
19:00: 60.01 points per post

The best period for posting an Other Post is between 13:00 (1pm) and 16:00 (4pm). Now we'll compare the result for different post types.

In [42]:
#print all the results
string2 = 'Top 5 Hours for Ask Posts Points:'
print(string2)
for row in sor_ask_average[:5]:
    string3 = dt.strptime(str(row[1]), '%H')
    string3 = dt.strftime(string3, '%H:%M')
    output = "{0}: {1:.2f} point per post".format(string3, row[0])
    print(output)
print('\n')

string4 = 'Top 5 Hours for Show Posts Points:'
print(string4)
for row in sor_show_pointsbh[:5]:
    string5 = dt.strptime(str(row[1]), '%H')
    string5 = dt.strftime(string5, '%H:%M')
    output = '{0}: {1:.2f} points per post'.format(string5, row[0])
    print(output)
print('\n')

string7 = 'Top 5 Hours for Other Posts Points:'
print(string7)
for row in sor_other_pointsbh[:5]:
    string8 = dt.strptime(str(row[1]), '%H')
    string8 = dt.strftime(string8, '%H:%M')
    oth_output = '{0}: {1:.2f} points per post'.format(string8, row[0])
    print(oth_output)
    
Top 5 Hours for Ask Posts Points:
15:00: 29.99 point per post
13:00: 24.26 point per post
16:00: 23.35 point per post
17:00: 19.41 point per post
10:00: 18.68 point per post


Top 5 Hours for Show Posts Points:
23:00: 42.39 points per post
12:00: 41.69 points per post
22:00: 40.35 points per post
00:00: 37.84 points per post
18:00: 36.31 points per post


Top 5 Hours for Other Posts Points:
13:00: 62.53 points per post
14:00: 61.79 points per post
15:00: 60.54 points per post
10:00: 60.48 points per post
19:00: 60.01 points per post

The Result

Maximum points on average receives a post not in the Show or Ask HN category. If you want to create such a post, the best time for it is between 13:00 (1pm) and 16:00 (4pm).

The second place is taken by the Show HN posts. The get less points. If you want to get maximum points, the best time for it is at 23:00 (11pm), 12:00 (12 am), and 22:00 (10pm).

The Ask HN post category receives half as many points as the Show HN one. You can get the maximum points if you create a post at 15:00 (3pm), 13:00 (1pm), 16:00 (4pm).

In [ ]: