Question

In: Computer Science

This is a question about coding in python. I think that the question is asking me...

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?)

Solutions

Expert Solution

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()


Related Solutions

This is Python coding question, and topic is loop Can anyone help me figuring these out?...
This is Python coding question, and topic is loop Can anyone help me figuring these out? (Please do not use build in function) Thanks. 1. Write a program that prints your name 100 times to the screen. 2. Write a program that takes a string s and an integer n as parameters and prints the string s a total of in n times(once per line) 3. Write a for loop that prints all the integers from 3141 to 5926, skipping...
Python Coding Question (Data Science) Hello I am having difficulty writing a code in python to...
Python Coding Question (Data Science) Hello I am having difficulty writing a code in python to do a specific task. I have two text files, Positive.txt and Practice_forhw1.txt. I want to write a script that will check if any words in Practice_forhw1.txt match any of the words in Positive.txt then the word would get replaced with a "+1" in the Practice_forhw1.txt and then print out the Practice_forhw1.txt.  (i.e if the word "happy" is in both Positive.txt and Practice_forhw1.txt then I want...
I have a python coding question: Write the function: eAapproximately (n), that takes a positive integer...
I have a python coding question: Write the function: eAapproximately (n), that takes a positive integer value n and returns an approximation of e as (1 + 1/n)^n I am not even sure what the question is asking me to do but I think it is asking me to code that function. Does anyone know how to do this?
1. I am interested in asking people what they think about the current election and who...
1. I am interested in asking people what they think about the current election and who their favorite candiate is. I decide to go to random subdivisons and city blocks and ask 15 people from each region who their favorite candiate is. I decide to go to random subdivisons and city blocks and ask 15 people from each region who their favorite candiate is. This is an example of: a. Stratified sampling b. Simple Random Sampling c. Systematic Sampling. d....
I am in this electrical engineering project course in which an assignment is asking me this...
I am in this electrical engineering project course in which an assignment is asking me this and I cannot make heads or tails of what it means by this... Discuss the tradeoffs involved between using patents and trade secrets to protect intellectual property. Please be as descriptive as possible, THANKS
note: this is the first time asking a question, I know this isn't a physcology question,...
note: this is the first time asking a question, I know this isn't a physcology question, but that was the only "subject" under social sciences that came anywhere near to the topic. is that what I should have done? James Madison, in the Federalist Papers, argued that “factions” (interest groups) will balance out and neutralize each other across topics, geographic regions, interests, incomes, ethnic and religious backgrounds, and so on. Thus, no single or no few groups will dominate. In...
Python Coding Question: Find the minimum number of coins that make a given value.
Python Coding Question: Find the minimum number of coins that make a given value.
<Python coding question string practice> Can anyone please answer these python string questions?? 1. Write a...
<Python coding question string practice> Can anyone please answer these python string questions?? 1. Write a function called username that takes as parameters a person's first and last names and prints out his or her username formatted as the last name followed by an underscore and the first initial. For example, username("Martin", "freeman") should print the string "freeman_m". 2. Write a function called letters that takes as a parameter a string and prints out the characters of the string, one...
Python 2.7 Question: If-Else Statement Is there a way for me to ask the user a...
Python 2.7 Question: If-Else Statement Is there a way for me to ask the user a series of questions with an if-else statement? For example, if a statement is true ask these questions, if false ask these questions. This is what I have so far: if user_input == "yes ": ques.check_string("Enter Name: ") ques.check_string("Enter email: ") elif user_input != "yes": ques.check_string("Enter Name:  ") ques.check_string("Enter phone number: ")   
I am not quite sure what this question is asking, nor do I understand how to...
I am not quite sure what this question is asking, nor do I understand how to apply the lower-of-cost-or-NRV. The controller of Alt Company is applying the lower-of-cost-or-net realizable value basis of valuing its ending inventory. The following information is available: Cost Net Realizable Value Lawnmowers: Self-propelled $14,800 $17,000 Push type 19,000 18,000 Total 33,800 35,000 Snowblowers: Manual 29,800 31,000 Self-start 19,000 21,000 Total 48,800 52,000 Total inventory $82,600 $87,000 Compute the value of the ending inventory by applying the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT