Advertisement
L1ghtsh0w

Untitled

Oct 23rd, 2016
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.58 KB | None | 0 0
  1. def dot_product(v, u):
  2.     """ (list of num, list of num) -> num
  3.        Returns the dot product of the two vectors v and u.
  4.        Precondition: len(v) == len(u)
  5.    """
  6.     try:
  7.         if v == [] and u == []:
  8.             return 0
  9.         else:
  10.             return v[0]*u[0] + dot_product(v[1:], u[1:])
  11.     except IndexError:
  12.          # the length of the vectors not the same
  13.          print('The length of the vectors is not the same')
  14.          return None
  15.     except TypeError:
  16.          # There is an incorrect type used
  17.          print("Incorrect type!")
  18.          return None
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement