Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need to solve this but i got stuck. I think i figured out the first two function but please can you check them and

I need to solve this but i got stuck. I think i figured out the first two function but please can you check them and tell me if there any errors? for the third function i just got stuck because i dont know how to update the third function so that the money can be transfered from the richest to the poorest with the longest name. Also, is the class ok? should i add something to it?

You must implement three functions: longest_name, filter_negative, and r0b1n_h00d. Each of these function takes one argument: account_list, which is a list of Account objects, representing all of the accounts for a given bank. You can assume that the list has at least one element.

  • longest_name should return the Account object in the list for which the account holders name is the longest. You can assume that there will be no ties for the longest name.

  • filter_negative should return a new list of Account objects, which should include only the objects in the original list that had a negative balance (that is, their balance is less than 0), in the order they appeared in the original list.

  • r0b1n_h00d should move $1337 from the Account with the maximum balance, to the account with the longest name out of those that have a negative balance. You can assume that there will be at least one account in the list with a negative balance. This function should not return anything. You must call longest_name and filter_negative as part of your solution.

#See instructions for details on each of the following functions: def longest_name(account_list): name_num = 0 person_account= None for account in account_list: if len(account.name) > name_num: name_num = len(account.name) person_account = account return person_account

def filter_negative(account_list): new_balance_list = [] for acc in account_list: if acc.balance < 0: new_balance_list = append(acc) return new_balance_list

def r0b1n_h00d(account_list): the_richest = account_list[0] for account in account_list: if account.balance > the_richest: the_richest = account

longest_name(filter_negative(account_list)) #this is where i got stuck, dont know what to do from here.

class Account: #Initialize an Account object. Account objects have two instance variables #self.name is a string, representing the name associated with the Account #self.balance is a float, representing the balance of the account in $. account_list = []

def __init__(self,name,balance): self.name = name self.balance = balance #String representation of an Account object - "name: $balance" def __repr__(self): return self.name + ": $" + str(self.balance)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions