Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PYTHON ASSIGNMENT Use currying and composition to calculate distance conversions. The first thing you will need in this section is the compose() function which accepts

PYTHON ASSIGNMENT

Use currying and composition to calculate distance conversions.

  • The first thing you will need in this section is the compose() function which accepts a series of functions and reduces it to a single function call. You can write the compose function any way you like, but here are two common implementations:

def compose(*functions):

return reduce(lambda f, g: lambda x: g(f(x)), functions)

OR

compose = lambda *F: reduce(lambda f, g: lambda x: g(f(x)), F)

  • Next, implement a series of functions to convert from one unit of measure to another. The idea is that we have the ability to convert from any one unit to another by tying a combination of these methods together in the right order.
    • Use the following chart to implement the methods.
    • The conversion factor is the value you must multiply by to convert from the input value to the output result.
    • Each method accepts a single value and returns the resulting calculation.

image text in transcribed

  • Once all of the functions are implemented, you can start tying them together using compose(). To convert from miles to feet, you would use compose in the following manner:

milesToFeet = compose(milesToYards, yardsToFeet)

  • Now you can execute the milesToFeet() function variable passing in the number of miles you want to convert to feet.
  • To go from miles to light years will require many functions to be chained together.

Here are the conversions you must perform in this exercise and the approximate results (your results may vary, but not by much):

  • Convert 2 miles to inches (result: 126720)
  • Convert 5 feet to meters (result: 1.524)
  • Convert 1 meter to inches (result: 39.37007874015748)
  • Convert 10 miles to kilometers (result: 16.09344)
  • Convert 1 kilometer to miles (result: 0.6213711922373341)
  • Convert 12.7 kilometers to inches (result: 500000.0)
  • Convert 500000 inches to kilometers (result: 12.7)
  • Convert 9,460,730,472,580,800 meters to light years (result: 1)
Function Conversion Factor 1760 0.0005681818181818 3 0.3333333333333333 12 0.0833333333333333 milesToYards yardsToMiles yardsToFeet feetToYards feetToInches inchesToFeet inchesToCm cmToInches cmToMeters metersToCm metersTokm kmToMeters kmToAu 2.54 0.3937007874015748 Description Converts miles to yards Inverse of milesToYards Converts yards to feet Inverse of yardsToFeet Converts feet to inches Inverse of feetToInches Converts inches to centimeters Inverse of inchesToCm Converts centimeters to meters Inverse of cmToMeters Converts meters to kilometers Inverse of metersTokm Converts kilometers to astronomical units (average distance from Earth to Sun) Inverse of kmToAu Converts AUs to light years (distance light travels in a year in a vacuum) Inverse of autoLY 0.01 100 0.001 1000 0.000000006684587122268445 au Tokm 149597870.700 0.00001581250740982065847572 auTOLY lyToAu 63241.07708426628026865358

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Public Finance

Authors: Harvey S. Rosen

5th Edition

025617329X, 978-0256173291

Students also viewed these Databases questions