Problem

Python

Suppose we want to write a program that takes in three input parameters: the initial height ($y_0$) initHeight, the initial velocity ($v_0$) initVelocity, the time after which we want to know how much a projectile has moved in the vertical direction from the initial height ($y_0$), and the gravity constant ($g$) gravityConstant. Write the program such that it takes these three parameters as pairs of option (keyword) and values plus an additional option unit which can have either SI or English values. Then it computes and prints the final height finalHeight via the following equation,

\[y(t) = y_0 + v_0\times t + \frac{1}{2}g\times t^2 ~~,\]

or in scripting terms,

finalHeight = initHeight + initVelocity * time + gravityConstant * time**2

(A) Write your code such that it works with the following example input option-value pair,

python getHeight.py unit SI initHeight 0 initVelocity 0 time 3 gravityConstant -10

Compute the height for the same input values as in the above, but for the gravity constant of the Moon $g \sim 1.62 ms^{-2}$. How does it compare to Earth?

(B) Rewrite your program such that the missing option value pairs from the input are assigned proper values. For example, if initHeight is missing, it should be assigned a value of 0. If initVelocity is missing, it should be assigned a value of 0. If gravityConstant is missing, it should be assigned a value of -10 $m/s^2$ if the input unit is SI, otherwise it should be assigned -32.17405 $ft/s^2$. If the unit is missing, it should be assigned SI as the default value.

(C) Now rewrite your program such that the missing option value pairs would be read from an input file, if the file path_to_file is given as option value pair. For example, your code should be able to parse the contents of these two example input files: input.1.txt or input.2.txt, when called with the file command-line option (and it should ignore any other input command-line option other than file and its value.

python getHeight.py file "input.1.txt"
Final Height = -45.0 in SI units.

Solution

Python

Here is an implementation of getHeight.py. Free fall on Moon appears to 5 times slower than on Earth!

Comments