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 [9]:
import requests # a better package than urllib2

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

In [11]:
def my_median(input_list):
    list_length = len(input_list)
    sorted_list = sorted(input_list)

    if list_length % 2 == 1: 
        return sorted_list[int(len(list_length)+1)/2] # if it's an odd list, we return the central number

    else: 
        mean_idx = int(list_length/2)
        return (sorted_list[mean_idx] + sorted_list[mean_idx-1]) / 2

In [12]:
input_list=[1,6,3,9,16]
new=sorted(input_list)

In [15]:
API_key = "ffaf60d7d82258e112dd4fb2b5e4e2d6:3:72421680"

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

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

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

In [19]:
wc_list = [int(i) for i in wc_list if i != None] # missing conversion to int type
wc_list


Out[19]:
[25, 576, 920, 868, 684, 96, 1101, 1217, 367]

In [20]:
my_mean(wc_list)


Out[20]:
650.4444444444445

In [21]:
import numpy as np

In [22]:
np.mean(wc_list)


Out[22]:
650.44444444444446

In [23]:
my_median(wc_list)


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-23-0f063962cfdc> in <module>()
----> 1 my_median(wc_list)

<ipython-input-11-27c23a75bbcd> in my_median(input_list)
      4 
      5     if list_length % 2 == 1:
----> 6         return sorted_list[int(len(list_length)+1)/2] # if it's an odd list, we return the central number
      7 
      8     else:

TypeError: object of type 'int' has no len()

In [24]:
np.median(wc_list)


Out[24]:
684.0

In [ ]: