DEV Community 👩‍💻👨‍💻

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

Collapse
 
seniru profile image
Seniru Pasan Indira

My try with python

import random as rand

class Student:
    def __init__(self, name, fives, tens, twenties):
        self.name = name
        self.fives = fives
        self.tens = tens
        self.twenties = twenties

    def getMoney(self):
        return self.fives * 5 + self.tens * 10 + self.twenties

    def __str__(self):
        return self.name + " " + str(self.getMoney())

#Creating a list of students       
students = sorted([Student('Student' + str(s), rand.randint(0, 10), rand.randint(0, 10), rand.randint(0, 10)) for s in range(20) ], key=lambda s: s.getMoney(), reverse=True)

for s in students:
    print(s)

print('Student with highest amount:', end=' ')

if len(students) == 1:
    print(students[1])
elif students[0] == students[::-1]:
    print('All')
else:
    print(students[0])