알고리즘 문제/CodeWar

Who has the most money?

BEstyle 2022. 11. 23. 13:14

DESCRIPTION:

You're going on a trip with some students and it's up to you to keep track of how much money each Student has. A student is defined like this:

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

As you can tell, each Student has some fives, tens, and twenties. Your job is to return the name of the student with the most money. If every student has the same amount, then return "all".

Notes:

  • Each student will have a unique name
  • There will always be a clear winner: either one person has the most, or everyone has the same amount
  • If there is only one student, then that student has the most money

def most_money(students):
    if len(students)==1:
        return students[0].name
    max=0
    candid=""
    alist=[]
    for student in students:
        temp=0
        temp+=student.twenties*20
        temp+=student.tens*10
        temp+=student.fives*5
        alist.append(temp)
        if temp>max:
            max=temp
            candid=student.name
    for i in range(len(alist)):
        for j in range(i+1,len(alist)):
            if alist[i]!=alist[j]:
                return candid
    return "all"