相対的なランキングの応用
相対的なランキングは現実世界で多くの応用があります。主なものは以下の通りです。
- スポーツ競技のランキング: スポーツ競技では、相対的なランキングが選手やチームのパフォーマンスを評価するために頻繁に使用されます。例えば、サッカーのリーグ戦では、勝利数、得点、失点などの指標に基づいてチームのランキングが決定されます。
- 学術ランキング: 学術分野では、大学、学部、専門分野、教員の評価や評価に相対的なランキングが利用されます。例えば、世界大学ランキングは、学術研究、教育品質、国際化レベルなどの指標に基づいて大学をランク付けします。
- 職業ランキング: 雇用市場では、異なる職業の就職見通しや給与水準を評価・比較するために相対的なランキングが使われます。例えば、給与、職業需要、仕事の満足度などの指標に基づいて、様々な職業のランキングが作成されます。
- ビジネスランキング: 商業分野では、会社、ブランド、製品の市場地位や評価を評価するために相対的なランキングが利用されます。例えば、市場シェア、収益、顧客満足度などの指標に基づいて、会社やブランドのランキングが決定されます。
- 政府ランキング: 政府分野では、政府機関や公務員のパフォーマンスや信頼性を評価するために相対的なランキングが使われます。例えば、政府の統治能力、汚職防止の度合い、公共サービスのレベルなどの指標に基づいて、異なる政府のランキングが作成されます。
これらは、相対的なランキングが現実世界で利用されるいくつかの例です。相対的なランキングを通じて、異なるエンティティを比較し評価し、意思決定や戦略策定を支援することができます。
1. 相対的なランキング
1-1. Pythonでの実装
問題:N人の選手のスコアが与えられた場合、相対的なランクを見つけ、最高スコアの上位3名にそれぞれ金メダル、銀メダル、銅メダルを授与します。すべての選手のスコアは一意であることが保証されています。
例:入力 [6, 3, 7, 2, 1] の場合、出力は ["Silver Medal", "Bronze Medal", "Gold Medal", "4", "5"] となります。
# 1. 問題の説明
# N人の選手のスコアに基づいて、相対的なランクを見つけ、最高スコアの上位3名にそれぞれ金、銀、銅メダルを授与します。
# すべての選手のスコアは一意であることが保証されています。
# 2. コードの実装
class Solution:
# パラメータ scores: 整数のリスト
# 戻り値: ランクのリスト
def findRelativeRanks(self, scores):
# スコアとその元のインデックスをマッピングする辞書を作成します。
index_map = {}
for i, score in enumerate(scores):
index_map[score] = i
# スコアを降順でソートします。
sorted_scores = sorted(scores, reverse=True)
# 結果を格納するリストを初期化します。
result = ["" for _ in range(len(scores))]
# メダルのテキストを定義します。
medals = ["Gold Medal", "Silver Medal", "Bronze Medal"]
# ソートされたスコアをループ処理し、ランクを割り当てます。
for rank, score in enumerate(sorted_scores):
original_index = index_map[score]
if rank < 3:
# 上位3名にはメダルを割り当てます。
result[original_index] = medals[rank]
else:
# それ以外のランクには数字を割り当てます。
result[original_index] = str(rank + 1)
return result
# メイン関数
if __name__ == '__main__':
input_scores = [6, 3, 7, 2, 1]
s = Solution()
print("入力:", input_scores)
print("出力:", s.findRelativeRanks(input_scores))
1-2. VBAでの実装
' 相対的なランキングを見つけるカスタム関数
Function findRelativeRanks(input_scores As Variant) As Variant
Dim index_dict As New Scripting.Dictionary ' Microsoft Scripting Runtimeへの参照が必要
Dim sorted_scores As Variant
Dim output_array As Variant
Dim i As Long
Dim rank_text As String
' 辞書にスコアとそのインデックスを追加します。
For i = LBound(input_scores) To UBound(input_scores)
index_dict.Add input_scores(i), i
Next i
' スコアを降順でソートします。
sorted_scores = SortDesc(input_scores)
' 結果を格納する配列を初期化します。
ReDim output_array(LBound(input_scores) To UBound(input_scores))
' ソートされたスコアをループ処理し、ランクを割り当てます。
For i = LBound(sorted_scores) To UBound(sorted_scores)
rank_text = CStr(i - LBound(sorted_scores) + 1)
If i = LBound(sorted_scores) Then
rank_text = "Gold Medal"
ElseIf i = LBound(sorted_scores) + 1 Then
rank_text = "Silver Medal"
ElseIf i = LBound(sorted_scores) + 2 Then
rank_text = "Bronze Medal"
End If
output_array(index_dict(sorted_scores(i))) = rank_text
Next i
findRelativeRanks = output_array
End Function
' 配列を降順でソートするカスタム関数
Function SortDesc(data As Variant) As Variant
Dim i As Long, j As Long
Dim temp_val As Variant
Dim n As Long
n = UBound(data) - LBound(data) + 1
ReDim sorted_data(LBound(data) To UBound(data))
For i = LBound(data) To UBound(data)
sorted_data(i) = data(i)
Next i
' バブルソートアルゴリズムを使用して降順にソートします。
For i = LBound(sorted_data) To UBound(sorted_data) - 1
For j = i + 1 To UBound(sorted_data)
If sorted_data(i) < sorted_data(j) Then
temp_val = sorted_data(i)
sorted_data(i) = sorted_data(j)
sorted_data(j) = temp_val
End If
Next j
Next i
SortDesc = sorted_data
End Function
' メインプロシージャ
Sub TestRelativeRanks()
Dim test_scores As Variant
Dim result As Variant
test_scores = Array(6, 3, 7, 2, 1)
result = findRelativeRanks(test_scores)
Debug.Print "入力: " & Join(test_scores, ", ")
Debug.Print "出力: " & Join(result, ", ")
End Sub