The writer of this code wants to count the mean and median article length for recent articles on gay marriage in the New York Times. This code has several issues, including errors. When they checked their custom functions against the numpy functions, they noticed some discrepancies. Fix the code so it executes properly, retrieves the articles, and outputs the correct result from the custom functions, compared to the numpy functions.


In [ ]:
import requests # a better package than urllib2

In [ ]:
def my_mean(input_list):
    list_sum = 0
    list_count = 0
    for el in input_list:
        list_sum += el
        list_count += 1
    return list_sum / list_count

In [ ]:
def my_median(input_list):
    list_length = len(input_list)
    return input_list[list_length/2]

In [ ]:
api_key = "ffaf60d7d82258e112dd4fb2b5e4e2d6:3:72421680"

In [ ]:
url = "http://api.nytimes.com/svc/search/v2/articlesearch.json?q=gay+marriage&api-key=%s" % API_key

In [ ]:
r = requests.get(url)

In [ ]:
wc_list = []
for article in r.json()['response']['docs']:
    wc_list.append(article['word_count'])

In [ ]:
my_mean(wc_list)

In [ ]:
import numpy as np

In [ ]:
np.mean(wc_list)

In [ ]:
my_median(wc_list)

In [ ]:
np.median(wc_list)

In [ ]: