DEV Community

Discussion on: Daily Challenge #121 - Who has the most money?

Collapse
 
hectorpascual profile image
Héctor Pascual

My Python attempt :

Using total_ordering decorator from func_tools, which only requires two implementations of the comparison methods.

from functools import total_ordering

@total_ordering
class Student:

  def __init__(self, name, fives, tens, twenties):
    self.name = name
    self.fives = fives
    self.tens = tens
    self.twenties = twenties
    # Added new property
    self.money = self.fives*5 + self.tens*10 + self.twenties*20

  def __eq__(self, other):
    return self.money == other.money
  def __lt__(self, other):
    return self.money < other.money

st1 = Student('Héctor',1,7,3)
st2 = Student('Albert',5,4,3)
st3 = Student('Jordi',1,4,3)

students = [st1, st2, st3]

print(max(students).name)