2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -2,19 +2,17 @@ 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)
|
||||
|> Map.new(fn {k,v} -> {k, Enum.map(v, &rem(&1, multiplier)) |> 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))
|
||||
defp print(plot_data, leaf_digits) do
|
||||
{min, max} = Map.keys(plot_data) |> Enum.min_max
|
||||
stem_width = length(to_charlist(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)
|
||||
leaves = Enum.map_join(Map.get(plot_data, stem, []), " ", fn leaf ->
|
||||
to_string(leaf) |> String.pad_leading(leaf_digits)
|
||||
end)
|
||||
:io.format fmt, [stem, leaves]
|
||||
end)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue