Answer to Question #292622 in Python for Dhanaraju

Question #292622

The Richest Person


You are given a N entries denoting incomes of people.Same person can have more than one income entry.Write a Program to print person's name and index of the first income entry of the person with highest total income.In case of tie in total income, choose the lexicographically smallest name.


Note:

Name will contain only alphabets and spaces.

Names are case insensitive (i,e Ravi and ravi are same).

Ignore spaces in the name while comparing (i,e Ravi Kishore and RaviKishore are same).


Input

The first line contains an integer N.

Each of next N lines contains the name and income entry of a person.


Sample input1

4

John Cena 1000

John 2000

Jack 4000

Tom 4000


sample output1

Jack 2


]


1
Expert's answer
2022-02-02T17:27:45-0500
n = int(input())


D = {}
L = []
for i in range(n):
    line = input()
    words = line.split()
    nameD = ''.join(words[:-1]).lower()
    nameL = ' '.join(words[:-1])
    income = int(words[-1])


    D[nameD] = D.get(nameD, 0) + income
    L.append(nameL)


maxIncome = 0
idx = -1
maxName = None
for i, nameL in enumerate(L):
    nameD = ''.join(nameL.split()).lower()
    if D[nameD] > maxIncome:
        maxIncome = D[nameD]
        idx = i
        maxName = nameD
    elif D[nameD] == maxIncome:
        if nameD < maxName:
            idx = i
            maxName = nameD


print()
print(L[idx], idx)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS