Another update from ingydotnet^djgoku

This commit is contained in:
Ingy döt Net 2015-11-18 06:14:39 +00:00
parent 91df62d461
commit 948b86eafa
7604 changed files with 108452 additions and 22726 deletions

View file

@ -0,0 +1,26 @@
defmodule Stem_and_leaf do
def plot(data, leaf_digits\\1) do
multiplier = Enum.reduce(1..leaf_digits, 1, fn _,acc -> acc*10 end)
Enum.group_by(data, fn x -> div(x, multiplier) end)
|> Enum.into(Map.new, fn {k,v} ->
{k, Enum.map(v, fn val -> rem(val, multiplier) end) |> Enum.sort}
end)
|> print(leaf_digits)
end
def print(plot_data, leaf_digits) do
{min, max} = Dict.keys(plot_data) |> Enum.min_max(keys)
stem_width = length(to_char_list(max))
fmt = "~#{stem_width}w | ~s~n"
Enum.each(min..max, fn stem ->
leaves = Enum.map_join(Dict.get(plot_data, stem, []), " ", fn leaf ->
to_string(leaf) |> String.rjust(leaf_digits)
end)
:io.format fmt, [stem, leaves]
end)
end
end
data = ~w(12 127 28 42 39 113 42 18 44 118 44 37 113 124 37 48 127 36 29 31 125 139 131 115 105 132 104 123 35 113 122 42 117 119 58 109 23 105 63 27 44 105 99 41 128 121 116 125 32 61 37 127 29 113 121 58 114 126 53 114 96 25 109 7 31 141 46 13 27 43 117 116 27 7 68 40 31 115 124 42 128 52 71 118 117 38 27 106 33 117 116 111 40 119 47 105 57 122 109 124 115 43 120 43 27 27 18 28 48 125 107 114 34 133 45 120 30 127 31 116 146)
|> Enum.map(&String.to_integer(&1))
Stem_and_leaf.plot(data)

View file

@ -0,0 +1,60 @@
SUBROUTINE COMBSORT(A,N)
INTEGER A(*) !The array.
INTEGER N !The count.
INTEGER H,T !Assistants.
LOGICAL CURSE
H = N - 1 !Last - First, and not +1.
1 H = MAX(1,H*10/13) !The special feature.
IF (H.EQ.9 .OR. H.EQ.10) H = 11 !A twiddle.
CURSE = .FALSE. !So far, so good.
DO I = N - H,1,-1 !If H = 1, this is a BubbleSort.
IF (A(I) .GT. A(I + H)) THEN !One compare.
T=A(I); A(I)=A(I+H); A(I+H)=T !One swap.
CURSE = .TRUE. !One curse.
END IF !One test.
END DO !One loop.
IF (CURSE .OR. H.GT.1) GO TO 1 !Work remains?
END SUBROUTINE COMBSORT !Good performance, small code.
SUBROUTINE TOPIARY(A,N) !Produces a "stem&leaf" display for the integers in A, damaging A.
INTEGER A(*) !An array of integers.
INTEGER N !Their number.
INTEGER CLIP !Semi-generalisation.
PARAMETER (CLIP = 10) !Or at least, annotation.
INTEGER I1,I2,STEM !Assistants.
CALL COMBSORT(A,N) !Rearrange the array!
STEM = A(1)/CLIP !The first stem value.
I1 = 1 !The first stem's span starts here.
I2 = I1 !And so far as I know, ends here.
10 I2 = I2 + 1 !Probe ahead one position.
IF (I2 .GT. N) GO TO 11 !Off the end? Don't look!
IF (A(I2)/CLIP .EQ.STEM) GO TO 10 !Still in the same stem? Probe on.
Cast forth a STEM line, corresponding to elements I1:I2 - 1.
11 WRITE (6,12) STEM,ABS(MOD(A(I1:I2 - 1),CLIP)) !ABS: MOD with negatives can be unexpected.
12 FORMAT (I4,"|",(100I1)) !Layout. If more than a hundred, starts a new line.
IF (I2 .GT. N) RETURN !Are we there yet?
I1 = I2 !No. This is my new span's start.
Chug along to the next STEM value.
13 STEM = STEM + 1 !Advance to the next stem.
IF (A(I2)/CLIP.GT.STEM) GO TO 11!Has the stem reached the impending value?
GO TO 10 !Yes. Scan its span.
END SUBROUTINE TOPIARY !The days of carefully-arranged output.
PROGRAM TEST
INTEGER VALUES(121) !The exact number of values.
DATA VALUES/ !As in the specified example.
o 12,127, 28, 42, 39,113, 42, 18, 44,118, !A regular array
1 44, 37,113,124, 37, 48,127, 36, 29, 31, !Makes counting easier.
2 125,139,131,115,105,132,104,123, 35,113,
3 122, 42,117,119, 58,109, 23,105, 63, 27,
4 44,105, 99, 41,128,121,116,125, 32, 61,
5 37,127, 29,113,121, 58,114,126, 53,114,
6 96, 25,109, 7, 31,141, 46, 13, 27, 43,
7 117,116, 27, 7, 68, 40, 31,115,124, 42,
8 128, 52, 71,118,117, 38, 27,106, 33,117,
9 116,111, 40,119, 47,105, 57,122,109,124,
o 115, 43,120, 43, 27, 27, 18, 28, 48,125,
1 107,114, 34,133, 45,120, 30,127, 31,116,
2 146/
CALL TOPIARY(VALUES,121)
END

View file

@ -0,0 +1,25 @@
function stemleaf{T<:Real}(a::Array{T,1}, leafsize=1)
ls = 10^int(log10(leafsize))
(stem, leaf) = divrem(sort(int(a/ls)), 10)
leaf[sign(stem) .== -1] *= -1
negzero = leaf .< 0
if any(negzero)
leaf[negzero] *= -1
nz = @sprintf "%10s | " "-0"
nz *= join(map(string, leaf[negzero]), " ")
nz *= "\n"
stem = stem[!negzero]
leaf = leaf[!negzero]
else
nz = ""
end
slp = ""
for i in stem[1]:stem[end]
i != 0 || (slp *= nz)
slp *= @sprintf "%10d | " i
slp *= join(map(string, leaf[stem .== i]), " ")
slp *= "\n"
end
slp *= " Leaf Unit = " * string(convert(T, ls)) * "\n"
return slp
end

View file

@ -0,0 +1,19 @@
println("Using the Task's Test Data")
test = """12 127 28 42 39 113 42 18 44 118 44 37 113 124 37 48 127 36 29
31 125 139 131 115 105 132 104 123 35 113 122 42 117 119 58 109 23 105
63 27 44 105 99 41 128 121 116 125 32 61 37 127 29 113 121 58 114 126
53 114 96 25 109 7 31 141 46 13 27 43 117 116 27 7 68 40 31 115 124 42
128 52 71 118 117 38 27 106 33 117 116 111 40 119 47 105 57 122 109
124 115 43 120 43 27 27 18 28 48 125 107 114 34 133 45 120 30 127 31
116 146"""
test = map(parseint, split(test, r"\s"))
println(stemleaf(test))
println("Test with Reals and Negative Zero Stem")
test = [-23.678758, -12.45, -3.4, 4.43, 5.5, 5.678, 16.87, 24.7, 56.8]
println(stemleaf(test))
println("Test with Leaf Size Scaling")
test = int(500*randn(20))
println("Using: ", test)
println(stemleaf(test, 10))

View file

@ -1,26 +1,26 @@
/*REXX program displays a stem-and-leaf plot of real numbers [-, 0, +].*/
min= /*This program handles negatives */
max= /* ··· and decimal fractions. */
parse arg data; if data='' then data=, /*Not specified? Then use default*/
12 127 28 42 39 113 42 18 44 118 44 37 113 124 37 48 127 36 29 31 125,
139 131 115 105 132 104 123 35 113 122 42 117 119 58 109 23 105 63 27,
44 105 99 41 128 121 116 125 32 61 37 127 29 113 121 58 114 126 53 114,
96 25 109 7 31 141 46 13 27 43 117 116 27 7 68 40 31 115 124 42 128 52,
71 118 117 38 27 106 33 117 116 111 40 119 47 105 57 122 109 124 115,
43 120 43 27 27 18 28 48 125 107 114 34 133 45 120 30 127 31 116 146
#.=
do j=1 for words(data); _=format(word(data,j),,0)/1 /*normalize*/
stem=left(_, max(1, length(_)-1)) /*extract the stem from the num. */
if length(_)==1 then stem=0 /*handle single-digit leaves. */
if min=='' then min=stem; if max=='' then max=stem
min=min(min, stem*sign(_)); max=max(max, stem*sign(_))
leaf=right(_,1) /*pick off the leaf from the num.*/
#.stem.leaf=#.stem.leaf leaf /*construct a sorted stem-&-leaf.*/
end /*j*/
/*REXX program displays a stem─and─leaf plot of any real numbers [-, 0, +]. */
parse arg data /* [↓] Not specified? Then use default*/
if data='' then data=12 127 28 42 39 113 42 18 44 118 44 37 113 124 37 48 127 36 29 31 125 ,
139 131 115 105 132 104 123 35 113 122 42 117 119 58 109 23 105 63 27 44 105 99 41 128 ,
121 116 125 32 61 37 127 29 113 121 58 114 126 53 114 96 25 109 7 31 141 46 13 27 43 117,
116 27 7 68 40 31 115 124 42 128 52 71 118 117 38 27 106 33 117 116 111 40 119 47 105 57,
122 109 124 115 43 120 43 27 27 18 28 48 125 107 114 34 133 45 120 30 127 31 116 146
parse var data bot . 1 top . '' @. /*define MIN & MAX as the first number.*/
/* [↑] define all @. elements as null.*/
do j=1 for words(data) /*◄─── process each number in the list.*/
_=format(word(data,j),,0)/1 /*normalize the numbers (not malformed)*/
stem=left(_, max(1, length(_)-1)) /*obtain stem (1st digit) from number.*/
parse var _ '' -1 leaf /* " leaf (last " ) " " */
if length(_)==1 then stem=0 /*special case: single─digit leaves. */
bot=min(bot, stem*sign(_)) /*obtain the minimum number (so far). */
top=max(top, stem*sign(_)) /* " " maximum " " " */
@.stem.leaf=@.stem.leaf leaf /*construct sorted stem-and-leaf entry.*/
end /*j*/
w=max(length(min),length(max)) /*width: used to align the stems.*/
do k=min to max; _=; do m=0 for 10; _=_ #.k.m; end /*m*/
say right(k,w) '' space(_)
end /*k*/
/*stick a fork in it, we're done.*/
w=max(length(min), length(max)) + 1 /*W: used to right─justify the output.*/
/* [↓] display the stem-and-leaf plot.*/
do k=bot to top; $= /*$: is the output string, a plot line*/
do m=0 for 10; $=$ @.k.m /*build a line for the stem─&─leaf plot*/
end /*m*/
say right(k,w) '' space($) /*display a line of stem─and─leaf plot.*/
end /*k*/ /*stick a fork in it, we're all done. */