-
Notifications
You must be signed in to change notification settings - Fork 183
/
03 - Sorting - Comparator.py
48 lines (39 loc) · 1.01 KB
/
03 - Sorting - Comparator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# ========================
# Information
# ========================
# Direct Link: https://www.hackerrank.com/challenges/ctci-comparator-sorting/problem
# Difficulty: Medium
# Max Score: 35
# Language: Python
# ========================
# Solution
# ========================
from functools import cmp_to_key
class Player:
def __init__(self, name, score):
self.name = name
self.score = score
def __repr__(self):
pass
def comparator(a, b):
if a.score < b.score:
return 1
elif a.score > b.score:
return -1
else:
if a.name > b.name:
return 1
elif a.name < b.name:
return -1
else:
return 0
n = int(input())
data = []
for i in range(n):
name, score = input().split()
score = int(score)
player = Player(name, score)
data.append(player)
data = sorted(data, key=cmp_to_key(Player.comparator))
for i in data:
print(i.name, i.score)