Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

raise NotImplementedError () NotImplementedError Traceback (most recent call las

ID: 3899712 • Letter: R

Question

raise NotImplementedError () NotImplementedError Traceback (most recent call last) in () > 2 raise NotImplementedError() NotImplementedError: For example vl - Vector ([2, 3, 4]) assert vl[1] 2 assert vl.dim() -- 3 assert repr (v1)Vector ([2, 3, 4])' v2 - Vector ([1, 2, 2]) v2[3] - 3 assert v2--Vector(, 2, 3]) assert vl + v2 - Vector ([3, 5, 7]) assert vl - v2 -- Vector ([1, 1, 1]) v2 * 2 Vector([2, 4, 6]) assert 2 * (v1 + v2) == vector( [ 6, 10, 141) Following raise exceptions: (remember, these are supposed to be mathematical vectors so we are starting indexing from 1, not 0 try: v1[0] # raises IndexError except IndexError: print("Successful test case.") try: v2 + [9] # raises ValueError except ValueError: print(" Successful test case.") try: V3 -Vector ( [ 1 , ' cat ' , 3 ] ) # Raises TypeError except TypeError: print( "Successful test case.")

Explanation / Answer

class Vector:
   def __init__(self,l):
      self.list = l

   def dim(self):
       return len(self.list)

   def __getitem__(self,i):
       try:
          a = self.list[i-1]
          return a
       except IndexError:
           print ("Out of index error")

   def __setitem__(self,i,x):
       try:
          self.list[i-1] = x
         
       except IndexError:
           print ("Out of index error")

   def __repr__(self):
       str1 = "Vector(["
       for i in range(len(self.list)):
           if i < len(self.list)-1
              str1 = str1 + self.list[i] + ", "
           else:
              str1 = str1 + self.list[i] + ", "

       return str1

   def __add__(self,other):
       try:
           if self.dim() != other.dim() or type(self) == type(other):
              raise ValueError
           for i in range(self.dim()):
               self[i] = self[i] + other[i]
       except ValueError:
           print("type mismatch")

   def __sub__(self,other):
       try:
          
           if self.dim() != other.dim() or type(self) == type(other):
              raise ValueError
           for i in range(len(self.list)):
               self[i] = self[i] - other[i]
       except ValueError:
           print("type mismatch")

   def __mul__(self,other):
       try:
           if self.dim() != other.dim():
              raise ValueError
           v = 9
           v1 = 9.0
           if type(other) != type(v) and type(other) != type(v1) && type(other) != type(self):
              raise AssertionError
           else:
              if type(other) != type(v) or type(other) != type(v1):
              self[i] = self[i] * other

           if type(self) == type(other):
              prod = 1
              for i in range(len(self.list)):
                 self[i] = prod + self[i] * other[i]
       except ValueError:
           print("type mismatch")
       except AssertionError:
           print("type mismatch")


   def __equal__(self,other):
       try:
           if type(other) != type(self):
              raise ValueError
           else:
              for i in range(self.dim()):
                  if self[i] != other[i]:
                     return False
           return True
       except AssertionError:
           print("type mismatch")