The code in Exercise4.ipynb is meant to search for New York Times articles on gay marriage and look at the mean and median word count, but the code has some problems

Follow the instructions in the notebook to fix the code and submit your fixed code

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

In [26]:
import numpy as np

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

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

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

In [47]:
articles= r.json()

In [49]:
articles.keys()


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

In [52]:
articles['response']['docs'][2]


Out[52]:
{'_id': '577615717988101bbb4b9908',
 '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 [100]:
wc_list = []
for article in articles['response']['docs']:
    if article['word_count']:
        wc_list.append(int(article['word_count']))
print(wc_list)


[25, 920, 576, 868, 1101, 684, 588, 367, 1358]

In [101]:
length = len(wc_list)
print(length)


9

In [102]:
sorted(wc_list)


Out[102]:
[25, 367, 576, 588, 684, 868, 920, 1101, 1358]

In [103]:
print(type(length/2))


<class 'float'>

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

In [106]:
my_mean(wc_list)


Out[106]:
720.7777777777778

In [107]:
np.mean(wc_list)


Out[107]:
720.77777777777783

In [108]:
my_median(wc_list)


Out[108]:
684

In [109]:
np.median(wc_list)


Out[109]:
684.0

In [ ]: