Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PYTHON PROGRAMING HELP Flight Trajectory Describe,formulaically,howyouwouldsolve

ID: 3747988 • Letter: P

Question

PYTHON PROGRAMING HELP

Flight Trajectory

Describe,formulaically,howyouwouldsolvefortheinstantaneousvelocity‘v’(both magnitude and direction) at time ‘t’ of a ball released with initial velocity ‘v0’. Assume that ‘t0’ = 0 and the only force applied on the ball is due to Earth’s surface gravity. Implementthisalgorithmwithintrajectory()inlab1.py.Thefunctioninputsandoutputs have been defined in the function descriptor.

List Search

Describe,indetail,howyouwoulddeterminewhetheraspecifiednumber‘x’exists within a given list. Assume the list is sorted in ascending order and you may query only one index at a time. Solve this as efficiently as possible. Implementthisalgorithmwithinlist_search()inlab1.py.Thefunctioninputsandoutputs have been defined in the function descriptor. Analyzethespaceandtimecomplexityofyourimplementationinrespecttolistsize‘n’. Consider only conditional checks for time complexity. Consider only additional space required beyond initial list storage for space complexity. Give your answer in big-O notation and show all appropriate work.

Curve Balls

3.

You have been provided function curve_balls() in lab1.py. Note, this function uses helper functions curve_ball() and split_ball().

Analyzethetimecomplexityofthisfunctioninrespecttoinputs‘t’and‘n’.Assumeeach explicit conditional check counts as an operation. Give your answer in big-O notation and show all appropriate work.

Analyzethespacecomplexityofthisfunctioninrespecttoinputs‘t’and‘n’.Only consider the creation of Balls towards the total. Give your answer in big-O notation and show all appropriate work.

Observethetwohelperfunctionscurve_ball()andsplit_ball().Writeanappropriatefull function descriptor (including parameters and returns as needed) for each helper function.

CODE TEMPLATE BELOW

"""

This is your template for lab1. Implement all questions in the appropriate

function. You are encouraged to implement helper functions as needed with

a short (one-line) description of their purpose.

"""

import random

ACCEL_G = 9.8 # m/s^2

def trajectory(p0, v0, t):

"""

Calculates instaneous free-fall trajectory given initial parameters.

Object may start with any real position and velocity. Assume

acceleration

is ACCEL_G as specified above.

Parameters

----------

p0 : [float, float]

the position of the object at t0, given as [pos_x, pos_y]

v0 : [float, float]

the velocity of the object at t0, given as [vel_x, vel_y]

t : float

time passed since t0

Returns

-------

list(float)

instantaneous trajectory of object given as [pos_x, pos_y, vel_x,

vel_y]

"""

# replace the line below with your implementation

pass

def list_search(n, my_list=None):

"""

Search for the specified number in a given list.

Assume the input list is sorted in ascending order, but not

necessarily

consecutive. Integers may be repeated. Shoot for an expected runtime

of

log(n) for full credit.

Parameters

----------

n : int

the specific element to search for

my_list : list(int)

list of elements to search through

Returns

-------

int

the element's index within the list or -1 if the element is not

present

"""

# Generate a random list if no list is specified

if my_list is None:

my_list = []

list_size = 1000

int_range = 10000

for _ in range(list_size):

elem = random.randint(-int_range/2, int_range/2)

my_list.append(elem)

my_list.sort()

# replace the line below with your implementation

pass

class Ball:

def __init__(self, mass=10):

self.x = 0

self.y = 0

self.mass = mass

def curve_balls(t, n):

"""

Propagate the random movement and splitting of balls over time.

Assume the input list is sorted in ascending order, but not

necessarily

consecutive. Integers may be repeated. Shoot for an expected runtime

of

log(n) for full credit.

Parameters

----------

t : int

the number of time steps to propagate the algorithm

n : int

number of balls to initially start with

"""

# initialize list of balls

balls = []

for i in range(n):

balls.append(Ball())

# propagate balls through each timestep

for _ in range(t):

balls_generated = []

for b in balls:

curve_ball(b)

ball_new = split_ball(b)

balls_generated.append(ball_new)

balls += balls_generated # append all newly generated balls to

the list of balls

def curve_ball(ball):

"""

Write your function descriptor here.

Parameters

----------

"""

ball.x += random.uniform(-1,1)

ball.y += random.uniform(-1,1)

if ball.x > 10:

ball.x = 10

elif ball.x < -10:

ball.x = -10

if ball.y > 10:

ball.y = 10

elif ball.y < -10:

ball.y = -10

def split_ball(ball_og):

"""

Write your function descriptor here.

Parameters

----------

Returns

-------

"""

ball_og.mass /= 2

ball_new = Ball(mass=ball_og.mass)

ball_new.x = ball_og.x

ball_new.y = ball_og.y

return ball_new

Explanation / Answer

"""
This is your template for lab1. Implement all questions in the appropriate
function. You are encouraged to implement helper functions as needed with
a short (one-line) description of their purpose.
"""

import random

ACCEL_G = 9.8 # m/s^2
def trajectory(p0, v0, t):

"""
Calculates instaneous free-fall trajectory given initial parameters.
Object may start with any real position and velocity. Assume
acceleration is ACCEL_G as specified above.
Parameters
----------
p0 : [float, float]
the position of the object at t0, given as [pos_x, pos_y]
v0 : [float, float]
the velocity of the object at t0, given as [vel_x, vel_y]
t : float
time passed since t0
Returns
-------
list(float)
instantaneous trajectory of object given as [pos_x, pos_y, vel_x, vel_y]
"""
# replace the line below with your implementation
vel_x = v0[0]
vel_y = v0[1] + ACCEL_G * t
pos_x = (v0[0] + vel_x) / 2 * t
pos_y = (v0[1] + vel_y) / 2 * t
return [pos_x, pos_y, vel_x, vel_y]

def list_search(n, my_list=None):

"""
Search for the specified number in a given list.
Assume the input list is sorted in ascending order, but not
necessarily
consecutive. Integers may be repeated. Shoot for an expected runtime
of
log(n) for full credit.
Parameters
----------
n : int
the specific element to search for
my_list : list(int)
list of elements to search through
Returns
-------
int
the element's index within the list or -1 if the element is not
present
"""
# Generate a random list if no list is specified
if my_list is None:

my_list = []
list_size = 1000
int_range = 10000
for _ in range(list_size):

elem = random.randint(-int_range/2, int_range/2)
my_list.append(elem)
my_list.sort()

# replace the line below with your implementation
# use binary search for efficiency
# Time Complexity: O(log(n))
# considering only conditonal checks
# Space Complexity: O(1)
# considering additional space required
return binarySearch(my_list, 0, len(my_list) - 1, n)

# Returns index of x in arr if present, else -1
def binarySearch (arr, l, r, x):

# Check base case
if r >= l:

mid = int(l + (r - l)/2)
# If element is present at the middle itself
if arr[mid] == x:

return mid

# If element is smaller than mid, then it
# can only be present in left subarray
elif arr[mid] > x:

return binarySearch(arr, l, mid-1, x)

# Else the element can only be present
# in right subarray
else:

return binarySearch(arr, mid+1, r, x)

else:

# Element is not present in the array
return -1

class Ball:

def __init__(self, mass=10):

self.x = 0
self.y = 0
self.mass = mass

# Time complexity: O(t*n)
# Space complexity: O(t*n)
def curve_balls(t, n):

"""
Propagate the random movement and splitting of balls over time.
Assume the input list is sorted in ascending order, but not
necessarily
consecutive. Integers may be repeated. Shoot for an expected runtime
of
log(n) for full credit.
Parameters
----------
t : int
the number of time steps to propagate the algorithm
n : int
number of balls to initially start with
"""
# initialize list of balls
balls = []
for i in range(n):

balls.append(Ball())

# propagate balls through each timestep
for _ in range(t):

balls_generated = []
for b in balls:

curve_ball(b)
ball_new = split_ball(b)
balls_generated.append(ball_new)

balls += balls_generated # append all newly generated balls to the list of balls

def curve_ball(ball):

"""
Gives a random x and y position to a ball within -10 to +10
Parameters
----------
ball: object
an instance of ball class
"""
ball.x += random.uniform(-1,1)
ball.y += random.uniform(-1,1)
if ball.x > 10:

ball.x = 10

elif ball.x < -10:

ball.x = -10

if ball.y > 10:

ball.y = 10

elif ball.y < -10:

ball.y = -10

def split_ball(ball_og):

"""
Splits the old ball to two new balls
Weights of new balls are half the weight of old ball
Position of both the new balls are same as the old ball
Parameters
----------
ball_og: object
instance of old ball
Returns
-------
object
instance of new ball
"""
ball_og.mass /= 2
ball_new = Ball(mass=ball_og.mass)
ball_new.x = ball_og.x
ball_new.y = ball_og.y
return ball_new