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

Published on 12 May 2024 09:40 PM

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

Source code in the wren programming language

import "/fmt" for Fmt

var mapRange = Fn.new { |a, b, s| b.from + (s - a.from) * (b.to - b.from) / (a.to - a.from) }

var a = 0..10
var b = -1..0
for (s in a) {
    var t = mapRange.call(a, b, s)
    var f = (t >= 0) ? " " : "" 
    System.print("%(Fmt.d(2, s)) maps to %(f)%(t)")
}

  

You may also check:How to resolve the algorithm Population count step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Keyboard input/Flush the keyboard buffer step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Zig-zag matrix step by step in the Ring programming language
You may also check:How to resolve the algorithm Faulhaber's triangle step by step in the C++ programming language
You may also check:How to resolve the algorithm 99 bottles of beer step by step in the Set lang programming language