Assignment 4

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

``````
``````

In [2]:

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 [3]:

def my_median(input_list):
list_length = len(input_list)
return sorted(input_list)[int(list_length/2)]
# Problem 03 Wrong definition of median
# List sorted and int function added

``````
``````

In [4]:

api_key = "ffaf60d7d82258e112dd4fb2b5e4e2d6:3:72421680"

``````
``````

In [5]:

url = "http://api.nytimes.com/svc/search/v2/articlesearch.json?q=gay+marriage&api-key=%s" % api_key
# Problem 01 : wrong variaable
# Solution : Change API_key -> api_key

``````
``````

In [6]:

r = requests.get(url)

``````
``````

In [7]:

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

``````
``````

Out[7]:

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

``````
``````

In [8]:

# Problem #03-1 : inputs are string, therefore, must numerize them.
# Problem #03-2 : Must ignore None or convert  to 0
# Solution : Numerize the list
wc_list = [int(x) for x in wc_list if x is not None]
wc_list

``````
``````

Out[8]:

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

``````
``````

In [9]:

import numpy as np

``````
``````

In [10]:

my_mean(wc_list)

``````
``````

Out[10]:

720.7777777777778

``````
``````

In [11]:

np.mean(wc_list)

``````
``````

Out[11]:

720.77777777777783

``````
``````

In [12]:

my_median(wc_list)

``````
``````

Out[12]:

684

``````
``````

In [13]:

np.median(wc_list)

``````
``````

Out[13]:

684.0

``````
``````

In [ ]:

``````