'''20210921 Robin Dawes ''' def dot_product(vec1, vec2): ''' Compute the dot product of two numeric vectors represented as lists Parameters: vec1 and vec2 must be numeric vectors of equal length Returns a numeric value if the dot product can be computed. Returns None if the dot product cannot be computed. ''' if not(type(vec1) is list) or not(type(vec2) is list): return None elif len(vec1) != len(vec2): return None else: result = 0 for i in range(len(vec1)): if isinstance(vec1[i], (int, float)) and isinstance(vec2[i],(int, float)): result += vec1[i] * vec2[i] else: return None return result def test_dot_product_1(): '''This test should fail - it is included to demonstrate what pytest does when a test fails''' assert dot_product([2],[3]) == 0 def test_dot_product_2(): assert dot_product([1,1],[2,3]) == 5 def test_dot_product_3(): assert dot_product('a',[2,3,4]) == None def test_dot_product_4(): assert dot_product([1,2],[1,2,3]) == None def test_dot_product_5(): assert dot_product([1,1],['a','b']) == None