DEV Community

Discussion on: Daily Challenge #26 - Ranking Position

Collapse
 
matrossuch profile image
Mat-R-Such

Python

r=[
  {
    'name': "John",
    'points': 100,
  },
  {
    'name': "Bob",
    'points': 130,
  },
  {
    'name': "Mary",
    'points': 120,
  },
  {
    'name': "Kate",
    'points': 120,
  },
]

def points(e):
    return e['points']

p=1
r.sort(reverse= True , key=points)
p_max=r[0]['points']
r[0]['position']= p

for i in range(1,len(r)):
    if r[i]['points'] == p_max:     r[i]['position']= p
    else:
        p+=1
        p_max=r[i]['points']
        r[i]['position']=p
print(r)