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 [1]:
import requests # a better package than urllib2
import numpy as np

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

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

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

In [5]:
nyt_recent_articles= r.json()

In [6]:
nyt_recent_articles.keys()


Out[6]:
dict_keys(['copyright', 'status', 'response'])

In [7]:
nyt_recent_articles['response']['docs'][3]


Out[7]:
{'_id': '5776157438f0d87c9d837a62',
 'abstract': None,
 'blog': [],
 'byline': {'original': 'By CAMPBELL ROBERTSON',
  'person': [{'firstname': 'Campbell',
    'lastname': 'ROBERTSON',
    'organization': '',
    'rank': 1,
    'role': 'reported'}]},
 'document_type': 'article',
 'headline': {'main': 'Mississippi Law Protecting Opponents of Gay Marriage Is Blocked',
  'print_headline': 'Mississippi Judge Blocks Anti-Gay Marriage Law'},
 'keywords': [{'is_major': 'N',
   'name': 'glocations',
   'rank': '1',
   'value': 'Mississippi'},
  {'is_major': 'N',
   'name': 'subject',
   'rank': '2',
   'value': 'Same-Sex Marriage, Civil Unions and Domestic Partnerships'},
  {'is_major': 'N',
   'name': 'subject',
   'rank': '3',
   'value': 'Decisions and Verdicts'},
  {'is_major': 'N',
   'name': 'persons',
   'rank': '4',
   'value': 'Reeves, Carlton W (1964- )'}],
 'lead_paragraph': 'Minutes before it was to go into effect, a federal judge blocked a measure that would go beyond religious freedom laws in protecting those opposed to same-sex marriages.',
 'multimedia': [{'height': 126,
   'legacy': {'wide': 'images/2016/07/02/us/02mississippi/02mississippi-thumbWide.jpg',
    'wideheight': '126',
    'widewidth': '190'},
   'subtype': 'wide',
   'type': 'image',
   'url': 'images/2016/07/02/us/02mississippi/02mississippi-thumbWide.jpg',
   'width': 190},
  {'height': 369,
   'legacy': {'xlarge': 'images/2016/07/02/us/02mississippi/02mississippi-articleLarge.jpg',
    'xlargeheight': '369',
    'xlargewidth': '600'},
   'subtype': 'xlarge',
   'type': 'image',
   'url': 'images/2016/07/02/us/02mississippi/02mississippi-articleLarge.jpg',
   'width': 600},
  {'height': 75,
   'legacy': {'thumbnail': 'images/2016/07/02/us/02mississippi/02mississippi-thumbStandard.jpg',
    'thumbnailheight': '75',
    'thumbnailwidth': '75'},
   'subtype': 'thumbnail',
   'type': 'image',
   'url': 'images/2016/07/02/us/02mississippi/02mississippi-thumbStandard.jpg',
   'width': 75}],
 'news_desk': 'National',
 'print_page': '11',
 'pub_date': '2016-07-02T00:00:00Z',
 'section_name': 'U.S.',
 'slideshow_credits': None,
 'snippet': 'Minutes before it was to go into effect, a federal judge blocked a measure that would go beyond religious freedom laws in protecting those opposed to same-sex marriages.',
 'source': 'The New York Times',
 'subsection_name': None,
 'type_of_material': 'News',
 'web_url': 'http://www.nytimes.com/2016/07/02/us/mississippi-law-protecting-opponents-of-gay-marriage-is-blocked.html',
 'word_count': '920'}

In [10]:
wc_list = []
for nyt_recent_article in nyt_recent_articles['response']['docs']:
    if nyt_recent_article['word_count']:
        wc_list.append(int(nyt_recent_article['word_count']))
print(wc_list)


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

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 [11]:
def my_median(input_list):
    input_list_sorted = sorted(input_list)
    list_length = len(input_list_sorted)
    return input_list_sorted[int(list_length/2)]

In [12]:
my_mean(wc_list)


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-12-0d341f097dad> in <module>()
----> 1 my_mean(wc_list)

NameError: name 'my_mean' is not defined

In [13]:
np.mean(wc_list)


Out[13]:
650.44444444444446

In [14]:
my_median(wc_list)


Out[14]:
684

In [15]:
np.median(wc_list)


Out[15]:
684.0

In [ ]: