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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Map range step by step in the Clojure 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 Clojure programming language

Source code in the clojure programming language

(defn maprange [[a1 a2] [b1 b2] s]
	(+ b1 (/ (* (- s a1) (- b2 b1)) (- a2 a1))))
 
> (doseq [s (range 11)]
       (printf "%2s maps to %s\n" s (maprange [0 10] [-1 0] 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 Ethiopian multiplication step by step in the Nim programming language
You may also check:How to resolve the algorithm Middle three digits step by step in the Prolog programming language
You may also check:How to resolve the algorithm Character codes step by step in the Oforth programming language
You may also check:How to resolve the algorithm Arithmetic/Integer step by step in the Plain English programming language
You may also check:How to resolve the algorithm Anagrams/Deranged anagrams step by step in the BASIC programming language