Vector Module

You are part of a game development team, and you've been assigned the task of creating a module to perform operations on vectors. These operations are frequently used in gaming mechanics, such as calculating the trajectory of an object or determining the distance between two entities in the game world.
The vector is represented as a list of numbers, where each number represents the component of the vector along each axis (i.e., [x, y, z] for a 3-dimensional vector).
Your task is to create a Python module named vector.py that provides the following functionalities:
  • add(): Adding two vectors should result in a new vector where each component is the result of the addition of the components of the input vectors at the same position.
  • subtract(): Subtracting two vectors should result in a new vector where each component is the result of the subtracting of the components of the input vectors at the same position.
  • dot_product(): Calculating the dot product of two vectors by multiplying the corresponding components of the two vectors and adding all these products.
  • magnitude(): Calculating the magnitude of a vector as the square root of the sum of the squares of all the components of the vector.
The input and output will be handled by the main.py module, you just need to implement the vector.py module.
Input
Output
print(add([1, 2, 3, 2], [4, 5, 6, -1]))
[5, 7, 9, 1]
print(subtract([5, 7, 9], [4, 5, 6]))
[1, 2, 3]
print(dot_product([1, 2, 3], [4, 5, 6]))
32
print(magnitude([1, 2, 3]))
3.7416573867739413
 

Constraints

Time limit: 2 seconds

Memory limit: 512 MB

Output limit: 1 MB

To check your solution you need to sign in
Sign in to continue