gouf
10/5/2017 - 10:22 AM

同順を含む順位ラベルを付加する(Ruby 版)

同順を含む順位ラベルを付加する(Ruby 版)

# Ref: [PHP - タイ順位を表示させたい(95165)|teratail](https://teratail.com/questions/95165)

target = [[100] * 2, [97] * 3, [90] * 5].flatten
# => [100, 100, 97, 97, 97, 90, 90, 90, 90, 90]

def combine_rank_label(desc_sorted_list)
  rank_index = 1

  rank_value_cache, *target = desc_sorted_list

  target.map do |x|
    rank_index += 1 unless rank_value_cache.eql?(x)
    rank_value_cache = x
    "#{rank_index}位: #{x}"
  end
end

# puts combine_rank_label(target.sort.reverse)

print combine_rank_label(target.sort.reverse).join("\n")
# =>
# 1位: 100
# 2位: 97
# 2位: 97
# 2位: 97
# 3位: 90
# 3位: 90
# 3位: 90
# 3位: 90
# 3位: 90