In: Computer Science
This is a question about coding in python. I think that the question is asking me to code the following: Implement the scalar product of 2 vectors in 2-space, using two tuple parameters (the function scalarProduct2). Write a docstring; then write some test function calls (can I pick any data for this? - the question gave these examples data examples: (1., 1.) dot (2,3) = (1.,1.) dot (2,0) = (1., 1.) dot (0,2) = (1., 1.,) dot (4,5) = ); then implement the body of the function ( I am not sure what this means - run the function?)
Thanks for the question.
Here is the completed code for this problem
Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change.
If you are satisfied with the solution, please rate the answer.
Thanks
===========================================================================
Here is the formula to compute the scalar product of two vectors a and b
def scalarProduct(vectorOne, vectorTwo): # first check the element count in both the tuples are same # if they are same only then we proceed to find the scalar product if len(vectorOne) == len(vectorTwo): scalar_product = 0 # iterate over each element of the two tuples for i in range(len(vectorTwo)): # multiply the elements at the same index # and add it to the scalar_product variable scalar_product += vectorOne[i] * vectorTwo[i] # return the value return scalar_product else: # incase the element counts are not same print('The dimensions of the two vectors are different. Cannot compute scalar product.') def main(): # create 3 tuples assuming they are vectors v_one = (1., 1.) v_two = (2., 3.) v_three = (4, 5) print('Scalar Product is: {}'.format(scalarProduct(v_one, v_two))) print('Scalar Product is: {}'.format(scalarProduct(v_one, v_three))) print('Scalar Product is: {}'.format(scalarProduct(v_three, v_two))) main()