Created
October 5, 2017 10:22
-
-
Save gouf/94d37e775549790d85c09b28e2adc4fb to your computer and use it in GitHub Desktop.
同順を含む順位ラベルを付加する(Ruby 版)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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