Answer to Question #282689 in Python for ramk

Question #282689

Write down a python program to calculate the total loan amount availed by each person from each bank and the total loan amount for each person irrespective of the bank using the following records.

Input

Ramesh CUB 10000 Rajesh IndianBank 20000 Ramesh CUB 20000 Arya ICICI 200000 Ramesh ICICI 20000 Rajesh KVB 30000 Vimala SBI 3000 Vimala YesBank 200000 Rajesh IndianBank 2000 Arya ICICI 2000 Vimala SBI 30000 Ramesh CUB 10000

Expected Output

Ramesh CUB 40000

Ramesh ICICI 20000

Rajesh IndianBank 22000

Rajesh KVB 30000

Arya ICICI 202000

Vimala SBI 33000

Vimala YesBank 200000

Ramesh 60000

Rajesh 52000

Arya 202000

Vimala 233000


1
Expert's answer
2021-12-27T08:39:32-0500
my_input = "Ramesh CUB 10000 Rajesh IndianBank 20000 Ramesh CUB 20000 Arya ICICI 200000 Ramesh ICICI 20000 Rajesh " \
           "KVB 30000 Vimala SBI 3000 Vimala YesBank 200000 Rajesh IndianBank 2000 Arya ICICI 2000 Vimala SBI 30000 " \
           "Ramesh CUB 10000"

my_input_split = my_input.split()

persons = my_input_split[0::3]
banks = my_input_split[1::3]
amounts = my_input_split[2::3]

person_bank_amount = {}

for index, person in enumerate(persons):
    person_bank_amount[person] = person_bank_amount.setdefault(person, {})
    person_bank_amount[person][banks[index]] = person_bank_amount[person].setdefault(banks[index], 0) \
                                               + int(amounts[index])

for k, v in person_bank_amount.items():
    for k2, v2 in v.items():
        print(k, k2, v2)

person_amount = {}
for index, person in enumerate(persons):
    person_amount[person] = person_amount.setdefault(person, 0) + int(amounts[index])

for k, v in person_amount.items():
    print(k, v)

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