Challenge: Overload the Operators for Vectors

Overload the operators for Vector class.

Problem statement

In this challenge, you are required to overload the following three operators for the Vector class we implemented in the previous lesson:

  • -: Unary negation operator
  • -: Subtraction operator

In the case of vectors, - (unary negation) negates all the components of a vector. Whereas, - (subtraction) gives a vector as a result of subtraction b/w two vectors.

Input

  • Unary negation operator: A vector
  • Subtraction operator: Two vectors

Output

  • Unary negation operator: A vector with every component negated.
  • Subtraction operator: A resultant vector after a pairwise subtraction of two vectors.

Sample input

Suppose we have two vectors: vector1 and vector2.

  • Unary negation operator:
    -vector1
    
  • Subtraction operator:
    vector1 - vector2
    

Sample output

For example, if vector1 is 2i - 3j + 5k and vector2 is -4i - 2j + 11k then:

  • Unary negation operator: -2i + 3j - 5k
  • Subtraction operator: 6i - 1j - 6k

🧐 Note: Assume, that operands are of the correct data type.

Do not forget to use the concepts taught in the previous lessons. Feel free to view the solution, after giving it a few shots. Good luck!

Overload the negation operator

Override __neg__() to overload the - operator.

Get hands-on with 1400+ tech skills courses.