Shrinking numbers
CJam, 18 bytes
q~_$0=f-_$W=df/';*
Note that the online interpreter wrongfully represents 0d
as 0
instead of 0.0
.
Example run
$ cjam shrink.cjam <<< '[5 -20 30]'; echo
0.5;0.0;1.0
$ cjam shrink.cjam <<< '[1 2 3 4 5]'; echo
0.0;0.25;0.5;0.75;1.0
$ cjam shrink.cjam <<< '[0 5 100 400]'; echo
0.0;0.0125;0.25;1.0
How it works
q~ " P := eval(input()) ";
_$0= " S := sorted(P)[0] ";
f- " Q := { X - S : X ∊ P } ";
_$W=d " D := double(sorted(Q)[-1]) ";
f/ " R := { X / D : X ∊ Q } ";
';* " print(join(R, ';')) ";
JavaScript, ES6, 81 bytes
Thanks to @edc65 for the toFixed
trick
F=a=>a.map(v=>((v-n)/d).toFixed(2),n=Math.min(...a),d=Math.max(...a)-n).join(';')
Run it in latest Firefox Console.
This creates a function f
which you can invoke like
F([5,-20,30])
CJam, 24 23 bytes
l~_$)\(:M\;-\Mf-\df/';*
Input be like:
[5 -20 30]
Try it online here Note that online compiler prints Double 0
as 0
only. Run in the java interpreter which prints correctly.
How it works:
l~ "Evaluate input and convert each element to double";
_$ "Copy the array and sort the copied array";
) "Pop the last element out of the array. This is Max";
\ "Swap last two stack elements, bring sorted array on top";
(:M "Pop the first element of array and store it in M. This is Min";
\; "Bring the remaining of sorted array on top and remove it from stack";
-\ "Subtract Max and Min and bring the original array to top of stack"
Mf- "Push min to stack and subtract it from each array element";
\df/ "Bring (Double)(Max-Min) to top and divide each array element by it";
';* "Push the character ; to stack and join the array with it";