In: Computer Science
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))
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.