How to resolve the algorithm Map range step by step in the Python programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Map range step by step in the Python programming language

Table of Contents

Problem Statement

Given two ranges:   where:

Write a function/subroutine/... that takes two ranges and a real number, and returns the mapping of the real number from the first to the second range. Use this function to map values from the range   [0, 10]   to the range   [-1, 0].

Show additional idiomatic ways of performing the mapping, using tools available to the language.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Map range step by step in the Python programming language

Explanation:

The Python code defines a function called maprange() and demonstrates its use with different inputs.

Function Definition:

def maprange( a, b, s):
   (a1, a2), (b1, b2) = a, b
   return  b1 + ((s - a1) * (b2 - b1) / (a2 - a1))
  • The maprange() function takes three arguments:

    • a: A tuple representing the source range as (a1, a2).
    • b: A tuple representing the target range as (b1, b2).
    • s: The input value within the source range.
  • It calculates the corresponding value within the target range using linear interpolation.

Demonstration:

for s in range(11):
   print("%2g maps to %g" % (s, maprange( (0, 10), (-1, 0), s)))
  • Two different ranges are used: (0, 10) as the source range and (-1, 0) as the target range.
  • The loop iterates through values of s from 0 to 10 and prints the corresponding mapped value.
  • It shows how values in the source range are linearly transformed to the target range.

Using Fractions:

from fractions import Fraction
for s in range(11):
   print("%2g maps to %s" % (s, maprange( (0, 10), (-1, 0), Fraction(s))))
  • This part of the code demonstrates the use of the Fraction class from the fractions module.
  • It creates Fraction objects for the input s values and passes them to the maprange() function.
  • The result is printed as a string representation of the fraction.

Output:

  • The output shows the original value (s) mapped to the corresponding value in the target range using two different representations:
    • As a floating-point number (indicated by %g format) using integers for s.
    • As a fraction using Fraction objects, also represented as strings.

The output demonstrates the linear transformation of values from the source range to the target range.

Source code in the python programming language

>>> def maprange( a, b, s):
	(a1, a2), (b1, b2) = a, b
	return  b1 + ((s - a1) * (b2 - b1) / (a2 - a1))

>>> for s in range(11):
	print("%2g maps to %g" % (s, maprange( (0, 10), (-1, 0), s)))

	
 0 maps to -1
 1 maps to -0.9
 2 maps to -0.8
 3 maps to -0.7
 4 maps to -0.6
 5 maps to -0.5
 6 maps to -0.4
 7 maps to -0.3
 8 maps to -0.2
 9 maps to -0.1
10 maps to 0


>>> from fractions import Fraction
>>> for s in range(11):
	print("%2g maps to %s" % (s, maprange( (0, 10), (-1, 0), Fraction(s))))

	
 0 maps to -1
 1 maps to -9/10
 2 maps to -4/5
 3 maps to -7/10
 4 maps to -3/5
 5 maps to -1/2
 6 maps to -2/5
 7 maps to -3/10
 8 maps to -1/5
 9 maps to -1/10
10 maps to 0
>>>


  

You may also check:How to resolve the algorithm Linear congruential generator step by step in the Batch File programming language
You may also check:How to resolve the algorithm Sum digits of an integer step by step in the zkl programming language
You may also check:How to resolve the algorithm Bitcoin/public point to address step by step in the Python programming language
You may also check:How to resolve the algorithm Classes step by step in the Haskell programming language
You may also check:How to resolve the algorithm Character codes step by step in the Maple programming language