Hacker New Posts Issue

Screen Link:

My Code: import datetime as dt

result_list =
counts_by_hour = {}
comments_by_hour = {}

for row in ask_posts:
created_at = row[6]
num_comments = int(row[4])
result_list.append([created_at, num_comments])

counts_by_hour = {}
comments_by_hour = {}
for row in result_list:
hour = dt.datetime.strptime(row[0],"%m/%d/%Y %H:%M")
hour = hour.strftime("%H")
if hour in comments_by_hour:
comments_by_hour[hour] = int(row[1])
counts_by_hour[hour] += 1
else:
comments_by_hour[hour] = int(row[1])
counts_by_hour[hour] = 1

print(comments_by_hour)

avg_comments_per_hour =
for hour in comments_by_hour:
avg_comments_per_hour.append([hour, counts_by_hour[hour]/comments_by_hour[hour]])
print(avg_comments_per_hour)

What I expected to happen:
The code is running, but the output according to DQ should be [
[‘09’, 5.5777777777777775],
[‘13’, 14.741176470588234],
[‘10’, 13.440677966101696],
[‘14’, 13.233644859813085],
[‘16’, 16.796296296296298],
[‘23’, 7.985294117647059],
[‘12’, 9.41095890410959],
[‘17’, 11.46],
[‘15’, 38.5948275862069],
[‘21’, 16.009174311926607],
[‘20’, 21.525],
[‘02’, 23.810344827586206],
[‘18’, 13.20183486238532],
[‘03’, 7.796296296296297],
[‘05’, 10.08695652173913],
[‘19’, 10.8],
[‘01’, 11.383333333333333],
[‘22’, 6.746478873239437],
[‘08’, 10.25],
[‘04’, 7.170212765957447],
[‘00’, 8.127272727272727],
[‘06’, 9.022727272727273],
[‘07’, 7.852941176470588],
[‘11’, 11.051724137931034]
]
but mine is

[[‘04’, 23.5], [‘19’, 55.0], [‘21’, 13.625], [‘20’, 8.88888888888889], [‘10’, 59.0], [‘22’, 71.0], [‘23’, 34.0], [‘13’, 6.538461538461538], [‘18’, 0.5477386934673367], [‘06’, 2.0], [‘00’, 3.6666666666666665], [‘05’, 23.0], [‘03’, 54.0], [‘15’, 116.0], [‘11’, 2.0], [‘12’, 24.333333333333332], [‘09’, 22.5], [‘17’, 20.0], [‘08’, 24.0], [‘14’, 5.944444444444445], [‘01’, 15.0], [‘07’, 34.0], [‘02’, 9.666666666666666], [‘16’, 54.0]]

My “avg_ask_comments” from earlier was correct, so I figured the error was in this step. Any help would be appreciated!

The problem is here:

It should be if hour in counts_by_hour: .

Hello @adam.t.rafael, welcome to the community!

The first error is in this step:

if hour in comments_by_hour:
    comments_by_hour[hour] = int(row[1])
    counts_by_hour[hour] += 1

The correct code should be:

if hour in comments_by_hour:
    comments_by_hour[hour] += int(row[1])
    counts_by_hour[hour] += 1

The second error is in this step:

for hour in comments_by_hour:
    avg_comments_per_hour.append([hour, counts_by_hour[hour]/comments_by_hour[hour]])

The correct code should be:

for hour in comments_by_hour:
    avg_comments_per_hour.append([hour, (comments_by_hour[hour]/counts_by_hour[hour])])

I hope this helps.

Thank you for reaching out! Unfortunately, this lead to the same answer

This worked! Thank you for the help. I understand why there was an issue for the first one (I was setting it equal to the amount of comments, not adding them together) but what was the issue with the second portion?

@adam.t.rafael,

It’s my pleasure.

For the second portion, the value of the average comments per hour is gotten by comments_by_hour[hour]/counts_by_hour[hour]
not
counts_by_hour[hour]/comments_by_hour[hour]

I hope this answers your question.

@adam.t.rafael,

Also, if your problem is solved, Kindly mark the reply as a solution. :blush: