Question

In: Computer Science

Using Python 1. #A significant part of introductory physics is calculating #the net force on an...

Using Python

1.

#A significant part of introductory physics is calculating
#the net force on an object based on several different
#magnitudes and directions. If you're unfamiliar with how
#this works, we recommend checking out this WikiHow article:
#https://www.wikihow.com/Find-Net-Force
#
#Each force acting on the object is defined by its angle and
#magnitude. The process for calculating net force is:
#
# - For each force, break the force into its horizontal and
# vertical components. The horizontal component can be
# calculated as magnitude * cos(angle), and the vertical
# component can be calculated as magnitude * sin(angle).
# - Sum all the horizontal components to find the total
# horizontal force, and sum the vertical components to find
# the total vertical force.
# - Use the Pythagorean theorem to calculate the total
# magnitude: sqrt(total_horizontal ^ 2 + total_vertical ^ 2)
# - Use inverse tangent to calculate the angle:
# atan(total_vertical / total_horizontal)
#
#Write a function called find_net_force. find_net_force should
#take one parameter as input: a list of 2-tuples. Each 2-tuple
#in the list is a (magnitude, angle) pair. angle will be in
#degrees between -180 and 180.
#
#Return a 2-tuple containing the final magnitude and angle of
#all the forces. angle should again be in degrees. You should
#round both magnitude and angle to one decimal place, which
#you can do using round(magnitude, 1) and round(angle, 1).
#
#To do this, you'll need to use a few functions from the math
#module in Python. The line below will import these:

from math import sin, cos, tan, asin, acos, atan2, radians, degrees, sqrt

#sin, cos, and tan are the trigonometric functions for sine,
#cosine, and tangent. Each takes one argument, an angle in
#radians, and returns its sine, cosine, or tangent.
#
#asin, acos, and atan2 are their inverse functions. Each
#takes two arguments, a vertical component and a horizontal
#component (in that order), and returns the corresponding
#angle in radians.
#
#Note that sin, cos, and tan all assume the angle is in
#radians, and asin, acos, and atan2 will all return an
#angle in radians. So, you'll need to convert your angles to
#radians before or after using these functions, using things
#like this: angle_in_radians = radians(angle)

# angle_in_degrees = degrees(angle_in_radians)

#sqrt will find the square root of a number, e.g. sqrt(4) = 2.
#Note that you should only need sin, cos, atan2, degrees,
#radians, and sqrt: we've imported the others just in case you
#want to use them.


#Add your function here!

#Below are some lines of code that will test your function.
#You can change the value of the variable(s) to test your
#function with different inputs.
#
#If your function works correctly, this will originally
#print: (87.0, 54.4)

forces = [(10, 90), (10, -90), (100, 45), (20, 180)]
print(find_net_force(forces))

2.

#Write a function called one_dimensional_booleans.
#one_dimensional_booleans should have two parameters:
#a list of booleans called bool_list and a boolean called
#use_and. You may assume that bool_list will be a list
#where every value is a boolean (True or False).
#
#The function should perform as follows:
#
# - If use_and is True, the function should return True if
# every item in the list is True (simulating the and
# operator).
# - If use_and is False, the function should return True if
# any item in the list is True (simulating the or
# operator).


#Write your function here!

#Below are some lines of code that will test your function.
#You can change the value of the variable(s) to test your
#function with different inputs.
#
#If your function works correctly, this will originally
#print: True, False, True, False.
print(one_dimensional_booleans([True, True, True], True))
print(one_dimensional_booleans([True, False, True], True))
print(one_dimensional_booleans([True, False, True], False))
print(one_dimensional_booleans([False, False, False], False))

3.

#Last exercise, you wrote a function called
#one_dimensional_booleans that performed some reasoning
#over a one-dimensional list of boolean values. Now,
#let's extend that.
#
#Imagine you have a two-dimensional list of booleans,
#like this one:
#[[True, True, True], [True, False, True], [False, False, False]]
#
#Notice the two sets of brackets: this is a list of lists.
#We'll call the big list the superlist and each smaller
#list a sublist.
#
#Write a function called two_dimensional_booleans that
#does the same thing as one_dimensonal_booleans. It should
#look at each sublist in the superlist, test it for the
#given operator, and then return a list of the results.
#
#For example, if the list above was called a_superlist,
#then we'd see these results:
#
# two_dimensional_booleans(a_superlist, True) -> [True, False, False]
# two_dimensional_booleans(a_superlist, False) -> [True, True, False]
#
#When use_and is True, then only the first sublist gets
#a value of True. When use_and is False, then the first
#and second sublists get values of True in the final
#list.
#
#Hint: This problem can be extremely difficult or
#extremely simple. Try to use your answer or our
#code from the sample answer in the previous problem --
#it can make your work a lot easier! You may even want
#to use multiple functions.


#Write your function here!

#Below are some lines of code that will test your function.
#You can change the value of the variable(s) to test your
#function with different inputs.
#
#If your function works correctly, this will originally
#print:
#[True, False, False]
#[True, True, False]
bool_superlist = [[True, True, True], [True, False, True], [False, False, False]]
print(two_dimensional_booleans(bool_superlist, True))
print(two_dimensional_booleans(bool_superlist, False))

Solutions

Expert Solution

PART 1 -

CODE -

from math import sin, cos, tan, atan2, radians, degrees, sqrt

def find_net_force(forces):

    # Initialize total horizontal force and total vertical force to 0

    force_x = 0;

    force_y = 0;

    # Iterate through the list of tuples

    for force in forces:

        # Convert angle to radians

        angle_in_radians = radians(force[1])

        # Calculate x component of force and add to the total horizontal force

        force_x += force[0] * cos(angle_in_radians)

        # Calculate y component of force and add to the total vertical force

        force_y += force[0] * sin(angle_in_radians)

    # Calculate total force

    total_force = sqrt(force_x**2 + force_y**2)

    # Find angle of the force

    angle = atan2(force_y, force_x)

    # Convert angle to degrees

    angle_in_degrees = degrees(angle)

    # Round the results to 1 decimal place

    total_force = round(total_force, 1)

    angle = round(angle_in_degrees, 1)

    # Return a tuple of magnitude and angle of force

    return (total_force, angle)

# Testing our function

forces = [(10, 90), (10, -90), (100, 45), (20, 180)]

print(find_net_force(forces))

SCREENSHOT -

PART 2 -

CODE -

def one_dimensional_booleans(bool_list, use_and):

    if use_and == True:

        # Set flag variable to True

        flag = True

        # Iterate through the list of booleans

        for element in bool_list:

            # Set flag to False and break out of loop if any element in the list is False

            if element != True:

                flag = False

                break

    elif use_and == False:

        # Set flag variable to False

        flag = False

        # Iterate through the list of booleans

        for element in bool_list:

            # Set flag to True and break out of loop if any element in the list is True

            if element == True:

                flag = True

                break

    # Return True if flag is True, otherwise return False

    if flag == True:

        return True

    else:

        return False


# Test our function

print(one_dimensional_booleans([True, True, True], True))

print(one_dimensional_booleans([True, False, True], True))

print(one_dimensional_booleans([True, False, True], False))

print(one_dimensional_booleans([False, False, False], False))

SCREENSHOT -

PART 3 -

CODE -

def two_dimensional_booleans(superlist, use_and):

    # Create an empty list to store the results

    results = []

    if use_and == True:

        # Iterate through the superlist

        for sublist in superlist:

            # Set flag variable to True

            flag = True

            # Iterate through the sublist

            for element in sublist:

                # Set flag to False and break out of loop if any element in the sublist is False

                if element != True:

                    flag = False

                    break

            # Append True to the results list if flag is True, otherwise append False

            if flag == True:

                results.append(True)

            else:

                results.append(False)

    elif use_and == False:

        # Iterate through the superlist

        for sublist in superlist:

            # Set flag variable to False

            flag = False

            # Iterate through the sublist

            for element in sublist:

                # Set flag to True and break out of loop if any element in the sublist is True

                if element == True:

                    flag = True

                    break

            # Append True to the results list if flag is True, otherwise append False

            if flag == True:

                results.append(True)

            else:

                results.append(False)

    

    # Return the results list

    return results

# Test our function

bool_superlist = [[True, True, True], [True, False, True], [False, False, False]]

print(two_dimensional_booleans(bool_superlist, True))

print(two_dimensional_booleans(bool_superlist, False))

SCREENSHOT -

If you have any doubt regarding the solution, then do comment.
Do upvote.


Related Solutions

3. Solve for frictional force in part A and the net force in Part B ....
3. Solve for frictional force in part A and the net force in Part B . Write your final answers for parts A and B rounded to 3 significant digits. a. A 2.14 kg block is sliding along a rough horizontal surface, and the only unbalanced force acting on the block is a constant frictional force. Suppose the block is moving at speed 88.8 m/s at t = 0, and after 38.9 seconds it comes to rest. Find the magnitude...
Is this part of the task taken into account when calculating net cash flow of the...
Is this part of the task taken into account when calculating net cash flow of the project? "The launching of the new product is expected to lead to a reduction in sales of the existing product. The contribution from existing sales is expected to reduce by €50,000 per annum over the life of the new product. Working capital of €500 000 will be required at the beginning of the project."
Part 1: Write an introductory paragraph introducing the reader to the statistical study. Part 2: Write...
Part 1: Write an introductory paragraph introducing the reader to the statistical study. Part 2: Write a summary of what statistics you calculated for the independent variable (exercise) and the dependent variable (systolic blood pressure) and a summary Part 3: Write a summary of what statistics you calculated for the independent variable (exercise) and the dependent variable (systolic blood pressure) and a summary. Part 4: Write a summary of what statistics you calculated for the independent variable (educational level) and...
How Does SpaceX's rocket launch relate to Physical science? Using force and physics? With 200 words...
How Does SpaceX's rocket launch relate to Physical science? Using force and physics? With 200 words please explain. Artcile: https://www.scientificamerican.com/article/spacex-launches-rocket-with-highest-ever-reentry-force/
​Part A Calculating equilibrium concentrations when the net reaction proceeds forward Consider mixture B, which will...
​Part A Calculating equilibrium concentrations when the net reaction proceeds forward Consider mixture B, which will cause the net reaction to proceed forward. Concentration (M) initial: change: equilibrium: [XY] 0.500 −x 0.500−x net→ ⇌ [X] 0.100 +x 0.100+x + [Y] 0.100 +x 0.100+x The change in concentration, x , is negative for the reactants because they are consumed and positive for the products because they are produced. Based on a Kc value of 0.160 and the given data table, what...
​Part A Calculating equilibrium concentrations when the net reaction proceeds forward Consider mixture B, which will...
​Part A Calculating equilibrium concentrations when the net reaction proceeds forward Consider mixture B, which will cause the net reaction to proceed forward. Concentration (M) initial: change: equilibrium: [XY] 0.500 −x 0.500−x net→ ⇌ [X] 0.100 +x 0.100+x + [Y] 0.100 +x 0.100+x The change in concentration, x , is negative for the reactants because they are consumed and positive for the products because they are produced. Based on a Kc value of 0.160 and the given data table, what...
What is the magnitude of the net force on the first wire in (figure 1)?
Figure 1 What is the magnitude of the net force on the first wire in (Figure 1)? What is the magnitude of the net force on the second wire in (Figure 1)? What is the magnitude of the net force on the third wire in (Figure 1)?
Understanding Physics - Problem 3) Part 1) The figure is a section of a conducting rod...
Understanding Physics - Problem 3) Part 1) The figure is a section of a conducting rod of radius R1 = 1.40 mm and length L = 14.40 m inside a thin-walled coaxial conducting cylindrical shell of radius R2 = 11.1R1 and the (same) length L. The net charge on the rod is Q1 = +3.72 × 10-12 C; that on the shell is Q2 = -2.12Q1. What are the (a) magnitude E and (b) direction (radially inward or outward) of...
Which of the following should be deducted from net income in calculating net cash flow from operating activities using the indirect method
19. Which of the following should be deducted from net income in calculating net cash flow from operating activities using the indirect method? a depreciation expense b. gain on sale of land c a loss on the sale of equipment d dividends declared and paid 20. Which of the following below increases cash? a depreciation expense b. acquisition of treasury stock c. borrowing money by issuing a six-month note d. the declaration of a cash dividend 21. Which one of the following...
1) A professor using an open-source introductory statistics book predicts that 60% of the students will...
1) A professor using an open-source introductory statistics book predicts that 60% of the students will purchase a hard copy of the book, 25% will print it out from the web, and 15% will read it online. At the end of the semester she asks her students to complete a survey where they indicate what format of the book they used. Of the 126 students, 71 said they bought a hard copy of the book, 30 said they printed it...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT