return None
return float(sum(l)) / len(l)
+
+
+def filter_animals(date, time, animals, count, species):
+ """
+ Return only the data that apply to species.
+
+ """
+ filtered_date = []
+ filtered_time = []
+ filtered_animals = []
+ filtered_count = []
+
+ for i, a in enumerate(animals):
+ if a == species:
+ filtered_date.append(date[i])
+ filtered_time.append(time[i])
+ filtered_animals.append(animals[i])
+ filtered_count.append(count[i])
+
+ return filtered_date, filtered_time, filtered_animals, filtered_count
+
+
+def mean_animals_sighted(filename, species):
+ """
+ Return the mean number of animals seen per sighting
+ in filename.
+
+ """
+ date, time, animals, count = read_animals(filename)
+ date, time, animals, count = \
+ filter_animals(date, time, animals, count, species)
+
+ if len(count) == 0:
+ raise ValueError('That animal is not in the file!')
+
+ mean_sighted = mean(count)
+ return mean_sighted
--- /dev/null
+#!/usr/bin/env python
+import sys
+
+print sys.argv
--- /dev/null
+#!/usr/bin/env python
+import sys
+
+import animals
+
+if len(sys.argv) != 3:
+ print 'Usage: meananimals filename species'
+ sys.exit()
+
+filename = sys.argv[1]
+species = sys.argv[2]
+
+sighted = animals.mean_animals_sighted(filename, species)
+print sighted
def test_mean4():
l = []
assert animals.mean(l) == None
+
+
+def test_filter_animals():
+ date, time, animal, count = \
+ animals.read_animals('animals.txt')
+
+ species = 'Elk'
+ date, time, animal, count = \
+ animals.filter_animals(date, time, animal, count, species)
+
+ assert count == [25, 26]
+ assert animal == ['Elk', 'Elk']
+
+
+def test_mean_animals_sighted1():
+ mean_sighted = animals.mean_animals_sighted('animals.txt',
+ 'Wolverine')
+ assert mean_sighted == 31
+
+
+def test_mean_animals_sighted2():
+ mean_sighted = animals.mean_animals_sighted('animals.txt',
+ 'Elk')
+ assert mean_sighted == 25.5
+
+
+import nose
+@nose.tools.raises(ValueError)
+def test_mean_animals_sighted3():
+ mean_sighted = animals.mean_animals_sighted('animals.txt',
+ 'Pangolin')