Skip to content

Instantly share code, notes, and snippets.

@gouf
Created October 5, 2017 10:22
Show Gist options
  • Select an option

  • Save gouf/94d37e775549790d85c09b28e2adc4fb to your computer and use it in GitHub Desktop.

Select an option

Save gouf/94d37e775549790d85c09b28e2adc4fb to your computer and use it in GitHub Desktop.
同順を含む順位ラベルを付加する(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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment