Ranking methods: Difference between revisions
Content added Content deleted
No edit summary |
(Added Elixir) |
||
Line 271: | Line 271: | ||
C:\Users\RC\Code></pre> |
C:\Users\RC\Code></pre> |
||
=={{header|Elixir}}== |
|||
{{trans|Ruby}} |
|||
<lang elixir>defmodule Ranking do |
|||
def methods(data) do |
|||
IO.puts "stand.\tmod.\tdense\tord.\tfract." |
|||
Enum.group_by(data, fn {score,_name} -> score end) |
|||
|> Enum.map(fn {score,pairs} -> |
|||
names = Enum.map(pairs, fn {_,name} -> name end) |> Enum.reverse |
|||
{score, names} |
|||
end) |
|||
|> Enum.sort_by(fn {score,_} -> -score end) |
|||
|> Enum.with_index |
|||
|> Enum.reduce({1,0,0}, fn {{score, names}, i}, {s_rnk, m_rnk, o_rnk} -> |
|||
d_rnk = i + 1 |
|||
m_rnk = m_rnk + length(names) |
|||
f_rnk = ((s_rnk + m_rnk) / 2) |> to_string |> String.replace(".0","") |
|||
o_rnk = Enum.reduce(names, o_rnk, fn name,acc -> |
|||
IO.puts "#{s_rnk}\t#{m_rnk}\t#{d_rnk}\t#{acc+1}\t#{f_rnk}\t#{score} #{name}" |
|||
acc + 1 |
|||
end) |
|||
{s_rnk+length(names), m_rnk, o_rnk} |
|||
end) |
|||
end |
|||
end |
|||
~w"44 Solomon |
|||
42 Jason |
|||
42 Errol |
|||
41 Garry |
|||
41 Bernard |
|||
41 Barry |
|||
39 Stephen" |
|||
|> Enum.chunk(2) |
|||
|> Enum.map(fn [score,name] -> {String.to_integer(score),name} end) |
|||
|> Ranking.methods</lang> |
|||
{{out}} |
|||
<pre> |
|||
stand. mod. dense ord. fract. |
|||
1 1 1 1 1 44 Solomon |
|||
2 3 2 2 2.5 42 Jason |
|||
2 3 2 3 2.5 42 Errol |
|||
4 6 3 4 5 41 Garry |
|||
4 6 3 5 5 41 Bernard |
|||
4 6 3 6 5 41 Barry |
|||
7 7 4 7 7 39 Stephen |
|||
</pre> |
|||
=={{header|Go}}== |
=={{header|Go}}== |
||
Line 556: | Line 604: | ||
7.0 39 Stephen |
7.0 39 Stephen |
||
</pre> |
</pre> |
||
=={{header|J}}== |
=={{header|J}}== |
||
Implementation: |
Implementation: |
||
Line 1,600: | Line 1,649: | ||
7 7 4 7 7 39 Stephen |
7 7 4 7 7 39 Stephen |
||
</pre> |
</pre> |
||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
This example uses a type-safe singly-linked object model with no mutable state variables, which makes it longer than the Ruby version above, but demonstrates an object-oriented functional programming approach. |
This example uses a type-safe singly-linked object model with no mutable state variables, which makes it longer than the Ruby version above, but demonstrates an object-oriented functional programming approach. |