Advertisement
Guest User

Untitled

a guest
Jul 9th, 2020
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.29 KB | None | 0 0
  1. class Vector:
  2.  
  3.     def __init__(self, values):
  4.         self.values = values
  5.  
  6.     @staticmethod
  7.     def itol(value):
  8.         i = 0
  9.         ret = []
  10.         while i < value:
  11.             ret.append(float(i))
  12.             i += 1
  13.         return ret
  14.  
  15.     @staticmethod
  16.     def ttol(value):
  17.         ret = []
  18.         for i in range(value[0], value[1]):
  19.             ret.append(float(i))
  20.         return ret
  21.  
  22.     @staticmethod
  23.     def is_float_list(value):
  24.         for nbr in value:
  25.             if not(isinstance(nbr, float)):
  26.                 return False
  27.         return True
  28.  
  29.     @property
  30.     def values(self):
  31.         return self._values
  32.  
  33.     @property
  34.     def size(self):
  35.         return len(self._values)
  36.  
  37.     @values.setter
  38.     def values(self, value):
  39.         try:
  40.             if not value:
  41.                 raise ValueError
  42.             elif isinstance(value, int):
  43.                 self._values = self.itol(value)
  44.             elif isinstance(value, tuple):
  45.                 self._values = self.ttol(value)
  46.             elif isinstance(value, list):
  47.                 if not(self.is_float_list(value)):
  48.                     raise TypeError
  49.                 self._values = value
  50.             else:
  51.                 raise TypeError
  52.         except ValueError:
  53.             print("Error:\nNo argument found")
  54.         except TypeError:
  55.             print("Error:\n\
  56. You should be able to initialize the object with:\n\
  57. • a list of floats: Vector([0.0, 1.0, 2.0, 3.0])\n\
  58. • a size Vector(3) -> the vector will have values = [0.0, 1.0, 2.0]\n\
  59. • a range or Vector((10,15)) -> the vector will have values = [10.0, 11.0, \
  60. 12.0, 13.0, 14.0]\
  61.                ")
  62.  
  63.     def __str__(self):
  64.         txt = f"Vector {self.values}"
  65.         return txt
  66.  
  67.     def __add__(self, other):
  68.         ret = []
  69.         try:
  70.             if not(isinstance(other, (Vector, int, float))):
  71.                 raise TypeError
  72.             elif isinstance(other, Vector):
  73.                 if self.size != other.size:
  74.                     raise ValueError
  75.                 for i in range(0, self.size):
  76.                     ret.append(self.values[i] + other.values[i])
  77.             else:
  78.                 for t in range(0, self.size):
  79.                     ret.append(self.values[t] + other)
  80.         except TypeError:
  81.             print("Error:\nVector instance can only be sum with another \
  82. Vector instance or with a scalar(int, float)")
  83.         except ValueError:
  84.             print("Error:\nVector instance can only be sum with the same \
  85. dimension")
  86.         return ret
  87.  
  88.     def __radd__(self, other):
  89.         ret = []
  90.         try:
  91.             if not(isinstance(other, (int, float))):
  92.                 raise TypeError
  93.             else:
  94.                 for t in range(0, self.size):
  95.                     ret.append(self.values[t] + other)
  96.         except TypeError:
  97.             print("Error:\nVector instance can only be sum with another \
  98. Vector instance or with a scalar(int, float)")
  99.         return ret
  100.  
  101.     def __sub__(self, other):
  102.         ret = []
  103.         try:
  104.             if not(isinstance(other, (Vector, int, float))):
  105.                 raise TypeError
  106.             elif isinstance(other, Vector):
  107.                 if self.size != other.size:
  108.                     raise ValueError
  109.                 for i in range(0, self.size):
  110.                     ret.append(self.values[i] - other.values[i])
  111.             else:
  112.                 for t in range(0, self.size):
  113.                     ret.append(self.values[t] - other)
  114.         except TypeError:
  115.             print("Error:\nVector instance can only be sub with another \
  116. Vector instance or with a scalar(int, float)")
  117.         except ValueError:
  118.             print("Error:\nVector instance can only be sub with the same \
  119. dimension")
  120.         return ret
  121.  
  122.     def __rsub__(self, other):
  123.         ret = []
  124.         try:
  125.             if not(isinstance(other, (int, float))):
  126.                 raise TypeError
  127.             else:
  128.                 for t in range(0, self.size):
  129.                     ret.append(self.values[t] - other)
  130.         except TypeError:
  131.             print("Error:\nVector instance can only be sub with another \
  132. Vector instance or with a scalar(int, float)")
  133.         return ret
  134.  
  135.     def __truediv__(self, other):
  136.         ret = []
  137.         try:
  138.             if not(isinstance(other, (int, float))):
  139.                 raise TypeError
  140.             else:
  141.                 for t in range(0, self.size):
  142.                     ret.append(self.values[t] / other)
  143.         except TypeError:
  144.             print("Error:\nVector instance can only be div with a \
  145. scalar(int, float)")
  146.         return ret
  147.  
  148.     def __rtruediv__(self, other):
  149.         ret = []
  150.         try:
  151.             if not(isinstance(other, (int, float))):
  152.                 raise TypeError
  153.             else:
  154.                 for t in range(0, self.size):
  155.                     ret.append(self.values[t] / other)
  156.         except TypeError:
  157.             print("Error:\nVector instance can only be div with a \
  158. scalar(int, float)")
  159.         return ret
  160.  
  161.     def __mul__(self, other):
  162.         try:
  163.             if not(isinstance(other, (Vector, int, float))):
  164.                 raise TypeError
  165.             elif isinstance(other, Vector):
  166.                 ret = 0
  167.                 if self.size != other.size:
  168.                     raise ValueError
  169.                 for i in range(0, self.size):
  170.                     ret += self.values[i] * other.values[i]
  171.             else:
  172.                 ret = []
  173.                 for t in range(0, self.size):
  174.                     ret.append(self.values[t] * other)
  175.         except TypeError:
  176.             print("Error:\nVector instance can only be multiplication \
  177. with another Vector instance or with a scalar(int, float)")
  178.         except ValueError:
  179.             print("Error:\nVector instance can only be multiplicated with \
  180. the same dimension")
  181.         return ret
  182.  
  183.     def __rmul__(self, other):
  184.         ret = []
  185.         try:
  186.             if not(isinstance(other, (int, float))):
  187.                 raise TypeError
  188.             else:
  189.                 for t in range(0, self.size):
  190.                     ret.append(self.values[t] * other)
  191.         except TypeError:
  192.             print("Error:\nVector instance can only be multiplicated with \
  193. another Vector instance or with a scalar(int, float)")
  194.         return ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement