0

Let's say I have a range like 100-1. Now, I want to compress that range by 10% on the top, and 25% from the bottom. So my final range would be 90-25.

Any ideas on how to make a function that given any number from 100 to 1 are transformed to the 90-25 range?

Enzo
  • 4,111
  • 4
  • 21
  • 33
  • 1
    Solution is here https://stackoverflow.com/questions/929103/convert-a-number-range-to-another-range-maintaining-ratio – Enzo Jun 07 '21 at 10:15
  • 1
    Ah okay, finally I understand your question now :-D to me it was missing that you want to keep ratios in between and not just "throw away all outside the new range". Thanks for linking to that one! – Nico Albers Jun 07 '21 at 18:27
  • I didn't explain myself properly. My bad. – Enzo Jun 08 '21 at 13:53
  • no worries - glad you find a working solution! – Nico Albers Jun 08 '21 at 13:57

1 Answers1

0

Let u be the upper number (in your example: 100) and l be the lower number (example: 1), so you want to transform the interval/range/... [1, 100] to a shrinked one.

With usual math knowledge one sees that removing some percent from the bottom/top is the same as just moving that number, so in your case you would want to use the interval [l * 0.25, u * 0.9] instead of [l, u]. This generalizes to all other values of course.

In case you don't want rational numbers but only integers, you'll have to think about whether you want to shrink more or less than 25/10% because you can't be exact then.

Using this new interval now you can create that function using pseudocode:

def transform(n, lower, upper):
    interval = [lower * 0.25, upper * 0.9]
    if n in interval:
        return n
    else if n < lower * 0.25:
        return lower * 0.25
    else if n > upper * 0.9:
        return  upper * 0.9
Nico Albers
  • 1,556
  • 1
  • 15
  • 32
  • In your example is not clear to me how to make the transformation of a number. My functions should be something like `transform(n, u=0.1, l=0.25)` If n is 1 the output should be 25 but if n is 100 the output should be 90. – Enzo Jun 06 '21 at 18:46
  • 1
    ah okay, now I think I understand, that was not clear to me from the question. So e.g. 50 should be outputted 50 then? EDIT: updated the code. – Nico Albers Jun 06 '21 at 19:03