def test_with_set(self): sequence = {1, 2, 3, 4, 5} initial = 5 self.assertEqual(my_reduce(add, sequence), 15) self.assertEqual(my_reduce(add, sequence, initial), 20)
def test_num_args_less_than_2(self): with self.assertRaises(TypeError): my_reduce() with self.assertRaises(TypeError): my_reduce(add)
def test_with_one_item_in_the_list(self): self.assertEqual(1, my_reduce(add, [1]))
def test_with_list(self): sequence = [1, 2, 3] initial = 4 self.assertEqual(my_reduce(add, sequence), 6) self.assertEqual(my_reduce(add, sequence, initial), 10)
def test_no_function_provided(self): with self.assertRaises(TypeError): my_reduce(1, [1,2,3])
def test_empty_sequence_provided(self): with self.assertRaises(TypeError): my_reduce(add, [])
def test_arg_two_must_support_iteration(self): does_not_support_iteration = 4 with self.assertRaises(TypeError): my_reduce(add, does_not_support_iteration)
def test_num_args_greater_than_3(self): with self.assertRaises(TypeError): my_reduce(add, {1, 2, 3}, 5, 99) with self.assertRaises(TypeError): my_reduce(add, {1, 2, 3}, 5, 99, 33)
import sys import my_map import my_reduce key_value = dict() for filename in ['data_2010.txt', 'data_2011.txt', 'data_2012.txt', 'data_2013.txt', 'data_2014.txt', 'data_2015.txt']: in_file = open(filename, 'r') key_value_per_file = my_map.my_map(in_file) for key in key_value_per_file: if(key in key_value): key_value[key].append(key_value_per_file[key]) else: key_value[key]=[key_value_per_file[key]] reduced = dict() for key in key_value: reduced[key] = my_reduce.my_reduce(key_value[key]) for key in reduced: print(key + ": " + str(reduced[key]))