report - University of California, Santa Cruz

lengthConvert.py Implementation Reasoning
Brian Vargas
AMS209 Foundations of Scientific Computing
University of California, Santa Cruz
November 9, 2016
The objective was to create a Python 2 program to convert between lengths.
The available length units were miles, meters, yards, feet, and inches. It was
also expanded to include the following metric units: nm, um, mm, cm, km.
1
1.1
Implementation
Input & Output
For inputs, the user is asked to specify a quantitative length as well as a unit
type. The program then interacts with this information and in the end produces
two lists. The first containing the main unit conversions - excluding the inputted
unit type - and the second list containing the equivalent metric conversions.
1.2
Algorithm
We could have easily created many conversion factors to convert between units
depending on which unit type was inputted. For example, if we inputted inches
then we know 1 yard is 36 inches. However, that would require us to have too
many manually inputted conversions for each possible unit type. Instead, it is
easier to write a single conversion by first converting the inputted length into
meters and from meters converting to whichever other unit we desire. Converting everything to meters instead of a different unit is not only the best option
because these are the values provided in the assignment prompt but because
when we convert to the other metric types, it is as simple as just multiplying
by a power of 10.
1
The following equivalences for 1 meter were used to form the conversion factors:
1meter = 0.000621miles
= 39.270079inches
= 3.28084f eet
= 1.093613yards
= 109 nm
= 106 um
= 103 mm
= 102 cm
= 10−3 km
We thus formed a list in which each unit conversion was printed excluding the
unit type that was inputted. The conversion was done by converting to meters
by dividing by its corresponding value. If we inputted yards, we divided the
inputted value by 1.093613 to obtain meters. Once we converted to meters, we
converted to anything else by multiplying by the corresponding value on the
table. If we wanted to convert meters to miles, we would multiply by 0.000621.
1.3
Data Types
I primarily used two Python data types to complete this task: dictionaries and
lists. I used dictionaries to hold the conversion information. I used lists to hold
the outputted conversions.
The first dictionary unitDict held the equivalent conversions for 1 meter in
which the keys were the unit type and the values were the corresponding conversion values. The other dictionary siDict held the SI unit conversions from
1 meter to the other metric types - the keys were again the unit types and the
values were the corresponding conversion values.
The first list out1 held string elements defining the conversion from the inputted
values to the conversions of the main units. The second list out2 held string
elements defining the conversion from the inputted values to the conversions of
the SI units. These two lists were printed in the end as the final results.
2
Examples
In this section we run the code in the terminal using ”python lengthConvert.py”
in the appropriate working directory using different inputs. The outputs are
shown below.
I show 3 examples using different unit types to show my code works. I also
2
show 1 example in which the input is incorrect and doesn’t run my code - the
spelling must match and be singular.
2.1
Example
Please input a length (number only): 15
Please type a unit system (meter, mile, inch, foot, yard): yard
[’540.000150876 inch’, ’45.000013716 feet’, ’0.00851763832361 mile’, ’13.7160037417 meter
[’13716003.7417 um’, ’13716.0037417 mm’, ’0.0137160037417 km’, ’13716003741.7 nm’, ’1371.
2.2
Example
Please input a length (number only): 129.05
Please type a unit system (meter, mile, inch, foot, yard): inch
[’10.7541669398 feet’, ’0.00203555725657 mile’, ’3.58472122065 yard’, ’3.27786997837 mete
[’3277869.97837 um’, ’3277.86997837 mm’, ’0.00327786997837 km’, ’3277869978.37 nm’, ’327.
2.3
Example
Please input a length (number only): 2.974
Please type a unit system (meter, mile, inch, foot, yard): meter
[’117.086614946 inch’, ’9.75721816 feet’, ’0.001846854 mile’, ’3.252405062 yard’]
[’2974000.0 um’, ’2974.0 mm’, ’0.002974 km’, ’2974000000.0 nm’, ’297.4 cm’]
2.4
Example
Please input a length (number only): 3.1
Please type a unit system (meter, mile, inch, foot, yard): miles
ERROR - invalid unit type
3