def filter_amount(expenses, limit): to_delete = [] for idx, expense in enumerate(expenses): if get_amount(expense) >= int(limit): to_delete.append(idx) count = len(to_delete) for offset, index in enumerate(to_delete): index -= offset del expenses[index] print('Successfully deleted ' + str(count) + ' entries')
def compute_total_amount(expenses, apartment): """ Computes the total amount for a certain apartment :param expenses: :param apartment: :return: """ sum = 0 for expense in expenses: if get_apartment(expense) == apartment: sum += get_amount(expense) return sum
def sum_of_expenses(expenses, params): """ Prints the sum of the amount for the given type :param expenses: :param params: :return: """ type = params[0] sum = 0 for expense in expenses: if get_type(expense) == type: sum += get_amount(expense) print('sum of ' + type + ' is ' + str(sum))
def sort_type(expenses): types = ('water', 'heating', 'electricity', 'gas', 'other') types_and_amounts = dict.fromkeys(types, 0) for expense in expenses: types_and_amounts[get_type(expense)] += get_amount(expense) sorted_type_list = sorted(types_and_amounts.items(), key=lambda kv: (kv[1], kv[0])) print('sorted by type') for elem in sorted_type_list: print('type: ' + str(elem[0]) + ' total amount: ' + str(elem[1]))
def sort_apartment(expenses): """ Sorts by apartments :param expenses: the expenses list :return: a list of the apartments expenses """ apartments_set = get_set_of_apartments(expenses) apartments_sum = dict.fromkeys(apartments_set, 0) for expense in expenses: apartments_sum[get_apartment(expense)] += get_amount(expense) sorted_apt_list = sorted(apartments_sum.items(), key=lambda kv: (kv[1], kv[0])) print('sorted by apartment') for elem in sorted_apt_list: print('apartment: ' + str(elem[0]) + ' total amount: ' + str(elem[1]))
def max_of_expenses(expenses, params): """ Prints the maximum amount for the expenses having the given apartment :param expenses: :param params: :return: """ apartment = params[0] types = ('water', 'heating', 'electricity', 'gas', 'other') for type in types: max_of_type = 0 max_index = -1 for expense in expenses: if get_amount(expense) > max_of_type and get_apartment(expense) == apartment and get_type(expense) == type: max_of_type = get_amount(expense) max_index = expenses.index(expense) if max_index != -1: print("max for " + type + ' for apartment ' + str(apartment) + ' is ' + str(max_of_type)) else: print("No expense for type " + type + ' for apartment ' + str(apartment))
def display(expenses, params): """ Displays expenses :param expenses: :param params: :return: """ if len(params) == 0: for expense in expenses: print_expense(expense) elif len(params) == 1: for expense in expenses: if get_apartment(expense) == params[0]: print_expense(expense) elif len(params) == 2: sign = params[1] amount = params[0] def get_operator_fn(op): """ Returns an operator from a string :param op: string :return: operator """ return { '<': operator.lt, '<=': operator.le, '=': operator.eq, '>=': operator.ge, '>': operator.gt, }[op] sign = get_operator_fn(sign) for expense in expenses: if sign(get_amount(expense), amount): print_expense(expense)
def print_expense(expense): print('apartment: ' + str(get_apartment(expense)) + ' type: ' + get_type(expense) + ' amount: ' + str(get_amount(expense)))