From 830eda6e1ce3d666f4a331af7fc74c7e0474d8b2 Mon Sep 17 00:00:00 2001 From: Tim Diller Date: Wed, 22 Oct 2025 13:19:59 -0500 Subject: [PATCH] GIAT convert_mass example from class --- README.md | 3 ++- files/convert_mass.py | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 files/convert_mass.py diff --git a/README.md b/README.md index 0f46650..f9053ab 100644 --- a/README.md +++ b/README.md @@ -29,4 +29,5 @@ $ pip install -e . # Day 3 Resources - *Python Typing is for Humans*: [Unital.dev blog](https://www.unital.dev/blog/python-typing-is-for-humans/) or [LinkedIn](https://www.linkedin.com/pulse/python-typing-humans-corran-webster-u66ee) - [PyData Sphinx Theme](https://pydata-sphinx-theme.readthedocs.io/en/stable/) -- [Sphinx AutoAPI](https://sphinx-autoapi.readthedocs.io/en/latest/) \ No newline at end of file +- [Sphinx AutoAPI](https://sphinx-autoapi.readthedocs.io/en/latest/) +- Find the refactoring Give It A Try in `files/convert_mass.py` diff --git a/files/convert_mass.py b/files/convert_mass.py new file mode 100644 index 0000000..a814456 --- /dev/null +++ b/files/convert_mass.py @@ -0,0 +1,27 @@ +''' Refactor the following code +mass_oz = [0.5, 0.6, 1.2, 3.4] +mass_g = [mass * 28.3496 for mass in mass_oz] +mass_kg = [mass * 28.3496 / 1000 for mass in mass_oz] +''' + +# Refactor Step 1 +G_OZ = 28.3496 +KG_OZ = G_OZ / 1000 + +mass_oz = [0.5, 0.6, 1.2, 3.4] +mass_g = [mass * G_OZ for mass in mass_oz] +mass_kg = [mass * KG_OZ for mass in mass_oz] + +# Refactor Step 2 +def convert_mass(mass_oz, conv_factor): + return [m * conv_factor for m in mass_oz] + +# Refactor Step 3 +if __name__ == "__main__": + mass_oz = [0.5, 0.6, 1.2, 3.4, 5.6, 7.8, 9.1] + print("Ounces") + print(", ".join(str(m) for m in mass_oz)) + print("Grams") + print(", ".join(str(m) for m in convert_mass(mass_oz, G_OZ))) + print("Kilograms") + print(", ".join(str(m) for m in convert_mass(mass_oz, KG_OZ))) \ No newline at end of file